/* Generated by Nim Compiler v0.12.0 */
/*   (c) 2015 Andreas Rumpf */
/* The generated code is subject to the original license. */
#define NIM_INTBITS 64

#include "nimbase.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "winsock2.h"
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct Cell48946 Cell48946;
typedef struct TNimType TNimType;
typedef struct Cellseq48962 Cellseq48962;
typedef struct Gcheap51018 Gcheap51018;
typedef struct Gcstack51016 Gcstack51016;
typedef struct Cellset48958 Cellset48958;
typedef struct Pagedesc48954 Pagedesc48954;
typedef struct Memregion30891 Memregion30891;
typedef struct Smallchunk30843 Smallchunk30843;
typedef struct Llchunk30885 Llchunk30885;
typedef struct Bigchunk30845 Bigchunk30845;
typedef struct Intset30817 Intset30817;
typedef struct Trunk30813 Trunk30813;
typedef struct Avlnode30889 Avlnode30889;
typedef struct Gcstat51014 Gcstat51014;
typedef struct Optparser211211 Optparser211211;
typedef struct TNimObject TNimObject;
typedef struct TNimNode TNimNode;
typedef struct TY577426 TY577426;
typedef struct Socketimpl569407 Socketimpl569407;
typedef struct Basechunk30841 Basechunk30841;
typedef struct Freecell30833 Freecell30833;
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
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 Smallchunk30843* TY30906[512];
typedef Trunk30813* Trunkbuckets30815[256];
struct  Intset30817  {
Trunkbuckets30815 data;
};
struct  Memregion30891  {
NI minlargeobj;
NI maxlargeobj;
TY30906 freesmallchunks;
Llchunk30885* llmem;
NI currmem;
NI maxmem;
NI freemem;
NI lastsize;
Bigchunk30845* freechunkslist;
Intset30817 chunkstarts;
Avlnode30889* root;
Avlnode30889* deleted;
Avlnode30889* last;
Avlnode30889* 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;
Memregion30891 region;
Gcstat51014 stat;
};
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  TNimObject  {
TNimType* m_type;
};
struct  Optparser211211  {
  TNimObject Sup;
NimStringDesc* cmd;
NI pos;
NIM_BOOL inshortstate;
NU8 kind;
NimStringDesc* key;
NimStringDesc* val;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
typedef NimStringDesc* TY255724[1];
typedef N_NIMCALL_PTR(void, TY577256) (void);
struct  TY577426  {
NI HEX3Astate;
Socketimpl569407* stdoutsocket577408;
};
typedef struct {
N_NIMCALL_PTR(void, ClPrc) (NimStringDesc* output, void* ClEnv);
void* ClEnv;
} TY193438;
struct  Gcstack51016  {
Gcstack51016* prev;
Gcstack51016* next;
void* starts;
void* pos;
NI maxstacksize;
};
typedef NI TY30822[8];
struct  Pagedesc48954  {
Pagedesc48954* next;
NI key;
TY30822 bits;
};
struct  Basechunk30841  {
NI prevsize;
NI size;
NIM_BOOL used;
};
struct  Smallchunk30843  {
  Basechunk30841 Sup;
Smallchunk30843* next;
Smallchunk30843* prev;
Freecell30833* freelist;
NI free;
NI acc;
NF data;
};
struct  Llchunk30885  {
NI size;
NI acc;
Llchunk30885* next;
};
struct  Bigchunk30845  {
  Basechunk30841 Sup;
Bigchunk30845* next;
Bigchunk30845* prev;
NI align;
NF data;
};
struct  Trunk30813  {
Trunk30813* next;
NI key;
TY30822 bits;
};
typedef Avlnode30889* TY30896[2];
struct  Avlnode30889  {
TY30896 link;
NI key;
NI upperbound;
NI level;
};
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;
};
struct  Freecell30833  {
Freecell30833* next;
NI zerofield;
};
N_NIMCALL(NimStringDesc*, copyStringRC1)(NimStringDesc* src);
static N_INLINE(void, nimGCunrefNoCycle)(void* p);
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, initoptparser_211364)(NimStringDesc* cmdline, Optparser211211* Result);
N_NOINLINE(void, chckNil)(void* p);
N_NIMCALL(void, nponext)(Optparser211211* p);
static N_INLINE(NIM_BOOL, eqStrings)(NimStringDesc* a, NimStringDesc* b);
N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
N_NIMCALL(NIM_BOOL, processargument_214854)(NU8 pass, Optparser211211* p, NI* argscount);
N_NIMCALL(void, processswitch_214841)(NU8 pass, Optparser211211* p);
N_NIMCALL(NimStringDesc*, nsuNormalize)(NimStringDesc* s);
N_NIMCALL(void, rawmessage_195675)(NU16 msg, NimStringDesc** args, NI argsLen0);
N_NIMCALL(void, nimGCvisit)(void* d, NI op);
N_NIMCALL(void, TMP5253)(void* p, NI op);
N_NIMCALL(void*, newObj)(TNimType* typ, NI size);
N_NIMCALL(NimStringDesc*, getconfigvar_169335)(NimStringDesc* key);
N_NIMCALL(NimStringDesc*, readline_16916)(FILE* f);
N_NIMCALL(void, processcmdline_577209)(NU8 pass, NimStringDesc* cmd);
N_NIMCALL(Socketimpl569407*, newsocket_569833)(NU8 domain, NU8 socktype, NU8 protocol, NIM_BOOL buffered);
N_NIMCALL(NI, nsuParseInt)(NimStringDesc* s);
N_NIMCALL(void, bindaddr_570619)(Socketimpl569407* socket, NU16 port, NimStringDesc* address);
N_NIMCALL(void, listen_570609)(Socketimpl569407* socket, int backlog);
static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src);
N_CLOSURE(void, HEX3Aanonymous_577409)(NimStringDesc* line, void* ClEnv);
N_NIMCALL(void, send_574007)(Socketimpl569407* socket, NimStringDesc* data, NU8 flags);
static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src);
N_NIMCALL(NimStringDesc*, rawNewString)(NI space);
static N_INLINE(void, asgnRef)(void** dest, void* src);
static N_INLINE(void, incref_55022)(Cell48946* c);
static N_INLINE(NIM_BOOL, canbecycleroot_52667)(Cell48946* c);
static N_INLINE(void, rtladdcycleroot_53423)(Cell48946* c);
N_NOINLINE(void, incl_49665)(Cellset48958* s, Cell48946* cell);
static N_INLINE(void, decref_54604)(Cell48946* c);
N_NIMCALL(void, accept_571401)(Socketimpl569407* server, Socketimpl569407** client, NU8 flags);
N_NIMCALL(void, readline_573608)(Socketimpl569407* socket, NimStringDesc** line, NI timeout, NU8 flags);
N_NIMCALL(void, close_571417)(Socketimpl569407* socket);
N_NIMCALL(void, msgquit_193488)(NI8 x);
STRING_LITERAL(TMP1171, "", 0);
STRING_LITERAL(TMP1643, " ", 1);
STRING_LITERAL(TMP1644, "-", 1);
STRING_LITERAL(TMP2001, "run", 3);
STRING_LITERAL(TMP5254, "server.type", 11);
STRING_LITERAL(TMP5255, "stdin", 5);
STRING_LITERAL(TMP5256, "tcp", 3);
STRING_LITERAL(TMP5257, "quit", 4);
STRING_LITERAL(TMP5259, "server.port", 11);
STRING_LITERAL(TMP5260, "server.address", 14);
STRING_LITERAL(TMP5263, "\015\012", 2);
STRING_LITERAL(TMP5271, "Invalid server.type:", 20);
NimStringDesc* curcaascmd_577204;
extern Gcheap51018 gch_51059;
NimStringDesc* lastcaascmd_577205;
extern TNimType NTI211211; /* OptParser */
extern NU32 gglobaloptions_169132;
extern NimStringDesc* arguments_214837;
extern NimStringDesc* command_169240;
TNimType NTI577426; /* object */
extern TNimType NTI348033; /* range -1..0(int) */
extern TNimType NTI569409; /* Socket */
TNimType NTI577448; /* ref object */
extern NI gerrorcounter_193396;
extern TY193438 writelnhook_193441;

