/* Generated by Nim Compiler v0.11.2 */
/*   (c) 2015 Andreas Rumpf */
/* The generated code is subject to the original license. */
#define NIM_INTBITS 32
#include "nimbase.h"

#include "winsock2.h"

#include <string.h>

#include <setjmp.h>
typedef struct Socketimpl514407 Socketimpl514407;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct Addrinfo105868 Addrinfo105868;
typedef struct Oserror3433 Oserror3433;
typedef struct Systemerror3429 Systemerror3429;
typedef struct Exception Exception;
typedef struct TNimObject TNimObject;
typedef struct Tcell46346 Tcell46346;
typedef struct Tcellseq46362 Tcellseq46362;
typedef struct Tgcheap48216 Tgcheap48216;
typedef struct Tcellset46358 Tcellset46358;
typedef struct Tpagedesc46354 Tpagedesc46354;
typedef struct Tmemregion28210 Tmemregion28210;
typedef struct Tsmallchunk27440 Tsmallchunk27440;
typedef struct Tllchunk28204 Tllchunk28204;
typedef struct Tbigchunk27442 Tbigchunk27442;
typedef struct Tintset27417 Tintset27417;
typedef struct Ttrunk27413 Ttrunk27413;
typedef struct Tavlnode28208 Tavlnode28208;
typedef struct Tgcstat48214 Tgcstat48214;
typedef struct Timeouterror514415 Timeouterror514415;
typedef struct TY517225 TY517225;
typedef struct TSafePoint TSafePoint;
typedef struct Tbasechunk27438 Tbasechunk27438;
typedef struct Tfreecell27430 Tfreecell27430;
typedef NIM_CHAR TY514420[4001];
struct  Socketimpl514407  {
NI fd;
NIM_BOOL isbuffered;
union{
struct {TY514420 buffer;
NI currpos;
NI buflen;
} S1;
} isbufferedU;
NI32 lasterror;
};
typedef N_NIMCALL_PTR(void, TY3289) (void* p, NI op);
typedef N_NIMCALL_PTR(void*, TY3294) (void* p);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY3289 marker;
TY3294 deepcopy;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
typedef NIM_CHAR TY105890[8];
struct  Addrinfo105868  {
int aiflags;
int aifamily;
int aisocktype;
int aiprotocol;
NI aiaddrlen;
NCSTRING aicanonname;
SOCKADDR* aiaddr;
Addrinfo105868* ainext;
};
typedef N_STDCALL_PTR(int, TY106138) (NI s, void* buf, int len, int flags);
struct  TNimObject  {
TNimType* m_type;
};
struct  Exception  {
  TNimObject Sup;
Exception* parent;
NCSTRING name;
NimStringDesc* message;
NimStringDesc* trace;
};
struct  Systemerror3429  {
  Exception Sup;
};
struct  Oserror3433  {
  Systemerror3429 Sup;
NI32 errorcode;
};
struct  Tcell46346  {
NI refcount;
TNimType* typ;
};
struct  Tcellseq46362  {
NI len;
NI cap;
Tcell46346** d;
};
struct  Tcellset46358  {
NI counter;
NI max;
Tpagedesc46354* head;
Tpagedesc46354** data;
};
typedef Tsmallchunk27440* TY28222[512];
typedef Ttrunk27413* Ttrunkbuckets27415[256];
struct  Tintset27417  {
Ttrunkbuckets27415 data;
};
struct  Tmemregion28210  {
NI minlargeobj;
NI maxlargeobj;
TY28222 freesmallchunks;
Tllchunk28204* llmem;
NI currmem;
NI maxmem;
NI freemem;
NI lastsize;
Tbigchunk27442* freechunkslist;
Tintset27417 chunkstarts;
Tavlnode28208* root;
Tavlnode28208* deleted;
Tavlnode28208* last;
Tavlnode28208* freeavlnodes;
};
struct  Tgcstat48214  {
NI stackscans;
NI cyclecollections;
NI maxthreshold;
NI maxstacksize;
NI maxstackcells;
NI cycletablesize;
NI64 maxpause;
};
struct  Tgcheap48216  {
void* stackbottom;
NI cyclethreshold;
Tcellseq46362 zct;
Tcellseq46362 decstack;
Tcellset46358 cycleroots;
Tcellseq46362 tempstack;
NI recgclock;
Tmemregion28210 region;
Tgcstat48214 stat;
};
typedef N_STDCALL_PTR(NI, TY106038) (NI s, SOCKADDR* a, unsigned int* addrlen);
typedef N_STDCALL_PTR(NCSTRING, TY106263) (IN_ADDR i);
struct  Timeouterror514415  {
  Exception Sup;
};
typedef NI TY517214[1];
typedef N_STDCALL_PTR(int, TY106104) (NI s, void* buf, int len, int flags);
typedef struct {
N_NIMCALL_PTR(NIM_BOOL, ClPrc) (Exception* e, void* ClEnv);
void* ClEnv;
} TY15609;
struct  TSafePoint  {
TSafePoint* prev;
NI status;
jmp_buf context;
NIM_BOOL hasRaiseAction;
TY15609 raiseAction;
};
typedef NIM_CHAR TY105883[14];
typedef NI TY27420[16];
struct  Tpagedesc46354  {
Tpagedesc46354* next;
NI key;
TY27420 bits;
};
struct  Tbasechunk27438  {
NI prevsize;
NI size;
NIM_BOOL used;
};
struct  Tsmallchunk27440  {
  Tbasechunk27438 Sup;
Tsmallchunk27440* next;
Tsmallchunk27440* prev;
Tfreecell27430* freelist;
NI free;
NI acc;
NF data;
};
struct  Tllchunk28204  {
NI size;
NI acc;
Tllchunk28204* next;
};
struct  Tbigchunk27442  {
  Tbasechunk27438 Sup;
Tbigchunk27442* next;
Tbigchunk27442* prev;
NI align;
NF data;
};
struct  Ttrunk27413  {
Ttrunk27413* next;
NI key;
TY27420 bits;
};
typedef Tavlnode28208* TY28214[2];
struct  Tavlnode28208  {
TY28214 link;
NI key;
NI upperbound;
NI level;
};
struct  Tfreecell27430  {
Tfreecell27430* next;
NI zerofield;
};
struct TY517225 {
  TGenericSeq Sup;
  NI data[SEQ_DECL_SIZE];
};
N_NIMCALL(void, TMP4984)(void* p, NI op);
N_NIMCALL(NI, newrawsocket_510004)(NU8 domain, NU8 typ, NU8 protocol);
N_NIMCALL(void, raiseoserror_114430)(NI32 errorcode);
N_NIMCALL(NI32, oslasterror_114454)(void);
N_NIMCALL(Socketimpl514407*, newsocket_514708)(NI fd, NIM_BOOL isbuff);
N_NIMCALL(void*, newObj)(TNimType* typ, NI size);
N_NIMCALL(int, toint_509829)(NU8 domain);
N_NIMCALL(NI16, htons_511017)(NI16 x);
N_NIMCALL(NI32, htonl_511007)(NI32 x);
N_NIMCALL(int, bindaddr_510207)(NI socket, SOCKADDR* name, unsigned int namelen);
N_NIMCALL(Addrinfo105868*, getaddrinfo_510408)(NimStringDesc* address, NU16 port, NU8 af, NU8 typ, NU8 prot);
N_NIMCALL(void, dealloc_510608)(Addrinfo105868* ai);
N_NIMCALL(int, listen_510220)(NI socket, int backlog);
N_NIMCALL(NI, send_518716)(Socketimpl514407* socket, void* data, NI size);
N_NIMCALL(NIM_BOOL, isdisconnectionerror_514481)(NU8 flags, NI32 lasterror);
N_NIMCALL(void, socketerror_515027)(Socketimpl514407* socket, NI err, NIM_BOOL async, NI32 lasterror);
N_NIMCALL(NI32, getsocketerror_515001)(Socketimpl514407* socket);
N_NIMCALL(NimStringDesc*, copyStringRC1)(NimStringDesc* src);
static N_INLINE(void, nimGCunrefNoCycle)(void* p);
static N_INLINE(Tcell46346*, usrtocell_49846)(void* usr);
static N_INLINE(void, rtladdzct_51404)(Tcell46346* c);
N_NOINLINE(void, addzct_49817)(Tcellseq46362* s, Tcell46346* c);
N_NIMCALL(void, raiseException)(Exception* e, NCSTRING ename);
N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
N_NIMCALL(void, acceptaddr_515812)(Socketimpl514407* server, Socketimpl514407** client, NimStringDesc** address, NU8 flags);
N_NIMCALL(NimStringDesc*, cstrToNimstr)(NCSTRING str);
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(NimStringDesc*, setLengthStr)(NimStringDesc* s, NI newlen);
N_NIMCALL(NI, waitfor_517819)(Socketimpl514407* socket, NF* waited, NI timeout, NI size, NimStringDesc* funcname);
N_NIMCALL(void, nimGCvisit)(void* d, NI op);
N_NIMCALL(void, TMP4997)(void* p, NI op);
static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src);
N_NIMCALL(NimStringDesc*, rawNewString)(NI space);
static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src);
N_NIMCALL(NF, ntepochTime)(void);
N_NIMCALL(NI, select_517207)(Socketimpl514407* readfd, NI timeout);
N_NIMCALL(NIM_BOOL, hasdatabuffered_517001)(Socketimpl514407* s);
N_NIMCALL(void*, newSeq)(TNimType* typ, NI len);
N_NIMCALL(NI, select_512896)(TY517225** readfds, NI timeout);
static N_INLINE(void, HEX2BHEX3D_107688)(NF* x, NF y);
N_NIMCALL(NI, recv_517425)(Socketimpl514407* socket, void* data, NI size);
N_NIMCALL(NI, readintobuf_517235)(Socketimpl514407* socket, NI32 flags);
N_NIMCALL(NI, peekchar_518444)(Socketimpl514407* socket, NIM_CHAR* c);
N_NIMCALL(NimStringDesc*, resizeString)(NimStringDesc* dest, NI addlen);
N_NIMCALL(NimStringDesc*, addChar)(NimStringDesc* s, NIM_CHAR c);
static N_INLINE(void, pushSafePoint)(TSafePoint* s);
static N_INLINE(void, popSafePoint)(void);
N_NIMCALL(void, close_510028)(NI socket);
N_NIMCALL(void, reraiseException)(void);
TNimNode* NimDT_514407_isbuffered[3];
STRING_LITERAL(TMP4993, "No valid socket error code available", 36);
STRING_LITERAL(TMP4994, "Could not send all data.", 24);
STRING_LITERAL(TMP4996, "", 0);
STRING_LITERAL(TMP4998, "Call to \'", 9);
STRING_LITERAL(TMP4999, "\' timed out.", 12);
STRING_LITERAL(TMP5001, "readLine", 8);
STRING_LITERAL(TMP5002, "\015\012", 2);
TNimType NTI514407; /* SocketImpl */
extern TNimType NTI108; /* int */
extern TNimType NTI138; /* bool */
extern TNimType NTI147; /* char */
TNimType NTI514420; /* array[0..4000, char] */
extern TNimType NTI114; /* int32 */
TNimType NTI514409; /* Socket */
extern NI osinvalidsocket_509651;
extern TY106138 Dl_106137;
extern TNimType NTI114433; /* ref OSError */
extern TNimType NTI3433; /* OSError */
extern Tgcheap48216 gch_48244;
extern TY106038 Dl_106037;
extern TY106263 Dl_106262;
extern TNimType NTI3427; /* Exception */
TNimType NTI514415; /* TimeoutError */
TNimType NTI518014; /* ref TimeoutError */
extern TNimType NTI517225; /* seq[SocketHandle] */
extern TY106104 Dl_106103;
extern TSafePoint* exchandler_17043;
N_NIMCALL(void, TMP4984)(void* p, NI op) {
	Socketimpl514407* a;
	NI LOC1;
	a = (Socketimpl514407*)p;
	switch ((*a).isbuffered) {
	case NIM_TRUE:
	LOC1 = 0;
	for (LOC1 = 0; LOC1 < 4001; LOC1++) {
	}
	break;
	case NIM_FALSE:
	break;
	} 
}

