/* Generated by Nimrod Compiler v0.9.5 */
/*   (c) 2014 Andreas Rumpf */
/* The generated code is subject to the original license. */
/* Compiled for: Linux, i386, gcc */
/* Command for C compiler:
   gcc -c  -w -O3 -fno-strict-aliasing  -I/build/nimrod/src/nimrod/lib -o compiler/nimcache/stdlib_sockets.o compiler/nimcache/stdlib_sockets.c */
#define NIM_INTBITS 32
#include "nimbase.h"

#include <sys/socket.h>

#include <netinet/in.h>

#include <string.h>

#include <netdb.h>

#include <errno.h>

#include <arpa/inet.h>

#include <sys/select.h>

#include <unistd.h>
typedef struct tsocketimpl150605 tsocketimpl150605;
typedef struct tcell41090 tcell41090;
typedef struct TNimType TNimType;
typedef struct tcellseq41106 tcellseq41106;
typedef struct tgcheap43016 tgcheap43016;
typedef struct tcellset41102 tcellset41102;
typedef struct tpagedesc41098 tpagedesc41098;
typedef struct tmemregion23410 tmemregion23410;
typedef struct tsmallchunk22638 tsmallchunk22638;
typedef struct tllchunk23404 tllchunk23404;
typedef struct tbigchunk22640 tbigchunk22640;
typedef struct tintset22615 tintset22615;
typedef struct ttrunk22611 ttrunk22611;
typedef struct tavlnode23408 tavlnode23408;
typedef struct tgcstat43014 tgcstat43014;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct einvalidvalue2453 einvalidvalue2453;
typedef struct esynch2429 esynch2429;
typedef struct E_Base E_Base;
typedef struct TNimObject TNimObject;
typedef struct TNimNode TNimNode;
typedef struct eos2435 eos2435;
typedef struct esystem2431 esystem2431;
typedef struct etimeout150627 etimeout150627;
typedef struct TY158815 TY158815;
typedef struct tbasechunk22636 tbasechunk22636;
typedef struct tfreecell22628 tfreecell22628;
struct  tcell41090  {
NI Refcount;
TNimType* Typ;
};
struct  tcellseq41106  {
NI Len;
NI Cap;
tcell41090** D;
};
struct  tcellset41102  {
NI Counter;
NI Max;
tpagedesc41098* Head;
tpagedesc41098** Data;
};
typedef tsmallchunk22638* TY23422[512];
typedef ttrunk22611* ttrunkbuckets22613[256];
struct  tintset22615  {
ttrunkbuckets22613 Data;
};
struct  tmemregion23410  {
NI Minlargeobj;
NI Maxlargeobj;
TY23422 Freesmallchunks;
tllchunk23404* Llmem;
NI Currmem;
NI Maxmem;
NI Freemem;
NI Lastsize;
tbigchunk22640* Freechunkslist;
tintset22615 Chunkstarts;
tavlnode23408* Root;
tavlnode23408* Deleted;
tavlnode23408* Last;
tavlnode23408* Freeavlnodes;
};
struct  tgcstat43014  {
NI Stackscans;
NI Cyclecollections;
NI Maxthreshold;
NI Maxstacksize;
NI Maxstackcells;
NI Cycletablesize;
NI64 Maxpause;
};
struct  tgcheap43016  {
void* Stackbottom;
NI Cyclethreshold;
tcellseq41106 Zct;
tcellseq41106 Decstack;
tcellset41102 Cycleroots;
tcellseq41106 Tempstack;
NI Recgclock;
tmemregion23410 Region;
tgcstat43014 Stat;
};
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
typedef NIM_CHAR TY150630[4001];
struct  tsocketimpl150605  {
int Fd;
NIM_BOOL Isbuffered;
union {
struct {TY150630 Buffer;
NI Currpos;
NI Buflen;
} S1;
} isbufferedU;
NIM_BOOL Nonblocking;
};
typedef N_NIMCALL_PTR(void, TY2289) (void* p, NI op);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY2289 marker;
};
struct  TNimObject  {
TNimType* m_type;
};
struct  E_Base  {
  TNimObject Sup;
E_Base* parent;
NCSTRING name;
NimStringDesc* message;
NimStringDesc* trace;
};
struct  esynch2429  {
  E_Base Sup;
};
struct  einvalidvalue2453  {
  esynch2429 Sup;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct  esystem2431  {
  esynch2429 Sup;
};
struct  eos2435  {
  esystem2431 Sup;
NI32 Errorcode;
};
struct  etimeout150627  {
  esynch2429 Sup;
};
typedef tsocketimpl150605* TY158817[1];
typedef NI TY22618[16];
struct  tpagedesc41098  {
tpagedesc41098* Next;
NI Key;
TY22618 Bits;
};
struct  tbasechunk22636  {
NI Prevsize;
NI Size;
NIM_BOOL Used;
};
struct  tsmallchunk22638  {
  tbasechunk22636 Sup;
tsmallchunk22638* Next;
tsmallchunk22638* Prev;
tfreecell22628* Freelist;
NI Free;
NI Acc;
NF Data;
};
struct  tllchunk23404  {
NI Size;
NI Acc;
tllchunk23404* Next;
};
struct  tbigchunk22640  {
  tbasechunk22636 Sup;
tbigchunk22640* Next;
tbigchunk22640* Prev;
NI Align;
NF Data;
};
struct  ttrunk22611  {
ttrunk22611* Next;
NI Key;
TY22618 Bits;
};
typedef tavlnode23408* TY23414[2];
struct  tavlnode23408  {
TY23414 Link;
NI Key;
NI Upperbound;
NI Level;
};
typedef NIM_CHAR TY97549[256];
struct  tfreecell22628  {
tfreecell22628* Next;
NI Zerofield;
};
struct TY158815 {
  TGenericSeq Sup;
  tsocketimpl150605* data[SEQ_DECL_SIZE];
};
static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src);
static N_INLINE(tcell41090*, usrtocell_44443)(void* usr);
static N_INLINE(void, rtladdzct_46002)(tcell41090* c);
N_NOINLINE(void, addzct_44418)(tcellseq41106* s, tcell41090* c);
N_NIMCALL(void*, newObj)(TNimType* typ, NI size);
N_NIMCALL(NimStringDesc*, copyStringRC1)(NimStringDesc* src);
static N_INLINE(void, nimGCunrefNoCycle)(void* p);
N_NIMCALL(void, raiseException)(E_Base* e, NCSTRING ename);
N_NIMCALL(NI, send_160040)(tsocketimpl150605* socket, void* data, NI size);
N_NIMCALL(void, oserror_106205)(NI32 errorcode);
N_NIMCALL(NI32, oslasterror_106231)(void);
N_NIMCALL(tsocketimpl150605*, newtsocket_150802)(int fd, NIM_BOOL isbuff);
N_NIMCALL(void, TMP4536)(void* p, NI op);
N_NIMCALL(int, toint_151215)(NU8 domain);
N_NIMCALL(int, toint_151220)(NU8 typ);
N_NIMCALL(int, toint_151225)(NU8 p);
N_NIMCALL(NI16, htons_151209)(NI16 x);
N_NIMCALL(NI16, ntohs_151003)(NI16 x);
N_NIMCALL(NI32, htonl_151203)(NI32 x);
N_NIMCALL(NI32, ntohl_150860)(NI32 x);
N_NIMCALL(NimStringDesc*, HEX24_5001)(NU64 x);
N_NIMCALL(NimStringDesc*, cstrToNimstr)(NCSTRING str);
N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
N_NIMCALL(void, acceptaddr_153042)(tsocketimpl150605* server, tsocketimpl150605** client, NimStringDesc** address);
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(NimStringDesc*, setLengthStr)(NimStringDesc* s, NI newlen);
N_NIMCALL(NI, waitfor_158408)(tsocketimpl150605* socket, NF* waited, NI timeout, NI size, NimStringDesc* funcname);
N_NIMCALL(void, nimGCvisit)(void* d, NI op);
N_NIMCALL(void, TMP4540)(void* p, NI op);
static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src);
N_NIMCALL(NimStringDesc*, rawNewString)(NI space);
N_NIMCALL(void*, newSeq)(TNimType* typ, NI len);
N_NIMCALL(NF, ntepochTime)(void);
N_NIMCALL(NI, select_157881)(TY158815** readfds, NI timeout);
N_NIMCALL(NI, checkbuffer_157603)(TY158815** readfds);
N_NIMCALL(NIM_BOOL, hasdatabuffered_157504)(tsocketimpl150605* s);
N_NIMCALL(TGenericSeq*, incrSeq)(TGenericSeq* seq, NI elemsize);
N_NIMCALL(void, genericSeqAssign)(void* dest, void* src_74604, TNimType* mt);
N_NIMCALL(struct timeval, timevalfrommilliseconds_157201)(NI timeout);
N_NIMCALL(void, createfdset_157214)(fd_set* fd, TY158815* s, NI* m);
N_NIMCALL(void, prunesocketset_157423)(TY158815** s, fd_set* fd);
N_NIMCALL(TGenericSeq*, setLengthSeq)(TGenericSeq* seq, NI elemsize, NI newlen);
static N_INLINE(void, HEX2BHEX3D_94274)(NF* x_94280, NF y_94283);
N_NIMCALL(NI, recv_158096)(tsocketimpl150605* socket, void* data, NI size);
N_NIMCALL(NI, readintobuf_157938)(tsocketimpl150605* socket, NI32 flags);
N_NIMCALL(void, socketerror_151801)(tsocketimpl150605* socket, NI err, NIM_BOOL async);
N_NIMCALL(NI, peekchar_158920)(tsocketimpl150605* socket, NIM_CHAR* c);
N_NIMCALL(NimStringDesc*, resizeString)(NimStringDesc* dest, NI addlen);
N_NIMCALL(NimStringDesc*, addChar)(NimStringDesc* s, NIM_CHAR c);
STRING_LITERAL(TMP1860, "This function cannot be used on non-blocking sockets.", 53);
STRING_LITERAL(TMP1861, "Could not send all data.", 24);
TNimNode* NimDT_tsocketimpl_isbuffered[3];
STRING_LITERAL(TMP4539, "", 0);
STRING_LITERAL(TMP4541, "Call to \'", 9);
STRING_LITERAL(TMP4542, "\' timed out.", 12);
STRING_LITERAL(TMP4544, "readLine", 8);
STRING_LITERAL(TMP4545, "\015\012", 2);
tsocketimpl150605* invalidsocket_150678;
extern tgcheap43016 gch_43044;
int osinvalidsocket_150681;
extern TNimType NTI17801; /* ref EInvalidValue */
extern TNimType NTI2453; /* EInvalidValue */
extern TNimType NTI106208; /* ref EOS */
extern TNimType NTI2435; /* EOS */
TNimType NTI150605; /* TSocketImpl */
extern TNimType NTI4409; /* cint */
extern TNimType NTI134; /* bool */
extern TNimType NTI141; /* char */
TNimType NTI150630; /* array[0..4000, char] */
extern TNimType NTI106; /* int */
TNimType NTI150607; /* TSocket */
extern TNimType NTI2429; /* ESynch */
TNimType NTI150627; /* ETimeout */
TNimType NTI158679; /* ref ETimeout */
extern TNimType NTI158815; /* seq[TSocket] */

