#ifndef _TCP_INLINES_H
#define _TCP_INLINES_H

#include <sys/cdefs.h>
#include <inline/stubs.h>

__BEGIN_DECLS

/* Exec Library */

#ifndef BASE_EXT_DECL
#define BASE_EXT_DECL extern struct ExecBase * SysBase;
#endif
#ifndef BASE_PAR_DECL
#define BASE_PAR_DECL
#define BASE_PAR_DECL0 void
#endif
#ifndef BASE_NAME
#define BASE_NAME SysBase
#endif

static __inline BYTE 
AllocSignal (BASE_PAR_DECL long signalNum)
{
  BASE_EXT_DECL
  register BYTE  _res  __asm("d0");
  register struct ExecBase *a6 __asm("a6") = BASE_NAME;
  register long d0 __asm("d0") = signalNum;
  __asm __volatile ("jsr a6@(-0x14a)"
  : "=r" (_res)
  : "r" (a6), "r" (d0)
  : "a0","a1","d0","d1", "memory");
  return _res;
}

static __inline APTR 
AllocMem (BASE_PAR_DECL unsigned long byteSize,unsigned long requirements)
{
  BASE_EXT_DECL
  register APTR  _res  __asm("d0");
  register struct ExecBase *a6 __asm("a6") = BASE_NAME;
  register unsigned long d0 __asm("d0") = byteSize;
  register unsigned long d1 __asm("d1") = requirements;
  __asm __volatile ("jsr a6@(-0xc6)"
  : "=r" (_res)
  : "r" (a6), "r" (d0), "r" (d1)
  : "a0","a1","d0","d1", "memory");
  return _res;
}

static __inline void 
CloseLibrary (BASE_PAR_DECL struct Library *library)
{
  BASE_EXT_DECL
  register struct ExecBase *a6 __asm("a6") = BASE_NAME;
  register struct Library *a1 __asm("a1") = library;
  __asm __volatile ("jsr a6@(-0x19e)"
  : /* no output */
  : "r" (a6), "r" (a1)
  : "a0","a1","d0","d1", "memory");
}

static __inline struct Task *
FindTask (BASE_PAR_DECL UBYTE *name)
{
  BASE_EXT_DECL
  register struct Task * _res  __asm("d0");
  register struct ExecBase *a6 __asm("a6") = BASE_NAME;
  register UBYTE *a1 __asm("a1") = name;
  __asm __volatile ("jsr a6@(-0x126)"
  : "=r" (_res)
  : "r" (a6), "r" (a1)
  : "a0","a1","d0","d1", "memory");
  return _res;
}
static __inline void 
Forbid (BASE_PAR_DECL0)
{
  BASE_EXT_DECL
  register struct ExecBase *a6 __asm("a6") = BASE_NAME;
  __asm __volatile ("jsr a6@(-0x84)"
  : /* no output */
  : "r" (a6)
  : "a0","a1","d0","d1", "memory");
}

static __inline void 
FreeMem (BASE_PAR_DECL APTR memoryBlock,unsigned long byteSize)
{
  BASE_EXT_DECL
  register struct ExecBase *a6 __asm("a6") = BASE_NAME;
  register APTR a1 __asm("a1") = memoryBlock;
  register unsigned long d0 __asm("d0") = byteSize;
  __asm __volatile ("jsr a6@(-0xd2)"
  : /* no output */
  : "r" (a6), "r" (a1), "r" (d0)
  : "a0","a1","d0","d1", "memory");
}

static __inline struct Message *
GetMsg (BASE_PAR_DECL struct MsgPort *port)
{
  BASE_EXT_DECL
  register struct Message * _res  __asm("d0");
  register struct ExecBase *a6 __asm("a6") = BASE_NAME;
  register struct MsgPort *a0 __asm("a0") = port;
  __asm __volatile ("jsr a6@(-0x174)"
  : "=r" (_res)
  : "r" (a6), "r" (a0)
  : "a0","a1","d0","d1", "memory");
  return _res;
}