N_NIMCALL(Socketimpl514407*, newsocket_514708)(NI fd, NIM_BOOL isbuff) {
	Socketimpl514407* result;
	result = 0;
	result = (Socketimpl514407*) newObj((&NTI514409), sizeof(Socketimpl514407));
	(*result).fd = fd;
	(*result).isbuffered = isbuff;
	{
		if (!isbuff) goto LA3;
		(*result).isbufferedU.S1.currpos = ((NI) 0);
	}
	LA3: ;
	return result;
}

N_NIMCALL(Socketimpl514407*, newsocket_514830)(NU8 domain, NU8 typ, NU8 protocol, NIM_BOOL buffered) {
	Socketimpl514407* result;
	NI fd;
	result = 0;
	fd = newrawsocket_510004(domain, typ, protocol);
	{
		NI32 LOC5;
		if (!(fd == osinvalidsocket_509651)) goto LA3;
		LOC5 = 0;
		LOC5 = oslasterror_114454();
		raiseoserror_114430(LOC5);
	}
	LA3: ;
	result = newsocket_514708(fd, buffered);
	return result;
}

N_NIMCALL(void, bindaddr_515619)(Socketimpl514407* socket, NU16 port, NimStringDesc* address) {
	{
		SOCKADDR_IN name;
		int LOC5;
		if (!((address) && (address)->Sup.len == 0)) goto LA3;
		memset((void*)(&name), 0, sizeof(name));
		LOC5 = 0;
		LOC5 = toint_509829(((NU8) 2));
		name.sin_family = ((NI16) (LOC5));
		name.sin_port = htons_511017(((NI16) (port)));
		name.sin_addr.s_addr = htonl_511007(((NI32) 0));
		{
			int LOC8;
			NI32 LOC11;
			LOC8 = 0;
			LOC8 = bindaddr_510207((*socket).fd, ((SOCKADDR*) ((&name))), ((unsigned int) (((NI)sizeof(SOCKADDR_IN)))));
			if (!(LOC8 < ((NI32) 0))) goto LA9;
			LOC11 = 0;
			LOC11 = oslasterror_114454();
			raiseoserror_114430(LOC11);
		}
		LA9: ;
	}
	goto LA1;
	LA3: ;
	{
		Addrinfo105868* ailist;
		ailist = getaddrinfo_510408(address, port, ((NU8) 2), ((NU8) 1), ((NU8) 6));
		{
			int LOC15;
			NI32 LOC18;
			LOC15 = 0;
			LOC15 = bindaddr_510207((*socket).fd, (*ailist).aiaddr, ((unsigned int) ((*ailist).aiaddrlen)));
			if (!(LOC15 < ((NI32) 0))) goto LA16;
			dealloc_510608(ailist);
			LOC18 = 0;
			LOC18 = oslasterror_114454();
			raiseoserror_114430(LOC18);
		}
		LA16: ;
		dealloc_510608(ailist);
	}
	LA1: ;
}

