/* 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 <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <arpa/inet.h>
#include <setjmp.h>
typedef struct Socketimpl566407 Socketimpl566407;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct Timeouterror566415 Timeouterror566415;
typedef struct Exception Exception;
typedef struct TNimObject TNimObject;
typedef struct Cell48947 Cell48947;
typedef struct Cellseq48963 Cellseq48963;
typedef struct Gcheap51018 Gcheap51018;
typedef struct Gcstack51016 Gcstack51016;
typedef struct Cellset48959 Cellset48959;
typedef struct Pagedesc48955 Pagedesc48955;
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 TY569225 TY569225;
typedef struct TSafePoint TSafePoint;
typedef struct Basechunk30840 Basechunk30840;
typedef struct Freecell30832 Freecell30832;
typedef NIM_CHAR TY566421[4001];
struct  Socketimpl566407  {
int fd;
NIM_BOOL isbuffered;
union{
struct {TY566421 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];
};
struct  TNimObject  {
TNimType* m_type;
};
struct  Exception  {
  TNimObject Sup;
Exception* parent;
NCSTRING name;
NimStringDesc* message;
NimStringDesc* trace;
};
struct  Timeouterror566415  {
  Exception Sup;
};
struct  Cell48947  {
NI refcount;
TNimType* typ;
};
struct  Cellseq48963  {
NI len;
NI cap;
Cell48947** d;
};
struct  Cellset48959  {
NI counter;
NI max;
Pagedesc48955* head;
Pagedesc48955** 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;
Cellseq48963 zct;
Cellseq48963 decstack;
Cellset48959 cycleroots;
Cellseq48963 tempstack;
NI recgclock;
Memregion30890 region;
Gcstat51014 stat;
};
typedef int TY569214[1];
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 TY113386[256];
struct  Gcstack51016  {
Gcstack51016* prev;
Gcstack51016* next;
void* starts;
void* pos;
NI maxstacksize;
};
typedef NI TY30821[16];
struct  Pagedesc48955  {
Pagedesc48955* 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 TY569225 {
  TGenericSeq Sup;
  int data[SEQ_DECL_SIZE];
};
N_NIMCALL(void, TMP5254)(void* p, NI op);
N_NIMCALL(int, newnativesocket_561004)(NU8 domain, NU8 socktype, NU8 protocol);
N_NOINLINE(void, raiseoserror_126009)(NI32 errorcode, NimStringDesc* additionalinfo);
N_NIMCALL(NI32, oslasterror_126034)(void);
N_NIMCALL(Socketimpl566407*, newsocket_566708)(int fd, NU8 domain, NU8 socktype, NU8 protocol, NIM_BOOL buffered);
N_NIMCALL(void*, newObj)(TNimType* typ, NI size);
N_NIMCALL(int, toint_560829)(NU8 domain);
N_NIMCALL(NI16, ntohs_562007)(NI16 x);
N_NIMCALL(NI32, ntohl_561818)(NI32 x);
N_NIMCALL(int, bindaddr_561214)(int socket, struct sockaddr* name, socklen_t namelen);
N_NIMCALL(struct addrinfo*, getaddrinfo_561408)(NimStringDesc* address, NU16 port, NU8 domain, NU8 socktype, NU8 protocol);
N_NIMCALL(void, dealloc_561808)(struct addrinfo* ai);
N_NIMCALL(int, listen_561227)(int socket, int backlog);
N_NIMCALL(NI, send_570714)(Socketimpl566407* socket, void* data, NI size);
N_NIMCALL(NIM_BOOL, isdisconnectionerror_566497)(NU8 flags, NI32 lasterror);
N_NIMCALL(void, socketerror_566488)(Socketimpl566407* socket, NI err, NIM_BOOL async, NI32 lasterror);
N_NIMCALL(NI32, getsocketerror_567001)(Socketimpl566407* socket);
N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
N_NIMCALL(void, acceptaddr_567814)(Socketimpl566407* server, Socketimpl566407** 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_569819)(Socketimpl566407* socket, NF* waited, NI timeout, NI size, NimStringDesc* funcname);
N_NIMCALL(void, nimGCvisit)(void* d, NI op);
N_NIMCALL(void, TMP5266)(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(Cell48947*, usrtocell_52646)(void* usr);
static N_INLINE(void, rtladdzct_54204)(Cell48947* c);
N_NOINLINE(void, addzct_52617)(Cellseq48963* s, Cell48947* c);
N_NIMCALL(void, raiseException)(Exception* e, NCSTRING ename);
N_NIMCALL(NF, ntepochTime)(void);
N_NIMCALL(NI, select_569207)(Socketimpl566407* readfd, NI timeout);
N_NIMCALL(NIM_BOOL, hasdatabuffered_569001)(Socketimpl566407* s);
N_NIMCALL(void*, newSeq)(TNimType* typ, NI len);
N_NIMCALL(NI, select_565113)(TY569225** readfds, NI timeout);
static N_INLINE(void, HEX2BHEX3D_108699)(NF* x, NF y);
N_NIMCALL(NI, recv_569425)(Socketimpl566407* socket, void* data, NI size);
N_NIMCALL(NI, readintobuf_569235)(Socketimpl566407* socket, NI32 flags);
N_NIMCALL(NI, peekchar_570444)(Socketimpl566407* 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_561028)(int socket);
N_NIMCALL(void, reraiseException)(void);
TNimNode* NimDT_566407_isbuffered[3];
STRING_LITERAL(TMP5260, "", 0);
STRING_LITERAL(TMP5263, "No valid socket error code available", 36);
STRING_LITERAL(TMP5264, "Could not send all data.", 24);
STRING_LITERAL(TMP5267, "Call to \'", 9);
STRING_LITERAL(TMP5268, "\' timed out.", 12);
STRING_LITERAL(TMP5270, "readLine", 8);
STRING_LITERAL(TMP5271, "\015\012", 2);
TNimType NTI566407; /* SocketImpl */
extern TNimType NTI8211; /* cint */
extern TNimType NTI138; /* bool */
extern TNimType NTI147; /* char */
TNimType NTI566421; /* array[0..4000, char] */
extern TNimType NTI108; /* int */
extern TNimType NTI114; /* int32 */
extern TNimType NTI560604; /* Domain */
extern TNimType NTI560606; /* SockType */
extern TNimType NTI560608; /* Protocol */
TNimType NTI566409; /* Socket */
extern int osinvalidsocket_560650;
extern TNimType NTI4027; /* Exception */
TNimType NTI566415; /* TimeoutError */
TNimType NTI570014; /* ref TimeoutError */
extern Gcheap51018 gch_51059;
extern TNimType NTI569225; /* seq[SocketHandle] */
extern TSafePoint* exchandler_20843;
N_NIMCALL(void, TMP5254)(void* p, NI op) {
	Socketimpl566407* a;
	NI LOC1;
	a = (Socketimpl566407*)p;
	switch ((*a).isbuffered) {
	case NIM_TRUE:
	LOC1 = 0;
	for (LOC1 = 0; LOC1 < 4001; LOC1++) {
	}
	break;
	case NIM_FALSE:
	break;
	} 
}