static N_INLINE(tcell41090*, usrtocell_44443)(void* usr) {
	tcell41090* result;
	result = 0;
	result = ((tcell41090*) ((NI)((NU32)(((NI) (usr))) - (NU32)(((NI)sizeof(tcell41090))))));
	return result;
}

static N_INLINE(void, rtladdzct_46002)(tcell41090* c) {
	addzct_44418(&gch_43044.Zct, c);
}

static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src) {
	{
		tcell41090* c;
		if (!!((src == NIM_NIL))) goto LA3;
		c = usrtocell_44443(src);
		(*c).Refcount += 8;
	}
	LA3: ;
	{
		tcell41090* c;
		if (!!(((*dest) == NIM_NIL))) goto LA7;
		c = usrtocell_44443((*dest));
		{
			(*c).Refcount -= 8;
			if (!((NU32)((*c).Refcount) < (NU32)(8))) goto LA11;
			rtladdzct_46002(c);
		}
		LA11: ;
	}
	LA7: ;
	(*dest) = src;
}

static N_INLINE(void, nimGCunrefNoCycle)(void* p) {
	tcell41090* c;
	c = usrtocell_44443(p);
	{
		(*c).Refcount -= 8;
		if (!((NU32)((*c).Refcount) < (NU32)(8))) goto LA3;
		rtladdzct_46002(c);
	}
	LA3: ;
}