N_NIMCALL(void, listen_515609)(Socketimpl514407* socket, int backlog) {
	{
		int LOC3;
		NI32 LOC6;
		LOC3 = 0;
		LOC3 = listen_510220((*socket).fd, backlog);
		if (!(LOC3 < ((NI32) 0))) goto LA4;
		LOC6 = 0;
		LOC6 = oslasterror_114454();
		raiseoserror_114430(LOC6);
	}
	LA4: ;
}

N_NIMCALL(NI, send_518716)(Socketimpl514407* socket, void* data, NI size) {
	NI result;
	int LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = Dl_106137((*socket).fd, data, ((int) (size)), ((NI32) 0));
	result = ((NI) (LOC1));
	return result;
}

N_NIMCALL(NIM_BOOL, isdisconnectionerror_514481)(NU8 flags, NI32 lasterror) {
	NIM_BOOL result;
	NIM_BOOL LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = ((flags &(1<<((((NU8) 1))&7)))!=0);
	if (!(LOC1)) goto LA2;
	LOC1 = (lasterror == ((NI) 10054) || lasterror == ((NI) 10053) || lasterror == ((NI) 10052) || lasterror == ((NI) 10101) || lasterror == ((NI) 64));
	LA2: ;
	result = LOC1;
	return result;
}

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