N_NIMCALL(Socketimpl566407*, newsocket_566708)(int fd, NU8 domain, NU8 socktype, NU8 protocol, NIM_BOOL buffered) {
	Socketimpl566407* result;
	result = 0;
	result = (Socketimpl566407*) newObj((&NTI566409), sizeof(Socketimpl566407));
	(*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(Socketimpl566407*, newsocket_566833)(NU8 domain, NU8 socktype, NU8 protocol, NIM_BOOL buffered) {
	Socketimpl566407* result;
	int fd;
	result = 0;
	fd = newnativesocket_561004(domain, socktype, protocol);
	{
		NI32 LOC5;
		if (!(fd == osinvalidsocket_560650)) goto LA3;
		LOC5 = 0;
		LOC5 = oslasterror_126034();
		raiseoserror_126009(LOC5, ((NimStringDesc*) &TMP5260));
	}
	LA3: ;
	result = newsocket_566708(fd, domain, socktype, protocol, buffered);
	return result;
}

N_NIMCALL(void, bindaddr_567620)(Socketimpl566407* 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 = toint_560829(((NU8) 2));
		name.sin_port = ntohs_562007(((NI16) (port)));
		name.sin_addr.s_addr = ntohl_561818(INADDR_ANY);
		{
			int LOC7;
			NI32 LOC10;
			LOC7 = 0;
			LOC7 = bindaddr_561214((*socket).fd, ((struct sockaddr*) ((&name))), ((socklen_t) (((NI)sizeof(struct sockaddr_in)))));
			if (!(LOC7 < ((NI32) 0))) goto LA8;
			LOC10 = 0;
			LOC10 = oslasterror_126034();
			raiseoserror_126009(LOC10, ((NimStringDesc*) &TMP5260));
		}
		LA8: ;
	}
	goto LA1;
	LA3: ;
	{
		struct addrinfo* ailist;
		ailist = getaddrinfo_561408(address, port, (*socket).domain, ((NU8) 1), ((NU8) 6));
		{
			int LOC14;
			NI32 LOC17;
			LOC14 = 0;
			LOC14 = bindaddr_561214((*socket).fd, (*ailist).ai_addr, (*ailist).ai_addrlen);
			if (!(LOC14 < ((NI32) 0))) goto LA15;
			dealloc_561808(ailist);
			LOC17 = 0;
			LOC17 = oslasterror_126034();
			raiseoserror_126009(LOC17, ((NimStringDesc*) &TMP5260));
		}
		LA15: ;
		dealloc_561808(ailist);
	}
	LA1: ;
}

N_NIMCALL(void, listen_567610)(Socketimpl566407* socket, int backlog) {
	{
		int LOC3;
		NI32 LOC6;
		LOC3 = 0;
		LOC3 = listen_561227((*socket).fd, backlog);
		if (!(LOC3 < ((NI32) 0))) goto LA4;
		LOC6 = 0;
		LOC6 = oslasterror_126034();
		raiseoserror_126009(LOC6, ((NimStringDesc*) &TMP5260));
	}
	LA4: ;
}

N_NIMCALL(NI, send_570714)(Socketimpl566407* socket, void* data, NI size) {
	NI result;
	result = 0;
	result = send((*socket).fd, data, size, MSG_NOSIGNAL);
	return result;
}

N_NIMCALL(NIM_BOOL, isdisconnectionerror_566497)(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) (ECONNRESET)) || lasterror == ((NI) (EPIPE)) || lasterror == ((NI) (ENETRESET)));
	LA2: ;
	result = LOC1;
	return result;
}

