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

#include <stdio.h>
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct tstream124629 tstream124629;
typedef struct TNimObject TNimObject;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct tfilestream125156 tfilestream125156;
typedef struct eio2433 eio2433;
typedef struct esystem2431 esystem2431;
typedef struct esynch2429 esynch2429;
typedef struct E_Base E_Base;
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;
};
typedef N_NIMCALL_PTR(void, TY124630) (tstream124629* s);
typedef N_NIMCALL_PTR(NIM_BOOL, TY124634) (tstream124629* s);
typedef N_NIMCALL_PTR(void, TY124638) (tstream124629* s, NI pos);
typedef N_NIMCALL_PTR(NI, TY124643) (tstream124629* s);
typedef N_NIMCALL_PTR(NI, TY124647) (tstream124629* s, void* buffer, NI buflen);
typedef N_NIMCALL_PTR(void, TY124653) (tstream124629* s, void* buffer, NI buflen);
typedef N_NIMCALL_PTR(void, TY124659) (tstream124629* s);
struct  tstream124629  {
  TNimObject Sup;
TY124630 Closeimpl;
TY124634 Atendimpl;
TY124638 Setpositionimpl;
TY124643 Getpositionimpl;
TY124647 Readdataimpl;
TY124653 Writedataimpl;
TY124659 Flushimpl;
};
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
struct  tfilestream125156  {
  tstream124629 Sup;
FILE* F;
};
struct  E_Base  {
  TNimObject Sup;
E_Base* parent;
NCSTRING name;
NimStringDesc* message;
NimStringDesc* trace;
};
struct  esynch2429  {
  E_Base Sup;
};
struct  esystem2431  {
  esynch2429 Sup;
};
struct  eio2433  {
  esystem2431 Sup;
};
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 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, TMP2642)(void* p, NI op);
N_NIMCALL(void, writedata_124750)(tstream124629* s, void* buffer, NI buflen);
N_NIMCALL(void, TMP2652)(void* p, NI op);
N_NIMCALL(void*, newObj)(TNimType* typ, NI size);
N_NIMCALL(void, fsclose_125158)(tstream124629* s);
N_NIMCALL(NIM_BOOL, fsatend_125183)(tstream124629* s);
N_NIMCALL(NIM_BOOL, endoffile_10428)(FILE* f);
N_NIMCALL(void, fssetposition_125188)(tstream124629* s, NI pos);
N_NIMCALL(void, setfilepos_10651)(FILE* f, NI64 pos);
N_NIMCALL(NI, fsgetposition_125193)(tstream124629* s);
N_NIMCALL(NI64, getfilepos_10655)(FILE* f);
N_NIMCALL(NI, fsreaddata_125198)(tstream124629* s, void* buffer, NI buflen);
N_NIMCALL(NI, readbuffer_10627)(FILE* f, void* buffer, NI len);
N_NIMCALL(void, fswritedata_125205)(tstream124629* s, void* buffer, NI buflen);
N_NIMCALL(NI, writebuffer_10646)(FILE* f, void* buffer, NI len);
N_NIMCALL(eio2433*, neweio_124603)(NimStringDesc* msg);
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, raiseException)(E_Base* e, NCSTRING ename);
N_NIMCALL(void, fsflush_125179)(tstream124629* s);
N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
N_NIMCALL(NIM_CHAR, readchar_124811)(tstream124629* s);
N_NIMCALL(NI, readdata_124733)(tstream124629* s, void* buffer, NI buflen);
N_NIMCALL(NimStringDesc*, addChar)(NimStringDesc* s, NIM_CHAR c);
STRING_LITERAL(TMP2655, "cannot write to stream", 22);
STRING_LITERAL(TMP2656, "", 0);
extern TNimType NTI2409; /* TObject */
TNimType NTI124629; /* TStream */
TNimType NTI124630; /* proc (PStream) */
TNimType NTI124634; /* proc (PStream): bool */
TNimType NTI124638; /* proc (PStream, int) */
TNimType NTI124643; /* proc (PStream): int */
TNimType NTI124647; /* proc (PStream, pointer, int): int */
TNimType NTI124653; /* proc (PStream, pointer, int) */
TNimType NTI124659; /* proc (PStream) */
TNimType NTI124627; /* PStream */
TNimType NTI125156; /* TFileStream */
extern TNimType NTI10004; /* TFile */
TNimType NTI125154; /* PFileStream */
extern TNimType NTI72201; /* ref EIO */
extern TNimType NTI2433; /* EIO */
extern tgcheap43016 gch_43044;
N_NIMCALL(void, TMP2642)(void* p, NI op) {
	tstream124629* a;
	a = (tstream124629*)p;
}

N_NIMCALL(void, writedata_124750)(tstream124629* s, void* buffer, NI buflen) {
	(*s).Writedataimpl(s, buffer, buflen);
}