static N_INLINE(void, rtladdzct_51404)(Tcell46346* c) {
	addzct_49817((&gch_48244.zct), c);
}

static N_INLINE(void, nimGCunrefNoCycle)(void* p) {
	Tcell46346* c;
	c = usrtocell_49846(p);
	{
		(*c).refcount -= ((NI) 8);
		if (!((NU32)((*c).refcount) < (NU32)(((NI) 8)))) goto LA3;
		rtladdzct_51404(c);
	}
	LA3: ;
}

N_NIMCALL(NI32, getsocketerror_515001)(Socketimpl514407* socket) {
	NI32 result;
	result = 0;
	result = oslasterror_114454();
	{
		if (!(result == ((NI32) 0))) goto LA3;
		result = (*socket).lasterror;
	}
	LA3: ;
	{
		Oserror3433* e_515008;
		NimStringDesc* LOC9;
		if (!(result == ((NI32) 0))) goto LA7;
		e_515008 = 0;
		e_515008 = (Oserror3433*) newObj((&NTI114433), sizeof(Oserror3433));
		(*e_515008).Sup.Sup.Sup.m_type = (&NTI3433);
		LOC9 = 0;
		LOC9 = (*e_515008).Sup.Sup.message; (*e_515008).Sup.Sup.message = copyStringRC1(((NimStringDesc*) &TMP4993));
		if (LOC9) nimGCunrefNoCycle(LOC9);
		raiseException((Exception*)e_515008, "OSError");
	}
	LA7: ;
	return result;
}

N_NIMCALL(void, socketerror_515027)(Socketimpl514407* socket, NI err, NIM_BOOL async, NI32 lasterror) {
{	{
		NIM_BOOL LOC3;
		NI32 laste;
		LOC3 = 0;
		LOC3 = (err == ((NI) -1));
		if (!(LOC3)) goto LA4;
		LOC3 = NIM_TRUE;
		LA4: ;
		if (!LOC3) goto LA5;
		{
			if (!(((NI) (lasterror)) == ((NI) -1))) goto LA9;
			laste = getsocketerror_515001(socket);
		}
		goto LA7;
		LA9: ;
		{
			laste = lasterror;
		}
		LA7: ;
		{
			if (!async) goto LA14;
			{
				if (!(laste == ((NI32) 10035))) goto LA18;
				goto BeforeRet;
			}
			goto LA16;
			LA18: ;
			{
				raiseoserror_114430(laste);
			}
			LA16: ;
		}
		goto LA12;
		LA14: ;
		{
			raiseoserror_114430(laste);
		}
		LA12: ;
	}
	LA5: ;
	}BeforeRet: ;
}

