/* Generated by Nim Compiler v0.12.0 */
/*   (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 Socketimpl569407 Socketimpl569407;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct Addrinfo112868 Addrinfo112868;
typedef struct Timeouterror569415 Timeouterror569415;
typedef struct Exception Exception;
typedef struct TNimObject TNimObject;
typedef struct Cell48946 Cell48946;
typedef struct Cellseq48962 Cellseq48962;
typedef struct Gcheap51018 Gcheap51018;
typedef struct Gcstack51016 Gcstack51016;
typedef struct Cellset48958 Cellset48958;
typedef struct Pagedesc48954 Pagedesc48954;
typedef struct Memregion30890 Memregion30890;
typedef struct Smallchunk30842 Smallchunk30842;
typedef struct Llchunk30884 Llchunk30884;
typedef struct Bigchunk30844 Bigchunk30844;
typedef struct Intset30817 Intset30817;
typedef struct Trunk30813 Trunk30813;
typedef struct Avlnode30888 Avlnode30888;
typedef struct Gcstat51014 Gcstat51014;
typedef struct TY572225 TY572225;
typedef struct TSafePoint TSafePoint;
typedef struct Basechunk30840 Basechunk30840;
typedef struct Freecell30832 Freecell30832;
typedef NIM_CHAR TY569421[4001];
struct  Socketimpl569407  {
NI fd;
NIM_BOOL isbuffered;
union{
struct {TY569421 buffer;
NI currpos;
NI buflen;
} S1;
} isbufferedU;
NI32 lasterror;
NU8 domain;
NU8 socktype;
NU8 protocol;
};
typedef N_NIMCALL_PTR(void, TY3889) (void* p, NI op);
typedef N_NIMCALL_PTR(void*, TY3894) (void* p);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY3889 marker;
TY3894 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 TY112891[8];
struct  Addrinfo112868  {
int aiflags;
int aifamily;
int aisocktype;
int aiprotocol;
NI aiaddrlen;
NCSTRING aicanonname;
SOCKADDR* aiaddr;
Addrinfo112868* ainext;
};
typedef N_STDCALL_PTR(int, TY113148) (NI s, void* buf, int len, int flags);
typedef N_STDCALL_PTR(NI, TY113038) (NI s, SOCKADDR* a, unsigned int* addrlen);
typedef N_STDCALL_PTR(NCSTRING, TY113273) (IN_ADDR i);
struct  TNimObject  {
TNimType* m_type;
};
struct  Exception  {
  TNimObject Sup;
Exception* parent;
NCSTRING name;
NimStringDesc* message;
NimStringDesc* trace;
};
struct  Timeouterror569415  {
  Exception Sup;
};
struct  Cell48946  {
NI refcount;
TNimType* typ;
};
struct  Cellseq48962  {
NI len;
NI cap;
Cell48946** d;
};
struct  Cellset48958  {
NI counter;
NI max;
Pagedesc48954* head;
Pagedesc48954** data;
};
typedef Smallchunk30842* TY30905[512];
typedef Trunk30813* Trunkbuckets30815[256];
struct  Intset30817  {
Trunkbuckets30815 data;
};
struct  Memregion30890  {
NI minlargeobj;
NI maxlargeobj;
TY30905 freesmallchunks;
Llchunk30884* llmem;
NI currmem;
NI maxmem;
NI freemem;
NI lastsize;
Bigchunk30844* freechunkslist;
Intset30817 chunkstarts;
Avlnode30888* root;
Avlnode30888* deleted;
Avlnode30888* last;
Avlnode30888* freeavlnodes;
};
struct  Gcstat51014  {
NI stackscans;
NI cyclecollections;
NI maxthreshold;
NI maxstacksize;
NI maxstackcells;
NI cycletablesize;
NI64 maxpause;
};
struct  Gcheap51018  {
Gcstack51016* stack;
void* stackbottom;
NI cyclethreshold;
Cellseq48962 zct;
Cellseq48962 decstack;
Cellset48958 cycleroots;
Cellseq48962 tempstack;
NI recgclock;
Memregion30890 region;
Gcstat51014 stat;
};
typedef NI TY572214[1];
typedef N_STDCALL_PTR(int, TY113114) (NI s, void* buf, int len, int flags);
typedef struct {
N_NIMCALL_PTR(NIM_BOOL, ClPrc) (Exception* e, void* ClEnv);
void* ClEnv;
} TY19211;
struct  TSafePoint  {
TSafePoint* prev;
NI status;
jmp_buf context;
NIM_BOOL hasRaiseAction;
TY19211 raiseAction;
};
typedef NIM_CHAR TY112884[14];
struct  Gcstack51016  {
Gcstack51016* prev;
Gcstack51016* next;
void* starts;
void* pos;
NI maxstacksize;
};
typedef NI TY30821[16];
struct  Pagedesc48954  {
Pagedesc48954* next;
NI key;
TY30821 bits;
};
struct  Basechunk30840  {
NI prevsize;
NI size;
NIM_BOOL used;
};
struct  Smallchunk30842  {
  Basechunk30840 Sup;
Smallchunk30842* next;
Smallchunk30842* prev;
Freecell30832* freelist;
NI free;
NI acc;
NF data;
};
struct  Llchunk30884  {
NI size;
NI acc;
Llchunk30884* next;
};
struct  Bigchunk30844  {
  Basechunk30840 Sup;
Bigchunk30844* next;
Bigchunk30844* prev;
NI align;
NF data;
};
struct  Trunk30813  {
Trunk30813* next;
NI key;
TY30821 bits;
};
typedef Avlnode30888* TY30895[2];
struct  Avlnode30888  {
TY30895 link;
NI key;
NI upperbound;
NI level;
};
struct  Freecell30832  {
Freecell30832* next;
NI zerofield;
};
struct TY572225 {
  TGenericSeq Sup;
  NI data[SEQ_DECL_SIZE];
};
N_NIMCALL(void, TMP5252)(void* p, NI op);
N_NIMCALL(NI, newnativesocket_564004)(NU8 domain, NU8 socktype, NU8 protocol);
N_NOINLINE(void, raiseoserror_125030)(NI32 errorcode, NimStringDesc* additionalinfo);
N_NIMCALL(NI32, oslasterror_125055)(void);
N_NIMCALL(Socketimpl569407*, newsocket_569708)(NI fd, NU8 domain, NU8 socktype, NU8 protocol, NIM_BOOL buffered);
N_NIMCALL(void*, newObj)(TNimType* typ, NI size);
N_NIMCALL(int, toint_563829)(NU8 domain);
N_NIMCALL(NI16, ntohs_565007)(NI16 x);
N_NIMCALL(NI32, ntohl_564818)(NI32 x);
N_NIMCALL(int, bindaddr_564207)(NI socket, SOCKADDR* name, unsigned int namelen);
N_NIMCALL(Addrinfo112868*, getaddrinfo_564408)(NimStringDesc* address, NU16 port, NU8 domain, NU8 socktype, NU8 protocol);
N_NIMCALL(void, dealloc_564808)(Addrinfo112868* ai);
N_NIMCALL(int, listen_564220)(NI socket, int backlog);
N_NIMCALL(NI, send_573714)(Socketimpl569407* socket, void* data, NI size);
N_NIMCALL(NIM_BOOL, isdisconnectionerror_569497)(NU8 flags, NI32 lasterror);
N_NIMCALL(void, socketerror_569488)(Socketimpl569407* socket, NI err, NIM_BOOL async, NI32 lasterror);
N_NIMCALL(NI32, getsocketerror_570001)(Socketimpl569407* socket);
N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
N_NIMCALL(void, acceptaddr_570813)(Socketimpl569407* server, Socketimpl569407** 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_572819)(Socketimpl569407* socket, NF* waited, NI timeout, NI size, NimStringDesc* funcname);
N_NIMCALL(void, nimGCvisit)(void* d, NI op);
N_NIMCALL(void, TMP5264)(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);
static N_INLINE(Cell48946*, usrtocell_52646)(void* usr);
static N_INLINE(void, rtladdzct_54204)(Cell48946* c);
N_NOINLINE(void, addzct_52617)(Cellseq48962* s, Cell48946* c);
N_NIMCALL(void, raiseException)(Exception* e, NCSTRING ename);
N_NIMCALL(NF, ntepochTime)(void);
N_NIMCALL(NI, select_572207)(Socketimpl569407* readfd, NI timeout);
N_NIMCALL(NIM_BOOL, hasdatabuffered_572001)(Socketimpl569407* s);
N_NIMCALL(void*, newSeq)(TNimType* typ, NI len);
N_NIMCALL(NI, select_568096)(TY572225** readfds, NI timeout);
static N_INLINE(void, HEX2BHEX3D_114699)(NF* x, NF y);
N_NIMCALL(NI, recv_572425)(Socketimpl569407* socket, void* data, NI size);
N_NIMCALL(NI, readintobuf_572235)(Socketimpl569407* socket, NI32 flags);
N_NIMCALL(NI, peekchar_573444)(Socketimpl569407* 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_564028)(NI socket);
N_NIMCALL(void, reraiseException)(void);
TNimNode* NimDT_569407_isbuffered[3];
STRING_LITERAL(TMP5258, "", 0);
STRING_LITERAL(TMP5261, "No valid socket error code available", 36);
STRING_LITERAL(TMP5262, "Could not send all data.", 24);
STRING_LITERAL(TMP5265, "Call to \'", 9);
STRING_LITERAL(TMP5266, "\' timed out.", 12);
STRING_LITERAL(TMP5268, "readLine", 8);
STRING_LITERAL(TMP5269, "\015\012", 2);
TNimType NTI569407; /* SocketImpl */
extern TNimType NTI108; /* int */
extern TNimType NTI138; /* bool */
extern TNimType NTI147; /* char */
TNimType NTI569421; /* array[0..4000, char] */
extern TNimType NTI114; /* int32 */
extern TNimType NTI563604; /* Domain */
extern TNimType NTI563606; /* SockType */
extern TNimType NTI563608; /* Protocol */
TNimType NTI569409; /* Socket */
extern NI osinvalidsocket_563651;
extern TY113148 Dl_113147;
extern TY113038 Dl_113037;
extern TY113273 Dl_113272;
extern TNimType NTI4027; /* Exception */
TNimType NTI569415; /* TimeoutError */
TNimType NTI573014; /* ref TimeoutError */
extern Gcheap51018 gch_51059;
extern TNimType NTI572225; /* seq[SocketHandle] */
extern TY113114 Dl_113113;
extern TSafePoint* exchandler_20843;
N_NIMCALL(void, TMP5252)(void* p, NI op) {
	Socketimpl569407* a;
	NI LOC1;
	a = (Socketimpl569407*)p;
	switch ((*a).isbuffered) {
	case NIM_TRUE:
	LOC1 = 0;
	for (LOC1 = 0; LOC1 < 4001; LOC1++) {
	}
	break;
	case NIM_FALSE:
	break;
	} 
}