static __inline struct Library *
OpenLibrary (BASE_PAR_DECL UBYTE *libName,unsigned long version)
{
  BASE_EXT_DECL
  register struct Library * _res  __asm("d0");
  register struct ExecBase *a6 __asm("a6") = BASE_NAME;
  register UBYTE *a1 __asm("a1") = libName;
  register unsigned long d0 __asm("d0") = version;
  __asm __volatile ("jsr a6@(-0x228)"
  : "=r" (_res)
  : "r" (a6), "r" (a1), "r" (d0)
  : "a0","a1","d0","d1", "memory");
  return _res;
}

static __inline void 
Permit (BASE_PAR_DECL0)
{
  BASE_EXT_DECL
  register struct ExecBase *a6 __asm("a6") = BASE_NAME;
  __asm __volatile ("jsr a6@(-0x8a)"
  : /* no output */
  : "r" (a6)
  : "a0","a1","d0","d1", "memory");
}

static __inline void 
RawDoFmt (BASE_PAR_DECL UBYTE *formatString,APTR dataStream,void (*putChProc)(),APTR putChData)
{
  BASE_EXT_DECL
  register struct ExecBase *a6 __asm("a6") = BASE_NAME;
  register UBYTE *a0 __asm("a0") = formatString;
  register APTR a1 __asm("a1") = dataStream;
  register void (*a2)() __asm("a2") = putChProc;
  register APTR a3 __asm("a3") = putChData;
  __asm __volatile ("jsr a6@(-0x20a)"
  : /* no output */
  : "r" (a6), "r" (a0), "r" (a1), "r" (a2), "r" (a3)
  : "a0","a1","a2","a3","d0","d1", "memory");
}

static __inline void 
ReplyMsg (BASE_PAR_DECL struct Message *message)
{
  BASE_EXT_DECL
  register struct ExecBase *a6 __asm("a6") = BASE_NAME;
  register struct Message *a1 __asm("a1") = message;
  __asm __volatile ("jsr a6@(-0x17a)"
  : /* no output */
  : "r" (a6), "r" (a1)
  : "a0","a1","d0","d1", "memory");
}

static __inline ULONG 
SetSignal (BASE_PAR_DECL unsigned long newSignals,unsigned long signalSet)
{
  BASE_EXT_DECL
  register ULONG  _res  __asm("d0");
  register struct ExecBase *a6 __asm("a6") = BASE_NAME;
  register unsigned long d0 __asm("d0") = newSignals;
  register unsigned long d1 __asm("d1") = signalSet;
  __asm __volatile ("jsr a6@(-0x132)"
  : "=r" (_res)
  : "r" (a6), "r" (d0), "r" (d1)
  : "a0","a1","d0","d1", "memory");
  return _res;
}

static __inline ULONG 
Wait (BASE_PAR_DECL unsigned long signalSet)
{
  BASE_EXT_DECL
  register ULONG  _res  __asm("d0");
  register struct ExecBase *a6 __asm("a6") = BASE_NAME;
  register unsigned long d0 __asm("d0") = signalSet;
  __asm __volatile ("jsr a6@(-0x13e)"
  : "=r" (_res)
  : "r" (a6), "r" (d0)
  : "a0","a1","d0","d1", "memory");
  return _res;
}

static __inline struct Message *
WaitPort (BASE_PAR_DECL struct MsgPort *port)
{
  BASE_EXT_DECL
  register struct Message * _res  __asm("d0");
  register struct ExecBase *a6 __asm("a6") = BASE_NAME;
  register struct MsgPort *a0 __asm("a0") = port;
  __asm __volatile ("jsr a6@(-0x180)"
  : "=r" (_res)
  : "r" (a6), "r" (a0)
  : "a0","a1","d0","d1", "memory");
  return _res;
}

/* in addition.. */
__inline static struct List *NewList(struct List *list)
{
  list->lh_Tail = 0;
  list->lh_TailPred = (struct Node *)list;
  list->lh_Head = (struct Node *)&list->lh_Tail;
}

#undef BASE_EXT_DECL
#undef BASE_PAR_DECL
#undef BASE_PAR_DECL0
#undef BASE_NAME

/* Dos Library */

#ifndef BASE_EXT_DECL
#define BASE_EXT_DECL extern struct DosLibrary * DOSBase;
#endif
#ifndef BASE_PAR_DECL
#define BASE_PAR_DECL
#define BASE_PAR_DECL0 void
#endif
#ifndef BASE_NAME
#define BASE_NAME DOSBase
#endif