N_NIMCALL(void, send_519008)(Socketimpl514407* socket, NimStringDesc* data, NU8 flags) {
	NI sent;
{	sent = send_518716(socket, ((void*) (data->data)), (data ? data->Sup.len : 0));
	{
		NI32 lasterror;
		if (!(sent < ((NI) 0))) goto LA3;
		lasterror = oslasterror_114454();
		{
			NIM_BOOL LOC7;
			LOC7 = 0;
			LOC7 = isdisconnectionerror_514481(flags, lasterror);
			if (!LOC7) goto LA8;
			goto BeforeRet;
		}
		LA8: ;
		socketerror_515027(socket, ((NI) -1), NIM_FALSE, lasterror);
	}
	LA3: ;
	{
		Oserror3433* e_519019;
		NimStringDesc* LOC14;
		if (!!((sent == (data ? data->Sup.len : 0)))) goto LA12;
		e_519019 = 0;
		e_519019 = (Oserror3433*) newObj((&NTI114433), sizeof(Oserror3433));
		(*e_519019).Sup.Sup.Sup.m_type = (&NTI3433);
		LOC14 = 0;
		LOC14 = (*e_519019).Sup.Sup.message; (*e_519019).Sup.Sup.message = copyStringRC1(((NimStringDesc*) &TMP4994));
		if (LOC14) nimGCunrefNoCycle(LOC14);
		raiseException((Exception*)e_519019, "OSError");
	}
	LA12: ;
	}BeforeRet: ;
}

N_NIMCALL(void, acceptaddr_515812)(Socketimpl514407* server, Socketimpl514407** client, NimStringDesc** address, NU8 flags) {
	SOCKADDR_IN sockaddress;
	unsigned int addrlen;
	NI sock;
	memset((void*)(&sockaddress), 0, sizeof(sockaddress));
	addrlen = ((unsigned int) (((NI)sizeof(SOCKADDR_IN))));
	sock = Dl_106037((*server).fd, ((SOCKADDR*) ((&sockaddress))), (&addrlen));
	{
		NI32 err;
		if (!(sock == osinvalidsocket_509651)) goto LA3;
		err = oslasterror_114454();
		{
			NIM_BOOL LOC7;
			LOC7 = 0;
			LOC7 = isdisconnectionerror_514481(flags, err);
			if (!LOC7) goto LA8;
			acceptaddr_515812(server, client, address, flags);
		}
		LA8: ;
		raiseoserror_114430(err);
	}
	goto LA1;
	LA3: ;
	{
		NCSTRING LOC11;
		(*(*client)).fd = sock;
		(*(*client)).isbuffered = (*server).isbuffered;
		LOC11 = 0;
		LOC11 = Dl_106262(sockaddress.sin_addr);
		unsureAsgnRef((void**) (&(*address)), cstrToNimstr(LOC11));
	}
	LA1: ;
}

N_NIMCALL(void, accept_516401)(Socketimpl514407* server, Socketimpl514407** client, NU8 flags) {
	NimStringDesc* addrdummy;
	addrdummy = copyString(((NimStringDesc*) &TMP4996));
	acceptaddr_515812(server, client, (&addrdummy), flags);
}
N_NIMCALL(void, TMP4997)(void* p, NI op) {
	Timeouterror514415* a;
	a = (Timeouterror514415*)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)), (NI)((*src).Sup.len + ((NI) 1)));
	(*dest).Sup.len += (*src).Sup.len;
}

static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src) {
	{
		Tcell46346* c;
		if (!!((src == NIM_NIL))) goto LA3;
		c = usrtocell_49846(src);
		(*c).refcount += ((NI) 8);
	}
	LA3: ;
	{
		Tcell46346* c;
		if (!!(((*dest) == NIM_NIL))) goto LA7;
		c = usrtocell_49846((*dest));
		{
			(*c).refcount -= ((NI) 8);
			if (!((NU32)((*c).refcount) < (NU32)(((NI) 8)))) goto LA11;
			rtladdzct_51404(c);
		}
		LA11: ;
	}
	LA7: ;
	(*dest) = src;
}