N_NIMCALL(Socketimpl569407*, newsocket_569708)(NI fd, NU8 domain, NU8 socktype, NU8 protocol, NIM_BOOL buffered) {
	Socketimpl569407* result;
	result = 0;
	result = (Socketimpl569407*) newObj((&NTI569409), sizeof(Socketimpl569407));
	(*result).fd = fd;
	(*result).isbuffered = buffered;
	(*result).domain = domain;
	(*result).socktype = socktype;
	(*result).protocol = protocol;
	{
		if (!buffered) goto LA3;
		(*result).isbufferedU.S1.currpos = ((NI) 0);
	}
	LA3: ;
	return result;
}

N_NIMCALL(Socketimpl569407*, newsocket_569833)(NU8 domain, NU8 socktype, NU8 protocol, NIM_BOOL buffered) {
	Socketimpl569407* result;
	NI fd;
	result = 0;
	fd = newnativesocket_564004(domain, socktype, protocol);
	{
		NI32 LOC5;
		if (!(fd == osinvalidsocket_563651)) goto LA3;
		LOC5 = 0;
		LOC5 = oslasterror_125055();
		raiseoserror_125030(LOC5, ((NimStringDesc*) &TMP5258));
	}
	LA3: ;
	result = newsocket_569708(fd, domain, socktype, protocol, buffered);
	return result;
}