static __inline LONG 
DoPkt3 (BASE_PAR_DECL struct MsgPort *port,long action,long arg1,long arg2,long arg3)
{
  BASE_EXT_DECL
  register LONG  _res  __asm("d0");
  register struct DosLibrary *a6 __asm("a6") = BASE_NAME;
  register struct MsgPort *d1 __asm("d1") = port;
  register long d2 __asm("d2") = action;
  register long d3 __asm("d3") = arg1;
  register long d4 __asm("d4") = arg2;
  register long d5 __asm("d5") = arg3;
  __asm __volatile ("jsr a6@(-0xf0)"
  : "=r" (_res)
  : "r" (a6), "r" (d1), "r" (d2), "r" (d3), "r" (d4), "r" (d5)
  : "a0","a1","d0","d1","d2","d3","d4","d5", "memory");
  return _res;
}

static __inline void 
FreeArgs (BASE_PAR_DECL struct RDArgs *args)
{
  BASE_EXT_DECL
  register struct DosLibrary *a6 __asm("a6") = BASE_NAME;
  register struct RDArgs *d1 __asm("d1") = args;
  __asm __volatile ("jsr a6@(-0x35a)"
  : /* no output */
  : "r" (a6), "r" (d1)
  : "a0","a1","d0","d1", "memory");
}

static __inline BPTR 
Input (BASE_PAR_DECL0)
{
  BASE_EXT_DECL
  register BPTR  _res  __asm("d0");
  register struct DosLibrary *a6 __asm("a6") = BASE_NAME;
  __asm __volatile ("jsr a6@(-0x36)"
  : "=r" (_res)
  : "r" (a6)
  : "a0","a1","d0","d1", "memory");
  return _res;
}

static __inline BPTR 
Output (BASE_PAR_DECL0)
{
  BASE_EXT_DECL
  register BPTR  _res  __asm("d0");
  register struct DosLibrary *a6 __asm("a6") = BASE_NAME;
  __asm __volatile ("jsr a6@(-0x3c)"
  : "=r" (_res)
  : "r" (a6)
  : "a0","a1","d0","d1", "memory");
  return _res;
}

static __inline struct RDArgs *
ReadArgs (BASE_PAR_DECL STRPTR _template,LONG *array,struct RDArgs *args)
{
  BASE_EXT_DECL
  register struct RDArgs * _res  __asm("d0");
  register struct DosLibrary *a6 __asm("a6") = BASE_NAME;
  register STRPTR d1 __asm("d1") = _template;
  register LONG *d2 __asm("d2") = array;
  register struct RDArgs *d3 __asm("d3") = args;
  __asm __volatile ("jsr a6@(-0x31e)"
  : "=r" (_res)
  : "r" (a6), "r" (d1), "r" (d2), "r" (d3)
  : "a0","a1","d0","d1","d2","d3", "memory");
  return _res;
}

static __inline void 
SendPkt (BASE_PAR_DECL struct DosPacket *dp,struct MsgPort *port,struct MsgPort *replyport)
{
  BASE_EXT_DECL
  register struct DosLibrary *a6 __asm("a6") = BASE_NAME;
  register struct DosPacket *d1 __asm("d1") = dp;
  register struct MsgPort *d2 __asm("d2") = port;
  register struct MsgPort *d3 __asm("d3") = replyport;
  __asm __volatile ("jsr a6@(-0xf6)"
  : /* no output */
  : "r" (a6), "r" (d1), "r" (d2), "r" (d3)
  : "a0","a1","d0","d1","d2","d3", "memory");
}

static __inline STRPTR
SetArgStr (BASE_PAR_DECL STRPTR string)
{
  BASE_EXT_DECL
  register STRPTR  _res  __asm("d0");
  register struct DosLibrary *a6 __asm("a6") = BASE_NAME;
  register STRPTR d1 __asm("d1") = string;
  __asm __volatile ("jsr a6@(-0x21c)"
  : "=r" (_res)
  : "r" (a6), "r" (d1)
  : "a0","a1","d0","d1", "memory");
  return _res;
}