static N_INLINE(Cell48946*, usrtocell_52646)(void* usr) {
	Cell48946* result;
	result = 0;
	result = ((Cell48946*) ((NI)((NU64)(((NI) (usr))) - (NU64)(((NI)sizeof(Cell48946))))));
	return result;
}

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

static N_INLINE(void, nimGCunrefNoCycle)(void* p) {
	Cell48946* c;
	c = usrtocell_52646(p);
	{
		(*c).refcount -= ((NI) 8);
		if (!((NU64)((*c).refcount) < (NU64)(((NI) 8)))) goto LA3;
		rtladdzct_54204(c);
	}
	LA3: ;
}

static N_INLINE(NIM_BOOL, eqStrings)(NimStringDesc* a, NimStringDesc* b) {
	NIM_BOOL result;
	NIM_BOOL LOC11;
	int LOC13;
{	result = 0;
	{
		if (!(a == b)) goto LA3;
		result = NIM_TRUE;
		goto BeforeRet;
	}
	LA3: ;
	{
		NIM_BOOL LOC7;
		LOC7 = 0;
		LOC7 = (a == NIM_NIL);
		if (LOC7) goto LA8;
		LOC7 = (b == NIM_NIL);
		LA8: ;
		if (!LOC7) goto LA9;
		result = NIM_FALSE;
		goto BeforeRet;
	}
	LA9: ;
	LOC11 = 0;
	LOC11 = ((*a).Sup.len == (*b).Sup.len);
	if (!(LOC11)) goto LA12;
	LOC13 = 0;
	LOC13 = memcmp(((NCSTRING) ((*a).data)), ((NCSTRING) ((*b).data)), (*a).Sup.len);
	LOC11 = (LOC13 == ((NI32) 0));
	LA12: ;
	result = LOC11;
	goto BeforeRet;
	}BeforeRet: ;
	return result;
}