N_NIMCALL(void, bindaddr_570619)(Socketimpl569407* 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_563829(((NU8) 2));
		name.sin_family = ((NI16) (LOC5));
		name.sin_port = ntohs_565007(((NI16) (port)));
		name.sin_addr.s_addr = ntohl_564818(((NI32) 0));
		{
			int LOC8;
			NI32 LOC11;
			LOC8 = 0;
			LOC8 = bindaddr_564207((*socket).fd, ((SOCKADDR*) ((&name))), ((unsigned int) (((NI)sizeof(SOCKADDR_IN)))));
			if (!(LOC8 < ((NI32) 0))) goto LA9;
			LOC11 = 0;
			LOC11 = oslasterror_125055();
			raiseoserror_125030(LOC11, ((NimStringDesc*) &TMP5258));
		}
		LA9: ;
	}
	goto LA1;
	LA3: ;
	{
		Addrinfo112868* ailist;
		ailist = getaddrinfo_564408(address, port, (*socket).domain, ((NU8) 1), ((NU8) 6));
		{
			int LOC15;
			NI32 LOC18;
			LOC15 = 0;
			LOC15 = bindaddr_564207((*socket).fd, (*ailist).aiaddr, ((unsigned int) ((*ailist).aiaddrlen)));
			if (!(LOC15 < ((NI32) 0))) goto LA16;
			dealloc_564808(ailist);
			LOC18 = 0;
			LOC18 = oslasterror_125055();
			raiseoserror_125030(LOC18, ((NimStringDesc*) &TMP5258));
		}
		LA16: ;
		dealloc_564808(ailist);
	}
	LA1: ;
}