N_NIMCALL(NI32, getsocketerror_567001)(Socketimpl566407* socket) {
	NI32 result;
	result = 0;
	result = oslasterror_126034();
	{
		if (!(result == ((NI32) 0))) goto LA3;
		result = (*socket).lasterror;
	}
	LA3: ;
	{
		if (!(result == ((NI32) 0))) goto LA7;
		raiseoserror_126009(result, ((NimStringDesc*) &TMP5263));
	}
	LA7: ;
	return result;
}

N_NIMCALL(void, socketerror_566488)(Socketimpl566407* 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_567001(socket);
		}
		goto LA7;
		LA9: ;
		{
			laste = lasterror;
		}
		LA7: ;
		{
			if (!async) goto LA14;
			{
				NIM_BOOL LOC18;
				LOC18 = 0;
				LOC18 = (laste == EAGAIN);
				if (LOC18) goto LA19;
				LOC18 = (laste == EWOULDBLOCK);
				LA19: ;
				if (!LOC18) goto LA20;
				goto BeforeRet;
			}
			goto LA16;
			LA20: ;
			{
				raiseoserror_126009(laste, ((NimStringDesc*) &TMP5260));
			}
			LA16: ;
		}
		goto LA12;
		LA14: ;
		{
			raiseoserror_126009(laste, ((NimStringDesc*) &TMP5260));
		}
		LA12: ;
	}
	LA5: ;
	}BeforeRet: ;
}

N_NIMCALL(void, send_571208)(Socketimpl566407* socket, NimStringDesc* data, NU8 flags) {
	NI sent;
{	sent = send_570714(socket, ((void*) (data->data)), (data ? data->Sup.len : 0));
	{
		NI32 lasterror;
		if (!(sent < ((NI) 0))) goto LA3;
		lasterror = oslasterror_126034();
		{
			NIM_BOOL LOC7;
			LOC7 = 0;
			LOC7 = isdisconnectionerror_566497(flags, lasterror);
			if (!LOC7) goto LA8;
			goto BeforeRet;
		}
		LA8: ;
		socketerror_566488(socket, ((NI) -1), NIM_FALSE, lasterror);
	}
	LA3: ;
	{
		NI32 LOC14;
		if (!!((sent == (data ? data->Sup.len : 0)))) goto LA12;
		LOC14 = 0;
		LOC14 = oslasterror_126034();
		raiseoserror_126009(LOC14, ((NimStringDesc*) &TMP5264));
	}
	LA12: ;
	}BeforeRet: ;
}

