/* 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 <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.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 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 Optparser209211 Optparser209211;
typedef struct TNimObject TNimObject;
typedef struct TNimNode TNimNode;
typedef struct TY575426 TY575426;
typedef struct Socketimpl567407 Socketimpl567407;
typedef struct Basechunk30840 Basechunk30840;
typedef struct Freecell30832 Freecell30832;
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 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 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  Optparser209211  {
  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* TY253724[1];
typedef N_NIMCALL_PTR(void, TY575256) (void);
struct  TY575426  {
NI HEX3Astate;
Socketimpl567407* stdoutsocket575408;
};
typedef struct {
N_NIMCALL_PTR(void, ClPrc) (NimStringDesc* output, void* ClEnv);
void* ClEnv;
} TY191441;
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;
};
typedef NIM_CHAR TY567421[4001];
struct  Socketimpl567407  {
int fd;
NIM_BOOL isbuffered;
union{
struct {TY567421 buffer;
NI currpos;
NI buflen;
} S1;
} isbufferedU;
NI32 lasterror;
NU8 domain;
NU8 socktype;
NU8 protocol;
};
struct  Freecell30832  {
Freecell30832* 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_209364)(NimStringDesc* cmdline, Optparser209211* Result);
N_NOINLINE(void, chckNil)(void* p);
N_NIMCALL(void, nponext)(Optparser209211* p);
static N_INLINE(NIM_BOOL, eqStrings)(NimStringDesc* a, NimStringDesc* b);
N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
N_NIMCALL(NIM_BOOL, processargument_212854)(NU8 pass, Optparser209211* p, NI* argscount);
N_NIMCALL(void, processswitch_212841)(NU8 pass, Optparser209211* p);
N_NIMCALL(NimStringDesc*, nsuNormalize)(NimStringDesc* s);
N_NIMCALL(void, rawmessage_193675)(NU16 msg, NimStringDesc** args, NI argsLen0);
N_NIMCALL(void, nimGCvisit)(void* d, NI op);
N_NIMCALL(void, TMP5256)(void* p, NI op);
N_NIMCALL(void*, newObj)(TNimType* typ, NI size);
N_NIMCALL(NimStringDesc*, getconfigvar_167335)(NimStringDesc* key);
N_NIMCALL(NimStringDesc*, readline_16916)(FILE* f);
N_NIMCALL(void, processcmdline_575209)(NU8 pass, NimStringDesc* cmd);
N_NIMCALL(Socketimpl567407*, newsocket_567833)(NU8 domain, NU8 socktype, NU8 protocol, NIM_BOOL buffered);
N_NIMCALL(NI, nsuParseInt)(NimStringDesc* s);
N_NIMCALL(void, bindaddr_568620)(Socketimpl567407* socket, NU16 port, NimStringDesc* address);
N_NIMCALL(void, listen_568610)(Socketimpl567407* socket, int backlog);
static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src);
N_CLOSURE(void, HEX3Aanonymous_575409)(NimStringDesc* line, void* ClEnv);
N_NIMCALL(void, send_572208)(Socketimpl567407* 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_569401)(Socketimpl567407* server, Socketimpl567407** client, NU8 flags);
N_NIMCALL(void, readline_571608)(Socketimpl567407* socket, NimStringDesc** line, NI timeout, NU8 flags);
N_NIMCALL(void, close_569417)(Socketimpl567407* socket);
N_NIMCALL(void, msgquit_191491)(NI8 x);
STRING_LITERAL(TMP1158, "", 0);
STRING_LITERAL(TMP1637, " ", 1);
STRING_LITERAL(TMP1638, "-", 1);
STRING_LITERAL(TMP1993, "run", 3);
STRING_LITERAL(TMP5257, "server.type", 11);
STRING_LITERAL(TMP5258, "stdin", 5);
STRING_LITERAL(TMP5259, "tcp", 3);
STRING_LITERAL(TMP5260, "quit", 4);
STRING_LITERAL(TMP5262, "server.port", 11);
STRING_LITERAL(TMP5263, "server.address", 14);
STRING_LITERAL(TMP5266, "\015\012", 2);
STRING_LITERAL(TMP5274, "Invalid server.type:", 20);
NimStringDesc* curcaascmd_575204;
extern Gcheap51018 gch_51059;
NimStringDesc* lastcaascmd_575205;
extern TNimType NTI209211; /* OptParser */
extern NU32 gglobaloptions_167132;
extern NimStringDesc* arguments_212837;
extern NimStringDesc* command_167240;
TNimType NTI575426; /* object */
extern TNimType NTI346033; /* range -1..0(int) */
extern TNimType NTI567409; /* Socket */
TNimType NTI575448; /* ref object */
extern NI gerrorcounter_191399;
extern TY191441 writelnhook_191444;

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, nimGCunrefNoCycle)(void* p) {
	Cell48946* c;
	c = usrtocell_52646(p);
	{
		(*c).refcount -= ((NI) 8);
		if (!((NU32)((*c).refcount) < (NU32)(((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_575209)(NU8 pass, NimStringDesc* cmd) {
	Optparser209211 p;
	NI argscount;
	memset((void*)(&p), 0, sizeof(p));
	p.Sup.m_type = (&NTI209211);
	chckNil((void*)(&p));
	memset((void*)(&p), 0, sizeof(p));
	p.Sup.m_type = (&NTI209211);
	initoptparser_209364(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*) &TMP1637))) goto LA7;
					p.key = copyString(((NimStringDesc*) &TMP1638));
					{
						NIM_BOOL LOC11;
						LOC11 = 0;
						LOC11 = processargument_212854(pass, (&p), (&argscount));
						if (!LOC11) goto LA12;
						goto LA1;
					}
					LA12: ;
				}
				goto LA5;
				LA7: ;
				{
					processswitch_212841(pass, (&p));
				}
				LA5: ;
			}
			break;
			case ((NU8) 1):
			{
				{
					NIM_BOOL LOC18;
					LOC18 = 0;
					LOC18 = processargument_212854(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;
			TY253724 LOC34;
			LOC27 = 0;
			LOC28 = 0;
			LOC28 = !(((gglobaloptions_167132 &(1<<((((NU8) 13))&31)))!=0));
			if (!(LOC28)) goto LA29;
			LOC28 = !(((arguments_212837) && (arguments_212837)->Sup.len == 0));
			LA29: ;
			LOC27 = LOC28;
			if (!(LOC27)) goto LA30;
			LOC31 = 0;
			LOC31 = nsuNormalize(command_167240);
			LOC27 = !(eqStrings(LOC31, ((NimStringDesc*) &TMP1993)));
			LA30: ;
			if (!LOC27) goto LA32;
			memset((void*)LOC34, 0, sizeof(LOC34));
			rawmessage_193675(((NU16) 59), LOC34, 0);
		}
		LA32: ;
	}
	LA23: ;
}
N_NIMCALL(void, TMP5256)(void* p, NI op) {
	TY575426* a;
	a = (TY575426*)p;
	nimGCvisit((void*)(*a).stdoutsocket575408, 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 (!((NU32)((*c).refcount) < (NU32)(((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_575409)(NimStringDesc* line, void* ClEnv) {
	TY575426* HEX3Aenvp_575438;
	NimStringDesc* LOC1;
	HEX3Aenvp_575438 = (TY575426*) ClEnv;
	LOC1 = 0;
	LOC1 = rawNewString(line->Sup.len + 2);
appendString(LOC1, line);
appendString(LOC1, ((NimStringDesc*) &TMP5266));
	send_572208((*HEX3Aenvp_575438).stdoutsocket575408, 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)((NU32)((*c).refcount) + (NU32)(((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 (!((NU32)((*c).refcount) < (NU32)(((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_575254)(TY575256 action) {
	TY575426* HEX3Aenv_575447;
	NimStringDesc* typ;
	HEX3Aenv_575447 = 0;
	HEX3Aenv_575447 = (TY575426*) newObj((&NTI575448), sizeof(TY575426));
	typ = getconfigvar_167335(((NimStringDesc*) &TMP5257));
	if (eqStrings(typ, ((NimStringDesc*) &TMP5258))) goto LA1;
	if (eqStrings(typ, ((NimStringDesc*) &TMP5259))) goto LA2;
	if (eqStrings(typ, ((NimStringDesc*) &TMP1158))) goto LA2;
	goto LA3;
	LA1: ;
	{
		{
			while (1) {
				NimStringDesc* line;
				NimStringDesc* LOC12;
				line = readline_16916(stdin);
				{
					if (!eqStrings(line, ((NimStringDesc*) &TMP5260))) goto LA10;
					exit(((NI) 0));
				}
				LA10: ;
				LOC12 = 0;
				LOC12 = curcaascmd_575204; curcaascmd_575204 = copyStringRC1(line);
				if (LOC12) nimGCunrefNoCycle(LOC12);
				processcmdline_575209(((NU8) 1), line);
				action();
				gerrorcounter_191399 = ((NI) 0);
				printf("%s\012", ((NimStringDesc*) &TMP1158)? (((NimStringDesc*) &TMP1158))->data:"nil");
				fflush(stdout);
			}
		}
	}
	goto LA4;
	LA2: ;
	{
		Socketimpl567407* server;
		NimStringDesc* p;
		NU16 port;
		NimStringDesc* LOC20;
		NimStringDesc* inp;
		TY191441 LOC21;
		server = newsocket_567833(((NU8) 2), ((NU8) 1), ((NU8) 6), NIM_TRUE);
		p = getconfigvar_167335(((NimStringDesc*) &TMP5262));
		{
			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_167335(((NimStringDesc*) &TMP5263));
		bindaddr_568620(server, port, LOC20);
		inp = copyString(((NimStringDesc*) &TMP1158));
		listen_568610(server, SOMAXCONN);
		asgnRefNoCycle((void**) (&(*HEX3Aenv_575447).stdoutsocket575408), newsocket_567833(((NU8) 2), ((NU8) 1), ((NU8) 6), NIM_TRUE));
		memset((void*)(&LOC21), 0, sizeof(LOC21));
		LOC21.ClPrc = HEX3Aanonymous_575409; LOC21.ClEnv = HEX3Aenv_575447;
		asgnRef((void**) (&writelnhook_191444.ClEnv), LOC21.ClEnv);
		writelnhook_191444.ClPrc = LOC21.ClPrc;
		{
			while (1) {
				NimStringDesc* LOC24;
				accept_569401(server, &(*HEX3Aenv_575447).stdoutsocket575408, 2);
				readline_571608((*HEX3Aenv_575447).stdoutsocket575408, (&inp), ((NI) -1), 2);
				LOC24 = 0;
				LOC24 = curcaascmd_575204; curcaascmd_575204 = copyStringRC1(inp);
				if (LOC24) nimGCunrefNoCycle(LOC24);
				processcmdline_575209(((NU8) 1), inp);
				action();
				gerrorcounter_191399 = ((NI) 0);
				send_572208((*HEX3Aenv_575447).stdoutsocket575408, ((NimStringDesc*) &TMP5266), 2);
				close_569417((*HEX3Aenv_575447).stdoutsocket575408);
			}
		}
	}
	goto LA4;
	LA3: ;
	{
		printf("%s%s\012", ((NimStringDesc*) &TMP5274)? (((NimStringDesc*) &TMP5274))->data:"nil", typ? (typ)->data:"nil");
		msgquit_191491(((NI8) 1));
	}
	LA4: ;
}
NIM_EXTERNC N_NOINLINE(void, compiler_serviceInit000)(void) {
	NimStringDesc* LOC1;
	NimStringDesc* LOC2;
	LOC1 = 0;
	LOC1 = curcaascmd_575204; curcaascmd_575204 = copyStringRC1(((NimStringDesc*) &TMP1158));
	if (LOC1) nimGCunrefNoCycle(LOC1);
	LOC2 = 0;
	LOC2 = lastcaascmd_575205; lastcaascmd_575205 = copyStringRC1(((NimStringDesc*) &TMP1158));
	if (LOC2) nimGCunrefNoCycle(LOC2);
}

NIM_EXTERNC N_NOINLINE(void, compiler_serviceDatInit000)(void) {
static TNimNode* TMP5243[2];
static TNimNode TMP1152[3];
NTI575426.size = sizeof(TY575426);
NTI575426.kind = 18;
NTI575426.base = 0;
NTI575426.flags = 2;
TMP5243[0] = &TMP1152[1];
TMP1152[1].kind = 1;
TMP1152[1].offset = offsetof(TY575426, HEX3Astate);
TMP1152[1].typ = (&NTI346033);
TMP1152[1].name = ":state";
TMP5243[1] = &TMP1152[2];
TMP1152[2].kind = 1;
TMP1152[2].offset = offsetof(TY575426, stdoutsocket575408);
TMP1152[2].typ = (&NTI567409);
TMP1152[2].name = "stdoutSocket575408";
TMP1152[0].len = 2; TMP1152[0].kind = 2; TMP1152[0].sons = &TMP5243[0];
NTI575426.node = &TMP1152[0];
NTI575448.size = sizeof(TY575426*);
NTI575448.kind = 22;
NTI575448.base = (&NTI575426);
NTI575448.flags = 2;
NTI575448.marker = TMP5256;
}