N_NIMCALL(void, listen_570609)(Socketimpl569407* socket, int backlog) {
	{
		int LOC3;
		NI32 LOC6;
		LOC3 = 0;
		LOC3 = listen_564220((*socket).fd, backlog);
		if (!(LOC3 < ((NI32) 0))) goto LA4;
		LOC6 = 0;
		LOC6 = oslasterror_125055();
		raiseoserror_125030(LOC6, ((NimStringDesc*) &TMP5258));
	}
	LA4: ;
}

N_NIMCALL(NI, send_573714)(Socketimpl569407* socket, void* data, NI size) {
	NI result;
	int LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = Dl_113147((*socket).fd, data, ((int) (size)), ((NI32) 0));
	result = ((NI) (LOC1));
	return result;
}

N_NIMCALL(NIM_BOOL, isdisconnectionerror_569497)(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;
}

N_NIMCALL(NI32, getsocketerror_570001)(Socketimpl569407* socket) {
	NI32 result;
	result = 0;
	result = oslasterror_125055();
	{
		if (!(result == ((NI32) 0))) goto LA3;
		result = (*socket).lasterror;
	}
	LA3: ;
	{
		if (!(result == ((NI32) 0))) goto LA7;
		raiseoserror_125030(result, ((NimStringDesc*) &TMP5261));
	}
	LA7: ;
	return result;
}

N_NIMCALL(void, socketerror_569488)(Socketimpl569407* 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_570001(socket);
		}
		goto LA7;
		LA9: ;
		{
			laste = lasterror;
		}
		LA7: ;
		{
			if (!async) goto LA14;
			{
				if (!(laste == ((NI32) 10035))) goto LA18;
				goto BeforeRet;
			}
			goto LA16;
			LA18: ;
			{
				raiseoserror_125030(laste, ((NimStringDesc*) &TMP5258));
			}
			LA16: ;
		}
		goto LA12;
		LA14: ;
		{
			raiseoserror_125030(laste, ((NimStringDesc*) &TMP5258));
		}
		LA12: ;
	}
	LA5: ;
	}BeforeRet: ;
}

N_NIMCALL(void, send_574008)(Socketimpl569407* socket, NimStringDesc* data, NU8 flags) {
	NI sent;
{	sent = send_573714(socket, ((void*) (data->data)), (data ? data->Sup.len : 0));
	{
		NI32 lasterror;
		if (!(sent < ((NI) 0))) goto LA3;
		lasterror = oslasterror_125055();
		{
			NIM_BOOL LOC7;
			LOC7 = 0;
			LOC7 = isdisconnectionerror_569497(flags, lasterror);
			if (!LOC7) goto LA8;
			goto BeforeRet;
		}
		LA8: ;
		socketerror_569488(socket, ((NI) -1), NIM_FALSE, lasterror);
	}
	LA3: ;
	{
		NI32 LOC14;
		if (!!((sent == (data ? data->Sup.len : 0)))) goto LA12;
		LOC14 = 0;
		LOC14 = oslasterror_125055();
		raiseoserror_125030(LOC14, ((NimStringDesc*) &TMP5262));
	}
	LA12: ;
	}BeforeRet: ;
}