N_NIMCALL(void, write_124774)(tstream124629* s, NimStringDesc* x) {
	writedata_124750(s, ((void*) (x->data)), x->Sup.len);
}
N_NIMCALL(void, TMP2652)(void* p, NI op) {
	tfilestream125156* a;
	a = (tfilestream125156*)p;
}

N_NIMCALL(void, fsclose_125158)(tstream124629* s) {
	{
		if (!!(((*((tfilestream125156*) (s))).F == NIM_NIL))) goto LA3;
		fclose((*((tfilestream125156*) (s))).F);
		(*((tfilestream125156*) (s))).F = NIM_NIL;
	}
	LA3: ;
}

N_NIMCALL(NIM_BOOL, fsatend_125183)(tstream124629* s) {
	NIM_BOOL result;
	result = 0;
	result = endoffile_10428((*((tfilestream125156*) (s))).F);
	goto BeforeRet;
	BeforeRet: ;
	return result;
}

N_NIMCALL(void, fssetposition_125188)(tstream124629* s, NI pos) {
	setfilepos_10651((*((tfilestream125156*) (s))).F, ((NI64) (pos)));
}

N_NIMCALL(NI, fsgetposition_125193)(tstream124629* s) {
	NI result;
	NI64 LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = getfilepos_10655((*((tfilestream125156*) (s))).F);
	result = ((NI) (LOC1));
	goto BeforeRet;
	BeforeRet: ;
	return result;
}

