/* Generated by Nimrod Compiler v0.9.5 */
/*   (c) 2014 Andreas Rumpf */
/* The generated code is subject to the original license. */
/* Compiled for: Linux, i386, gcc */
/* Command for C compiler:
   gcc -c  -w -O3 -fno-strict-aliasing  -I/build/nimrod/src/nimrod/lib -o compiler/nimcache/service.o compiler/nimcache/service.c */
#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 tcell41090 tcell41090;
typedef struct TNimType TNimType;
typedef struct tcellseq41106 tcellseq41106;
typedef struct tgcheap43016 tgcheap43016;
typedef struct tcellset41102 tcellset41102;
typedef struct tpagedesc41098 tpagedesc41098;
typedef struct tmemregion23410 tmemregion23410;
typedef struct tsmallchunk22638 tsmallchunk22638;
typedef struct tllchunk23404 tllchunk23404;
typedef struct tbigchunk22640 tbigchunk22640;
typedef struct tintset22615 tintset22615;
typedef struct ttrunk22611 ttrunk22611;
typedef struct tavlnode23408 tavlnode23408;
typedef struct tgcstat43014 tgcstat43014;
typedef struct toptparser521807 toptparser521807;
typedef struct TNimObject TNimObject;
typedef struct TNimNode TNimNode;
typedef struct tlineinfo163527 tlineinfo163527;
typedef struct TY110805 TY110805;
typedef struct tsocketimpl150605 tsocketimpl150605;
typedef struct tbasechunk22636 tbasechunk22636;
typedef struct tfreecell22628 tfreecell22628;
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
struct  tcell41090  {
NI Refcount;
TNimType* Typ;
};
struct  tcellseq41106  {
NI Len;
NI Cap;
tcell41090** D;
};
struct  tcellset41102  {
NI Counter;
NI Max;
tpagedesc41098* Head;
tpagedesc41098** Data;
};
typedef tsmallchunk22638* TY23422[512];
typedef ttrunk22611* ttrunkbuckets22613[256];
struct  tintset22615  {
ttrunkbuckets22613 Data;
};
struct  tmemregion23410  {
NI Minlargeobj;
NI Maxlargeobj;
TY23422 Freesmallchunks;
tllchunk23404* Llmem;
NI Currmem;
NI Maxmem;
NI Freemem;
NI Lastsize;
tbigchunk22640* Freechunkslist;
tintset22615 Chunkstarts;
tavlnode23408* Root;
tavlnode23408* Deleted;
tavlnode23408* Last;
tavlnode23408* Freeavlnodes;
};
struct  tgcstat43014  {
NI Stackscans;
NI Cyclecollections;
NI Maxthreshold;
NI Maxstacksize;
NI Maxstackcells;
NI Cycletablesize;
NI64 Maxpause;
};
struct  tgcheap43016  {
void* Stackbottom;
NI Cyclethreshold;
tcellseq41106 Zct;
tcellseq41106 Decstack;
tcellset41102 Cycleroots;
tcellseq41106 Tempstack;
NI Recgclock;
tmemregion23410 Region;
tgcstat43014 Stat;
};
typedef N_NIMCALL_PTR(void, TY2289) (void* p, NI op);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY2289 marker;
};
struct  TNimObject  {
TNimType* m_type;
};
struct  toptparser521807  {
  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;
};
struct  tlineinfo163527  {
NI16 Line;
NI16 Col;
NI32 Fileindex;
};
typedef NimStringDesc* TY523288[1];
typedef N_NIMCALL_PTR(void, TY523294) (void);
typedef NIM_CHAR TY150630[4001];
struct  tsocketimpl150605  {
int Fd;
NIM_BOOL Isbuffered;
union {
struct {TY150630 Buffer;
NI Currpos;
NI Buflen;
} S1;
} isbufferedU;
NIM_BOOL Nonblocking;
};
typedef NI TY22618[16];
struct  tpagedesc41098  {
tpagedesc41098* Next;
NI Key;
TY22618 Bits;
};
struct  tbasechunk22636  {
NI Prevsize;
NI Size;
NIM_BOOL Used;
};
struct  tsmallchunk22638  {
  tbasechunk22636 Sup;
tsmallchunk22638* Next;
tsmallchunk22638* Prev;
tfreecell22628* Freelist;
NI Free;
NI Acc;
NF Data;
};
struct  tllchunk23404  {
NI Size;
NI Acc;
tllchunk23404* Next;
};
struct  tbigchunk22640  {
  tbasechunk22636 Sup;
tbigchunk22640* Next;
tbigchunk22640* Prev;
NI Align;
NF Data;
};
struct  ttrunk22611  {
ttrunk22611* Next;
NI Key;
TY22618 Bits;
};
typedef tavlnode23408* TY23414[2];
struct  tavlnode23408  {
TY23414 Link;
NI Key;
NI Upperbound;
NI Level;
};
struct  tfreecell22628  {
tfreecell22628* Next;
NI Zerofield;
};
struct TY110805 {
  TGenericSeq Sup;
  NimStringDesc* data[SEQ_DECL_SIZE];
};
N_NIMCALL(NimStringDesc*, copyStringRC1)(NimStringDesc* src);
static N_INLINE(void, nimGCunrefNoCycle)(void* p);
static N_INLINE(tcell41090*, usrtocell_44443)(void* usr);
static N_INLINE(void, rtladdzct_46002)(tcell41090* c);
N_NOINLINE(void, addzct_44418)(tcellseq41106* s, tcell41090* c);
N_NIMCALL(void, initoptparser_521819)(NimStringDesc* cmdline, toptparser521807* Result);
N_NOINLINE(void, chckNil)(void* p);
N_NIMCALL(void, nponext)(toptparser521807* p);
N_NIMCALL(NI, nsuFindChar)(NimStringDesc* s, NIM_CHAR sub, NI start);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI start_70624, NI last);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI first, NI last);
N_NIMCALL(NimStringDesc*, copyStr)(NimStringDesc* s, NI start);
N_NIMCALL(NimStringDesc*, copyStr)(NimStringDesc* s, NI first);
static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src);
static N_INLINE(void, appendChar)(NimStringDesc* dest, NIM_CHAR c);
N_NIMCALL(NimStringDesc*, rawNewString)(NI space);
N_NIMCALL(void, processswitch_173016)(NimStringDesc* switch_173018, NimStringDesc* arg, NU8 pass, tlineinfo163527 info);
N_NIMCALL(TGenericSeq*, incrSeq)(TGenericSeq* seq, NI elemsize);
N_NIMCALL(NimStringDesc*, nosunixToNativePath)(NimStringDesc* path, NimStringDesc* drive);
static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src);
N_NIMCALL(NimStringDesc*, npocmdLineRest)(toptparser521807* p);
N_NIMCALL(void, rawmessage_165292)(NU16 msg, NimStringDesc** args, NI argsLen0);
N_NIMCALL(NimStringDesc*, getconfigvar_137311)(NimStringDesc* key);
static N_INLINE(NIM_BOOL, eqStrings)(NimStringDesc* a, NimStringDesc* b);
N_NIMCALL(NimStringDesc*, readline_10484)(FILE* f);
N_NIMCALL(void, processcmdline_523207)(NU8 pass, NimStringDesc* cmd);
N_NIMCALL(tsocketimpl150605*, socket_151402)(NU8 domain, NU8 typ, NU8 protocol, NIM_BOOL buffered);
N_NIMCALL(void, oserror_106205)(NI32 errorcode);
N_NIMCALL(NI32, oslasterror_106231)(void);
N_NIMCALL(NI, nsuParseInt)(NimStringDesc* s);
N_NIMCALL(void, bindaddr_152584)(tsocketimpl150605* socket, NU16 port, NimStringDesc* address);
N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
N_NIMCALL(void, listen_152405)(tsocketimpl150605* socket, int backlog);
N_NIMCALL(void*, newObjRC1)(TNimType* typ, NI size);
N_NIMCALL(void, accept_154201)(tsocketimpl150605* server, tsocketimpl150605** client);
N_NIMCALL(void, readline_159033)(tsocketimpl150605* socket, NimStringDesc** line, NI timeout);
N_NIMCALL(void, send_160604)(tsocketimpl150605* socket, NimStringDesc* data);
N_NIMCALL(void, close_154266)(tsocketimpl150605* socket);
STRING_LITERAL(TMP974, "", 0);
STRING_LITERAL(TMP4529, "server.type", 11);
STRING_LITERAL(TMP4530, "stdin", 5);
STRING_LITERAL(TMP4531, "tcp", 3);
STRING_LITERAL(TMP4533, "quit", 4);
STRING_LITERAL(TMP4537, "server.port", 11);
STRING_LITERAL(TMP4538, "server.address", 14);
STRING_LITERAL(TMP4546, "\015\012", 2);
STRING_LITERAL(TMP4547, "Invalid server.type:", 20);
NimStringDesc* curcaascmd_523203;
extern tgcheap43016 gch_43044;
NimStringDesc* lastcaascmd_523204;
NimStringDesc* arguments_523205;
extern TNimType NTI521807; /* TOptParser */
extern tlineinfo163527 gcmdlineinfo_164104;
extern NimStringDesc* command_137212;
extern TY110805* commandargs_137234;
extern NimStringDesc* gprojectname_137206;
extern NU32 gglobaloptions_137111;
extern NI32 gdirtybufferidx_137127;
extern NI32 gdirtyoriginalidx_137128;
extern NI gerrorcounter_164182;
extern tsocketimpl150605* invalidsocket_150678;
extern tsocketimpl150605* stdoutsocket_164192;
extern TNimType NTI150607; /* TSocket */

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