N_NIMCALL(void, processcmdline_577209)(NU8 pass, NimStringDesc* cmd) {
	Optparser211211 p;
	NI argscount;
	memset((void*)(&p), 0, sizeof(p));
	p.Sup.m_type = (&NTI211211);
	chckNil((void*)(&p));
	memset((void*)(&p), 0, sizeof(p));
	p.Sup.m_type = (&NTI211211);
	initoptparser_211364(cmd, (&p));
	argscount = ((NI) 0);
	{
		while (1) {
			nponext((&p));
			switch (p.kind) {
			case ((NU8) 0):
			{
				goto LA1;
			}
			break;
			case ((NU8) 2):
			case ((NU8) 3):
			{
				{
					if (!eqStrings(p.key, ((NimStringDesc*) &TMP1643))) goto LA7;
					p.key = copyString(((NimStringDesc*) &TMP1644));
					{
						NIM_BOOL LOC11;
						LOC11 = 0;
						LOC11 = processargument_214854(pass, (&p), (&argscount));
						if (!LOC11) goto LA12;
						goto LA1;
					}
					LA12: ;
				}
				goto LA5;
				LA7: ;
				{
					processswitch_214841(pass, (&p));
				}
				LA5: ;
			}
			break;
			case ((NU8) 1):
			{
				{
					NIM_BOOL LOC18;
					LOC18 = 0;
					LOC18 = processargument_214854(pass, (&p), (&argscount));
					if (!LOC18) goto LA19;
					goto LA1;
				}
				LA19: ;
			}
			break;
			}
		}
	} LA1: ;
	{
		if (!(pass == ((NU8) 1))) goto LA23;
		{
			NIM_BOOL LOC27;
			NIM_BOOL LOC28;
			NimStringDesc* LOC31;
			TY255724 LOC34;
			LOC27 = 0;
			LOC28 = 0;
			LOC28 = !(((gglobaloptions_169132 &(1<<((((NU8) 13))&31)))!=0));
			if (!(LOC28)) goto LA29;
			LOC28 = !(((arguments_214837) && (arguments_214837)->Sup.len == 0));
			LA29: ;
			LOC27 = LOC28;
			if (!(LOC27)) goto LA30;
			LOC31 = 0;
			LOC31 = nsuNormalize(command_169240);
			LOC27 = !(eqStrings(LOC31, ((NimStringDesc*) &TMP2001)));
			LA30: ;
			if (!LOC27) goto LA32;
			memset((void*)LOC34, 0, sizeof(LOC34));
			rawmessage_195675(((NU16) 59), LOC34, 0);
		}
		LA32: ;
	}
	LA23: ;
}
N_NIMCALL(void, TMP5253)(void* p, NI op) {
	TY577426* a;
	a = (TY577426*)p;
	nimGCvisit((void*)(*a).stdoutsocket577408, op);
}

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 (!((NU64)((*c).refcount) < (NU64)(((NI) 8)))) goto LA11;
			rtladdzct_54204(c);
		}
		LA11: ;
	}
	LA7: ;
	(*dest) = src;
}

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;
}

