/* Generated by Nim Compiler v0.10.2 */
/*   (c) 2014 Andreas Rumpf */
/* The generated code is subject to the original license. */
#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 tsocketimpl181607 tsocketimpl181607;
typedef struct tcell44935 tcell44935;
typedef struct TNimType TNimType;
typedef struct tcellseq44951 tcellseq44951;
typedef struct tgcheap47016 tgcheap47016;
typedef struct tcellset44947 tcellset44947;
typedef struct tpagedesc44943 tpagedesc44943;
typedef struct tmemregion27210 tmemregion27210;
typedef struct tsmallchunk26440 tsmallchunk26440;
typedef struct tllchunk27204 tllchunk27204;
typedef struct tbigchunk26442 tbigchunk26442;
typedef struct tintset26417 tintset26417;
typedef struct ttrunk26413 ttrunk26413;
typedef struct tavlnode27208 tavlnode27208;
typedef struct tgcstat47014 tgcstat47014;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct valueerror3249 valueerror3249;
typedef struct Exception Exception;
typedef struct TNimObject TNimObject;
typedef struct TNimNode TNimNode;
typedef struct oserror3233 oserror3233;
typedef struct systemerror3229 systemerror3229;
typedef struct timeouterror181629 timeouterror181629;
typedef struct TY190015 TY190015;
typedef struct tbasechunk26438 tbasechunk26438;
typedef struct tfreecell26430 tfreecell26430;
struct  tcell44935  {
NI Refcount;
TNimType* Typ;
};
struct  tcellseq44951  {
NI Len;
NI Cap;
tcell44935** D;
};
struct  tcellset44947  {
NI Counter;
NI Max;
tpagedesc44943* Head;
tpagedesc44943** Data;
};
typedef tsmallchunk26440* TY27222[512];
typedef ttrunk26413* ttrunkbuckets26415[256];
struct  tintset26417  {
ttrunkbuckets26415 Data;
};
struct  tmemregion27210  {
NI Minlargeobj;
NI Maxlargeobj;
TY27222 Freesmallchunks;
tllchunk27204* Llmem;
NI Currmem;
NI Maxmem;
NI Freemem;
NI Lastsize;
tbigchunk26442* Freechunkslist;
tintset26417 Chunkstarts;
tavlnode27208* Root;
tavlnode27208* Deleted;
tavlnode27208* Last;
tavlnode27208* Freeavlnodes;
};
struct  tgcstat47014  {
NI Stackscans;
NI Cyclecollections;
NI Maxthreshold;
NI Maxstacksize;
NI Maxstackcells;
NI Cycletablesize;
NI64 Maxpause;
};
struct  tgcheap47016  {
void* Stackbottom;
NI Cyclethreshold;
tcellseq44951 Zct;
tcellseq44951 Decstack;
tcellset44947 Cycleroots;
tcellseq44951 Tempstack;
NI Recgclock;
tmemregion27210 Region;
tgcstat47014 Stat;
};
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
typedef NIM_CHAR TY181632[4001];
struct  tsocketimpl181607  {
int Fd;
NIM_BOOL Isbuffered;
union {
struct {TY181632 Buffer;
NI Currpos;
NI Buflen;
} S1;
} isbufferedU;
NIM_BOOL Nonblocking;
};
typedef N_NIMCALL_PTR(void, TY3089) (void* p, NI op);
typedef N_NIMCALL_PTR(void*, TY3094) (void* p);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY3089 marker;
TY3094 deepcopy;
};
struct  TNimObject  {
TNimType* m_type;
};
struct  Exception  {
  TNimObject Sup;
Exception* parent;
NCSTRING name;
NimStringDesc* message;
NimStringDesc* trace;
};
struct  valueerror3249  {
  Exception Sup;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct  systemerror3229  {
  Exception Sup;
};
struct  oserror3233  {
  systemerror3229 Sup;
NI32 Errorcode;
};
struct  timeouterror181629  {
  Exception Sup;
};
typedef tsocketimpl181607* TY190017[1];
typedef NI TY26420[16];
struct  tpagedesc44943  {
tpagedesc44943* Next;
NI Key;
TY26420 Bits;
};
struct  tbasechunk26438  {
NI Prevsize;
NI Size;
NIM_BOOL Used;
};
struct  tsmallchunk26440  {
  tbasechunk26438 Sup;
tsmallchunk26440* Next;
tsmallchunk26440* Prev;
tfreecell26430* Freelist;
NI Free;
NI Acc;
NF Data;
};
struct  tllchunk27204  {
NI Size;
NI Acc;
tllchunk27204* Next;
};
struct  tbigchunk26442  {
  tbasechunk26438 Sup;
tbigchunk26442* Next;
tbigchunk26442* Prev;
NI Align;
NF Data;
};
struct  ttrunk26413  {
ttrunk26413* Next;
NI Key;
TY26420 Bits;
};
typedef tavlnode27208* TY27214[2];
struct  tavlnode27208  {
TY27214 Link;
NI Key;
NI Upperbound;
NI Level;
};
typedef NIM_CHAR TY104554[256];
struct  tfreecell26430  {
tfreecell26430* Next;
NI Zerofield;
};
struct TY190015 {
  TGenericSeq Sup;
  tsocketimpl181607* data[SEQ_DECL_SIZE];
};
static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src);
static N_INLINE(tcell44935*, usrtocell_48646)(void* usr);
static N_INLINE(void, rtladdzct_50204)(tcell44935* c);
N_NOINLINE(void, addzct_48617)(tcellseq44951* s, tcell44935* 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)(Exception* e, NCSTRING ename);
N_NIMCALL(NI, send_191464)(tsocketimpl181607* socket, void* data, NI size);
N_NIMCALL(void, raiseoserror_113809)(NI32 errorcode);
N_NIMCALL(NI32, oslasterror_113841)(void);
N_NIMCALL(tsocketimpl181607*, newtsocket_182004)(int fd, NIM_BOOL isbuff);
N_NIMCALL(void, TMP4863)(void* p, NI op);
N_NIMCALL(int, toint_182427)(NU8 domain);
N_NIMCALL(int, toint_182434)(NU8 typ);
N_NIMCALL(int, toint_182441)(NU8 p);
N_NIMCALL(NI16, htons_182417)(NI16 x);
N_NIMCALL(NI16, ntohs_182207)(NI16 x);
N_NIMCALL(NI32, htonl_182407)(NI32 x);
N_NIMCALL(NI32, ntohl_182070)(NI32 x);
N_NIMCALL(NimStringDesc*, HEX24_6201)(NU64 x);
N_NIMCALL(NimStringDesc*, cstrToNimstr)(NCSTRING str);
N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
N_NIMCALL(void, acceptaddr_184248)(tsocketimpl181607* server, tsocketimpl181607** client, NimStringDesc** address);
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(NimStringDesc*, setLengthStr)(NimStringDesc* s, NI newlen);
N_NIMCALL(NI, waitfor_189618)(tsocketimpl181607* socket, NF* waited, NI timeout, NI size, NimStringDesc* funcname);
N_NIMCALL(void, nimGCvisit)(void* d, NI op);
N_NIMCALL(void, TMP4867)(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_189107)(TY190015** readfds, NI timeout);
N_NIMCALL(NI, checkbuffer_188807)(TY190015** readfds);
N_NIMCALL(NIM_BOOL, hasdatabuffered_188728)(tsocketimpl181607* s);
N_NIMCALL(TGenericSeq*, incrSeq)(TGenericSeq* seq, NI elemsize);
N_NIMCALL(void, genericSeqAssign)(void* dest, void* src_79604, TNimType* mt);
N_NIMCALL(struct timeval, timevalfrommilliseconds_188401)(NI timeout);
N_NIMCALL(void, createfdset_188418)(fd_set* fd, TY190015* s, NI* m);
N_NIMCALL(void, prunesocketset_188645)(TY190015** s, fd_set* fd);
N_NIMCALL(TGenericSeq*, setLengthSeq)(TGenericSeq* seq, NI elemsize, NI newlen);
static N_INLINE(void, HEX2BHEX3D_101492)(NF* x_101498, NF y_101501);
N_NIMCALL(NI, recv_189302)(tsocketimpl181607* socket, void* data, NI size);
N_NIMCALL(NI, readintobuf_189168)(tsocketimpl181607* socket, NI32 flags);
N_NIMCALL(void, raisesocketerror_183001)(tsocketimpl181607* socket, NI err, NIM_BOOL async);
N_NIMCALL(NI, peekchar_190138)(tsocketimpl181607* socket, NIM_CHAR* c);
N_NIMCALL(NimStringDesc*, resizeString)(NimStringDesc* dest, NI addlen);
N_NIMCALL(NimStringDesc*, addChar)(NimStringDesc* s, NIM_CHAR c);
STRING_LITERAL(TMP1980, "This function cannot be used on non-blocking sockets.", 53);
STRING_LITERAL(TMP1981, "Could not send all data.", 24);
TNimNode* NimDT_181607_isbuffered[3];
STRING_LITERAL(TMP4866, "", 0);
STRING_LITERAL(TMP4868, "Call to \'", 9);
STRING_LITERAL(TMP4869, "\' timed out.", 12);
STRING_LITERAL(TMP4871, "readLine", 8);
STRING_LITERAL(TMP4872, "\015\012", 2);
tsocketimpl181607* invalidsocket_181696;
extern tgcheap47016 gch_47044;
int osinvalidsocket_181805;
extern TNimType NTI21401; /* ref ValueError */
extern TNimType NTI3249; /* ValueError */
extern TNimType NTI113812; /* ref OSError */
extern TNimType NTI3233; /* OSError */
TNimType NTI181607; /* TSocketImpl */
extern TNimType NTI5611; /* cint */
extern TNimType NTI138; /* bool */
extern TNimType NTI147; /* char */
TNimType NTI181632; /* array[0..4000, char] */
extern TNimType NTI108; /* int */
TNimType NTI181609; /* Socket */
extern TNimType NTI3227; /* Exception */
TNimType NTI181629; /* TimeoutError */
TNimType NTI189879; /* ref TimeoutError */
extern TNimType NTI190015; /* seq[Socket] */

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