static N_INLINE(void, rtladdzct_46002)(tcell41090* c) {
	addzct_44418(&gch_43044.Zct, c);
}

static N_INLINE(void, nimGCunrefNoCycle)(void* p) {
	tcell41090* c;
	c = usrtocell_44443(p);
	{
		(*c).Refcount -= 8;
		if (!((NU32)((*c).Refcount) < (NU32)(8))) goto LA3;
		rtladdzct_46002(c);
	}
	LA3: ;
}

static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src) {
	memcpy(((NCSTRING) (&(*dest).data[((*dest).Sup.len)- 0])), ((NCSTRING) ((*src).data)), (NI32)((*src).Sup.len + 1));
	(*dest).Sup.len += (*src).Sup.len;
}

static N_INLINE(void, appendChar)(NimStringDesc* dest, NIM_CHAR c) {
	(*dest).data[((*dest).Sup.len)- 0] = c;
	(*dest).data[((NI32)((*dest).Sup.len + 1))- 0] = 0;
	(*dest).Sup.len += 1;
}

static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src) {
	{
		tcell41090* c;
		if (!!((src == NIM_NIL))) goto LA3;
		c = usrtocell_44443(src);
		(*c).Refcount += 8;
	}
	LA3: ;
	{
		tcell41090* c;
		if (!!(((*dest) == NIM_NIL))) goto LA7;
		c = usrtocell_44443((*dest));
		{
			(*c).Refcount -= 8;
			if (!((NU32)((*c).Refcount) < (NU32)(8))) goto LA11;
			rtladdzct_46002(c);
		}
		LA11: ;
	}
	LA7: ;
	(*dest) = src;
}

