/* 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 "Winsock2.h"

#include <string.h>
typedef struct tsocketimpl183607 tsocketimpl183607;
typedef struct tcell44733 tcell44733;
typedef struct TNimType TNimType;
typedef struct tcellseq44749 tcellseq44749;
typedef struct tgcheap46816 tgcheap46816;
typedef struct tcellset44745 tcellset44745;
typedef struct tpagedesc44741 tpagedesc44741;
typedef struct tmemregion27010 tmemregion27010;
typedef struct tsmallchunk26240 tsmallchunk26240;
typedef struct tllchunk27004 tllchunk27004;
typedef struct tbigchunk26242 tbigchunk26242;
typedef struct tintset26217 tintset26217;
typedef struct ttrunk26213 ttrunk26213;
typedef struct tavlnode27008 tavlnode27008;
typedef struct tgcstat46814 tgcstat46814;
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 addrinfo104668 addrinfo104668;
typedef struct timeouterror183629 timeouterror183629;
typedef struct TY192015 TY192015;
typedef struct timeval104666 timeval104666;
typedef struct tfdset104664 tfdset104664;
typedef struct tbasechunk26238 tbasechunk26238;
typedef struct tfreecell26230 tfreecell26230;
struct  tcell44733  {
NI Refcount;
TNimType* Typ;
};
struct  tcellseq44749  {
NI Len;
NI Cap;
tcell44733** D;
};
struct  tcellset44745  {
NI Counter;
NI Max;
tpagedesc44741* Head;
tpagedesc44741** Data;
};
typedef tsmallchunk26240* TY27022[512];
typedef ttrunk26213* ttrunkbuckets26215[256];
struct  tintset26217  {
ttrunkbuckets26215 Data;
};
struct  tmemregion27010  {
NI Minlargeobj;
NI Maxlargeobj;
TY27022 Freesmallchunks;
tllchunk27004* Llmem;
NI Currmem;
NI Maxmem;
NI Freemem;
NI Lastsize;
tbigchunk26242* Freechunkslist;
tintset26217 Chunkstarts;
tavlnode27008* Root;
tavlnode27008* Deleted;
tavlnode27008* Last;
tavlnode27008* Freeavlnodes;
};
struct  tgcstat46814  {
NI Stackscans;
NI Cyclecollections;
NI Maxthreshold;
NI Maxstacksize;
NI Maxstackcells;
NI Cycletablesize;
NI64 Maxpause;
};
struct  tgcheap46816  {
void* Stackbottom;
NI Cyclethreshold;
tcellseq44749 Zct;
tcellseq44749 Decstack;
tcellset44745 Cycleroots;
tcellseq44749 Tempstack;
NI Recgclock;
tmemregion27010 Region;
tgcstat46814 Stat;
};
typedef NIM_CHAR TY104673[257];
typedef NIM_CHAR TY104676[129];
typedef N_STDCALL_PTR(int, TY105041) (NI16 wversionrequired, WSADATA* wsdata);
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
typedef NIM_CHAR TY183632[4001];
struct  tsocketimpl183607  {
NI Fd;
NIM_BOOL Isbuffered;
union {
struct {TY183632 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;
};
typedef N_STDCALL_PTR(int, TY104938) (NI s, void* buf, int len, int flags);
struct  systemerror3229  {
  Exception Sup;
};
struct  oserror3233  {
  systemerror3229 Sup;
NI32 Errorcode;
};
typedef N_STDCALL_PTR(NI, TY104824) (int af, int typ, int protocol);
typedef NIM_CHAR TY104690[8];
typedef N_STDCALL_PTR(int, TY104848) (NI s, SOCKADDR* name, int namelen);
struct  addrinfo104668  {
int Aiflags;
int Aifamily;
int Aisocktype;
int Aiprotocol;
NI Aiaddrlen;
NCSTRING Aicanonname;
SOCKADDR* Aiaddr;
addrinfo104668* Ainext;
};
typedef N_STDCALL_PTR(int, TY105049) (NCSTRING nodename, NCSTRING servname, addrinfo104668* hints, addrinfo104668** res);
typedef N_STDCALL_PTR(int, TY104897) (NI s, int backlog);
typedef N_STDCALL_PTR(NI, TY104838) (NI s, SOCKADDR* a, int* addrlen);
typedef N_STDCALL_PTR(NCSTRING, TY105068) (IN_ADDR i);
struct  timeouterror183629  {
  Exception Sup;
};
typedef tsocketimpl183607* TY192017[1];
struct  timeval104666  {
NI32 Tvsec;
NI32 Tvusec;
};
typedef NI TY104715[64];
struct  tfdset104664  {
int Fdcount;
TY104715 Fdarray;
};
typedef N_STDCALL_PTR(int, TY104926) (int nfds, tfdset104664* readfds, tfdset104664* writefds, tfdset104664* exceptfds, timeval104666* timeout);
typedef N_STDCALL_PTR(int, TY104904) (NI s, void* buf, int len, int flags);
typedef N_STDCALL_PTR(int, TY104832) (NI s);
typedef NI TY26220[16];
struct  tpagedesc44741  {
tpagedesc44741* Next;
NI Key;
TY26220 Bits;
};
struct  tbasechunk26238  {
NI Prevsize;
NI Size;
NIM_BOOL Used;
};
struct  tsmallchunk26240  {
  tbasechunk26238 Sup;
tsmallchunk26240* Next;
tsmallchunk26240* Prev;
tfreecell26230* Freelist;
NI Free;
NI Acc;
NF Data;
};
struct  tllchunk27004  {
NI Size;
NI Acc;
tllchunk27004* Next;
};
struct  tbigchunk26242  {
  tbasechunk26238 Sup;
tbigchunk26242* Next;
tbigchunk26242* Prev;
NI Align;
NF Data;
};
struct  ttrunk26213  {
ttrunk26213* Next;
NI Key;
TY26220 Bits;
};
typedef tavlnode27008* TY27014[2];
struct  tavlnode27008  {
TY27014 Link;
NI Key;
NI Upperbound;
NI Level;
};
typedef NIM_CHAR TY104683[14];
struct  tfreecell26230  {
tfreecell26230* Next;
NI Zerofield;
};
struct TY192015 {
  TGenericSeq Sup;
  tsocketimpl183607* data[SEQ_DECL_SIZE];
};
static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src);
static N_INLINE(tcell44733*, usrtocell_48446)(void* usr);
static N_INLINE(void, rtladdzct_50004)(tcell44733* c);
N_NOINLINE(void, addzct_48417)(tcellseq44749* s, tcell44733* c);
N_NIMCALL(void, raiseoserror_112435)(NI32 errorcode);
N_NIMCALL(NI32, oslasterror_112467)(void);
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_193463)(tsocketimpl183607* socket, void* data, NI size);
N_NIMCALL(tsocketimpl183607*, newtsocket_184004)(NI fd, NIM_BOOL isbuff);
N_NIMCALL(void, TMP4861)(void* p, NI op);
N_NIMCALL(NI16, htons_184417)(NI16 x);
N_NIMCALL(NI16, ntohs_184207)(NI16 x);
N_NIMCALL(NI32, htonl_184407)(NI32 x);
N_NIMCALL(NI32, ntohl_184070)(NI32 x);
N_NIMCALL(int, toint_184427)(NU8 domain);
N_NIMCALL(int, toint_184442)(NU8 typ);
N_NIMCALL(int, toint_184458)(NU8 p);
N_NIMCALL(NimStringDesc*, HEX24_6201)(NU64 x);
N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
N_NIMCALL(void, acceptaddr_186256)(tsocketimpl183607* server, tsocketimpl183607** client, NimStringDesc** address);
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(NimStringDesc*, cstrToNimstr)(NCSTRING str);
N_NIMCALL(NimStringDesc*, setLengthStr)(NimStringDesc* s, NI newlen);
N_NIMCALL(NI, waitfor_191618)(tsocketimpl183607* socket, NF* waited, NI timeout, NI size, NimStringDesc* funcname);
N_NIMCALL(void, nimGCvisit)(void* d, NI op);
N_NIMCALL(void, TMP4865)(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_191017)(TY192015** readfds, NI timeout);
N_NIMCALL(NI, checkbuffer_190807)(TY192015** readfds);
N_NIMCALL(NIM_BOOL, hasdatabuffered_190711)(tsocketimpl183607* s);
N_NIMCALL(TGenericSeq*, incrSeq)(TGenericSeq* seq, NI elemsize);
N_NIMCALL(void, genericSeqAssign)(void* dest, void* src_81204, TNimType* mt);
N_NIMCALL(timeval104666, timevalfrommilliseconds_190401)(NI timeout);
N_NIMCALL(void, createfdset_190418)(tfdset104664* fd, TY192015* s, NI* m);
N_NIMCALL(void, fdzero_105030)(tfdset104664* s);
N_NIMCALL(void, fdset_105004)(NI socket, tfdset104664* s);
N_NIMCALL(void, prunesocketset_190637)(TY192015** s, tfdset104664* fd);
N_NIMCALL(int, fdisset_104992)(NI socket, tfdset104664* set);
N_NIMCALL(TGenericSeq*, setLengthSeq)(TGenericSeq* seq, NI elemsize, NI newlen);
static N_INLINE(void, HEX2BHEX3D_106692)(NF* x_106698, NF y_106701);
N_NIMCALL(NI, recv_191302)(tsocketimpl183607* socket, void* data, NI size);
N_NIMCALL(NI, readintobuf_191048)(tsocketimpl183607* socket, NI32 flags);
N_NIMCALL(void, raisesocketerror_185001)(tsocketimpl183607* socket, NI err, NIM_BOOL async);
N_NIMCALL(NI, peekchar_192138)(tsocketimpl183607* socket, NIM_CHAR* c);
N_NIMCALL(NimStringDesc*, resizeString)(NimStringDesc* dest, NI addlen);
N_NIMCALL(NimStringDesc*, addChar)(NimStringDesc* s, NIM_CHAR c);
STRING_LITERAL(TMP1984, "This function cannot be used on non-blocking sockets.", 53);
STRING_LITERAL(TMP1985, "Could not send all data.", 24);
TNimNode* NimDT_183607_isbuffered[3];
STRING_LITERAL(TMP4864, "", 0);
STRING_LITERAL(TMP4866, "Call to \'", 9);
STRING_LITERAL(TMP4867, "\' timed out.", 12);
STRING_LITERAL(TMP4869, "readLine", 8);
STRING_LITERAL(TMP4870, "\015\012", 2);
tsocketimpl183607* invalidsocket_183696;
extern tgcheap46816 gch_46844;
NI osinvalidsocket_183805;
WSADATA wsa_195239;
extern TY105041 Dl_105040;
extern TNimType NTI21401; /* ref ValueError */
extern TNimType NTI3249; /* ValueError */
extern TY104938 Dl_104937;
extern TNimType NTI112438; /* ref OSError */
extern TNimType NTI3233; /* OSError */
TNimType NTI183607; /* TSocketImpl */
extern TNimType NTI108; /* int */
extern TNimType NTI138; /* bool */
extern TNimType NTI147; /* char */
TNimType NTI183632; /* array[0..4000, char] */
TNimType NTI183609; /* Socket */
extern TY104824 Dl_104823;
extern TY104848 Dl_104847;
extern TY105049 Dl_105048;
extern TY104897 Dl_104896;
extern TY104838 Dl_104837;
extern TY105068 Dl_105067;
extern TNimType NTI3227; /* Exception */
TNimType NTI183629; /* TimeoutError */
TNimType NTI191879; /* ref TimeoutError */
extern TNimType NTI192015; /* seq[Socket] */
extern TY104926 Dl_104925;
extern TY104904 Dl_104903;
extern TY104832 Dl_104831;

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