static __inline LONG 
Write (BASE_PAR_DECL BPTR file,APTR buffer,long length)
{
  BASE_EXT_DECL
  register LONG  _res  __asm("d0");
  register struct DosLibrary *a6 __asm("a6") = BASE_NAME;
  register BPTR d1 __asm("d1") = file;
  register APTR d2 __asm("d2") = buffer;
  register long d3 __asm("d3") = length;
  __asm __volatile ("jsr a6@(-0x30)"
  : "=r" (_res)
  : "r" (a6), "r" (d1), "r" (d2), "r" (d3)
  : "a0","a1","d0","d1","d2","d3", "memory");
  return _res;
}

#undef BASE_EXT_DECL
#undef BASE_PAR_DECL
#undef BASE_PAR_DECL0
#undef BASE_NAME

/* Socket Library */

#ifndef NO_SOCKET_INLINES

#define BASE_EXT_DECL
#define BASE_PAR_DECL  struct Library * SocketBase,
#define BASE_PAR_DECL0 struct Library * SocketBase

#ifndef BASE_EXT_DECL
#define BASE_EXT_DECL extern struct Library * SocketBase;
#endif
#ifndef BASE_PAR_DECL
#define BASE_PAR_DECL
#define BASE_PAR_DECL0 void
#endif
#ifndef BASE_NAME
#define BASE_NAME SocketBase
#endif

static __inline LONG 
SocketBaseTagList (BASE_PAR_DECL ULONG * taglist)
{
  BASE_EXT_DECL
  register LONG  _res  __asm("d0");
  register struct Library * a6 __asm("a6") = BASE_NAME;
  register ULONG * a0 __asm("a0") = taglist;
  __asm __volatile ("jsr a6@(-0x126)"
  : "=r" (_res)
  : "r" (a6), "r" (a0)
  : "a0","a1","d0","d1", "memory");
  return _res;
}

#ifndef NO_INLINE_STDARG
#define SocketBaseTags(tags...) \
    ({ ULONG _tags[] = { tags }; \
       SocketBaseTagList(_tags); })
#endif /* not NO_INLINE_STDARG */

static __inline LONG 
connect (BASE_PAR_DECL LONG s,struct sockaddr * name,LONG  namelen)
{
  BASE_EXT_DECL
  register LONG  _res  __asm("d0");
  register struct Library* a6 __asm("a6") = BASE_NAME;
  register LONG d0 __asm("d0") = s;
  register struct sockaddr *a0 __asm("a0") =  name;
  register LONG d1 __asm("d1") =  namelen;
  __asm __volatile ("jsr a6@(-0x36)"
  : "=r" (_res)
  : "r" (a6), "r" (d0), "r" (a0), "r" (d1)
  : "a0","a1","d0","d1", "memory");
  return _res;
}

static __inline struct hostent *
gethostbyname (BASE_PAR_DECL const UBYTE *name)
{
  BASE_EXT_DECL
  register struct hostent * _res  __asm("d0");
  register struct Library * a6 __asm("a6") = BASE_NAME;
  register const UBYTE *a0 __asm("a0") = name;
  __asm __volatile ("jsr a6@(-0xd2)"
  : "=r" (_res)
  : "r" (a6), "r" (a0)
  : "a0","a1","d0","d1", "memory");
  return _res;
}

static __inline struct servent *
getservbyname (BASE_PAR_DECL const UBYTE *name,const UBYTE * proto)
{
  BASE_EXT_DECL
  register struct servent * _res  __asm("d0");
  register struct Library * a6 __asm("a6") = BASE_NAME;
  register const UBYTE *a0 __asm("a0") = name;
  register const UBYTE *a1 __asm("a1") =  proto;
  __asm __volatile ("jsr a6@(-0xea)"
  : "=r" (_res)
  : "r" (a6), "r" (a0), "r" (a1)
  : "a0","a1","d0","d1", "memory");
  return _res;
}

static __inline LONG 
IoctlSocket (BASE_PAR_DECL LONG d,ULONG request,char * argp)
{
  BASE_EXT_DECL
  register LONG  _res  __asm("d0");
  register struct Library* a6 __asm("a6") = BASE_NAME;
  register LONG d0 __asm("d0") = d;
  register ULONG d1 __asm("d1") =  request;
  register char *a0 __asm("a0") =  argp;
  __asm __volatile ("jsr a6@(-0x72)"
  : "=r" (_res)
  : "r" (a6), "r" (d0), "r" (d1), "r" (a0)
  : "a0","a1","d0","d1", "memory");
  return _res;
}