N_NIMCALL(void, acceptaddr_570813)(Socketimpl569407* server, Socketimpl569407** 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_113037((*server).fd, ((SOCKADDR*) ((&sockaddress))), (&addrlen));
	{
		NI32 err;
		if (!(sock == osinvalidsocket_563651)) goto LA3;
		err = oslasterror_125055();
		{
			NIM_BOOL LOC7;
			LOC7 = 0;
			LOC7 = isdisconnectionerror_569497(flags, err);
			if (!LOC7) goto LA8;
			acceptaddr_570813(server, client, address, flags);
		}
		LA8: ;
		raiseoserror_125030(err, ((NimStringDesc*) &TMP5258));
	}
	goto LA1;
	LA3: ;
	{
		NCSTRING LOC11;
		(*(*client)).fd = sock;
		(*(*client)).isbuffered = (*server).isbuffered;
		LOC11 = 0;
		LOC11 = Dl_113272(sockaddress.sin_addr);
		unsureAsgnRef((void**) (&(*address)), cstrToNimstr(LOC11));
	}
	LA1: ;
}

N_NIMCALL(void, accept_571401)(Socketimpl569407* server, Socketimpl569407** client, NU8 flags) {
	NimStringDesc* addrdummy;
	addrdummy = copyString(((NimStringDesc*) &TMP5258));
	acceptaddr_570813(server, client, (&addrdummy), flags);
}
N_NIMCALL(void, TMP5264)(void* p, NI op) {
	Timeouterror569415* a;
	a = (Timeouterror569415*)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(Cell48946*, usrtocell_52646)(void* usr) {
	Cell48946* result;
	result = 0;
	result = ((Cell48946*) ((NI)((NU32)(((NI) (usr))) - (NU32)(((NI)sizeof(Cell48946))))));
	return result;
}

static N_INLINE(void, rtladdzct_54204)(Cell48946* c) {
	addzct_52617((&gch_51059.zct), c);
}

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

N_NIMCALL(NIM_BOOL, hasdatabuffered_572001)(Socketimpl569407* 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_572207)(Socketimpl569407* readfd, NI timeout) {
	NI result;
	TY572225* fds;
	TY572214 LOC6;
{	result = 0;
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = hasdatabuffered_572001(readfd);
		if (!LOC3) goto LA4;
		result = ((NI) 1);
		goto BeforeRet;
	}
	LA4: ;
	fds = (TY572225*) newSeq((&NTI572225), 1);
	LOC6[0] = (*readfd).fd;
	fds->data[0] = LOC6[0];
	result = select_568096((&fds), timeout);
	}BeforeRet: ;
	return result;
}

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