N_NIMCALL(NI, send_160040)(tsocketimpl150605* socket, void* data, NI size) {
	NI result;
	result = 0;
	result = send((*socket).Fd, data, size, MSG_NOSIGNAL);
	return result;
}

N_NIMCALL(void, send_160604)(tsocketimpl150605* socket, NimStringDesc* data) {
	NI sent;
	{
		einvalidvalue2453* e_160610;
		NimStringDesc* LOC5;
		if (!(*socket).Nonblocking) goto LA3;
		e_160610 = 0;
		e_160610 = (einvalidvalue2453*) newObj((&NTI17801), sizeof(einvalidvalue2453));
		(*e_160610).Sup.Sup.Sup.m_type = (&NTI2453);
		LOC5 = 0;
		LOC5 = (*e_160610).Sup.Sup.message; (*e_160610).Sup.Sup.message = copyStringRC1(((NimStringDesc*) &TMP1860));
		if (LOC5) nimGCunrefNoCycle(LOC5);
		raiseException((E_Base*)e_160610, "EInvalidValue");
	}
	LA3: ;
	sent = send_160040(socket, ((void*) (data->data)), data->Sup.len);
	{
		NI32 LOC10;
		if (!(sent < 0)) goto LA8;
		LOC10 = 0;
		LOC10 = oslasterror_106231();
		oserror_106205(LOC10);
	}
	LA8: ;
	{
		eos2435* e_160803;
		NimStringDesc* LOC15;
		if (!!((sent == data->Sup.len))) goto LA13;
		e_160803 = 0;
		e_160803 = (eos2435*) newObj((&NTI106208), sizeof(eos2435));
		(*e_160803).Sup.Sup.Sup.Sup.m_type = (&NTI2435);
		LOC15 = 0;
		LOC15 = (*e_160803).Sup.Sup.Sup.message; (*e_160803).Sup.Sup.Sup.message = copyStringRC1(((NimStringDesc*) &TMP1861));
		if (LOC15) nimGCunrefNoCycle(LOC15);
		raiseException((E_Base*)e_160803, "EOS");
	}
	LA13: ;
}
N_NIMCALL(void, TMP4536)(void* p, NI op) {
	tsocketimpl150605* a;
	NI LOC1;
	a = (tsocketimpl150605*)p;
	switch ((*a).Isbuffered) {
	case NIM_TRUE:
	LOC1 = 0;
	for (LOC1 = 0; LOC1 < 4001; LOC1++) {
	}
	break;
	case NIM_FALSE:
	break;
	} 
}

N_NIMCALL(tsocketimpl150605*, newtsocket_150802)(int fd, NIM_BOOL isbuff) {
	tsocketimpl150605* result;
	result = 0;
	{
		if (!(fd == osinvalidsocket_150681)) goto LA3;
		result = NIM_NIL;
		goto BeforeRet;
	}
	LA3: ;
	result = (tsocketimpl150605*) newObj((&NTI150607), sizeof(tsocketimpl150605));
	(*result).Fd = fd;
	(*result).Isbuffered = isbuff;
	{
		if (!isbuff) goto LA7;
		(*result).isbufferedU.S1.Currpos = 0;
	}
	LA7: ;
	(*result).Nonblocking = NIM_FALSE;
	BeforeRet: ;
	return result;
}

N_NIMCALL(int, toint_151215)(NU8 domain) {
	int result;
	result = 0;
	switch (domain) {
	case ((NU8) 0):
	{
		result = AF_UNIX;
	}
	break;
	case ((NU8) 2):
	{
		result = AF_INET;
	}
	break;
	case ((NU8) 23):
	{
		result = AF_INET6;
	}
	break;
	default:
	{
	}
	break;
	}
	return result;
}

N_NIMCALL(int, toint_151220)(NU8 typ) {
	int result;
	result = 0;
	switch (typ) {
	case ((NU8) 1):
	{
		result = SOCK_STREAM;
	}
	break;
	case ((NU8) 2):
	{
		result = SOCK_DGRAM;
	}
	break;
	case ((NU8) 5):
	{
		result = SOCK_SEQPACKET;
	}
	break;
	case ((NU8) 3):
	{
		result = SOCK_RAW;
	}
	break;
	default:
	{
	}
	break;
	}
	return result;
}

N_NIMCALL(int, toint_151225)(NU8 p) {
	int result;
	result = 0;
	switch (p) {
	case ((NU8) 6):
	{
		result = IPPROTO_TCP;
	}
	break;
	case ((NU8) 17):
	{
		result = IPPROTO_UDP;
	}
	break;
	case ((NU8) 18):
	{
		result = IPPROTO_IP;
	}
	break;
	case ((NU8) 19):
	{
		result = IPPROTO_IPV6;
	}
	break;
	case ((NU8) 20):
	{
		result = IPPROTO_RAW;
	}
	break;
	case ((NU8) 21):
	{
		result = IPPROTO_ICMP;
	}
	break;
	default:
	{
	}
	break;
	}
	return result;
}