N_NIMCALL(void, processcmdline_523207)(NU8 pass, NimStringDesc* cmd) {
	toptparser521807 p;
	NI argscount;
	chckNil((void*)&p);
	memset((void*)&p, 0, sizeof(p));
	p.Sup.m_type = (&NTI521807);
	initoptparser_521819(cmd, &p);
	argscount = 0;
	while (1) {
		nponext(&p);
		switch (p.Kind) {
		case ((NU8) 0):
		{
			goto LA1;
		}
		break;
		case ((NU8) 2):
		case ((NU8) 3):
		{
			NI bracketle;
			bracketle = nsuFindChar(p.Key, 91, 0);
			{
				NimStringDesc* key;
				NimStringDesc* val;
				NimStringDesc* LOC8;
				NimStringDesc* LOC9;
				if (!(0 <= bracketle)) goto LA6;
				key = copyStrLast(p.Key, 0, (NI32)(bracketle - 1));
				LOC8 = 0;
				LOC9 = 0;
				LOC9 = copyStr(p.Key, (NI32)(bracketle + 1));
				LOC8 = rawNewString(LOC9->Sup.len + p.Val->Sup.len + 1);
appendString(LOC8, LOC9);
appendChar(LOC8, 58);
appendString(LOC8, p.Val);
				val = LOC8;
				processswitch_173016(key, val, pass, gcmdlineinfo_164104);
			}
			goto LA4;
			LA6: ;
			{
				processswitch_173016(p.Key, p.Val, pass, gcmdlineinfo_164104);
			}
			LA4: ;
		}
		break;
		case ((NU8) 1):
		{
			{
				NimStringDesc* LOC16;
				if (!(argscount == 0)) goto LA14;
				LOC16 = 0;
				LOC16 = command_137212; command_137212 = copyStringRC1(p.Key);
				if (LOC16) nimGCunrefNoCycle(LOC16);
			}
			goto LA12;
			LA14: ;
			{
				{
					NimStringDesc* LOC22;
					if (!(pass == ((NU8) 0))) goto LA20;
					commandargs_137234 = (TY110805*) incrSeq(&(commandargs_137234)->Sup, sizeof(NimStringDesc*));
					LOC22 = 0;
					LOC22 = commandargs_137234->data[commandargs_137234->Sup.len-1]; commandargs_137234->data[commandargs_137234->Sup.len-1] = copyStringRC1(p.Key);
					if (LOC22) nimGCunrefNoCycle(LOC22);
				}
				LA20: ;
				{
					if (!(argscount == 1)) goto LA25;
					asgnRefNoCycle((void**) &gprojectname_137206, nosunixToNativePath(p.Key, ((NimStringDesc*) &TMP974)));
					asgnRefNoCycle((void**) &arguments_523205, npocmdLineRest(&p));
					goto LA1;
				}
				LA25: ;
			}
			LA12: ;
			argscount += 1;
		}
		break;
		}
	} LA1: ;
	{
		if (!(pass == ((NU8) 1))) goto LA29;
		{
			NIM_BOOL LOC33;
			TY523288 LOC37;
			LOC33 = 0;
			LOC33 = !(((gglobaloptions_137111 &(1<<((((NU8) 13))&31)))!=0));
			if (!(LOC33)) goto LA34;
			LOC33 = !(((arguments_523205) && (arguments_523205)->Sup.len == 0));
			LA34: ;
			if (!LOC33) goto LA35;
			memset((void*)LOC37, 0, sizeof(LOC37));
			rawmessage_165292(((NU16) 57), LOC37, 0);
		}
		LA35: ;
	}
	LA29: ;
}

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)), (NI32)((*a).Sup.len * 1));
	LOC11 = (LOC13 == ((NI32) 0));
	LA12: ;
	result = LOC11;
	goto BeforeRet;
	BeforeRet: ;
	return result;
}

