/* 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/llstream.o compiler/nimcache/llstream.c */
#define NIM_INTBITS 32
#include "nimbase.h"

#include <string.h>

#include <stdio.h>
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct tllstream176204 tllstream176204;
typedef struct TNimObject TNimObject;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct tcell41090 tcell41090;
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 tbasechunk22636 tbasechunk22636;
typedef struct tfreecell22628 tfreecell22628;
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  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct  TNimObject  {
TNimType* m_type;
};
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
struct  tllstream176204  {
  TNimObject Sup;
NU8 Kind;
FILE* F;
NimStringDesc* S;
NI Rd;
NI Wr;
NI Lineoffset;
};
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 NU8 TY90161[32];
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;
};
N_NIMCALL(void, nimGCvisit)(void* d, NI op);
N_NIMCALL(void, TMP1387)(void* p, NI op);
N_NIMCALL(void*, newObj)(TNimType* typ, NI size);
N_NIMCALL(NIM_BOOL, open_10203)(FILE** f, NimStringDesc* filename, NU8 mode, NI bufsize);
N_NIMCALL(NI, readbuffer_10627)(FILE* f, void* buffer, NI len);
N_NIMCALL(NI, llreadfromstdin_176733)(tllstream176204* s, void* buf, NI buflen);
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(NimStringDesc*, rawNewString)(NI space);
N_NIMCALL(NimStringDesc*, rawNewString)(NI cap);
N_NIMCALL(NIM_BOOL, readlinefromstdin_176396)(NimStringDesc* prompt, NimStringDesc** line);
N_NIMCALL(void, write_10463)(FILE* f, NimStringDesc* s);
N_NIMCALL(NIM_BOOL, readline_10487)(FILE* f, NimStringDesc** line);
N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src);
N_NIMCALL(NimStringDesc*, resizeString)(NimStringDesc* dest, NI addlen);
N_NIMCALL(NI, counttriples_176676)(NimStringDesc* s);
static N_INLINE(NIM_BOOL, continueline_176652)(NimStringDesc* line, NIM_BOOL intriplestring);
N_NIMCALL(NIM_BOOL, endswith_176602)(NimStringDesc* x, TY90161 s);
N_NIMCALL(NimStringDesc*, setLengthStr)(NimStringDesc* s, NI newlen);
N_NIMCALL(NimStringDesc*, addChar)(NimStringDesc* s, NIM_CHAR c);
N_NIMCALL(NI, writebuffer_10646)(FILE* f, void* buffer, NI len);
N_NIMCALL(void, llstreamwrite_176258)(tllstream176204* s, NimStringDesc* data);
STRING_LITERAL(TMP1389, "", 0);
STRING_LITERAL(TMP1390, ">>> ", 4);
STRING_LITERAL(TMP1391, "... ", 4);
STRING_LITERAL(TMP1392, "\012", 1);
static NIM_CONST TY90161 TMP1393 = {
0x00, 0x00, 0x00, 0x00, 0x7A, 0xBC, 0x00, 0xF4,
0x01, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x50,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
;
static NIM_CONST TY90161 TMP1699 = {
0x00, 0x00, 0x00, 0x00, 0x72, 0xBC, 0x00, 0xD0,
0x01, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x50,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
;
extern TNimType NTI2409; /* TObject */
TNimType NTI176204; /* TLLStream */
TNimType NTI176202; /* TLLStreamKind */
extern TNimType NTI10004; /* TFile */
extern TNimType NTI143; /* string */
extern TNimType NTI106; /* int */
TNimType NTI176206; /* PLLStream */
extern tgcheap43016 gch_43044;
N_NIMCALL(void, TMP1387)(void* p, NI op) {
	tllstream176204* a;
	a = (tllstream176204*)p;
	nimGCvisit((void*)(*a).S, op);
}

N_NIMCALL(tllstream176204*, llstreamopen_176227)(NimStringDesc* filename, NU8 mode) {
	tllstream176204* result;
	result = 0;
	result = (tllstream176204*) newObj((&NTI176206), sizeof(tllstream176204));
	(*result).Sup.m_type = (&NTI176204);
	(*result).Kind = ((NU8) 2);
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = open_10203(&(*result).F, filename, mode, -1);
		if (!!(LOC3)) goto LA4;
		result = NIM_NIL;
	}
	LA4: ;
	return result;
}

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

N_NIMCALL(NIM_BOOL, readlinefromstdin_176396)(NimStringDesc* prompt, NimStringDesc** line) {
	NIM_BOOL result;
	result = 0;
	write_10463(stdout, prompt);
	result = readline_10487(stdin, line);
	return result;
}

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

N_NIMCALL(NI, counttriples_176676)(NimStringDesc* s) {
	NI result;
	NI i;
	result = 0;
	i = 0;
	while (1) {
		if (!(i < s->Sup.len)) goto LA1;
		{
			NIM_BOOL LOC4;
			NIM_BOOL LOC5;
			LOC4 = 0;
			LOC5 = 0;
			LOC5 = ((NU8)(s->data[i]) == (NU8)(34));
			if (!(LOC5)) goto LA6;
			LOC5 = ((NU8)(s->data[(NI32)(i + 1)]) == (NU8)(34));
			LA6: ;
			LOC4 = LOC5;
			if (!(LOC4)) goto LA7;
			LOC4 = ((NU8)(s->data[(NI32)(i + 2)]) == (NU8)(34));
			LA7: ;
			if (!LOC4) goto LA8;
			result += 1;
			i += 2;
		}
		LA8: ;
		i += 1;
	} LA1: ;
	return result;
}

N_NIMCALL(NIM_BOOL, endswith_176602)(NimStringDesc* x, TY90161 s) {
	NIM_BOOL result;
	NI i;
	result = 0;
	i = (NI32)(x->Sup.len - 1);
	while (1) {
		NIM_BOOL LOC2;
		LOC2 = 0;
		LOC2 = (0 <= i);
		if (!(LOC2)) goto LA3;
		LOC2 = ((NU8)(x->data[i]) == (NU8)(32));
		LA3: ;
		if (!LOC2) goto LA1;
		i -= 1;
	} LA1: ;
	{
		NIM_BOOL LOC6;
		LOC6 = 0;
		LOC6 = (0 <= i);
		if (!(LOC6)) goto LA7;
		LOC6 = ((s[((NU8)(x->data[i]))/8] &(1<<(((NU8)(x->data[i]))%8)))!=0);
		LA7: ;
		if (!LOC6) goto LA8;
		result = NIM_TRUE;
	}
	LA8: ;
	return result;
}

static N_INLINE(NIM_BOOL, continueline_176652)(NimStringDesc* line, NIM_BOOL intriplestring) {
	NIM_BOOL result;
	NIM_BOOL LOC1;
	NIM_BOOL LOC2;
	result = 0;
	LOC1 = 0;
	LOC2 = 0;
	LOC2 = intriplestring;
	if (LOC2) goto LA3;
	LOC2 = ((NU8)(line->data[0]) == (NU8)(32));
	LA3: ;
	LOC1 = LOC2;
	if (LOC1) goto LA4;
	LOC1 = endswith_176602(line, TMP1393);
	LA4: ;
	result = LOC1;
	return result;
}

N_NIMCALL(NI, llreadfromstdin_176733)(tllstream176204* s, void* buf, NI buflen) {
	NI result;
	NimStringDesc* LOC1;
	NimStringDesc* line;
	NI triples;
	result = 0;
	LOC1 = 0;
	LOC1 = (*s).S; (*s).S = copyStringRC1(((NimStringDesc*) &TMP1389));
	if (LOC1) nimGCunrefNoCycle(LOC1);
	(*s).Rd = 0;
	line = rawNewString(120);
	triples = 0;
	while (1) {
		NimStringDesc* LOC3;
		NIM_BOOL LOC9;
		NI LOC10;
		LOC3 = 0;
		{
			if (!((*s).S->Sup.len == 0)) goto LA6;
			LOC3 = copyString(((NimStringDesc*) &TMP1390));
		}
		goto LA4;
		LA6: ;
		{
			LOC3 = copyString(((NimStringDesc*) &TMP1391));
		}
		LA4: ;
		LOC9 = 0;
		LOC9 = readlinefromstdin_176396(LOC3, &line);
		if (!LOC9) goto LA2;
		(*s).S = resizeString((*s).S, line->Sup.len + 0);
appendString((*s).S, line);
		(*s).S = resizeString((*s).S, 1);
appendString((*s).S, ((NimStringDesc*) &TMP1392));
		LOC10 = 0;
		LOC10 = counttriples_176676(line);
		triples += LOC10;
		{
			NIM_BOOL LOC13;
			LOC13 = 0;
			LOC13 = continueline_176652(line, (((NI) ((NI)(triples & 1))) == 1));
			if (!!(LOC13)) goto LA14;
			goto LA2;
		}
		LA14: ;
	} LA2: ;
	(*s).Lineoffset += 1;
	result = ((buflen <= (NI32)((*s).S->Sup.len - (*s).Rd)) ? buflen : (NI32)((*s).S->Sup.len - (*s).Rd));
	{
		if (!(0 < result)) goto LA18;
		memcpy(buf, ((void*) (&(*s).S->data[(*s).Rd])), result);
		(*s).Rd += result;
	}
	LA18: ;
	return result;
}

N_NIMCALL(NI, llstreamread_176242)(tllstream176204* s, void* buf, NI buflen) {
	NI result;
	result = 0;
	switch ((*s).Kind) {
	case ((NU8) 0):
	{
		result = 0;
	}
	break;
	case ((NU8) 1):
	{
		result = ((buflen <= (NI32)((*s).S->Sup.len - (*s).Rd)) ? buflen : (NI32)((*s).S->Sup.len - (*s).Rd));
		{
			if (!(0 < result)) goto LA5;
			memcpy(buf, ((void*) (&(*s).S->data[(NI32)(0 + (*s).Rd)])), result);
			(*s).Rd += result;
		}
		LA5: ;
	}
	break;
	case ((NU8) 2):
	{
		result = readbuffer_10627((*s).F, buf, buflen);
	}
	break;
	case ((NU8) 3):
	{
		result = llreadfromstdin_176733(s, buf, buflen);
	}
	break;
	}
	return result;
}

N_NIMCALL(void, llstreamclose_176238)(tllstream176204* s) {
	switch ((*s).Kind) {
	case ((NU8) 0):
	case ((NU8) 1):
	case ((NU8) 3):
	{
	}
	break;
	case ((NU8) 2):
	{
		fclose((*s).F);
	}
	break;
	}
}

N_NIMCALL(NIM_BOOL, llstreamreadline_176248)(tllstream176204* s, NimStringDesc** line) {
	NIM_BOOL result;
	result = 0;
	(*line) = setLengthStr((*line), 0);
	switch ((*s).Kind) {
	case ((NU8) 0):
	{
		result = NIM_TRUE;
	}
	break;
	case ((NU8) 1):
	{
		NIM_BOOL LOC11;
		while (1) {
			if (!((*s).Rd < (*s).S->Sup.len)) goto LA3;
			switch (((NU8)((*s).S->data[(*s).Rd]))) {
			case 13:
			{
				(*s).Rd += 1;
				{
					if (!((NU8)((*s).S->data[(*s).Rd]) == (NU8)(10))) goto LA7;
					(*s).Rd += 1;
				}
				LA7: ;
				goto LA3;
			}
			break;
			case 10:
			{
				(*s).Rd += 1;
				goto LA3;
			}
			break;
			default:
			{
				(*line) = addChar((*line), (*s).S->data[(*s).Rd]);
				(*s).Rd += 1;
			}
			break;
			}
		} LA3: ;
		LOC11 = 0;
		LOC11 = (0 < (*line)->Sup.len);
		if (LOC11) goto LA12;
		LOC11 = ((*s).Rd < (*s).S->Sup.len);
		LA12: ;
		result = LOC11;
	}
	break;
	case ((NU8) 2):
	{
		result = readline_10487((*s).F, line);
	}
	break;
	case ((NU8) 3):
	{
		result = readline_10487(stdin, line);
	}
	break;
	}
	return result;
}

N_NIMCALL(tllstream176204*, llstreamopen_176218)(NimStringDesc* data) {
	tllstream176204* result;
	NimStringDesc* LOC1;
	result = 0;
	result = (tllstream176204*) newObj((&NTI176206), sizeof(tllstream176204));
	(*result).Sup.m_type = (&NTI176204);
	LOC1 = 0;
	LOC1 = (*result).S; (*result).S = copyStringRC1(data);
	if (LOC1) nimGCunrefNoCycle(LOC1);
	(*result).Kind = ((NU8) 1);
	return result;
}

N_NIMCALL(void, llstreamwrite_176258)(tllstream176204* s, NimStringDesc* data) {
	switch ((*s).Kind) {
	case ((NU8) 0):
	case ((NU8) 3):
	{
	}
	break;
	case ((NU8) 1):
	{
		(*s).S = resizeString((*s).S, data->Sup.len + 0);
appendString((*s).S, data);
		(*s).Wr += data->Sup.len;
	}
	break;
	case ((NU8) 2):
	{
		write_10463((*s).F, data);
	}
	break;
	}
}

N_NIMCALL(NIM_BOOL, endswithopr_176646)(NimStringDesc* x) {
	NIM_BOOL result;
	result = 0;
	result = endswith_176602(x, TMP1699);
	return result;
}

N_NIMCALL(void, llstreamwrite_176263)(tllstream176204* s, NIM_CHAR data) {
	NIM_CHAR c;
	c = 0;
	switch ((*s).Kind) {
	case ((NU8) 0):
	case ((NU8) 3):
	{
	}
	break;
	case ((NU8) 1):
	{
		(*s).S = addChar((*s).S, data);
		(*s).Wr += 1;
	}
	break;
	case ((NU8) 2):
	{
		NI LOC4;
		c = data;
		LOC4 = 0;
		LOC4 = writebuffer_10646((*s).F, ((void*) (&c)), 1);
	}
	break;
	}
}

N_NIMCALL(void, llstreamwriteln_176274)(tllstream176204* s, NimStringDesc* data) {
	llstreamwrite_176258(s, data);
	llstreamwrite_176258(s, ((NimStringDesc*) &TMP1392));
}

N_NIMCALL(tllstream176204*, llstreamopen_176222)(FILE** f) {
	tllstream176204* result;
	result = 0;
	result = (tllstream176204*) newObj((&NTI176206), sizeof(tllstream176204));
	(*result).Sup.m_type = (&NTI176204);
	(*result).F = (*f);
	(*result).Kind = ((NU8) 2);
	return result;
}

N_NIMCALL(tllstream176204*, llstreamopenstdin_176235)(void) {
	tllstream176204* result;
	NimStringDesc* LOC1;
	result = 0;
	result = (tllstream176204*) newObj((&NTI176206), sizeof(tllstream176204));
	(*result).Sup.m_type = (&NTI176204);
	(*result).Kind = ((NU8) 3);
	LOC1 = 0;
	LOC1 = (*result).S; (*result).S = copyStringRC1(((NimStringDesc*) &TMP1389));
	if (LOC1) nimGCunrefNoCycle(LOC1);
	(*result).Lineoffset = -1;
	return result;
}
N_NOINLINE(void, llstreamInit)(void) {
}

N_NOINLINE(void, llstreamDatInit)(void) {
static TNimNode* TMP1383[6];
static TNimNode* TMP1384[4];
NI TMP1386;
static char* NIM_CONST TMP1385[4] = {
"llsNone", 
"llsString", 
"llsFile", 
"llsStdIn"};
static TNimNode TMP705[12];
NTI176204.size = sizeof(tllstream176204);
NTI176204.kind = 17;
NTI176204.base = (&NTI2409);
TMP1383[0] = &TMP705[1];
NTI176202.size = sizeof(NU8);
NTI176202.kind = 14;
NTI176202.base = 0;
NTI176202.flags = 3;
for (TMP1386 = 0; TMP1386 < 4; TMP1386++) {
TMP705[TMP1386+2].kind = 1;
TMP705[TMP1386+2].offset = TMP1386;
TMP705[TMP1386+2].name = TMP1385[TMP1386];
TMP1384[TMP1386] = &TMP705[TMP1386+2];
}
TMP705[6].len = 4; TMP705[6].kind = 2; TMP705[6].sons = &TMP1384[0];
NTI176202.node = &TMP705[6];
TMP705[1].kind = 1;
TMP705[1].offset = offsetof(tllstream176204, Kind);
TMP705[1].typ = (&NTI176202);
TMP705[1].name = "kind";
TMP1383[1] = &TMP705[7];
TMP705[7].kind = 1;
TMP705[7].offset = offsetof(tllstream176204, F);
TMP705[7].typ = (&NTI10004);
TMP705[7].name = "f";
TMP1383[2] = &TMP705[8];
TMP705[8].kind = 1;
TMP705[8].offset = offsetof(tllstream176204, S);
TMP705[8].typ = (&NTI143);
TMP705[8].name = "s";
TMP1383[3] = &TMP705[9];
TMP705[9].kind = 1;
TMP705[9].offset = offsetof(tllstream176204, Rd);
TMP705[9].typ = (&NTI106);
TMP705[9].name = "rd";
TMP1383[4] = &TMP705[10];
TMP705[10].kind = 1;
TMP705[10].offset = offsetof(tllstream176204, Wr);
TMP705[10].typ = (&NTI106);
TMP705[10].name = "wr";
TMP1383[5] = &TMP705[11];
TMP705[11].kind = 1;
TMP705[11].offset = offsetof(tllstream176204, Lineoffset);
TMP705[11].typ = (&NTI106);
TMP705[11].name = "lineOffset";
TMP705[0].len = 6; TMP705[0].kind = 2; TMP705[0].sons = &TMP1383[0];
NTI176204.node = &TMP705[0];
NTI176206.size = sizeof(tllstream176204*);
NTI176206.kind = 22;
NTI176206.base = (&NTI176204);
NTI176206.marker = TMP1387;
}