static __inline LONG 
recv (BASE_PAR_DECL LONG s,char * buf,LONG  len,LONG  flags)
{
  BASE_EXT_DECL
  register LONG  _res  __asm("d0");
  register struct Library* a6 __asm("a6") = BASE_NAME;
  register LONG d0 __asm("d0") = s;
  register char *a0 __asm("a0") =  buf;
  register LONG d1 __asm("d1") =  len;
  register LONG d2 __asm("d2") =  flags;
  __asm __volatile ("jsr a6@(-0x4e)"
  : "=r" (_res)
  : "r" (a6), "r" (d0), "r" (a0), "r" (d1), "r" (d2)
  : "a0","a1","d0","d1","d2", "memory");
  return _res;
}

static __inline LONG 
send (BASE_PAR_DECL LONG s,char * msg,LONG  len,LONG  flags)
{
  BASE_EXT_DECL
  register LONG  _res  __asm("d0");
  register struct Library* a6 __asm("a6") = BASE_NAME;
  register LONG d0 __asm("d0") = s;
  register char *a0 __asm("a0") =  msg;
  register LONG d1 __asm("d1") =  len;
  register LONG d2 __asm("d2") =  flags;
  __asm __volatile ("jsr a6@(-0x42)"
  : "=r" (_res)
  : "r" (a6), "r" (d0), "r" (a0), "r" (d1), "r" (d2)
  : "a0","a1","d0","d1","d2", "memory");
  return _res;
}

static __inline LONG 
setsockopt (BASE_PAR_DECL LONG s,LONG  level,LONG  optname,char * optval,LONG  optlen)
{
  BASE_EXT_DECL
  register LONG  _res  __asm("d0");
  register struct Library* a6 __asm("a6") = BASE_NAME;
  register LONG d0 __asm("d0") = s;
  register LONG d1 __asm("d1") =  level;
  register LONG d2 __asm("d2") =  optname;
  register char *a0 __asm("a0") =  optval;
  register LONG d3 __asm("d3") =  optlen;
  __asm __volatile ("jsr a6@(-0x5a)"
  : "=r" (_res)
  : "r" (a6), "r" (d0), "r" (d1), "r" (d2), "r" (a0), "r" (d3)
  : "a0","a1","d0","d1","d2","d3", "memory");
  return _res;
}

static __inline LONG 
socket (BASE_PAR_DECL LONG domain,LONG  type,LONG  protocol)
{
  BASE_EXT_DECL
  register LONG  _res  __asm("d0");
  register struct Library* a6 __asm("a6") = BASE_NAME;
  register LONG d0 __asm("d0") = domain;
  register LONG d1 __asm("d1") =  type;
  register LONG d2 __asm("d2") =  protocol;
  __asm __volatile ("jsr a6@(-0x1e)"
  : "=r" (_res)
  : "r" (a6), "r" (d0), "r" (d1), "r" (d2)
  : "a0","a1","d0","d1","d2", "memory");
  return _res;
}

#undef BASE_EXT_DECL
#undef BASE_PAR_DECL
#undef BASE_PAR_DECL0
#undef BASE_NAME

#define	    gethostbyname(r...)	gethostbyname(SocketBase, r)
#define	    getservbyname(r...)	getservbyname(SocketBase, r)
#define		   socket(r...)	socket(SocketBase, r)
#define		  connect(r...)	connect(SocketBase, r)
#define	       WaitSelect(r...)	WaitSelect(SocketBase, r)
#define	      IoctlSocket(r...)	IoctlSocket(SocketBase, r)
#define		     recv(r...)	recv(SocketBase, r)
#define		     send(r...)	send(SocketBase, r)
#define	       setsockopt(r...)	setsockopt(SocketBase, r)
#define SocketBaseTagList(r...)	SocketBaseTagList(SocketBase, r)

#endif NO_SOCKET_INLINES

__END_DECLS

#endif /* _TCP_INLINES_H */