static N_INLINE(void, rtladdzct_50004)(tcell44733* c) {
	addzct_48417((&gch_46844.Zct), c);
}

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

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

N_NIMCALL(NI, send_193463)(tsocketimpl183607* socket, void* data, NI size) {
	NI result;
	int LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = Dl_104937((*socket).Fd, data, ((int) (size)), ((NI32) 0));
	result = ((NI) (LOC1));
	return result;
}

N_NIMCALL(void, send_193808)(tsocketimpl183607* socket, NimStringDesc* data) {
	NI sent;
	{
		valueerror3249* e_193814;
		NimStringDesc* LOC5;
		if (!(*socket).Nonblocking) goto LA3;
		e_193814 = 0;
		e_193814 = (valueerror3249*) newObj((&NTI21401), sizeof(valueerror3249));
		(*e_193814).Sup.Sup.m_type = (&NTI3249);
		LOC5 = 0;
		LOC5 = (*e_193814).Sup.message; (*e_193814).Sup.message = copyStringRC1(((NimStringDesc*) &TMP1984));
		if (LOC5) nimGCunrefNoCycle(LOC5);
		raiseException((Exception*)e_193814, "ValueError");
	}
	LA3: ;
	sent = send_193463(socket, ((void*) (data->data)), data->Sup.len);
	{
		NI32 LOC10;
		if (!(sent < 0)) goto LA8;
		LOC10 = 0;
		LOC10 = oslasterror_112467();
		raiseoserror_112435(LOC10);
	}
	LA8: ;
	{
		oserror3233* e_194003;
		NimStringDesc* LOC15;
		if (!!((sent == data->Sup.len))) goto LA13;
		e_194003 = 0;
		e_194003 = (oserror3233*) newObj((&NTI112438), sizeof(oserror3233));
		(*e_194003).Sup.Sup.Sup.m_type = (&NTI3233);
		LOC15 = 0;
		LOC15 = (*e_194003).Sup.Sup.message; (*e_194003).Sup.Sup.message = copyStringRC1(((NimStringDesc*) &TMP1985));
		if (LOC15) nimGCunrefNoCycle(LOC15);
		raiseException((Exception*)e_194003, "OSError");
	}
	LA13: ;
}
N_NIMCALL(void, TMP4861)(void* p, NI op) {
	tsocketimpl183607* a;
	NI LOC1;
	a = (tsocketimpl183607*)p;
	switch ((*a).Isbuffered) {
	case NIM_TRUE:
	LOC1 = 0;
	for (LOC1 = 0; LOC1 < 4001; LOC1++) {
	}
	break;
	case NIM_FALSE:
	break;
	} 
}