N_NIMCALL(NIM_BOOL, hasdatabuffered_517001)(Socketimpl514407* s) {
	NIM_BOOL result;
	result = 0;
	result = NIM_FALSE;
	{
		NIM_BOOL LOC5;
		if (!(*s).isbuffered) goto LA3;
		LOC5 = 0;
		LOC5 = (((NI) 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, select_517207)(Socketimpl514407* readfd, NI timeout) {
	NI result;
	TY517225* fds;
	TY517214 LOC6;
{	result = 0;
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = hasdatabuffered_517001(readfd);
		if (!LOC3) goto LA4;
		result = ((NI) 1);
		goto BeforeRet;
	}
	LA4: ;
	fds = (TY517225*) newSeq((&NTI517225), 1);
	LOC6[0] = (*readfd).fd;
	fds->data[0] = LOC6[0];
	result = select_512896((&fds), timeout);
	}BeforeRet: ;
	return result;
}

static N_INLINE(void, HEX2BHEX3D_107688)(NF* x, NF y) {
	(*x) = ((NF)((*x)) + (NF)(y));
}

N_NIMCALL(NI, waitfor_517819)(Socketimpl514407* socket, NF* waited, NI timeout, NI size, NimStringDesc* funcname) {
	NI result;
{	result = 0;
	result = ((NI) 1);
	{
		if (!(size <= ((NI) 0))) goto LA3;
	}
	LA3: ;
	{
		if (!(timeout == ((NI) -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 == ((NI) 0)));
		LA13: ;
		LOC11 = LOC12;
		if (!(LOC11)) goto LA14;
		LOC11 = !(((*socket).isbufferedU.S1.buflen == (*socket).isbufferedU.S1.currpos));
		LA14: ;
		if (!LOC11) goto LA15;
		result = (NI)((*socket).isbufferedU.S1.buflen - (*socket).isbufferedU.S1.currpos);
		result = ((result <= size) ? result : size);
	}
	goto LA9;
	LA15: ;
	{
		NF starttime;
		NI selret;
		NF LOC33;
		{
			Timeouterror514415* e_518013;
			NimStringDesc* LOC22;
			if (!((NI)(timeout - ((NI) (((NF)((*waited)) * (NF)(1.0000000000000000e+003))))) < ((NI) 1))) goto LA20;
			e_518013 = 0;
			e_518013 = (Timeouterror514415*) newObj((&NTI518014), sizeof(Timeouterror514415));
			(*e_518013).Sup.Sup.m_type = (&NTI514415);
			LOC22 = 0;
			LOC22 = rawNewString(funcname->Sup.len + 21);
appendString(LOC22, ((NimStringDesc*) &TMP4998));
appendString(LOC22, funcname);
appendString(LOC22, ((NimStringDesc*) &TMP4999));
			asgnRefNoCycle((void**) (&(*e_518013).Sup.message), LOC22);
			raiseException((Exception*)e_518013, "TimeoutError");
		}
		LA20: ;
		starttime = ntepochTime();
		selret = select_517207(socket, (NI)(timeout - ((NI) (((NF)((*waited)) * (NF)(1.0000000000000000e+003))))));
		{
			NI32 LOC27;
			if (!(selret < ((NI) 0))) goto LA25;
			LOC27 = 0;
			LOC27 = oslasterror_114454();
			raiseoserror_114430(LOC27);
		}
		LA25: ;
		{
			Timeouterror514415* e_518206;
			NimStringDesc* LOC32;
			if (!!((selret == ((NI) 1)))) goto LA30;
			e_518206 = 0;
			e_518206 = (Timeouterror514415*) newObj((&NTI518014), sizeof(Timeouterror514415));
			(*e_518206).Sup.Sup.m_type = (&NTI514415);
			LOC32 = 0;
			LOC32 = rawNewString(funcname->Sup.len + 21);
appendString(LOC32, ((NimStringDesc*) &TMP4998));
appendString(LOC32, funcname);
appendString(LOC32, ((NimStringDesc*) &TMP4999));
			asgnRefNoCycle((void**) (&(*e_518206).Sup.message), LOC32);
			raiseException((Exception*)e_518206, "TimeoutError");
		}
		LA30: ;
		LOC33 = 0;
		LOC33 = ntepochTime();
		HEX2BHEX3D_107688(waited, ((NF)(LOC33) - (NF)(starttime)));
	}
	LA9: ;
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NI, readintobuf_517235)(Socketimpl514407* socket, NI32 flags) {
	NI result;
	int LOC1;
{	result = 0;
	result = ((NI) 0);
	LOC1 = 0;
	LOC1 = Dl_106103((*socket).fd, ((void*) ((*socket).isbufferedU.S1.buffer)), ((int) 4000), flags);
	result = ((NI) (LOC1));
	{
		if (!(result < ((NI) 0))) goto LA4;
		(*socket).lasterror = oslasterror_114454();
	}
	LA4: ;
	{
		if (!(result <= ((NI) 0))) goto LA8;
		(*socket).isbufferedU.S1.buflen = ((NI) 0);
		(*socket).isbufferedU.S1.currpos = ((NI) 0);
		goto BeforeRet;
	}
	LA8: ;
	(*socket).isbufferedU.S1.buflen = result;
	(*socket).isbufferedU.S1.currpos = ((NI) 0);
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NI, recv_517425)(Socketimpl514407* socket, void* data, NI size) {
	NI result;
{	result = 0;
	{
		if (!(size == ((NI) 0))) goto LA3;
		goto BeforeRet;
	}
	LA3: ;
	{
		NI read;
		if (!(*socket).isbuffered) goto LA7;
		{
			NI res;
			if (!((*socket).isbufferedU.S1.buflen == ((NI) 0))) goto LA11;
			res = readintobuf_517235(socket, ((NI32) 0));
			{
				if (!(res <= ((NI) 0))) goto LA15;
				{
					if (!NIM_FALSE) goto LA19;
					result = ((NI) 0);
					goto BeforeRet;
				}
				goto LA17;
				LA19: ;
				{
					result = res;
					goto BeforeRet;
				}
				LA17: ;
			}
			LA15: ;
		}
		LA11: ;
		read = ((NI) 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_517235(socket, ((NI32) 0));
					{
						if (!(res <= ((NI) 0))) goto LA30;
						{
							if (!(((NI) 0) < read)) goto LA34;
							result = read;
							goto BeforeRet;
						}
						goto LA32;
						LA34: ;
						{
							result = res;
							goto BeforeRet;
						}
						LA32: ;
					}
					LA30: ;
				}
				LA26: ;
				chunk = (((NI)((*socket).isbufferedU.S1.buflen - (*socket).isbufferedU.S1.currpos) <= (NI)(size - read)) ? (NI)((*socket).isbufferedU.S1.buflen - (*socket).isbufferedU.S1.currpos) : (NI)(size - read));
				d = ((NCSTRING) (data));
				memcpy(((void*) ((&d[read]))), ((void*) ((&(*socket).isbufferedU.S1.buffer[((*socket).isbufferedU.S1.currpos)- 0]))), ((NI) (chunk)));
				read += chunk;
				(*socket).isbufferedU.S1.currpos += chunk;
			} LA23: ;
		}
		result = read;
	}
	goto LA5;
	LA7: ;
	{
		int LOC38;
		LOC38 = 0;
		LOC38 = Dl_106103((*socket).fd, data, ((int) (size)), ((NI32) 0));
		result = ((NI) (LOC38));
		{
			if (!(result < ((NI) 0))) goto LA41;
			(*socket).lasterror = oslasterror_114454();
		}
		LA41: ;
	}
	LA5: ;
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NI, peekchar_518444)(Socketimpl514407* socket, NIM_CHAR* c) {
	NI result;
	result = 0;
	{
		if (!(*socket).isbuffered) goto LA3;
		result = ((NI) 1);
		{
			NIM_BOOL LOC7;
			NI res;
			LOC7 = 0;
			LOC7 = ((*socket).isbufferedU.S1.buflen == ((NI) 0));
			if (LOC7) goto LA8;
			LOC7 = ((NI)((*socket).isbufferedU.S1.buflen - ((NI) 1)) < (*socket).isbufferedU.S1.currpos);
			LA8: ;
			if (!LOC7) goto LA9;
			res = readintobuf_517235(socket, ((NI32) 0));
			{
				if (!(res <= ((NI) 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_106103((*socket).fd, ((void*) (c)), ((int) 1), ((int) 2));
		result = ((NI) (LOC16));
	}
	LA1: ;
	return result;
}

N_NIMCALL(void, readline_518608)(Socketimpl514407* socket, NimStringDesc** line, NI timeout, NU8 flags) {
	NF waited;
{	waited = 0.0;
	(*line) = setLengthStr((*line), ((NI) 0));
	{
		while (1) {
			NIM_CHAR c;
			NI LOC3;
			NI n;
			c = 0;
			LOC3 = 0;
			LOC3 = waitfor_517819(socket, (&waited), timeout, ((NI) 1), ((NimStringDesc*) &TMP5001));
			n = recv_517425(socket, ((void*) ((&c))), ((NI) 1));
			{
				NI32 lasterror;
				if (!(n < ((NI) 0))) goto LA6;
				lasterror = getsocketerror_515001(socket);
				{
					NIM_BOOL LOC10;
					LOC10 = 0;
					LOC10 = isdisconnectionerror_514481(flags, lasterror);
					if (!LOC10) goto LA11;
					(*line) = setLengthStr((*line), ((NI) 0));
					goto BeforeRet;
				}
				LA11: ;
				socketerror_515027(socket, n, NIM_FALSE, lasterror);
			}
			goto LA4;
			LA6: ;
			{
				if (!(n == ((NI) 0))) goto LA14;
				(*line) = setLengthStr((*line), ((NI) 0));
				goto BeforeRet;
			}
			goto LA4;
			LA14: ;
			LA4: ;
			{
				NI LOC20;
				if (!((NU8)(c) == (NU8)(13))) goto LA18;
				LOC20 = 0;
				LOC20 = waitfor_517819(socket, (&waited), timeout, ((NI) 1), ((NimStringDesc*) &TMP5001));
				n = peekchar_518444(socket, (&c));
				{
					NIM_BOOL LOC23;
					NI LOC27;
					LOC23 = 0;
					LOC23 = (((NI) 0) < n);
					if (!(LOC23)) goto LA24;
					LOC23 = ((NU8)(c) == (NU8)(10));
					LA24: ;
					if (!LOC23) goto LA25;
					LOC27 = 0;
					LOC27 = recv_517425(socket, ((void*) ((&c))), ((NI) 1));
				}
				goto LA21;
				LA25: ;
				{
					NI32 lasterror;
					if (!(n <= ((NI) 0))) goto LA29;
					lasterror = getsocketerror_515001(socket);
					{
						NIM_BOOL LOC33;
						LOC33 = 0;
						LOC33 = isdisconnectionerror_514481(flags, lasterror);
						if (!LOC33) goto LA34;
						(*line) = setLengthStr((*line), ((NI) 0));
						goto BeforeRet;
					}
					LA34: ;
					socketerror_515027(socket, n, NIM_FALSE, lasterror);
				}
				goto LA21;
				LA29: ;
				LA21: ;
				{
					if (!(((*line) ? (*line)->Sup.len : 0) == ((NI) 0))) goto LA38;
					(*line) = resizeString((*line), 2);
appendString((*line), ((NimStringDesc*) &TMP5002));
				}
				LA38: ;
				goto BeforeRet;
			}
			goto LA16;
			LA18: ;
			{
				if (!((NU8)(c) == (NU8)(10))) goto LA41;
				{
					if (!(((*line) ? (*line)->Sup.len : 0) == ((NI) 0))) goto LA45;
					(*line) = resizeString((*line), 2);
appendString((*line), ((NimStringDesc*) &TMP5002));
				}
				LA45: ;
				goto BeforeRet;
			}
			goto LA16;
			LA41: ;
			LA16: ;
			(*line) = addChar((*line), c);
		}
	}
	}BeforeRet: ;
}

static N_INLINE(void, pushSafePoint)(TSafePoint* s) {
	(*s).hasRaiseAction = NIM_FALSE;
	(*s).prev = exchandler_17043;
	exchandler_17043 = s;
}

static N_INLINE(void, popSafePoint)(void) {
	exchandler_17043 = (*exchandler_17043).prev;
}

N_NIMCALL(void, close_516417)(Socketimpl514407* socket) {
	TSafePoint TMP5003;
	pushSafePoint(&TMP5003);
	TMP5003.status = setjmp(TMP5003.context);
	if (TMP5003.status == 0) {
		popSafePoint();
	}
	else {
		popSafePoint();
	}
	{
		close_510028((*socket).fd);
	}
	if (TMP5003.status != 0) reraiseException();
}
NIM_EXTERNC N_NOINLINE(void, stdlib_netInit)(void) {
}

NIM_EXTERNC N_NOINLINE(void, stdlib_netDatInit)(void) {
static TNimNode* TMP4982[3];
static TNimNode* TMP4983[3];
static TNimNode TMP1074[10];
NTI514407.size = sizeof(Socketimpl514407);
NTI514407.kind = 18;
NTI514407.base = 0;
NTI514407.flags = 3;
TMP4982[0] = &TMP1074[1];
TMP1074[1].kind = 1;
TMP1074[1].offset = offsetof(Socketimpl514407, fd);
TMP1074[1].typ = (&NTI108);
TMP1074[1].name = "fd";
TMP4982[1] = &TMP1074[2];
TMP1074[2].kind = 3;
TMP1074[2].offset = offsetof(Socketimpl514407, isbuffered);
TMP1074[2].typ = (&NTI138);
TMP1074[2].name = "isBuffered";
TMP1074[2].sons = &NimDT_514407_isbuffered[0];
TMP1074[2].len = 2;
TMP4983[0] = &TMP1074[4];
NTI514420.size = sizeof(TY514420);
NTI514420.kind = 16;
NTI514420.base = (&NTI147);
NTI514420.flags = 3;
TMP1074[4].kind = 1;
TMP1074[4].offset = offsetof(Socketimpl514407, isbufferedU.S1.buffer);
TMP1074[4].typ = (&NTI514420);
TMP1074[4].name = "buffer";
TMP4983[1] = &TMP1074[5];
TMP1074[5].kind = 1;
TMP1074[5].offset = offsetof(Socketimpl514407, isbufferedU.S1.currpos);
TMP1074[5].typ = (&NTI108);
TMP1074[5].name = "currPos";
TMP4983[2] = &TMP1074[6];
TMP1074[6].kind = 1;
TMP1074[6].offset = offsetof(Socketimpl514407, isbufferedU.S1.buflen);
TMP1074[6].typ = (&NTI108);
TMP1074[6].name = "bufLen";
TMP1074[3].len = 3; TMP1074[3].kind = 2; TMP1074[3].sons = &TMP4983[0];
NimDT_514407_isbuffered[1] = &TMP1074[3];
TMP1074[7].len = 0; TMP1074[7].kind = 2;
NimDT_514407_isbuffered[0] = &TMP1074[7];
TMP4982[2] = &TMP1074[8];
TMP1074[8].kind = 1;
TMP1074[8].offset = offsetof(Socketimpl514407, lasterror);
TMP1074[8].typ = (&NTI114);
TMP1074[8].name = "lastError";
TMP1074[0].len = 3; TMP1074[0].kind = 2; TMP1074[0].sons = &TMP4982[0];
NTI514407.node = &TMP1074[0];
NTI514409.size = sizeof(Socketimpl514407*);
NTI514409.kind = 22;
NTI514409.base = (&NTI514407);
NTI514409.flags = 2;
NTI514409.marker = TMP4984;
NTI514415.size = sizeof(Timeouterror514415);
NTI514415.kind = 17;
NTI514415.base = (&NTI3427);
TMP1074[9].len = 0; TMP1074[9].kind = 2;
NTI514415.node = &TMP1074[9];
NTI518014.size = sizeof(Timeouterror514415*);
NTI518014.kind = 22;
NTI518014.base = (&NTI514415);
NTI518014.marker = TMP4997;
}