N_NIMCALL(NI, waitfor_572819)(Socketimpl569407* 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;
		{
			Timeouterror569415* e_573013;
			NimStringDesc* LOC22;
			if (!((NI)(timeout - ((NI) (((NF)((*waited)) * (NF)(1.0000000000000000e+03))))) < ((NI) 1))) goto LA20;
			e_573013 = 0;
			e_573013 = (Timeouterror569415*) newObj((&NTI573014), sizeof(Timeouterror569415));
			(*e_573013).Sup.Sup.m_type = (&NTI569415);
			LOC22 = 0;
			LOC22 = rawNewString(funcname->Sup.len + 21);
appendString(LOC22, ((NimStringDesc*) &TMP5265));
appendString(LOC22, funcname);
appendString(LOC22, ((NimStringDesc*) &TMP5266));
			asgnRefNoCycle((void**) (&(*e_573013).Sup.message), LOC22);
			raiseException((Exception*)e_573013, "TimeoutError");
		}
		LA20: ;
		starttime = ntepochTime();
		selret = select_572207(socket, (NI)(timeout - ((NI) (((NF)((*waited)) * (NF)(1.0000000000000000e+03))))));
		{
			NI32 LOC27;
			if (!(selret < ((NI) 0))) goto LA25;
			LOC27 = 0;
			LOC27 = oslasterror_125055();
			raiseoserror_125030(LOC27, ((NimStringDesc*) &TMP5258));
		}
		LA25: ;
		{
			Timeouterror569415* e_573206;
			NimStringDesc* LOC32;
			if (!!((selret == ((NI) 1)))) goto LA30;
			e_573206 = 0;
			e_573206 = (Timeouterror569415*) newObj((&NTI573014), sizeof(Timeouterror569415));
			(*e_573206).Sup.Sup.m_type = (&NTI569415);
			LOC32 = 0;
			LOC32 = rawNewString(funcname->Sup.len + 21);
appendString(LOC32, ((NimStringDesc*) &TMP5265));
appendString(LOC32, funcname);
appendString(LOC32, ((NimStringDesc*) &TMP5266));
			asgnRefNoCycle((void**) (&(*e_573206).Sup.message), LOC32);
			raiseException((Exception*)e_573206, "TimeoutError");
		}
		LA30: ;
		LOC33 = 0;
		LOC33 = ntepochTime();
		HEX2BHEX3D_114699(waited, ((NF)(LOC33) - (NF)(starttime)));
	}
	LA9: ;
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NI, readintobuf_572235)(Socketimpl569407* socket, NI32 flags) {
	NI result;
	int LOC1;
{	result = 0;
	result = ((NI) 0);
	LOC1 = 0;
	LOC1 = Dl_113113((*socket).fd, ((void*) ((*socket).isbufferedU.S1.buffer)), ((int) 4000), flags);
	result = ((NI) (LOC1));
	{
		if (!(result < ((NI) 0))) goto LA4;
		(*socket).lasterror = oslasterror_125055();
	}
	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_572425)(Socketimpl569407* 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_572235(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_572235(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_113113((*socket).fd, data, ((int) (size)), ((NI32) 0));
		result = ((NI) (LOC38));
		{
			if (!(result < ((NI) 0))) goto LA41;
			(*socket).lasterror = oslasterror_125055();
		}
		LA41: ;
	}
	LA5: ;
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NI, peekchar_573444)(Socketimpl569407* 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_572235(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_113113((*socket).fd, ((void*) (c)), ((int) 1), ((int) 2));
		result = ((NI) (LOC16));
	}
	LA1: ;
	return result;
}

N_NIMCALL(void, readline_573608)(Socketimpl569407* 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_572819(socket, (&waited), timeout, ((NI) 1), ((NimStringDesc*) &TMP5268));
			n = recv_572425(socket, ((void*) ((&c))), ((NI) 1));
			{
				NI32 lasterror;
				if (!(n < ((NI) 0))) goto LA6;
				lasterror = getsocketerror_570001(socket);
				{
					NIM_BOOL LOC10;
					LOC10 = 0;
					LOC10 = isdisconnectionerror_569497(flags, lasterror);
					if (!LOC10) goto LA11;
					(*line) = setLengthStr((*line), ((NI) 0));
					goto BeforeRet;
				}
				LA11: ;
				socketerror_569488(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_572819(socket, (&waited), timeout, ((NI) 1), ((NimStringDesc*) &TMP5268));
				n = peekchar_573444(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_572425(socket, ((void*) ((&c))), ((NI) 1));
				}
				goto LA21;
				LA25: ;
				{
					NI32 lasterror;
					if (!(n <= ((NI) 0))) goto LA29;
					lasterror = getsocketerror_570001(socket);
					{
						NIM_BOOL LOC33;
						LOC33 = 0;
						LOC33 = isdisconnectionerror_569497(flags, lasterror);
						if (!LOC33) goto LA34;
						(*line) = setLengthStr((*line), ((NI) 0));
						goto BeforeRet;
					}
					LA34: ;
					socketerror_569488(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*) &TMP5269));
				}
				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*) &TMP5269));
				}
				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_20843;
	exchandler_20843 = s;
}

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

N_NIMCALL(void, close_571417)(Socketimpl569407* socket) {
	TSafePoint TMP5270;
	pushSafePoint(&TMP5270);
	TMP5270.status = setjmp(TMP5270.context);
	if (TMP5270.status == 0) {
		popSafePoint();
	}
	else {
		popSafePoint();
	}
	{
		close_564028((*socket).fd);
	}
	if (TMP5270.status != 0) reraiseException();
}
NIM_EXTERNC N_NOINLINE(void, stdlib_netInit000)(void) {
}