N_NIMCALL(void, acceptaddr_567814)(Socketimpl566407* server, Socketimpl566407** client, NimStringDesc** address, NU8 flags) {
	struct sockaddr_in sockaddress;
	socklen_t addrlen;
	int sock;
	memset((void*)(&sockaddress), 0, sizeof(sockaddress));
	addrlen = ((socklen_t) (((NI)sizeof(struct sockaddr_in))));
	sock = accept((*server).fd, ((struct sockaddr*) ((&sockaddress))), (&addrlen));
	{
		NI32 err;
		if (!(sock == osinvalidsocket_560650)) goto LA3;
		err = oslasterror_126034();
		{
			NIM_BOOL LOC7;
			LOC7 = 0;
			LOC7 = isdisconnectionerror_566497(flags, err);
			if (!LOC7) goto LA8;
			acceptaddr_567814(server, client, address, flags);
		}
		LA8: ;
		raiseoserror_126009(err, ((NimStringDesc*) &TMP5260));
	}
	goto LA1;
	LA3: ;
	{
		NCSTRING LOC11;
		(*(*client)).fd = sock;
		(*(*client)).isbuffered = (*server).isbuffered;
		LOC11 = 0;
		LOC11 = inet_ntoa(sockaddress.sin_addr);
		unsureAsgnRef((void**) (&(*address)), cstrToNimstr(LOC11));
	}
	LA1: ;
}

N_NIMCALL(void, accept_568401)(Socketimpl566407* server, Socketimpl566407** client, NU8 flags) {
	NimStringDesc* addrdummy;
	addrdummy = copyString(((NimStringDesc*) &TMP5260));
	acceptaddr_567814(server, client, (&addrdummy), flags);
}
N_NIMCALL(void, TMP5266)(void* p, NI op) {
	Timeouterror566415* a;
	a = (Timeouterror566415*)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(Cell48947*, usrtocell_52646)(void* usr) {
	Cell48947* result;
	result = 0;
	result = ((Cell48947*) ((NI)((NU32)(((NI) (usr))) - (NU32)(((NI)sizeof(Cell48947))))));
	return result;
}

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

static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src) {
	{
		Cell48947* c;
		if (!!((src == NIM_NIL))) goto LA3;
		c = usrtocell_52646(src);
		(*c).refcount += ((NI) 8);
	}
	LA3: ;
	{
		Cell48947* 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_569001)(Socketimpl566407* 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_569207)(Socketimpl566407* readfd, NI timeout) {
	NI result;
	TY569225* fds;
	TY569214 LOC6;
{	result = 0;
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = hasdatabuffered_569001(readfd);
		if (!LOC3) goto LA4;
		result = ((NI) 1);
		goto BeforeRet;
	}
	LA4: ;
	fds = (TY569225*) newSeq((&NTI569225), 1);
	LOC6[0] = (*readfd).fd;
	fds->data[0] = LOC6[0];
	result = select_565113((&fds), timeout);
	}BeforeRet: ;
	return result;
}

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