static N_INLINE(void, rtladdzct_50204)(tcell44935* c) {
	addzct_48617((&gch_47044.Zct), c);
}

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

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

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

N_NIMCALL(void, send_192008)(tsocketimpl181607* socket, NimStringDesc* data) {
	NI sent;
	{
		valueerror3249* e_192014;
		NimStringDesc* LOC5;
		if (!(*socket).Nonblocking) goto LA3;
		e_192014 = 0;
		e_192014 = (valueerror3249*) newObj((&NTI21401), sizeof(valueerror3249));
		(*e_192014).Sup.Sup.m_type = (&NTI3249);
		LOC5 = 0;
		LOC5 = (*e_192014).Sup.message; (*e_192014).Sup.message = copyStringRC1(((NimStringDesc*) &TMP1980));
		if (LOC5) nimGCunrefNoCycle(LOC5);
		raiseException((Exception*)e_192014, "ValueError");
	}
	LA3: ;
	sent = send_191464(socket, ((void*) (data->data)), data->Sup.len);
	{
		NI32 LOC10;
		if (!(sent < 0)) goto LA8;
		LOC10 = 0;
		LOC10 = oslasterror_113841();
		raiseoserror_113809(LOC10);
	}
	LA8: ;
	{
		oserror3233* e_192203;
		NimStringDesc* LOC15;
		if (!!((sent == data->Sup.len))) goto LA13;
		e_192203 = 0;
		e_192203 = (oserror3233*) newObj((&NTI113812), sizeof(oserror3233));
		(*e_192203).Sup.Sup.Sup.m_type = (&NTI3233);
		LOC15 = 0;
		LOC15 = (*e_192203).Sup.Sup.message; (*e_192203).Sup.Sup.message = copyStringRC1(((NimStringDesc*) &TMP1981));
		if (LOC15) nimGCunrefNoCycle(LOC15);
		raiseException((Exception*)e_192203, "OSError");
	}
	LA13: ;
}
N_NIMCALL(void, TMP4863)(void* p, NI op) {
	tsocketimpl181607* a;
	NI LOC1;
	a = (tsocketimpl181607*)p;
	switch ((*a).Isbuffered) {
	case NIM_TRUE:
	LOC1 = 0;
	for (LOC1 = 0; LOC1 < 4001; LOC1++) {
	}
	break;
	case NIM_FALSE:
	break;
	} 
}