NIM_EXTERNC N_NOINLINE(void, stdlib_netDatInit000)(void) {
static TNimNode* TMP5241[6];
static TNimNode* TMP5242[3];
static TNimNode TMP1160[13];
NTI569407.size = sizeof(Socketimpl569407);
NTI569407.kind = 18;
NTI569407.base = 0;
NTI569407.flags = 3;
TMP5241[0] = &TMP1160[1];
TMP1160[1].kind = 1;
TMP1160[1].offset = offsetof(Socketimpl569407, fd);
TMP1160[1].typ = (&NTI108);
TMP1160[1].name = "fd";
TMP5241[1] = &TMP1160[2];
TMP1160[2].kind = 3;
TMP1160[2].offset = offsetof(Socketimpl569407, isbuffered);
TMP1160[2].typ = (&NTI138);
TMP1160[2].name = "isBuffered";
TMP1160[2].sons = &NimDT_569407_isbuffered[0];
TMP1160[2].len = 2;
TMP5242[0] = &TMP1160[4];
NTI569421.size = sizeof(TY569421);
NTI569421.kind = 16;
NTI569421.base = (&NTI147);
NTI569421.flags = 3;
TMP1160[4].kind = 1;
TMP1160[4].offset = offsetof(Socketimpl569407, isbufferedU.S1.buffer);
TMP1160[4].typ = (&NTI569421);
TMP1160[4].name = "buffer";
TMP5242[1] = &TMP1160[5];
TMP1160[5].kind = 1;
TMP1160[5].offset = offsetof(Socketimpl569407, isbufferedU.S1.currpos);
TMP1160[5].typ = (&NTI108);
TMP1160[5].name = "currPos";
TMP5242[2] = &TMP1160[6];
TMP1160[6].kind = 1;
TMP1160[6].offset = offsetof(Socketimpl569407, isbufferedU.S1.buflen);
TMP1160[6].typ = (&NTI108);
TMP1160[6].name = "bufLen";
TMP1160[3].len = 3; TMP1160[3].kind = 2; TMP1160[3].sons = &TMP5242[0];
NimDT_569407_isbuffered[1] = &TMP1160[3];
TMP1160[7].len = 0; TMP1160[7].kind = 2;
NimDT_569407_isbuffered[0] = &TMP1160[7];
TMP5241[2] = &TMP1160[8];
TMP1160[8].kind = 1;
TMP1160[8].offset = offsetof(Socketimpl569407, lasterror);
TMP1160[8].typ = (&NTI114);
TMP1160[8].name = "lastError";
TMP5241[3] = &TMP1160[9];
TMP1160[9].kind = 1;
TMP1160[9].offset = offsetof(Socketimpl569407, domain);
TMP1160[9].typ = (&NTI563604);
TMP1160[9].name = "domain";
TMP5241[4] = &TMP1160[10];
TMP1160[10].kind = 1;
TMP1160[10].offset = offsetof(Socketimpl569407, socktype);
TMP1160[10].typ = (&NTI563606);
TMP1160[10].name = "sockType";
TMP5241[5] = &TMP1160[11];
TMP1160[11].kind = 1;
TMP1160[11].offset = offsetof(Socketimpl569407, protocol);
TMP1160[11].typ = (&NTI563608);
TMP1160[11].name = "protocol";
TMP1160[0].len = 6; TMP1160[0].kind = 2; TMP1160[0].sons = &TMP5241[0];
NTI569407.node = &TMP1160[0];
NTI569409.size = sizeof(Socketimpl569407*);
NTI569409.kind = 22;
NTI569409.base = (&NTI569407);
NTI569409.flags = 2;
NTI569409.marker = TMP5252;
NTI569415.size = sizeof(Timeouterror569415);
NTI569415.kind = 17;
NTI569415.base = (&NTI4027);
TMP1160[12].len = 0; TMP1160[12].kind = 2;
NTI569415.node = &TMP1160[12];
NTI573014.size = sizeof(Timeouterror569415*);
NTI573014.kind = 22;
NTI573014.base = (&NTI569415);
NTI573014.marker = TMP5264;
}