N_CLOSURE(void, HEX3Aanonymous_577409)(NimStringDesc* line, void* ClEnv) {
	TY577426* HEX3Aenvp_577438;
	NimStringDesc* LOC1;
	HEX3Aenvp_577438 = (TY577426*) ClEnv;
	LOC1 = 0;
	LOC1 = rawNewString(line->Sup.len + 2);
appendString(LOC1, line);
appendString(LOC1, ((NimStringDesc*) &TMP5263));
	send_574007((*HEX3Aenvp_577438).stdoutsocket577408, LOC1, 2);
}

static N_INLINE(NIM_BOOL, canbecycleroot_52667)(Cell48946* c) {
	NIM_BOOL result;
	result = 0;
	result = !((((*(*c).typ).flags &(1<<((((NU8) 1))&7)))!=0));
	return result;
}

static N_INLINE(void, rtladdcycleroot_53423)(Cell48946* c) {
	{
		if (!!((((NI) ((NI)((*c).refcount & ((NI) 3)))) == ((NI) 3)))) goto LA3;
		(*c).refcount = (NI)((NI)((*c).refcount & ((NI) -4)) | ((NI) 3));
		incl_49665((&gch_51059.cycleroots), c);
	}
	LA3: ;
}

static N_INLINE(void, incref_55022)(Cell48946* c) {
	(*c).refcount = (NI)((NU64)((*c).refcount) + (NU64)(((NI) 8)));
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = canbecycleroot_52667(c);
		if (!LOC3) goto LA4;
		rtladdcycleroot_53423(c);
	}
	LA4: ;
}

static N_INLINE(void, decref_54604)(Cell48946* c) {
	{
		(*c).refcount -= ((NI) 8);
		if (!((NU64)((*c).refcount) < (NU64)(((NI) 8)))) goto LA3;
		rtladdzct_54204(c);
	}
	goto LA1;
	LA3: ;
	{
		NIM_BOOL LOC6;
		LOC6 = 0;
		LOC6 = canbecycleroot_52667(c);
		if (!LOC6) goto LA7;
		rtladdcycleroot_53423(c);
	}
	goto LA1;
	LA7: ;
	LA1: ;
}

static N_INLINE(void, asgnRef)(void** dest, void* src) {
	{
		Cell48946* LOC5;
		if (!!((src == NIM_NIL))) goto LA3;
		LOC5 = 0;
		LOC5 = usrtocell_52646(src);
		incref_55022(LOC5);
	}
	LA3: ;
	{
		Cell48946* LOC10;
		if (!!(((*dest) == NIM_NIL))) goto LA8;
		LOC10 = 0;
		LOC10 = usrtocell_52646((*dest));
		decref_54604(LOC10);
	}
	LA8: ;
	(*dest) = src;
}