N_NIMCALL(NI, waitfor_569819)(Socketimpl566407* 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;
		{
			Timeouterror566415* e_570013;
			NimStringDesc* LOC22;
			if (!((NI)(timeout - ((NI) (((NF)((*waited)) * (NF)(1.0000000000000000e+03))))) < ((NI) 1))) goto LA20;
			e_570013 = 0;
			e_570013 = (Timeouterror566415*) newObj((&NTI570014), sizeof(Timeouterror566415));
			(*e_570013).Sup.Sup.m_type = (&NTI566415);
			LOC22 = 0;
			LOC22 = rawNewString(funcname->Sup.len + 21);
appendString(LOC22, ((NimStringDesc*) &TMP5267));
appendString(LOC22, funcname);
appendString(LOC22, ((NimStringDesc*) &TMP5268));
			asgnRefNoCycle((void**) (&(*e_570013).Sup.message), LOC22);
			raiseException((Exception*)e_570013, "TimeoutError");
		}
		LA20: ;
		starttime = ntepochTime();
		selret = select_569207(socket, (NI)(timeout - ((NI) (((NF)((*waited)) * (NF)(1.0000000000000000e+03))))));
		{
			NI32 LOC27;
			if (!(selret < ((NI) 0))) goto LA25;
			LOC27 = 0;
			LOC27 = oslasterror_126034();
			raiseoserror_126009(LOC27, ((NimStringDesc*) &TMP5260));
		}
		LA25: ;
		{
			Timeouterror566415* e_570206;
			NimStringDesc* LOC32;
			if (!!((selret == ((NI) 1)))) goto LA30;
			e_570206 = 0;
			e_570206 = (Timeouterror566415*) newObj((&NTI570014), sizeof(Timeouterror566415));
			(*e_570206).Sup.Sup.m_type = (&NTI566415);
			LOC32 = 0;
			LOC32 = rawNewString(funcname->Sup.len + 21);
appendString(LOC32, ((NimStringDesc*) &TMP5267));
appendString(LOC32, funcname);
appendString(LOC32, ((NimStringDesc*) &TMP5268));
			asgnRefNoCycle((void**) (&(*e_570206).Sup.message), LOC32);
			raiseException((Exception*)e_570206, "TimeoutError");
		}
		LA30: ;
		LOC33 = 0;
		LOC33 = ntepochTime();
		HEX2BHEX3D_108699(waited, ((NF)(LOC33) - (NF)(starttime)));
	}
	LA9: ;
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NI, readintobuf_569235)(Socketimpl566407* socket, NI32 flags) {
	NI result;
{	result = 0;
	result = ((NI) 0);
	result = recv((*socket).fd, ((void*) ((*socket).isbufferedU.S1.buffer)), ((NI) 4000), flags);
	{
		if (!(result < ((NI) 0))) goto LA3;
		(*socket).lasterror = oslasterror_126034();
	}
	LA3: ;
	{
		if (!(result <= ((NI) 0))) goto LA7;
		(*socket).isbufferedU.S1.buflen = ((NI) 0);
		(*socket).isbufferedU.S1.currpos = ((NI) 0);
		goto BeforeRet;
	}
	LA7: ;
	(*socket).isbufferedU.S1.buflen = result;
	(*socket).isbufferedU.S1.currpos = ((NI) 0);
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NI, recv_569425)(Socketimpl566407* 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_569235(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_569235(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: ;
	{
		result = recv((*socket).fd, data, ((NI) (((int) (size)))), ((NI32) 0));
		{
			if (!(result < ((NI) 0))) goto LA40;
			(*socket).lasterror = oslasterror_126034();
		}
		LA40: ;
	}
	LA5: ;
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NI, peekchar_570444)(Socketimpl566407* 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_569235(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: ;
	{
		result = recv((*socket).fd, ((void*) (c)), ((NI) 1), MSG_PEEK);
	}
	LA1: ;
	return result;
}

N_NIMCALL(void, readline_570608)(Socketimpl566407* 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_569819(socket, (&waited), timeout, ((NI) 1), ((NimStringDesc*) &TMP5270));
			n = recv_569425(socket, ((void*) ((&c))), ((NI) 1));
			{
				NI32 lasterror;
				if (!(n < ((NI) 0))) goto LA6;
				lasterror = getsocketerror_567001(socket);
				{
					NIM_BOOL LOC10;
					LOC10 = 0;
					LOC10 = isdisconnectionerror_566497(flags, lasterror);
					if (!LOC10) goto LA11;
					(*line) = setLengthStr((*line), ((NI) 0));
					goto BeforeRet;
				}
				LA11: ;
				socketerror_566488(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_569819(socket, (&waited), timeout, ((NI) 1), ((NimStringDesc*) &TMP5270));
				n = peekchar_570444(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_569425(socket, ((void*) ((&c))), ((NI) 1));
				}
				goto LA21;
				LA25: ;
				{
					NI32 lasterror;
					if (!(n <= ((NI) 0))) goto LA29;
					lasterror = getsocketerror_567001(socket);
					{
						NIM_BOOL LOC33;
						LOC33 = 0;
						LOC33 = isdisconnectionerror_566497(flags, lasterror);
						if (!LOC33) goto LA34;
						(*line) = setLengthStr((*line), ((NI) 0));
						goto BeforeRet;
					}
					LA34: ;
					socketerror_566488(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*) &TMP5271));
				}
				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*) &TMP5271));
				}
				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_568417)(Socketimpl566407* socket) {
	TSafePoint TMP5272;
	pushSafePoint(&TMP5272);
	TMP5272.status = _setjmp(TMP5272.context);
	if (TMP5272.status == 0) {
		popSafePoint();
	}
	else {
		popSafePoint();
	}
	{
		close_561028((*socket).fd);
	}
	if (TMP5272.status != 0) reraiseException();
}
NIM_EXTERNC N_NOINLINE(void, stdlib_netInit000)(void) {
}