N_NIMCALL(tsocketimpl181607*, newtsocket_182004)(int fd, NIM_BOOL isbuff) {
	tsocketimpl181607* result;
	result = 0;
	{
		if (!(fd == osinvalidsocket_181805)) goto LA3;
		result = NIM_NIL;
		goto BeforeRet;
	}
	LA3: ;
	result = (tsocketimpl181607*) newObj((&NTI181609), sizeof(tsocketimpl181607));
	(*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_182427)(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_182434)(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_182441)(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(tsocketimpl181607*, socket_182604)(NU8 domain, NU8 typ, NU8 protocol, NIM_BOOL buffered) {
	tsocketimpl181607* result;
	int LOC1;
	int LOC2;
	int LOC3;
	int LOC4;
	result = 0;
	LOC1 = 0;
	LOC1 = toint_182427(domain);
	LOC2 = 0;
	LOC2 = toint_182434(typ);
	LOC3 = 0;
	LOC3 = toint_182441(protocol);
	LOC4 = 0;
	LOC4 = socket(LOC1, LOC2, LOC3);
	result = newtsocket_182004(LOC4, buffered);
	return result;
}

N_NIMCALL(NI16, ntohs_182207)(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_182417)(NI16 x) {
	NI16 result;
	result = 0;
	result = ntohs_182207(x);
	return result;
}

N_NIMCALL(NI32, ntohl_182070)(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_182407)(NI32 x) {
	NI32 result;
	result = 0;
	result = ntohl_182070(x);
	return result;
}

N_NIMCALL(void, bindaddr_183774)(tsocketimpl181607* 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_182417(((NI16) (port)));
		name.sin_addr.s_addr = htonl_182407(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_113841();
			raiseoserror_113809(LOC10);
		}
		LA8: ;
	}
	goto LA1;
	LA3: ;
	{
		struct addrinfo hints;
		struct addrinfo* ailist;
		memset((void*)(&hints), 0, sizeof(hints));
		ailist = NIM_NIL;
		hints.ai_family = toint_182427(((NU8) 2));
		hints.ai_socktype = toint_182434(((NU8) 1));
		hints.ai_protocol = toint_182441(((NU8) 6));
		{
			int gairesult_183808;
			NimStringDesc* LOC13;
			LOC13 = 0;
			LOC13 = HEX24_6201(port);
			gairesult_183808 = getaddrinfo(address->data, LOC13->data, (&hints), &ailist);
			{
				oserror3233* e_184002;
				NCSTRING LOC18;
				if (!!((gairesult_183808 == ((NI32) 0)))) goto LA16;
				e_184002 = 0;
				e_184002 = (oserror3233*) newObj((&NTI113812), sizeof(oserror3233));
				(*e_184002).Sup.Sup.Sup.m_type = (&NTI3233);
				LOC18 = 0;
				LOC18 = gai_strerror(gairesult_183808);
				asgnRefNoCycle((void**) (&(*e_184002).Sup.Sup.message), cstrToNimstr(LOC18));
				raiseException((Exception*)e_184002, "OSError");
			}
			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_113841();
			raiseoserror_113809(LOC24);
		}
		LA22: ;
	}
	LA1: ;
}

N_NIMCALL(void, listen_183609)(tsocketimpl181607* socket, int backlog) {
	{
		int LOC3;
		NI32 LOC6;
		LOC3 = 0;
		LOC3 = listen((*socket).Fd, backlog);
		if (!(LOC3 < ((NI32) 0))) goto LA4;
		LOC6 = 0;
		LOC6 = oslasterror_113841();
		raiseoserror_113809(LOC6);
	}
	LA4: ;
}

N_NIMCALL(void, acceptaddr_184248)(tsocketimpl181607* server, tsocketimpl181607** client, NimStringDesc** address) {
	struct sockaddr_in sockaddress_184256;
	socklen_t addrlen_184258;
	int sock_184260;
	memset((void*)(&sockaddress_184256), 0, sizeof(sockaddress_184256));
	addrlen_184258 = ((socklen_t) (((NI)sizeof(struct sockaddr_in))));
	sock_184260 = accept((*server).Fd, ((struct sockaddr*) ((&sockaddress_184256))), (&addrlen_184258));
	{
		NI32 err_184262;
		if (!(sock_184260 == osinvalidsocket_181805)) goto LA3;
		err_184262 = oslasterror_113841();
		{
			NIM_BOOL LOC7;
			LOC7 = 0;
			LOC7 = (err_184262 == EAGAIN);
			if (LOC7) goto LA8;
			LOC7 = (err_184262 == EWOULDBLOCK);
			LA8: ;
			if (!LOC7) goto LA9;
			unsureAsgnRef((void**) (&(*client)), invalidsocket_181696);
			unsureAsgnRef((void**) (&(*address)), copyString(((NimStringDesc*) &TMP4866)));
			goto BeforeRet;
		}
		goto LA5;
		LA9: ;
		{
			raiseoserror_113809(err_184262);
		}
		LA5: ;
	}
	goto LA1;
	LA3: ;
	{
		NCSTRING LOC13;
		(*(*client)).Fd = sock_184260;
		(*(*client)).Isbuffered = (*server).Isbuffered;
		LOC13 = 0;
		LOC13 = inet_ntoa(sockaddress_184256.sin_addr);
		unsureAsgnRef((void**) (&(*address)), cstrToNimstr(LOC13));
		goto BeforeRet;
	}
	LA1: ;
	BeforeRet: ;
}

N_NIMCALL(void, accept_185401)(tsocketimpl181607* server, tsocketimpl181607** client) {
	NimStringDesc* addrdummy;
	addrdummy = copyString(((NimStringDesc*) &TMP4866));
	acceptaddr_184248(server, client, (&addrdummy));
}
N_NIMCALL(void, TMP4867)(void* p, NI op) {
	timeouterror181629* a;
	a = (timeouterror181629*)p;
	nimGCvisit((void*)(*a).Sup.parent, op);
	nimGCvisit((void*)(*a).Sup.message, op);
	nimGCvisit((void*)(*a).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_188728)(tsocketimpl181607* 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_188807)(TY190015** readfds) {
	NI result;
	TY190015* res;
	result = 0;
	res = (TY190015*) newSeq((&NTI190015), 0);
	result = 0;
	{
		tsocketimpl181607* s_188847;
		TY190015* HEX3Atmp_188881;
		NI i_188884;
		NI l_188886;
		s_188847 = 0;
		HEX3Atmp_188881 = 0;
		unsureAsgnRef((void**) (&HEX3Atmp_188881), (*readfds));
		i_188884 = 0;
		l_188886 = HEX3Atmp_188881->Sup.len;
		{
			while (1) {
				if (!(i_188884 < l_188886)) goto LA3;
				s_188847 = HEX3Atmp_188881->data[i_188884];
				{
					NIM_BOOL LOC6;
					LOC6 = 0;
					LOC6 = hasdatabuffered_188728(s_188847);
					if (!LOC6) goto LA7;
					result += 1;
					res = (TY190015*) incrSeq(&(res)->Sup, sizeof(tsocketimpl181607*));
					asgnRefNoCycle((void**) (&res->data[res->Sup.len-1]), s_188847);
				}
				LA7: ;
				i_188884 += 1;
			} LA3: ;
		}
	}
	{
		if (!(0 < result)) goto LA11;
		genericSeqAssign((&(*readfds)), res, (&NTI190015));
	}
	LA11: ;
	return result;
}

N_NIMCALL(struct timeval, timevalfrommilliseconds_188401)(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_188418)(fd_set* fd, TY190015* s, NI* m) {
	FD_ZERO(fd);
	{
		tsocketimpl181607* i_188611;
		NI i_188627;
		NI l_188629;
		i_188611 = 0;
		i_188627 = 0;
		l_188629 = s->Sup.len;
		{
			while (1) {
				if (!(i_188627 < l_188629)) goto LA3;
				i_188611 = s->data[i_188627];
				(*m) = (((*m) >= ((NI) ((*i_188611).Fd))) ? (*m) : ((NI) ((*i_188611).Fd)));
				FD_SET((*i_188611).Fd, fd);
				i_188627 += 1;
			} LA3: ;
		}
	}
}

N_NIMCALL(void, prunesocketset_188645)(TY190015** s, fd_set* fd) {
	NI i;
	NI l;
	i = 0;
	l = (*s)->Sup.len;
	{
		while (1) {
			if (!(i < l)) goto LA2;
			{
				int LOC5;
				LOC5 = 0;
				LOC5 = FD_ISSET((*(*s)->data[i]).Fd, fd);
				if (!(LOC5 == ((NI32) 0))) goto LA6;
				asgnRefNoCycle((void**) (&(*s)->data[i]), (*s)->data[(NI32)(l - 1)]);
				l -= 1;
			}
			goto LA3;
			LA6: ;
			{
				i += 1;
			}
			LA3: ;
		} LA2: ;
	}
	(*s) = (TY190015*) setLengthSeq(&((*s))->Sup, sizeof(tsocketimpl181607*), l);
}

N_NIMCALL(NI, select_189107)(TY190015** readfds, NI timeout) {
	NI result;
	NI buffersfilled;
	struct timeval tv;
	fd_set rd;
	NI m;
	result = 0;
	buffersfilled = checkbuffer_188807(readfds);
	{
		if (!(0 < buffersfilled)) goto LA3;
		result = buffersfilled;
		goto BeforeRet;
	}
	LA3: ;
	tv = timevalfrommilliseconds_188401(timeout);
	memset((void*)(&rd), 0, sizeof(rd));
	m = 0;
	createfdset_188418((&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_188645(readfds, (&rd));
	BeforeRet: ;
	return result;
}

static N_INLINE(void, HEX2BHEX3D_101492)(NF* x_101498, NF y_101501) {
	(*x_101498) = ((NF)((*x_101498)) + (NF)(y_101501));
}

N_NIMCALL(NI, waitfor_189618)(tsocketimpl181607* 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: ;
	{
		TY190015* s;
		TY190017 LOC23;
		NF starttime;
		NI selret;
		NF LOC34;
		{
			timeouterror181629* e_189878;
			NimStringDesc* LOC22;
			if (!((NI32)(timeout - ((NI) (((NF)((*waited)) * (NF)(1.0000000000000000e+03))))) < 1)) goto LA20;
			e_189878 = 0;
			e_189878 = (timeouterror181629*) newObj((&NTI189879), sizeof(timeouterror181629));
			(*e_189878).Sup.Sup.m_type = (&NTI181629);
			LOC22 = 0;
			LOC22 = rawNewString(funcname->Sup.len + 21);
appendString(LOC22, ((NimStringDesc*) &TMP4868));
appendString(LOC22, funcname);
appendString(LOC22, ((NimStringDesc*) &TMP4869));
			asgnRefNoCycle((void**) (&(*e_189878).Sup.message), LOC22);
			raiseException((Exception*)e_189878, "TimeoutError");
		}
		LA20: ;
		s = (TY190015*) newSeq((&NTI190015), 1);
		memset((void*)LOC23, 0, sizeof(LOC23));
		LOC23[0] = socket;
		asgnRefNoCycle((void**) (&s->data[0]), LOC23[0]);
		starttime = ntepochTime();
		selret = select_189107((&s), (NI32)(timeout - ((NI) (((NF)((*waited)) * (NF)(1.0000000000000000e+03))))));
		{
			NI32 LOC28;
			if (!(selret < 0)) goto LA26;
			LOC28 = 0;
			LOC28 = oslasterror_113841();
			raiseoserror_113809(LOC28);
		}
		LA26: ;
		{
			timeouterror181629* e_190027;
			NimStringDesc* LOC33;
			if (!!((selret == 1))) goto LA31;
			e_190027 = 0;
			e_190027 = (timeouterror181629*) newObj((&NTI189879), sizeof(timeouterror181629));
			(*e_190027).Sup.Sup.m_type = (&NTI181629);
			LOC33 = 0;
			LOC33 = rawNewString(funcname->Sup.len + 21);
appendString(LOC33, ((NimStringDesc*) &TMP4868));
appendString(LOC33, funcname);
appendString(LOC33, ((NimStringDesc*) &TMP4869));
			asgnRefNoCycle((void**) (&(*e_190027).Sup.message), LOC33);
			raiseException((Exception*)e_190027, "TimeoutError");
		}
		LA31: ;
		LOC34 = 0;
		LOC34 = ntepochTime();
		HEX2BHEX3D_101492(waited, ((NF)(LOC34) - (NF)(starttime)));
	}
	LA9: ;
	BeforeRet: ;
	return result;
}

N_NIMCALL(NI, readintobuf_189168)(tsocketimpl181607* 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_189302)(tsocketimpl181607* 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_189168(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 LA23;
				{
					NI res;
					if (!((*socket).isbufferedU.S1.Buflen <= (*socket).isbufferedU.S1.Currpos)) goto LA26;
					res = readintobuf_189168(socket, ((NI32) 0));
					{
						if (!(res <= 0)) goto LA30;
						{
							if (!(0 < read)) goto LA34;
							result = read;
							goto BeforeRet;
						}
						goto LA32;
						LA34: ;
						{
							result = res;
							goto BeforeRet;
						}
						LA32: ;
					}
					LA30: ;
				}
				LA26: ;
				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;
			} LA23: ;
		}
		result = read;
	}
	goto LA5;
	LA7: ;
	{
		result = recv((*socket).Fd, data, ((NI) (((int) (size)))), ((NI32) 0));
	}
	LA5: ;
	BeforeRet: ;
	return result;
}

N_NIMCALL(void, raisesocketerror_183001)(tsocketimpl181607* 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_113841();
		{
			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: ;
			{
				raiseoserror_113809(lasterror);
			}
			LA11: ;
		}
		goto LA7;
		LA9: ;
		{
			raiseoserror_113809(lasterror);
		}
		LA7: ;
	}
	LA5: ;
	BeforeRet: ;
}

N_NIMCALL(NI, peekchar_190138)(tsocketimpl181607* 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_189168(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_190447)(tsocketimpl181607* socket, NimStringDesc** line, NI timeout) {
	NF waited;
	waited = 0.0;
	(*line) = setLengthStr((*line), 0);
	{
		while (1) {
			NIM_CHAR c;
			NI LOC3;
			NI n;
			c = 0;
			LOC3 = 0;
			LOC3 = waitfor_189618(socket, (&waited), timeout, 1, ((NimStringDesc*) &TMP4871));
			n = recv_189302(socket, ((void*) ((&c))), 1);
			{
				if (!(n < 0)) goto LA6;
				raisesocketerror_183001(socket, -1, NIM_FALSE);
			}
			goto LA4;
			LA6: ;
			{
				if (!(n == 0)) goto LA9;
				goto BeforeRet;
			}
			goto LA4;
			LA9: ;
			LA4: ;
			{
				NI LOC15;
				if (!((NU8)(c) == (NU8)(13))) goto LA13;
				LOC15 = 0;
				LOC15 = waitfor_189618(socket, (&waited), timeout, 1, ((NimStringDesc*) &TMP4871));
				n = peekchar_190138(socket, (&c));
				{
					NIM_BOOL LOC18;
					NI LOC22;
					LOC18 = 0;
					LOC18 = (0 < n);
					if (!(LOC18)) goto LA19;
					LOC18 = ((NU8)(c) == (NU8)(10));
					LA19: ;
					if (!LOC18) goto LA20;
					LOC22 = 0;
					LOC22 = recv_189302(socket, ((void*) ((&c))), 1);
				}
				goto LA16;
				LA20: ;
				{
					if (!(n <= 0)) goto LA24;
					raisesocketerror_183001(socket, -1, NIM_FALSE);
				}
				goto LA16;
				LA24: ;
				LA16: ;
				{
					if (!((*line)->Sup.len == 0)) goto LA28;
					(*line) = resizeString((*line), 2);
appendString((*line), ((NimStringDesc*) &TMP4872));
				}
				LA28: ;
				goto BeforeRet;
			}
			goto LA11;
			LA13: ;
			{
				if (!((NU8)(c) == (NU8)(10))) goto LA31;
				{
					if (!((*line)->Sup.len == 0)) goto LA35;
					(*line) = resizeString((*line), 2);
appendString((*line), ((NimStringDesc*) &TMP4872));
				}
				LA35: ;
				goto BeforeRet;
			}
			goto LA11;
			LA31: ;
			LA11: ;
			(*line) = addChar((*line), c);
		}
	}
	BeforeRet: ;
}

N_NIMCALL(void, close_185478)(tsocketimpl181607* socket) {
	int LOC1;
	LOC1 = 0;
	LOC1 = close((*socket).Fd);
}
NIM_EXTERNC N_NOINLINE(void, stdlib_socketsInit)(void) {
	asgnRefNoCycle((void**) (&invalidsocket_181696), NIM_NIL);
	osinvalidsocket_181805 = ((int) -1);
}

NIM_EXTERNC N_NOINLINE(void, stdlib_socketsDatInit)(void) {
static TNimNode* TMP4861[3];
static TNimNode* TMP4862[3];
static TNimNode TMP126[10];
NTI181607.size = sizeof(tsocketimpl181607);
NTI181607.kind = 18;
NTI181607.base = 0;
NTI181607.flags = 3;
TMP4861[0] = &TMP126[1];
TMP126[1].kind = 1;
TMP126[1].offset = offsetof(tsocketimpl181607, Fd);
TMP126[1].typ = (&NTI5611);
TMP126[1].name = "fd";
TMP4861[1] = &TMP126[2];
TMP126[2].kind = 3;
TMP126[2].offset = offsetof(tsocketimpl181607, Isbuffered);
TMP126[2].typ = (&NTI138);
TMP126[2].name = "isBuffered";
TMP126[2].sons = &NimDT_181607_isbuffered[0];
TMP126[2].len = 2;
TMP4862[0] = &TMP126[4];
NTI181632.size = sizeof(TY181632);
NTI181632.kind = 16;
NTI181632.base = (&NTI147);
NTI181632.flags = 3;
TMP126[4].kind = 1;
TMP126[4].offset = offsetof(tsocketimpl181607, isbufferedU.S1.Buffer);
TMP126[4].typ = (&NTI181632);
TMP126[4].name = "buffer";
TMP4862[1] = &TMP126[5];
TMP126[5].kind = 1;
TMP126[5].offset = offsetof(tsocketimpl181607, isbufferedU.S1.Currpos);
TMP126[5].typ = (&NTI108);
TMP126[5].name = "currPos";
TMP4862[2] = &TMP126[6];
TMP126[6].kind = 1;
TMP126[6].offset = offsetof(tsocketimpl181607, isbufferedU.S1.Buflen);
TMP126[6].typ = (&NTI108);
TMP126[6].name = "bufLen";
TMP126[3].len = 3; TMP126[3].kind = 2; TMP126[3].sons = &TMP4862[0];
NimDT_181607_isbuffered[1] = &TMP126[3];
TMP126[7].len = 0; TMP126[7].kind = 2;
NimDT_181607_isbuffered[0] = &TMP126[7];
TMP4861[2] = &TMP126[8];
TMP126[8].kind = 1;
TMP126[8].offset = offsetof(tsocketimpl181607, Nonblocking);
TMP126[8].typ = (&NTI138);
TMP126[8].name = "nonblocking";
TMP126[0].len = 3; TMP126[0].kind = 2; TMP126[0].sons = &TMP4861[0];
NTI181607.node = &TMP126[0];
NTI181609.size = sizeof(tsocketimpl181607*);
NTI181609.kind = 22;
NTI181609.base = (&NTI181607);
NTI181609.flags = 2;
NTI181609.marker = TMP4863;
NTI181629.size = sizeof(timeouterror181629);
NTI181629.kind = 17;
NTI181629.base = (&NTI3227);
TMP126[9].len = 0; TMP126[9].kind = 2;
NTI181629.node = &TMP126[9];
NTI189879.size = sizeof(timeouterror181629*);
NTI189879.kind = 22;
NTI189879.base = (&NTI181629);
NTI189879.marker = TMP4867;
}