N_NIMCALL(NI, fsreaddata_125198)(tstream124629* s, void* buffer, NI buflen) {
	NI result;
	result = 0;
	result = readbuffer_10627((*((tfilestream125156*) (s))).F, buffer, buflen);
	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(eio2433*, neweio_124603)(NimStringDesc* msg) {
	eio2433* result;
	NimStringDesc* LOC1;
	result = 0;
	result = (eio2433*) newObj((&NTI72201), sizeof(eio2433));
	(*result).Sup.Sup.Sup.Sup.m_type = (&NTI2433);
	LOC1 = 0;
	LOC1 = (*result).Sup.Sup.Sup.message; (*result).Sup.Sup.Sup.message = copyStringRC1(msg);
	if (LOC1) nimGCunrefNoCycle(LOC1);
	return result;
}

N_NIMCALL(void, fswritedata_125205)(tstream124629* s, void* buffer, NI buflen) {
	{
		NI LOC3;
		eio2433* LOC6;
		LOC3 = 0;
		LOC3 = writebuffer_10646((*((tfilestream125156*) (s))).F, buffer, buflen);
		if (!!((LOC3 == buflen))) goto LA4;
		LOC6 = 0;
		LOC6 = neweio_124603(((NimStringDesc*) &TMP2655));
		raiseException((E_Base*)LOC6, "EIO");
	}
	LA4: ;
}

N_NIMCALL(void, fsflush_125179)(tstream124629* s) {
	fflush((*((tfilestream125156*) (s))).F);
}

N_NIMCALL(tfilestream125156*, newfilestream_125211)(FILE* f) {
	tfilestream125156* result;
	result = 0;
	result = (tfilestream125156*) newObj((&NTI125154), sizeof(tfilestream125156));
	(*result).Sup.Sup.m_type = (&NTI125156);
	(*result).F = f;
	(*result).Sup.Closeimpl = fsclose_125158;
	(*result).Sup.Atendimpl = fsatend_125183;
	(*result).Sup.Setpositionimpl = fssetposition_125188;
	(*result).Sup.Getpositionimpl = fsgetposition_125193;
	(*result).Sup.Readdataimpl = fsreaddata_125198;
	(*result).Sup.Writedataimpl = fswritedata_125205;
	(*result).Sup.Flushimpl = fsflush_125179;
	return result;
}

N_NIMCALL(void, close_124676)(tstream124629* s) {
	{
		if (!!((*s).Closeimpl == 0)) goto LA3;
		(*s).Closeimpl(s);
	}
	LA3: ;
}

N_NIMCALL(NIM_BOOL, atend_124694)(tstream124629* s) {
	NIM_BOOL result;
	result = 0;
	result = (*s).Atendimpl(s);
	return result;
}

N_NIMCALL(NI, readdata_124733)(tstream124629* s, void* buffer, NI buflen) {
	NI result;
	result = 0;
	result = (*s).Readdataimpl(s, buffer, buflen);
	return result;
}

N_NIMCALL(NIM_CHAR, readchar_124811)(tstream124629* s) {
	NIM_CHAR result;
	result = 0;
	{
		NI LOC3;
		LOC3 = 0;
		LOC3 = readdata_124733(s, ((void*) (&result)), 1);
		if (!!((LOC3 == 1))) goto LA4;
		result = 0;
	}
	LA4: ;
	return result;
}

N_NIMCALL(NimStringDesc*, readline_125017)(tstream124629* s) {
	NimStringDesc* result;
	result = 0;
	result = copyString(((NimStringDesc*) &TMP2656));
	while (1) {
		NIM_CHAR c;
		c = readchar_124811(s);
		{
			if (!((NU8)(c) == (NU8)(13))) goto LA4;
			c = readchar_124811(s);
			goto LA1;
		}
		LA4: ;
		{
			NIM_BOOL LOC8;
			LOC8 = 0;
			LOC8 = ((NU8)(c) == (NU8)(10));
			if (LOC8) goto LA9;
			LOC8 = ((NU8)(c) == (NU8)(0));
			LA9: ;
			if (!LOC8) goto LA10;
			goto LA1;
		}
		goto LA6;
		LA10: ;
		{
			result = addChar(result, c);
		}
		LA6: ;
	} LA1: ;
	return result;
}
N_NOINLINE(void, stdlibstreamsInit)(void) {
}

N_NOINLINE(void, stdlibstreamsDatInit)(void) {
static TNimNode* TMP2641[7];
static TNimNode TMP31[9];
NTI124629.size = sizeof(tstream124629);
NTI124629.kind = 17;
NTI124629.base = (&NTI2409);
NTI124629.flags = 1;
TMP2641[0] = &TMP31[1];
NTI124630.size = sizeof(TY124630);
NTI124630.kind = 25;
NTI124630.base = 0;
NTI124630.flags = 3;
TMP31[1].kind = 1;
TMP31[1].offset = offsetof(tstream124629, Closeimpl);
TMP31[1].typ = (&NTI124630);
TMP31[1].name = "closeImpl";
TMP2641[1] = &TMP31[2];
NTI124634.size = sizeof(TY124634);
NTI124634.kind = 25;
NTI124634.base = 0;
NTI124634.flags = 3;
TMP31[2].kind = 1;
TMP31[2].offset = offsetof(tstream124629, Atendimpl);
TMP31[2].typ = (&NTI124634);
TMP31[2].name = "atEndImpl";
TMP2641[2] = &TMP31[3];
NTI124638.size = sizeof(TY124638);
NTI124638.kind = 25;
NTI124638.base = 0;
NTI124638.flags = 3;
TMP31[3].kind = 1;
TMP31[3].offset = offsetof(tstream124629, Setpositionimpl);
TMP31[3].typ = (&NTI124638);
TMP31[3].name = "setPositionImpl";
TMP2641[3] = &TMP31[4];
NTI124643.size = sizeof(TY124643);
NTI124643.kind = 25;
NTI124643.base = 0;
NTI124643.flags = 3;
TMP31[4].kind = 1;
TMP31[4].offset = offsetof(tstream124629, Getpositionimpl);
TMP31[4].typ = (&NTI124643);
TMP31[4].name = "getPositionImpl";
TMP2641[4] = &TMP31[5];
NTI124647.size = sizeof(TY124647);
NTI124647.kind = 25;
NTI124647.base = 0;
NTI124647.flags = 3;
TMP31[5].kind = 1;
TMP31[5].offset = offsetof(tstream124629, Readdataimpl);
TMP31[5].typ = (&NTI124647);
TMP31[5].name = "readDataImpl";
TMP2641[5] = &TMP31[6];
NTI124653.size = sizeof(TY124653);
NTI124653.kind = 25;
NTI124653.base = 0;
NTI124653.flags = 3;
TMP31[6].kind = 1;
TMP31[6].offset = offsetof(tstream124629, Writedataimpl);
TMP31[6].typ = (&NTI124653);
TMP31[6].name = "writeDataImpl";
TMP2641[6] = &TMP31[7];
NTI124659.size = sizeof(TY124659);
NTI124659.kind = 25;
NTI124659.base = 0;
NTI124659.flags = 3;
TMP31[7].kind = 1;
TMP31[7].offset = offsetof(tstream124629, Flushimpl);
TMP31[7].typ = (&NTI124659);
TMP31[7].name = "flushImpl";
TMP31[0].len = 7; TMP31[0].kind = 2; TMP31[0].sons = &TMP2641[0];
NTI124629.node = &TMP31[0];
NTI124627.size = sizeof(tstream124629*);
NTI124627.kind = 22;
NTI124627.base = (&NTI124629);
NTI124627.marker = TMP2642;
NTI125156.size = sizeof(tfilestream125156);
NTI125156.kind = 17;
NTI125156.base = (&NTI124629);
NTI125156.flags = 1;
TMP31[8].kind = 1;
TMP31[8].offset = offsetof(tfilestream125156, F);
TMP31[8].typ = (&NTI10004);
TMP31[8].name = "f";
NTI125156.node = &TMP31[8];
NTI125154.size = sizeof(tfilestream125156*);
NTI125154.kind = 22;
NTI125154.base = (&NTI125156);
NTI125154.marker = TMP2652;
}