N_NIMCALL(tsocketimpl150605*, socket_151402)(NU8 domain, NU8 typ, NU8 protocol, NIM_BOOL buffered) {
	tsocketimpl150605* result;
	int LOC1;
	int LOC2;
	int LOC3;
	int LOC4;
	result = 0;
	LOC1 = 0;
	LOC1 = toint_151215(domain);
	LOC2 = 0;
	LOC2 = toint_151220(typ);
	LOC3 = 0;
	LOC3 = toint_151225(protocol);
	LOC4 = 0;
	LOC4 = socket(LOC1, LOC2, LOC3);
	result = newtsocket_150802(LOC4, buffered);
	return result;
}

N_NIMCALL(NI16, ntohs_151003)(NI16 x) {
	NI16 result;
	result = 0;
	result = (NI16)((NI16)((NU16)(x) >> (NU16)(((NI16) 8))) | (NI16)((NU16)(x) << (NU16)(((NI16) 8))));
	return result;
}

N_NIMCALL(NI16, htons_151209)(NI16 x) {
	NI16 result;
	result = 0;
	result = ntohs_151003(x);
	return result;
}

N_NIMCALL(NI32, ntohl_150860)(NI32 x) {
	NI32 result;
	result = 0;
	result = (NI32)((NI32)((NI32)((NI32)((NU32)(x) >> (NU32)(((NI32) 24))) | (NI32)((NI32)((NU32)(x) >> (NU32)(((NI32) 8))) & ((NI32) 65280))) | (NI32)((NI32)((NU32)(x) << (NU32)(((NI32) 8))) & ((NI32) 16711680))) | (NI32)((NU32)(x) << (NU32)(((NI32) 24))));
	return result;
}

N_NIMCALL(NI32, htonl_151203)(NI32 x) {
	NI32 result;
	result = 0;
	result = ntohl_150860(x);
	return result;
}

N_NIMCALL(void, bindaddr_152584)(tsocketimpl150605* socket, NU16 port, NimStringDesc* address) {
	{
		struct sockaddr_in name;
		if (!((address) && (address)->Sup.len == 0)) goto LA3;
		memset((void*)&name, 0, sizeof(name));
		name.sin_family = AF_INET;
		name.sin_port = htons_151209(((NI16) (port)));
		name.sin_addr.s_addr = htonl_151203(INADDR_ANY);
		{
			int LOC7;
			NI32 LOC10;
			LOC7 = 0;
			LOC7 = bind((*socket).Fd, ((struct sockaddr*) (&name)), ((socklen_t) (((NI)sizeof(struct sockaddr_in)))));
			if (!(LOC7 < ((NI32) 0))) goto LA8;
			LOC10 = 0;
			LOC10 = oslasterror_106231();
			oserror_106205(LOC10);
		}
		LA8: ;
	}
	goto LA1;
	LA3: ;
	{
		struct addrinfo hints;
		struct addrinfo* ailist;
		memset((void*)&hints, 0, sizeof(hints));
		ailist = NIM_NIL;
		hints.ai_family = toint_151215(((NU8) 2));
		hints.ai_socktype = toint_151220(((NU8) 1));
		hints.ai_protocol = toint_151225(((NU8) 6));
		{
			int gairesult_152608;
			NimStringDesc* LOC13;
			LOC13 = 0;
			LOC13 = HEX24_5001(port);
			gairesult_152608 = getaddrinfo(address->data, LOC13->data, &hints, &ailist);
			{
				eos2435* e_152802;
				NCSTRING LOC18;
				if (!!((gairesult_152608 == ((NI32) 0)))) goto LA16;
				e_152802 = 0;
				e_152802 = (eos2435*) newObj((&NTI106208), sizeof(eos2435));
				(*e_152802).Sup.Sup.Sup.Sup.m_type = (&NTI2435);
				LOC18 = 0;
				LOC18 = gai_strerror(gairesult_152608);
				asgnRefNoCycle((void**) &(*e_152802).Sup.Sup.Sup.message, cstrToNimstr(LOC18));
				raiseException((E_Base*)e_152802, "EOS");
			}
			LA16: ;
		}
		{
			int LOC21;
			NI32 LOC24;
			LOC21 = 0;
			LOC21 = bind((*socket).Fd, (*ailist).ai_addr, (*ailist).ai_addrlen);
			if (!(LOC21 < ((NI32) 0))) goto LA22;
			LOC24 = 0;
			LOC24 = oslasterror_106231();
			oserror_106205(LOC24);
		}
		LA22: ;
	}
	LA1: ;
}

N_NIMCALL(void, listen_152405)(tsocketimpl150605* socket, int backlog) {
	{
		int LOC3;
		NI32 LOC6;
		LOC3 = 0;
		LOC3 = listen((*socket).Fd, backlog);
		if (!(LOC3 < ((NI32) 0))) goto LA4;
		LOC6 = 0;
		LOC6 = oslasterror_106231();
		oserror_106205(LOC6);
	}
	LA4: ;
}