N_NIMCALL(tsocketimpl183607*, newtsocket_184004)(NI fd, NIM_BOOL isbuff) {
	tsocketimpl183607* result;
	result = 0;
	{
		if (!(fd == osinvalidsocket_183805)) goto LA3;
		result = NIM_NIL;
		goto BeforeRet;
	}
	LA3: ;
	result = (tsocketimpl183607*) newObj((&NTI183609), sizeof(tsocketimpl183607));
	(*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(tsocketimpl183607*, socket_184604)(NU8 domain, NU8 typ, NU8 protocol, NIM_BOOL buffered) {
	tsocketimpl183607* result;
	NI LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = Dl_104823(((int) (domain)), ((int) (typ)), ((int) (protocol)));
	result = newtsocket_184004(LOC1, buffered);
	return result;
}

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

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

N_NIMCALL(int, toint_184427)(NU8 domain) {
	int result;
	result = 0;
	result = ((int) (((NI16)(NU16)(NU)(((NI) (domain))))));
	return result;
}

N_NIMCALL(int, toint_184442)(NU8 typ) {
	int result;
	result = 0;
	result = ((int) (typ));
	return result;
}

N_NIMCALL(int, toint_184458)(NU8 p) {
	int result;
	result = 0;
	result = ((int) (p));
	return result;
}

N_NIMCALL(void, bindaddr_185773)(tsocketimpl183607* socket, NU16 port, NimStringDesc* address) {
	{
		SOCKADDR_IN name;
		if (!((address) && (address)->Sup.len == 0)) goto LA3;
		memset((void*)(&name), 0, sizeof(name));
		name.sin_family = ((NI16) 2);
		name.sin_port = htons_184417(((NI16) (port)));
		name.sin_addr.s_addr = htonl_184407(((NI32) 0));
		{
			int LOC7;
			NI32 LOC10;
			LOC7 = 0;
			LOC7 = Dl_104847((*socket).Fd, ((SOCKADDR*) ((&name))), ((int) (((NI)sizeof(SOCKADDR_IN)))));
			if (!(LOC7 < ((NI32) 0))) goto LA8;
			LOC10 = 0;
			LOC10 = oslasterror_112467();
			raiseoserror_112435(LOC10);
		}
		LA8: ;
	}
	goto LA1;
	LA3: ;
	{
		addrinfo104668 hints;
		addrinfo104668* ailist;
		memset((void*)(&hints), 0, sizeof(hints));
		ailist = NIM_NIL;
		hints.Aifamily = toint_184427(((NU8) 2));
		hints.Aisocktype = toint_184442(((NU8) 1));
		hints.Aiprotocol = toint_184458(((NU8) 6));
		{
			int gairesult_185816;
			NimStringDesc* LOC13;
			LOC13 = 0;
			LOC13 = HEX24_6201(port);
			gairesult_185816 = Dl_105048(address->data, LOC13->data, (&hints), &ailist);
			{
				NI32 LOC18;
				if (!!((gairesult_185816 == ((NI32) 0)))) goto LA16;
				LOC18 = 0;
				LOC18 = oslasterror_112467();
				raiseoserror_112435(LOC18);
			}
			LA16: ;
		}
		{
			int LOC21;
			NI32 LOC24;
			LOC21 = 0;
			LOC21 = Dl_104847((*socket).Fd, (*ailist).Aiaddr, ((int) ((*ailist).Aiaddrlen)));
			if (!(LOC21 < ((NI32) 0))) goto LA22;
			LOC24 = 0;
			LOC24 = oslasterror_112467();
			raiseoserror_112435(LOC24);
		}
		LA22: ;
	}
	LA1: ;
}

N_NIMCALL(void, listen_185608)(tsocketimpl183607* socket, int backlog) {
	{
		int LOC3;
		NI32 LOC6;
		LOC3 = 0;
		LOC3 = Dl_104896((*socket).Fd, backlog);
		if (!(LOC3 < ((NI32) 0))) goto LA4;
		LOC6 = 0;
		LOC6 = oslasterror_112467();
		raiseoserror_112435(LOC6);
	}
	LA4: ;
}

N_NIMCALL(void, acceptaddr_186256)(tsocketimpl183607* server, tsocketimpl183607** client, NimStringDesc** address) {
	SOCKADDR_IN sockaddress_186264;
	int addrlen_186266;
	NI sock_186268;
	memset((void*)(&sockaddress_186264), 0, sizeof(sockaddress_186264));
	addrlen_186266 = ((int) (((NI)sizeof(SOCKADDR_IN))));
	sock_186268 = Dl_104837((*server).Fd, ((SOCKADDR*) ((&sockaddress_186264))), (&addrlen_186266));
	{
		NI32 err_186270;
		if (!(sock_186268 == osinvalidsocket_183805)) goto LA3;
		err_186270 = oslasterror_112467();
		{
			if (!(err_186270 == ((NI32) 10036))) goto LA7;
			unsureAsgnRef((void**) (&(*client)), invalidsocket_183696);
			unsureAsgnRef((void**) (&(*address)), copyString(((NimStringDesc*) &TMP4864)));
			goto BeforeRet;
		}
		goto LA5;
		LA7: ;
		{
			raiseoserror_112435(err_186270);
		}
		LA5: ;
	}
	goto LA1;
	LA3: ;
	{
		NCSTRING LOC11;
		(*(*client)).Fd = sock_186268;
		(*(*client)).Isbuffered = (*server).Isbuffered;
		LOC11 = 0;
		LOC11 = Dl_105067(sockaddress_186264.sin_addr);
		unsureAsgnRef((void**) (&(*address)), cstrToNimstr(LOC11));
		goto BeforeRet;
	}
	LA1: ;
	BeforeRet: ;
}

N_NIMCALL(void, accept_187401)(tsocketimpl183607* server, tsocketimpl183607** client) {
	NimStringDesc* addrdummy;
	addrdummy = copyString(((NimStringDesc*) &TMP4864));
	acceptaddr_186256(server, client, (&addrdummy));
}
N_NIMCALL(void, TMP4865)(void* p, NI op) {
	timeouterror183629* a;
	a = (timeouterror183629*)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_190711)(tsocketimpl183607* 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_190807)(TY192015** readfds) {
	NI result;
	TY192015* res;
	result = 0;
	res = (TY192015*) newSeq((&NTI192015), 0);
	result = 0;
	{
		tsocketimpl183607* s_190847;
		TY192015* HEX3Atmp_190881;
		NI i_190884;
		NI l_190886;
		s_190847 = 0;
		HEX3Atmp_190881 = 0;
		unsureAsgnRef((void**) (&HEX3Atmp_190881), (*readfds));
		i_190884 = 0;
		l_190886 = HEX3Atmp_190881->Sup.len;
		{
			while (1) {
				if (!(i_190884 < l_190886)) goto LA3;
				s_190847 = HEX3Atmp_190881->data[i_190884];
				{
					NIM_BOOL LOC6;
					LOC6 = 0;
					LOC6 = hasdatabuffered_190711(s_190847);
					if (!LOC6) goto LA7;
					result += 1;
					res = (TY192015*) incrSeq(&(res)->Sup, sizeof(tsocketimpl183607*));
					asgnRefNoCycle((void**) (&res->data[res->Sup.len-1]), s_190847);
				}
				LA7: ;
				i_190884 += 1;
			} LA3: ;
		}
	}
	{
		if (!(0 < result)) goto LA11;
		genericSeqAssign((&(*readfds)), res, (&NTI192015));
	}
	LA11: ;
	return result;
}

N_NIMCALL(timeval104666, timevalfrommilliseconds_190401)(NI timeout) {
	timeval104666 result;
	memset((void*)(&result), 0, sizeof(result));
	{
		NI seconds;
		if (!!((timeout == -1))) goto LA3;
		seconds = (NI32)(timeout / 1000);
		result.Tvsec = ((NI32) (seconds));
		result.Tvusec = ((NI32) ((NI32)((NI32)(timeout - (NI32)(seconds * 1000)) * 1000)));
	}
	LA3: ;
	return result;
}

N_NIMCALL(void, createfdset_190418)(tfdset104664* fd, TY192015* s, NI* m) {
	fdzero_105030(fd);
	{
		tsocketimpl183607* i_190611;
		NI i_190619;
		NI l_190621;
		i_190611 = 0;
		i_190619 = 0;
		l_190621 = s->Sup.len;
		{
			while (1) {
				if (!(i_190619 < l_190621)) goto LA3;
				i_190611 = s->data[i_190619];
				(*m) = (((*m) >= (*i_190611).Fd) ? (*m) : (*i_190611).Fd);
				fdset_105004((*i_190611).Fd, fd);
				i_190619 += 1;
			} LA3: ;
		}
	}
}

N_NIMCALL(void, prunesocketset_190637)(TY192015** s, tfdset104664* fd) {
	NI i;
	NI l;
	i = 0;
	l = (*s)->Sup.len;
	{
		while (1) {
			if (!(i < l)) goto LA2;
			{
				int LOC5;
				LOC5 = 0;
				LOC5 = fdisset_104992((*(*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) = (TY192015*) setLengthSeq(&((*s))->Sup, sizeof(tsocketimpl183607*), l);
}

N_NIMCALL(NI, select_191017)(TY192015** readfds, NI timeout) {
	NI result;
	NI buffersfilled;
	timeval104666 tv;
	tfdset104664 rd;
	NI m;
	result = 0;
	buffersfilled = checkbuffer_190807(readfds);
	{
		if (!(0 < buffersfilled)) goto LA3;
		result = buffersfilled;
		goto BeforeRet;
	}
	LA3: ;
	tv = timevalfrommilliseconds_190401(timeout);
	memset((void*)(&rd), 0, sizeof(rd));
	m = 0;
	createfdset_190418((&rd), (*readfds), (&m));
	{
		int LOC9;
		if (!!((timeout == -1))) goto LA7;
		LOC9 = 0;
		LOC9 = Dl_104925(((int) ((NI32)(m + 1))), (&rd), NIM_NIL, NIM_NIL, (&tv));
		result = ((NI) (LOC9));
	}
	goto LA5;
	LA7: ;
	{
		int LOC11;
		LOC11 = 0;
		LOC11 = Dl_104925(((int) ((NI32)(m + 1))), (&rd), NIM_NIL, NIM_NIL, NIM_NIL);
		result = ((NI) (LOC11));
	}
	LA5: ;
	prunesocketset_190637(readfds, (&rd));
	BeforeRet: ;
	return result;
}

static N_INLINE(void, HEX2BHEX3D_106692)(NF* x_106698, NF y_106701) {
	(*x_106698) = ((NF)((*x_106698)) + (NF)(y_106701));
}

N_NIMCALL(NI, waitfor_191618)(tsocketimpl183607* 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: ;
	{
		TY192015* s;
		TY192017 LOC23;
		NF starttime;
		NI selret;
		NF LOC34;
		{
			timeouterror183629* e_191878;
			NimStringDesc* LOC22;
			if (!((NI32)(timeout - ((NI) (((NF)((*waited)) * (NF)(1.0000000000000000e+03))))) < 1)) goto LA20;
			e_191878 = 0;
			e_191878 = (timeouterror183629*) newObj((&NTI191879), sizeof(timeouterror183629));
			(*e_191878).Sup.Sup.m_type = (&NTI183629);
			LOC22 = 0;
			LOC22 = rawNewString(funcname->Sup.len + 21);
appendString(LOC22, ((NimStringDesc*) &TMP4866));
appendString(LOC22, funcname);
appendString(LOC22, ((NimStringDesc*) &TMP4867));
			asgnRefNoCycle((void**) (&(*e_191878).Sup.message), LOC22);
			raiseException((Exception*)e_191878, "TimeoutError");
		}
		LA20: ;
		s = (TY192015*) newSeq((&NTI192015), 1);
		memset((void*)LOC23, 0, sizeof(LOC23));
		LOC23[0] = socket;
		asgnRefNoCycle((void**) (&s->data[0]), LOC23[0]);
		starttime = ntepochTime();
		selret = select_191017((&s), (NI32)(timeout - ((NI) (((NF)((*waited)) * (NF)(1.0000000000000000e+03))))));
		{
			NI32 LOC28;
			if (!(selret < 0)) goto LA26;
			LOC28 = 0;
			LOC28 = oslasterror_112467();
			raiseoserror_112435(LOC28);
		}
		LA26: ;
		{
			timeouterror183629* e_192027;
			NimStringDesc* LOC33;
			if (!!((selret == 1))) goto LA31;
			e_192027 = 0;
			e_192027 = (timeouterror183629*) newObj((&NTI191879), sizeof(timeouterror183629));
			(*e_192027).Sup.Sup.m_type = (&NTI183629);
			LOC33 = 0;
			LOC33 = rawNewString(funcname->Sup.len + 21);
appendString(LOC33, ((NimStringDesc*) &TMP4866));
appendString(LOC33, funcname);
appendString(LOC33, ((NimStringDesc*) &TMP4867));
			asgnRefNoCycle((void**) (&(*e_192027).Sup.message), LOC33);
			raiseException((Exception*)e_192027, "TimeoutError");
		}
		LA31: ;
		LOC34 = 0;
		LOC34 = ntepochTime();
		HEX2BHEX3D_106692(waited, ((NF)(LOC34) - (NF)(starttime)));
	}
	LA9: ;
	BeforeRet: ;
	return result;
}

N_NIMCALL(NI, readintobuf_191048)(tsocketimpl183607* socket, NI32 flags) {
	NI result;
	int LOC1;
	result = 0;
	result = 0;
	LOC1 = 0;
	LOC1 = Dl_104903((*socket).Fd, ((void*) ((*socket).isbufferedU.S1.Buffer)), ((int) 4000), flags);
	result = ((NI) (LOC1));
	{
		if (!(result <= 0)) goto LA4;
		(*socket).isbufferedU.S1.Buflen = 0;
		(*socket).isbufferedU.S1.Currpos = 0;
		goto BeforeRet;
	}
	LA4: ;
	(*socket).isbufferedU.S1.Buflen = result;
	(*socket).isbufferedU.S1.Currpos = 0;
	BeforeRet: ;
	return result;
}

N_NIMCALL(NI, recv_191302)(tsocketimpl183607* 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_191048(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_191048(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: ;
	{
		int LOC38;
		LOC38 = 0;
		LOC38 = Dl_104903((*socket).Fd, data, ((int) (size)), ((NI32) 0));
		result = ((NI) (LOC38));
	}
	LA5: ;
	BeforeRet: ;
	return result;
}

N_NIMCALL(void, raisesocketerror_185001)(tsocketimpl183607* 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_112467();
		{
			if (!async) goto LA9;
			{
				if (!(lasterror == ((NI32) 10035))) goto LA13;
				goto BeforeRet;
			}
			goto LA11;
			LA13: ;
			{
				raiseoserror_112435(lasterror);
			}
			LA11: ;
		}
		goto LA7;
		LA9: ;
		{
			raiseoserror_112435(lasterror);
		}
		LA7: ;
	}
	LA5: ;
	BeforeRet: ;
}

N_NIMCALL(NI, peekchar_192138)(tsocketimpl183607* 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_191048(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: ;
	{
		int LOC16;
		LOC16 = 0;
		LOC16 = Dl_104903((*socket).Fd, ((void*) (c)), ((int) 1), ((int) 2));
		result = ((NI) (LOC16));
	}
	LA1: ;
	return result;
}

N_NIMCALL(void, readline_192447)(tsocketimpl183607* 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_191618(socket, (&waited), timeout, 1, ((NimStringDesc*) &TMP4869));
			n = recv_191302(socket, ((void*) ((&c))), 1);
			{
				if (!(n < 0)) goto LA6;
				raisesocketerror_185001(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_191618(socket, (&waited), timeout, 1, ((NimStringDesc*) &TMP4869));
				n = peekchar_192138(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_191302(socket, ((void*) ((&c))), 1);
				}
				goto LA16;
				LA20: ;
				{
					if (!(n <= 0)) goto LA24;
					raisesocketerror_185001(socket, -1, NIM_FALSE);
				}
				goto LA16;
				LA24: ;
				LA16: ;
				{
					if (!((*line)->Sup.len == 0)) goto LA28;
					(*line) = resizeString((*line), 2);
appendString((*line), ((NimStringDesc*) &TMP4870));
				}
				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*) &TMP4870));
				}
				LA35: ;
				goto BeforeRet;
			}
			goto LA11;
			LA31: ;
			LA11: ;
			(*line) = addChar((*line), c);
		}
	}
	BeforeRet: ;
}

N_NIMCALL(void, close_187478)(tsocketimpl183607* socket) {
	int LOC1;
	LOC1 = 0;
	LOC1 = Dl_104831((*socket).Fd);
}
NIM_EXTERNC N_NOINLINE(void, stdlib_socketsInit)(void) {
	asgnRefNoCycle((void**) (&invalidsocket_183696), NIM_NIL);
	osinvalidsocket_183805 = INVALID_SOCKET;
	{
		int LOC3;
		NI32 LOC6;
		LOC3 = 0;
		LOC3 = Dl_105040(((NI16) 257), (&wsa_195239));
		if (!!((LOC3 == ((NI32) 0)))) goto LA4;
		LOC6 = 0;
		LOC6 = oslasterror_112467();
		raiseoserror_112435(LOC6);
	}
	LA4: ;
}

NIM_EXTERNC N_NOINLINE(void, stdlib_socketsDatInit)(void) {
static TNimNode* TMP4859[3];
static TNimNode* TMP4860[3];
static TNimNode TMP128[10];
NTI183607.size = sizeof(tsocketimpl183607);
NTI183607.kind = 18;
NTI183607.base = 0;
NTI183607.flags = 3;
TMP4859[0] = &TMP128[1];
TMP128[1].kind = 1;
TMP128[1].offset = offsetof(tsocketimpl183607, Fd);
TMP128[1].typ = (&NTI108);
TMP128[1].name = "fd";
TMP4859[1] = &TMP128[2];
TMP128[2].kind = 3;
TMP128[2].offset = offsetof(tsocketimpl183607, Isbuffered);
TMP128[2].typ = (&NTI138);
TMP128[2].name = "isBuffered";
TMP128[2].sons = &NimDT_183607_isbuffered[0];
TMP128[2].len = 2;
TMP4860[0] = &TMP128[4];
NTI183632.size = sizeof(TY183632);
NTI183632.kind = 16;
NTI183632.base = (&NTI147);
NTI183632.flags = 3;
TMP128[4].kind = 1;
TMP128[4].offset = offsetof(tsocketimpl183607, isbufferedU.S1.Buffer);
TMP128[4].typ = (&NTI183632);
TMP128[4].name = "buffer";
TMP4860[1] = &TMP128[5];
TMP128[5].kind = 1;
TMP128[5].offset = offsetof(tsocketimpl183607, isbufferedU.S1.Currpos);
TMP128[5].typ = (&NTI108);
TMP128[5].name = "currPos";
TMP4860[2] = &TMP128[6];
TMP128[6].kind = 1;
TMP128[6].offset = offsetof(tsocketimpl183607, isbufferedU.S1.Buflen);
TMP128[6].typ = (&NTI108);
TMP128[6].name = "bufLen";
TMP128[3].len = 3; TMP128[3].kind = 2; TMP128[3].sons = &TMP4860[0];
NimDT_183607_isbuffered[1] = &TMP128[3];
TMP128[7].len = 0; TMP128[7].kind = 2;
NimDT_183607_isbuffered[0] = &TMP128[7];
TMP4859[2] = &TMP128[8];
TMP128[8].kind = 1;
TMP128[8].offset = offsetof(tsocketimpl183607, Nonblocking);
TMP128[8].typ = (&NTI138);
TMP128[8].name = "nonblocking";
TMP128[0].len = 3; TMP128[0].kind = 2; TMP128[0].sons = &TMP4859[0];
NTI183607.node = &TMP128[0];
NTI183609.size = sizeof(tsocketimpl183607*);
NTI183609.kind = 22;
NTI183609.base = (&NTI183607);
NTI183609.flags = 2;
NTI183609.marker = TMP4861;
NTI183629.size = sizeof(timeouterror183629);
NTI183629.kind = 17;
NTI183629.base = (&NTI3227);
TMP128[9].len = 0; TMP128[9].kind = 2;
NTI183629.node = &TMP128[9];
NTI191879.size = sizeof(timeouterror183629*);
NTI191879.kind = 22;
NTI191879.base = (&NTI183629);
NTI191879.marker = TMP4865;
}