N_NIMCALL(void, serve_577254)(TY577256 action) {
	TY577426* HEX3Aenv_577447;
	NimStringDesc* typ;
	HEX3Aenv_577447 = 0;
	HEX3Aenv_577447 = (TY577426*) newObj((&NTI577448), sizeof(TY577426));
	typ = getconfigvar_169335(((NimStringDesc*) &TMP5254));
	if (eqStrings(typ, ((NimStringDesc*) &TMP5255))) goto LA1;
	if (eqStrings(typ, ((NimStringDesc*) &TMP5256))) goto LA2;
	if (eqStrings(typ, ((NimStringDesc*) &TMP1171))) goto LA2;
	goto LA3;
	LA1: ;
	{
		{
			while (1) {
				NimStringDesc* line;
				NimStringDesc* LOC12;
				line = readline_16916(stdin);
				{
					if (!eqStrings(line, ((NimStringDesc*) &TMP5257))) goto LA10;
					exit(((NI) 0));
				}
				LA10: ;
				LOC12 = 0;
				LOC12 = curcaascmd_577204; curcaascmd_577204 = copyStringRC1(line);
				if (LOC12) nimGCunrefNoCycle(LOC12);
				processcmdline_577209(((NU8) 1), line);
				action();
				gerrorcounter_193396 = ((NI) 0);
				printf("%s\015\012", ((NimStringDesc*) &TMP1171)? (((NimStringDesc*) &TMP1171))->data:"nil");
				fflush(stdout);
			}
		}
	}
	goto LA4;
	LA2: ;
	{
		Socketimpl569407* server;
		NimStringDesc* p;
		NU16 port;
		NimStringDesc* LOC20;
		NimStringDesc* inp;
		TY193438 LOC21;
		server = newsocket_569833(((NU8) 2), ((NU8) 1), ((NU8) 6), NIM_TRUE);
		p = getconfigvar_169335(((NimStringDesc*) &TMP5259));
		{
			NI LOC18;
			if (!(((NI) 0) < (p ? p->Sup.len : 0))) goto LA16;
			LOC18 = 0;
			LOC18 = nsuParseInt(p);
			port = ((NU16) (LOC18));
		}
		goto LA14;
		LA16: ;
		{
			port = ((NU16) 6000);
		}
		LA14: ;
		LOC20 = 0;
		LOC20 = getconfigvar_169335(((NimStringDesc*) &TMP5260));
		bindaddr_570619(server, port, LOC20);
		inp = copyString(((NimStringDesc*) &TMP1171));
		listen_570609(server, SOMAXCONN);
		asgnRefNoCycle((void**) (&(*HEX3Aenv_577447).stdoutsocket577408), newsocket_569833(((NU8) 2), ((NU8) 1), ((NU8) 6), NIM_TRUE));
		memset((void*)(&LOC21), 0, sizeof(LOC21));
		LOC21.ClPrc = HEX3Aanonymous_577409; LOC21.ClEnv = HEX3Aenv_577447;
		asgnRef((void**) (&writelnhook_193441.ClEnv), LOC21.ClEnv);
		writelnhook_193441.ClPrc = LOC21.ClPrc;
		{
			while (1) {
				NimStringDesc* LOC24;
				accept_571401(server, &(*HEX3Aenv_577447).stdoutsocket577408, 2);
				readline_573608((*HEX3Aenv_577447).stdoutsocket577408, (&inp), ((NI) -1), 2);
				LOC24 = 0;
				LOC24 = curcaascmd_577204; curcaascmd_577204 = copyStringRC1(inp);
				if (LOC24) nimGCunrefNoCycle(LOC24);
				processcmdline_577209(((NU8) 1), inp);
				action();
				gerrorcounter_193396 = ((NI) 0);
				send_574007((*HEX3Aenv_577447).stdoutsocket577408, ((NimStringDesc*) &TMP5263), 2);
				close_571417((*HEX3Aenv_577447).stdoutsocket577408);
			}
		}
	}
	goto LA4;
	LA3: ;
	{
		printf("%s%s\015\012", ((NimStringDesc*) &TMP5271)? (((NimStringDesc*) &TMP5271))->data:"nil", typ? (typ)->data:"nil");
		msgquit_193488(((NI8) 1));
	}
	LA4: ;
}
NIM_EXTERNC N_NOINLINE(void, compiler_serviceInit000)(void) {
	NimStringDesc* LOC1;
	NimStringDesc* LOC2;
	LOC1 = 0;
	LOC1 = curcaascmd_577204; curcaascmd_577204 = copyStringRC1(((NimStringDesc*) &TMP1171));
	if (LOC1) nimGCunrefNoCycle(LOC1);
	LOC2 = 0;
	LOC2 = lastcaascmd_577205; lastcaascmd_577205 = copyStringRC1(((NimStringDesc*) &TMP1171));
	if (LOC2) nimGCunrefNoCycle(LOC2);
}

NIM_EXTERNC N_NOINLINE(void, compiler_serviceDatInit000)(void) {
static TNimNode* TMP5240[2];
static TNimNode TMP1158[3];
NTI577426.size = sizeof(TY577426);
NTI577426.kind = 18;
NTI577426.base = 0;
NTI577426.flags = 2;
TMP5240[0] = &TMP1158[1];
TMP1158[1].kind = 1;
TMP1158[1].offset = offsetof(TY577426, HEX3Astate);
TMP1158[1].typ = (&NTI348033);
TMP1158[1].name = ":state";
TMP5240[1] = &TMP1158[2];
TMP1158[2].kind = 1;
TMP1158[2].offset = offsetof(TY577426, stdoutsocket577408);
TMP1158[2].typ = (&NTI569409);
TMP1158[2].name = "stdoutSocket577408";
TMP1158[0].len = 2; TMP1158[0].kind = 2; TMP1158[0].sons = &TMP5240[0];
NTI577426.node = &TMP1158[0];
NTI577448.size = sizeof(TY577426*);
NTI577448.kind = 22;
NTI577448.base = (&NTI577426);
NTI577448.flags = 2;
NTI577448.marker = TMP5253;
}