N_NIMCALL(void, acceptaddr_153042)(tsocketimpl150605* server, tsocketimpl150605** client, NimStringDesc** address) {
	struct sockaddr_in sockaddress_153050;
	socklen_t addrlen_153052;
	int sock_153054;
	memset((void*)&sockaddress_153050, 0, sizeof(sockaddress_153050));
	addrlen_153052 = ((socklen_t) (((NI)sizeof(struct sockaddr_in))));
	sock_153054 = accept((*server).Fd, ((struct sockaddr*) (&sockaddress_153050)), &addrlen_153052);
	{
		NI32 err_153056;
		if (!(sock_153054 == osinvalidsocket_150681)) goto LA3;
		err_153056 = oslasterror_106231();
		{
			NIM_BOOL LOC7;
			LOC7 = 0;
			LOC7 = (err_153056 == EAGAIN);
			if (LOC7) goto LA8;
			LOC7 = (err_153056 == EWOULDBLOCK);
			LA8: ;
			if (!LOC7) goto LA9;
			unsureAsgnRef((void**) &(*client), invalidsocket_150678);
			unsureAsgnRef((void**) &(*address), copyString(((NimStringDesc*) &TMP4539)));
			goto BeforeRet;
		}
		goto LA5;
		LA9: ;
		{
			oserror_106205(err_153056);
		}
		LA5: ;
	}
	goto LA1;
	LA3: ;
	{
		NCSTRING LOC13;
		(*(*client)).Fd = sock_153054;
		(*(*client)).Isbuffered = (*server).Isbuffered;
		LOC13 = 0;
		LOC13 = inet_ntoa(sockaddress_153050.sin_addr);
		unsureAsgnRef((void**) &(*address), cstrToNimstr(LOC13));
		goto BeforeRet;
	}
	LA1: ;
	BeforeRet: ;
}

N_NIMCALL(void, accept_154201)(tsocketimpl150605* server, tsocketimpl150605** client) {
	NimStringDesc* addrdummy;
	addrdummy = copyString(((NimStringDesc*) &TMP4539));
	acceptaddr_153042(server, client, &addrdummy);
}
N_NIMCALL(void, TMP4540)(void* p, NI op) {
	etimeout150627* a;
	a = (etimeout150627*)p;
	nimGCvisit((void*)(*a).Sup.Sup.parent, op);
	nimGCvisit((void*)(*a).Sup.Sup.message, op);
	nimGCvisit((void*)(*a).Sup.Sup.trace, op);
}

static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src) {
	memcpy(((NCSTRING) (&(*dest).data[((*dest).Sup.len)- 0])), ((NCSTRING) ((*src).data)), (NI32)((*src).Sup.len + 1));
	(*dest).Sup.len += (*src).Sup.len;
}

N_NIMCALL(NIM_BOOL, hasdatabuffered_157504)(tsocketimpl150605* s) {
	NIM_BOOL result;
	result = 0;
	result = NIM_FALSE;
	{
		NIM_BOOL LOC5;
		if (!(*s).Isbuffered) goto LA3;
		LOC5 = 0;
		LOC5 = (0 < (*s).isbufferedU.S1.Buflen);
		if (!(LOC5)) goto LA6;
		LOC5 = !(((*s).isbufferedU.S1.Currpos == (*s).isbufferedU.S1.Buflen));
		LA6: ;
		result = LOC5;
	}
	LA3: ;
	return result;
}

N_NIMCALL(NI, checkbuffer_157603)(TY158815** readfds) {
	NI result;
	TY158815* res;
	tsocketimpl150605* s_157643;
	TY158815* HEX3Atmp_157680;
	NI i_157682;
	NI l_157684;
	result = 0;
	res = (TY158815*) newSeq((&NTI158815), 0);
	result = 0;
	s_157643 = 0;
	HEX3Atmp_157680 = 0;
	unsureAsgnRef((void**) &HEX3Atmp_157680, (*readfds));
	i_157682 = 0;
	l_157684 = HEX3Atmp_157680->Sup.len;
	while (1) {
		if (!(i_157682 < l_157684)) goto LA1;
		s_157643 = HEX3Atmp_157680->data[i_157682];
		{
			NIM_BOOL LOC4;
			LOC4 = 0;
			LOC4 = hasdatabuffered_157504(s_157643);
			if (!LOC4) goto LA5;
			result += 1;
			res = (TY158815*) incrSeq(&(res)->Sup, sizeof(tsocketimpl150605*));
			asgnRefNoCycle((void**) &res->data[res->Sup.len-1], s_157643);
		}
		LA5: ;
		i_157682 += 1;
	} LA1: ;
	{
		if (!(0 < result)) goto LA9;
		genericSeqAssign(&(*readfds), res, (&NTI158815));
	}
	LA9: ;
	return result;
}

N_NIMCALL(struct timeval, timevalfrommilliseconds_157201)(NI timeout) {
	struct timeval result;
	memset((void*)&result, 0, sizeof(result));
	{
		NI seconds;
		if (!!((timeout == -1))) goto LA3;
		seconds = (NI32)(timeout / 1000);
		result.tv_sec = ((NI) (((NI32) (seconds))));
		result.tv_usec = ((NI) (((NI32) ((NI32)((NI32)(timeout - (NI32)(seconds * 1000)) * 1000)))));
	}
	LA3: ;
	return result;
}

N_NIMCALL(void, createfdset_157214)(fd_set* fd, TY158815* s, NI* m) {
	tsocketimpl150605* i_157403;
	NI i_157417;
	NI l_157419;
	FD_ZERO(fd);
	i_157403 = 0;
	i_157417 = 0;
	l_157419 = s->Sup.len;
	while (1) {
		if (!(i_157417 < l_157419)) goto LA1;
		i_157403 = s->data[i_157417];
		(*m) = (((*m) >= ((NI) ((*i_157403).Fd))) ? (*m) : ((NI) ((*i_157403).Fd)));
		FD_SET((*i_157403).Fd, fd);
		i_157417 += 1;
	} LA1: ;
}