NIM_EXTERNC N_NOINLINE(void, stdlib_netDatInit000)(void) {
static TNimNode* TMP5243[6];
static TNimNode* TMP5244[3];
static TNimNode TMP1152[13];
NTI566407.size = sizeof(Socketimpl566407);
NTI566407.kind = 18;
NTI566407.base = 0;
NTI566407.flags = 3;
TMP5243[0] = &TMP1152[1];
TMP1152[1].kind = 1;
TMP1152[1].offset = offsetof(Socketimpl566407, fd);
TMP1152[1].typ = (&NTI8211);
TMP1152[1].name = "fd";
TMP5243[1] = &TMP1152[2];
TMP1152[2].kind = 3;
TMP1152[2].offset = offsetof(Socketimpl566407, isbuffered);
TMP1152[2].typ = (&NTI138);
TMP1152[2].name = "isBuffered";
TMP1152[2].sons = &NimDT_566407_isbuffered[0];
TMP1152[2].len = 2;
TMP5244[0] = &TMP1152[4];
NTI566421.size = sizeof(TY566421);
NTI566421.kind = 16;
NTI566421.base = (&NTI147);
NTI566421.flags = 3;
TMP1152[4].kind = 1;
TMP1152[4].offset = offsetof(Socketimpl566407, isbufferedU.S1.buffer);
TMP1152[4].typ = (&NTI566421);
TMP1152[4].name = "buffer";
TMP5244[1] = &TMP1152[5];
TMP1152[5].kind = 1;
TMP1152[5].offset = offsetof(Socketimpl566407, isbufferedU.S1.currpos);
TMP1152[5].typ = (&NTI108);
TMP1152[5].name = "currPos";
TMP5244[2] = &TMP1152[6];
TMP1152[6].kind = 1;
TMP1152[6].offset = offsetof(Socketimpl566407, isbufferedU.S1.buflen);
TMP1152[6].typ = (&NTI108);
TMP1152[6].name = "bufLen";
TMP1152[3].len = 3; TMP1152[3].kind = 2; TMP1152[3].sons = &TMP5244[0];
NimDT_566407_isbuffered[1] = &TMP1152[3];
TMP1152[7].len = 0; TMP1152[7].kind = 2;
NimDT_566407_isbuffered[0] = &TMP1152[7];
TMP5243[2] = &TMP1152[8];
TMP1152[8].kind = 1;
TMP1152[8].offset = offsetof(Socketimpl566407, lasterror);
TMP1152[8].typ = (&NTI114);
TMP1152[8].name = "lastError";
TMP5243[3] = &TMP1152[9];
TMP1152[9].kind = 1;
TMP1152[9].offset = offsetof(Socketimpl566407, domain);
TMP1152[9].typ = (&NTI560604);
TMP1152[9].name = "domain";
TMP5243[4] = &TMP1152[10];
TMP1152[10].kind = 1;
TMP1152[10].offset = offsetof(Socketimpl566407, socktype);
TMP1152[10].typ = (&NTI560606);
TMP1152[10].name = "sockType";
TMP5243[5] = &TMP1152[11];
TMP1152[11].kind = 1;
TMP1152[11].offset = offsetof(Socketimpl566407, protocol);
TMP1152[11].typ = (&NTI560608);
TMP1152[11].name = "protocol";
TMP1152[0].len = 6; TMP1152[0].kind = 2; TMP1152[0].sons = &TMP5243[0];
NTI566407.node = &TMP1152[0];
NTI566409.size = sizeof(Socketimpl566407*);
NTI566409.kind = 22;
NTI566409.base = (&NTI566407);
NTI566409.flags = 2;
NTI566409.marker = TMP5254;
NTI566415.size = sizeof(Timeouterror566415);
NTI566415.kind = 17;
NTI566415.base = (&NTI4027);
TMP1152[12].len = 0; TMP1152[12].kind = 2;
NTI566415.node = &TMP1152[12];
NTI570014.size = sizeof(Timeouterror566415*);
NTI570014.kind = 22;
NTI570014.base = (&NTI566415);
NTI570014.marker = TMP5266;
}