N_NIMCALL(void, serve_523292)(TY523294 action) {
	NimStringDesc* typ;
	typ = getconfigvar_137311(((NimStringDesc*) &TMP4529));
	if (eqStrings(typ, ((NimStringDesc*) &TMP4530))) goto LA1;
	if (eqStrings(typ, ((NimStringDesc*) &TMP4531))) goto LA2;
	if (eqStrings(typ, ((NimStringDesc*) &TMP974))) goto LA2;
	goto LA3;
	LA1: ;
	{
		while (1) {
			NimStringDesc* line;
			NimStringDesc* LOC11;
			line = readline_10484(stdin);
			{
				if (!eqStrings(line, ((NimStringDesc*) &TMP4533))) goto LA9;
				exit(0);
			}
			LA9: ;
			LOC11 = 0;
			LOC11 = curcaascmd_523203; curcaascmd_523203 = copyStringRC1(line);
			if (LOC11) nimGCunrefNoCycle(LOC11);
			processcmdline_523207(((NU8) 1), line);
			action();
			gdirtybufferidx_137127 = ((NI32) 0);
			gdirtyoriginalidx_137128 = ((NI32) 0);
			gerrorcounter_164182 = 0;
			printf("%s\012", (((NimStringDesc*) &TMP974))->data);
			fflush(stdout);
		}
	}
	goto LA4;
	LA2: ;
	{
		tsocketimpl150605* server;
		NimStringDesc* p;
		NU16 port;
		NimStringDesc* LOC24;
		NimStringDesc* inp;
		server = socket_151402(((NU8) 2), ((NU8) 1), ((NU8) 6), NIM_TRUE);
		{
			NI32 LOC17;
			if (!(server == invalidsocket_150678)) goto LA15;
			LOC17 = 0;
			LOC17 = oslasterror_106231();
			oserror_106205(LOC17);
		}
		LA15: ;
		p = getconfigvar_137311(((NimStringDesc*) &TMP4537));
		{
			NI LOC22;
			if (!(0 < p->Sup.len)) goto LA20;
			LOC22 = 0;
			LOC22 = nsuParseInt(p);
			port = ((NU16) (LOC22));
		}
		goto LA18;
		LA20: ;
		{
			port = ((NU16) 6000);
		}
		LA18: ;
		LOC24 = 0;
		LOC24 = getconfigvar_137311(((NimStringDesc*) &TMP4538));
		bindaddr_152584(server, port, LOC24);
		inp = copyString(((NimStringDesc*) &TMP974));
		listen_152405(server, SOMAXCONN);
		if (stdoutsocket_164192) nimGCunrefNoCycle(stdoutsocket_164192);
		stdoutsocket_164192 = (tsocketimpl150605*) newObjRC1((&NTI150607), sizeof(tsocketimpl150605));
		while (1) {
			NimStringDesc* LOC26;
			accept_154201(server, &stdoutsocket_164192);
			readline_159033(stdoutsocket_164192, &inp, -1);
			LOC26 = 0;
			LOC26 = curcaascmd_523203; curcaascmd_523203 = copyStringRC1(inp);
			if (LOC26) nimGCunrefNoCycle(LOC26);
			processcmdline_523207(((NU8) 1), inp);
			action();
			gdirtybufferidx_137127 = ((NI32) 0);
			gdirtyoriginalidx_137128 = ((NI32) 0);
			gerrorcounter_164182 = 0;
			send_160604(stdoutsocket_164192, ((NimStringDesc*) &TMP4546));
			close_154266(stdoutsocket_164192);
		}
	}
	goto LA4;
	LA3: ;
	{
		printf("%s%s\012", (((NimStringDesc*) &TMP4547))->data, (typ)->data);
		exit(1);
	}
	LA4: ;
}
N_NOINLINE(void, serviceInit)(void) {
	NimStringDesc* LOC1;
	NimStringDesc* LOC2;
	NimStringDesc* LOC3;
	LOC1 = 0;
	LOC1 = curcaascmd_523203; curcaascmd_523203 = copyStringRC1(((NimStringDesc*) &TMP974));
	if (LOC1) nimGCunrefNoCycle(LOC1);
	LOC2 = 0;
	LOC2 = lastcaascmd_523204; lastcaascmd_523204 = copyStringRC1(((NimStringDesc*) &TMP974));
	if (LOC2) nimGCunrefNoCycle(LOC2);
	LOC3 = 0;
	LOC3 = arguments_523205; arguments_523205 = copyStringRC1(((NimStringDesc*) &TMP974));
	if (LOC3) nimGCunrefNoCycle(LOC3);
}

N_NOINLINE(void, serviceDatInit)(void) {
}