N_NIMCALL(void, prunesocketset_157423)(TY158815** s, fd_set* fd) {
	NI i;
	NI l;
	i = 0;
	l = (*s)->Sup.len;
	while (1) {
		if (!(i < l)) goto LA1;
		{
			int LOC4;
			LOC4 = 0;
			LOC4 = FD_ISSET((*(*s)->data[i]).Fd, fd);
			if (!(LOC4 == ((NI32) 0))) goto LA5;
			asgnRefNoCycle((void**) &(*s)->data[i], (*s)->data[(NI32)(l - 1)]);
			l -= 1;
		}
		goto LA2;
		LA5: ;
		{
			i += 1;
		}
		LA2: ;
	} LA1: ;
	(*s) = (TY158815*) setLengthSeq(&((*s))->Sup, sizeof(tsocketimpl150605*), l);
}

N_NIMCALL(NI, select_157881)(TY158815** readfds, NI timeout) {
	NI result;
	NI buffersfilled;
	struct timeval tv;
	fd_set rd;
	NI m;
	result = 0;
	buffersfilled = checkbuffer_157603(readfds);
	{
		if (!(0 < buffersfilled)) goto LA3;
		result = buffersfilled;
		goto BeforeRet;
	}
	LA3: ;
	tv = timevalfrommilliseconds_157201(timeout);
	memset((void*)&rd, 0, sizeof(rd));
	m = 0;
	createfdset_157214(&rd, (*readfds), &m);
	{
		int LOC9;
		if (!!((timeout == -1))) goto LA7;
		LOC9 = 0;
		LOC9 = select(((int) ((NI32)(m + 1))), &rd, NIM_NIL, NIM_NIL, &tv);
		result = ((NI) (LOC9));
	}
	goto LA5;
	LA7: ;
	{
		int LOC11;
		LOC11 = 0;
		LOC11 = select(((int) ((NI32)(m + 1))), &rd, NIM_NIL, NIM_NIL, NIM_NIL);
		result = ((NI) (LOC11));
	}
	LA5: ;
	prunesocketset_157423(readfds, &rd);
	BeforeRet: ;
	return result;
}

static N_INLINE(void, HEX2BHEX3D_94274)(NF* x_94280, NF y_94283) {
	(*x_94280) = ((NF)((*x_94280)) + (NF)(y_94283));
}

N_NIMCALL(NI, waitfor_158408)(tsocketimpl150605* socket, NF* waited, NI timeout, NI size, NimStringDesc* funcname) {
	NI result;
	result = 0;
	result = 1;
	{
		if (!(size <= 0)) goto LA3;
	}
	LA3: ;
	{
		if (!(timeout == -1)) goto LA7;
		result = size;
		goto BeforeRet;
	}
	LA7: ;
	{
		NIM_BOOL LOC11;
		NIM_BOOL LOC12;
		LOC11 = 0;
		LOC12 = 0;
		LOC12 = (*socket).Isbuffered;
		if (!(LOC12)) goto LA13;
		LOC12 = !(((*socket).isbufferedU.S1.Buflen == 0));
		LA13: ;
		LOC11 = LOC12;
		if (!(LOC11)) goto LA14;
		LOC11 = !(((*socket).isbufferedU.S1.Buflen == (*socket).isbufferedU.S1.Currpos));
		LA14: ;
		if (!LOC11) goto LA15;
		result = (NI32)((*socket).isbufferedU.S1.Buflen - (*socket).isbufferedU.S1.Currpos);
		result = ((result <= size) ? result : size);
	}
	goto LA9;
	LA15: ;
	{
		TY158815* s;
		TY158817 LOC23;
		NF starttime;
		NI selret;
		NF LOC34;
		{
			etimeout150627* e_158678;
			NimStringDesc* LOC22;
			if (!((NI32)(timeout - ((NI) (((NF)((*waited)) * (NF)(1.0000000000000000e+03))))) < 1)) goto LA20;
			e_158678 = 0;
			e_158678 = (etimeout150627*) newObj((&NTI158679), sizeof(etimeout150627));
			(*e_158678).Sup.Sup.Sup.m_type = (&NTI150627);
			LOC22 = 0;
			LOC22 = rawNewString(funcname->Sup.len + 21);
appendString(LOC22, ((NimStringDesc*) &TMP4541));
appendString(LOC22, funcname);
appendString(LOC22, ((NimStringDesc*) &TMP4542));
			asgnRefNoCycle((void**) &(*e_158678).Sup.Sup.message, LOC22);
			raiseException((E_Base*)e_158678, "ETimeout");
		}
		LA20: ;
		s = (TY158815*) newSeq((&NTI158815), 1);
		memset((void*)LOC23, 0, sizeof(LOC23));
		LOC23[0] = socket;
		asgnRefNoCycle((void**) &s->data[0], LOC23[0]);
		starttime = ntepochTime();
		selret = select_157881(&s, (NI32)(timeout - ((NI) (((NF)((*waited)) * (NF)(1.0000000000000000e+03))))));
		{
			NI32 LOC28;
			if (!(selret < 0)) goto LA26;
			LOC28 = 0;
			LOC28 = oslasterror_106231();
			oserror_106205(LOC28);
		}
		LA26: ;
		{
			etimeout150627* e_158827;
			NimStringDesc* LOC33;
			if (!!((selret == 1))) goto LA31;
			e_158827 = 0;
			e_158827 = (etimeout150627*) newObj((&NTI158679), sizeof(etimeout150627));
			(*e_158827).Sup.Sup.Sup.m_type = (&NTI150627);
			LOC33 = 0;
			LOC33 = rawNewString(funcname->Sup.len + 21);
appendString(LOC33, ((NimStringDesc*) &TMP4541));
appendString(LOC33, funcname);
appendString(LOC33, ((NimStringDesc*) &TMP4542));
			asgnRefNoCycle((void**) &(*e_158827).Sup.Sup.message, LOC33);
			raiseException((E_Base*)e_158827, "ETimeout");
		}
		LA31: ;
		LOC34 = 0;
		LOC34 = ntepochTime();
		HEX2BHEX3D_94274(waited, ((NF)(LOC34) - (NF)(starttime)));
	}
	LA9: ;
	BeforeRet: ;
	return result;
}

N_NIMCALL(NI, readintobuf_157938)(tsocketimpl150605* socket, NI32 flags) {
	NI result;
	result = 0;
	result = 0;
	result = recv((*socket).Fd, ((void*) ((*socket).isbufferedU.S1.Buffer)), 4000, flags);
	{
		if (!(result <= 0)) goto LA3;
		(*socket).isbufferedU.S1.Buflen = 0;
		(*socket).isbufferedU.S1.Currpos = 0;
		goto BeforeRet;
	}
	LA3: ;
	(*socket).isbufferedU.S1.Buflen = result;
	(*socket).isbufferedU.S1.Currpos = 0;
	BeforeRet: ;
	return result;
}

N_NIMCALL(NI, recv_158096)(tsocketimpl150605* socket, void* data, NI size) {
	NI result;
	result = 0;
	{
		if (!(size == 0)) goto LA3;
		goto BeforeRet;
	}
	LA3: ;
	{
		NI read;
		if (!(*socket).Isbuffered) goto LA7;
		{
			NI res;
			if (!((*socket).isbufferedU.S1.Buflen == 0)) goto LA11;
			res = readintobuf_157938(socket, ((NI32) 0));
			{
				if (!(res <= 0)) goto LA15;
				{
					if (!NIM_FALSE) goto LA19;
					result = 0;
					goto BeforeRet;
				}
				goto LA17;
				LA19: ;
				{
					result = res;
					goto BeforeRet;
				}
				LA17: ;
			}
			LA15: ;
		}
		LA11: ;
		read = 0;
		while (1) {
			NI chunk;
			NCSTRING d;
			if (!(read < size)) goto LA22;
			{
				NI res;
				if (!((*socket).isbufferedU.S1.Buflen <= (*socket).isbufferedU.S1.Currpos)) goto LA25;
				res = readintobuf_157938(socket, ((NI32) 0));
				{
					if (!(res <= 0)) goto LA29;
					{
						if (!(0 < read)) goto LA33;
						result = read;
						goto BeforeRet;
					}
					goto LA31;
					LA33: ;
					{
						result = res;
						goto BeforeRet;
					}
					LA31: ;
				}
				LA29: ;
			}
			LA25: ;
			chunk = (((NI32)((*socket).isbufferedU.S1.Buflen - (*socket).isbufferedU.S1.Currpos) <= (NI32)(size - read)) ? (NI32)((*socket).isbufferedU.S1.Buflen - (*socket).isbufferedU.S1.Currpos) : (NI32)(size - read));
			d = ((NCSTRING) (data));
			memcpy(((void*) (&d[read])), ((void*) (&(*socket).isbufferedU.S1.Buffer[((*socket).isbufferedU.S1.Currpos)- 0])), chunk);
			read += chunk;
			(*socket).isbufferedU.S1.Currpos += chunk;
		} LA22: ;
		result = read;
	}
	goto LA5;
	LA7: ;
	{
		result = recv((*socket).Fd, data, ((NI) (((int) (size)))), ((NI32) 0));
	}
	LA5: ;
	BeforeRet: ;
	return result;
}

N_NIMCALL(void, socketerror_151801)(tsocketimpl150605* socket, NI err, NIM_BOOL async) {
	{
		NIM_BOOL LOC3;
		NI32 lasterror;
		LOC3 = 0;
		LOC3 = (err == -1);
		if (!(LOC3)) goto LA4;
		LOC3 = NIM_TRUE;
		LA4: ;
		if (!LOC3) goto LA5;
		lasterror = oslasterror_106231();
		{
			if (!async) goto LA9;
			{
				NIM_BOOL LOC13;
				LOC13 = 0;
				LOC13 = (lasterror == EAGAIN);
				if (LOC13) goto LA14;
				LOC13 = (lasterror == EWOULDBLOCK);
				LA14: ;
				if (!LOC13) goto LA15;
				goto BeforeRet;
			}
			goto LA11;
			LA15: ;
			{
				oserror_106205(lasterror);
			}
			LA11: ;
		}
		goto LA7;
		LA9: ;
		{
			oserror_106205(lasterror);
		}
		LA7: ;
	}
	LA5: ;
	BeforeRet: ;
}

N_NIMCALL(NI, peekchar_158920)(tsocketimpl150605* socket, NIM_CHAR* c) {
	NI result;
	result = 0;
	{
		if (!(*socket).Isbuffered) goto LA3;
		result = 1;
		{
			NIM_BOOL LOC7;
			NI res;
			LOC7 = 0;
			LOC7 = ((*socket).isbufferedU.S1.Buflen == 0);
			if (LOC7) goto LA8;
			LOC7 = ((NI32)((*socket).isbufferedU.S1.Buflen - 1) < (*socket).isbufferedU.S1.Currpos);
			LA8: ;
			if (!LOC7) goto LA9;
			res = readintobuf_157938(socket, ((NI32) 0));
			{
				if (!(res <= 0)) goto LA13;
				result = res;
			}
			LA13: ;
		}
		LA9: ;
		(*c) = (*socket).isbufferedU.S1.Buffer[((*socket).isbufferedU.S1.Currpos)- 0];
	}
	goto LA1;
	LA3: ;
	{
		result = recv((*socket).Fd, ((void*) (c)), 1, MSG_PEEK);
	}
	LA1: ;
	return result;
}

N_NIMCALL(void, readline_159033)(tsocketimpl150605* socket, NimStringDesc** line, NI timeout) {
	NF waited;
	waited = 0.0;
	(*line) = setLengthStr((*line), 0);
	while (1) {
		NIM_CHAR c;
		NI LOC2;
		NI n;
		c = 0;
		LOC2 = 0;
		LOC2 = waitfor_158408(socket, &waited, timeout, 1, ((NimStringDesc*) &TMP4544));
		n = recv_158096(socket, ((void*) (&c)), 1);
		{
			if (!(n < 0)) goto LA5;
			socketerror_151801(socket, -1, NIM_FALSE);
		}
		goto LA3;
		LA5: ;
		{
			if (!(n == 0)) goto LA8;
			goto BeforeRet;
		}
		goto LA3;
		LA8: ;
		LA3: ;
		{
			NI LOC14;
			if (!((NU8)(c) == (NU8)(13))) goto LA12;
			LOC14 = 0;
			LOC14 = waitfor_158408(socket, &waited, timeout, 1, ((NimStringDesc*) &TMP4544));
			n = peekchar_158920(socket, &c);
			{
				NIM_BOOL LOC17;
				NI LOC21;
				LOC17 = 0;
				LOC17 = (0 < n);
				if (!(LOC17)) goto LA18;
				LOC17 = ((NU8)(c) == (NU8)(10));
				LA18: ;
				if (!LOC17) goto LA19;
				LOC21 = 0;
				LOC21 = recv_158096(socket, ((void*) (&c)), 1);
			}
			goto LA15;
			LA19: ;
			{
				if (!(n <= 0)) goto LA23;
				socketerror_151801(socket, -1, NIM_FALSE);
			}
			goto LA15;
			LA23: ;
			LA15: ;
			{
				if (!((*line)->Sup.len == 0)) goto LA27;
				(*line) = resizeString((*line), 2);
appendString((*line), ((NimStringDesc*) &TMP4545));
			}
			LA27: ;
			goto BeforeRet;
		}
		goto LA10;
		LA12: ;
		{
			if (!((NU8)(c) == (NU8)(10))) goto LA30;
			{
				if (!((*line)->Sup.len == 0)) goto LA34;
				(*line) = resizeString((*line), 2);
appendString((*line), ((NimStringDesc*) &TMP4545));
			}
			LA34: ;
			goto BeforeRet;
		}
		goto LA10;
		LA30: ;
		LA10: ;
		(*line) = addChar((*line), c);
	}
	BeforeRet: ;
}

N_NIMCALL(void, close_154266)(tsocketimpl150605* socket) {
	int LOC1;
	LOC1 = 0;
	LOC1 = close((*socket).Fd);
}
N_NOINLINE(void, stdlibsocketsInit)(void) {
	asgnRefNoCycle((void**) &invalidsocket_150678, NIM_NIL);
	osinvalidsocket_150681 = ((int) -1);
}

N_NOINLINE(void, stdlibsocketsDatInit)(void) {
static TNimNode* TMP4534[3];
static TNimNode* TMP4535[3];
static TNimNode TMP126[10];
NTI150605.size = sizeof(tsocketimpl150605);
NTI150605.kind = 18;
NTI150605.base = 0;
NTI150605.flags = 3;
TMP4534[0] = &TMP126[1];
TMP126[1].kind = 1;
TMP126[1].offset = offsetof(tsocketimpl150605, Fd);
TMP126[1].typ = (&NTI4409);
TMP126[1].name = "fd";
TMP4534[1] = &TMP126[2];
TMP126[2].kind = 3;
TMP126[2].offset = offsetof(tsocketimpl150605, Isbuffered);
TMP126[2].typ = (&NTI134);
TMP126[2].name = "isBuffered";
TMP126[2].sons = &NimDT_tsocketimpl_isbuffered[0];
TMP126[2].len = 2;
TMP4535[0] = &TMP126[4];
NTI150630.size = sizeof(TY150630);
NTI150630.kind = 16;
NTI150630.base = (&NTI141);
NTI150630.flags = 3;
TMP126[4].kind = 1;
TMP126[4].offset = offsetof(tsocketimpl150605, isbufferedU.S1.Buffer);
TMP126[4].typ = (&NTI150630);
TMP126[4].name = "buffer";
TMP4535[1] = &TMP126[5];
TMP126[5].kind = 1;
TMP126[5].offset = offsetof(tsocketimpl150605, isbufferedU.S1.Currpos);
TMP126[5].typ = (&NTI106);
TMP126[5].name = "currPos";
TMP4535[2] = &TMP126[6];
TMP126[6].kind = 1;
TMP126[6].offset = offsetof(tsocketimpl150605, isbufferedU.S1.Buflen);
TMP126[6].typ = (&NTI106);
TMP126[6].name = "bufLen";
TMP126[3].len = 3; TMP126[3].kind = 2; TMP126[3].sons = &TMP4535[0];
NimDT_tsocketimpl_isbuffered[1] = &TMP126[3];
TMP126[7].len = 0; TMP126[7].kind = 2;
NimDT_tsocketimpl_isbuffered[0] = &TMP126[7];
TMP4534[2] = &TMP126[8];
TMP126[8].kind = 1;
TMP126[8].offset = offsetof(tsocketimpl150605, Nonblocking);
TMP126[8].typ = (&NTI134);
TMP126[8].name = "nonblocking";
TMP126[0].len = 3; TMP126[0].kind = 2; TMP126[0].sons = &TMP4534[0];
NTI150605.node = &TMP126[0];
NTI150607.size = sizeof(tsocketimpl150605*);
NTI150607.kind = 22;
NTI150607.base = (&NTI150605);
NTI150607.flags = 2;
NTI150607.marker = TMP4536;
NTI150627.size = sizeof(etimeout150627);
NTI150627.kind = 17;
NTI150627.base = (&NTI2429);
TMP126[9].len = 0; TMP126[9].kind = 2;
NTI150627.node = &TMP126[9];
NTI158679.size = sizeof(etimeout150627*);
NTI158679.kind = 22;
NTI158679.base = (&NTI150627);
NTI158679.marker = TMP4540;
}

