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

#include <string.h>

#include <fcntl.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <sys/time.h>

#include <sys/mman.h>
typedef struct tmemfile234204 tmemfile234204;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
struct  tmemfile234204  {
void* Mem;
NI Size;
int Handle;
};
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  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
N_NIMCALL(void, oserror_106205)(NI32 errorcode);
N_NIMCALL(NI32, oslasterror_106231)(void);
TNimType NTI234204; /* TMemFile */
extern TNimType NTI147; /* pointer */
extern TNimType NTI106; /* int */
extern TNimType NTI4409; /* cint */

N_NIMCALL(tmemfile234204, open_234803)(NimStringDesc* filename, NU8 mode, NI mappedsize, NI offset, NI newfilesize) {
	tmemfile234204 result;
	NIM_BOOL readonly;
	int flags;
	int LOC52;
	int LOC58;
	memset((void*)&result, 0, sizeof(result));
	readonly = (mode == ((NU8) 0));
	{
		if (!readonly) goto LA3;
		flags = O_RDONLY;
	}
	goto LA1;
	LA3: ;
	{
		flags = O_RDWR;
	}
	LA1: ;
	{
		if (!!((newfilesize == -1))) goto LA8;
		flags = (NI32)((NI32)(flags | O_CREAT) | O_TRUNC);
	}
	LA8: ;
	result.Handle = open(filename->data, flags);
	{
		NI32 LOC19;
		if (!(result.Handle == ((NI32) -1))) goto LA12;
		result.Mem = NIM_NIL;
		result.Size = 0;
		{
			int LOC18;
			if (!!((result.Handle == ((NI32) 0)))) goto LA16;
			LOC18 = 0;
			LOC18 = close(result.Handle);
		}
		LA16: ;
		LOC19 = 0;
		LOC19 = oslasterror_106231();
		oserror_106205(LOC19);
	}
	LA12: ;
	{
		if (!!((newfilesize == -1))) goto LA22;
		{
			int LOC26;
			NI32 LOC34;
			LOC26 = 0;
			LOC26 = ftruncate(result.Handle, ((off_t) (newfilesize)));
			if (!(LOC26 == ((NI32) -1))) goto LA27;
			result.Mem = NIM_NIL;
			result.Size = 0;
			{
				int LOC33;
				if (!!((result.Handle == ((NI32) 0)))) goto LA31;
				LOC33 = 0;
				LOC33 = close(result.Handle);
			}
			LA31: ;
			LOC34 = 0;
			LOC34 = oslasterror_106231();
			oserror_106205(LOC34);
		}
		LA27: ;
	}
	LA22: ;
	{
		if (!!((mappedsize == -1))) goto LA37;
		result.Size = mappedsize;
	}
	goto LA35;
	LA37: ;
	{
		struct stat stat;
		memset((void*)&stat, 0, sizeof(stat));
		{
			int LOC42;
			LOC42 = 0;
			LOC42 = fstat(result.Handle, &stat);
			if (!!((LOC42 == ((NI32) -1)))) goto LA43;
			result.Size = ((NI) (stat.st_size));
		}
		goto LA40;
		LA43: ;
		{
			NI32 LOC51;
			result.Mem = NIM_NIL;
			result.Size = 0;
			{
				int LOC50;
				if (!!((result.Handle == ((NI32) 0)))) goto LA48;
				LOC50 = 0;
				LOC50 = close(result.Handle);
			}
			LA48: ;
			LOC51 = 0;
			LOC51 = oslasterror_106231();
			oserror_106205(LOC51);
		}
		LA40: ;
	}
	LA35: ;
	LOC52 = 0;
	{
		if (!readonly) goto LA55;
		LOC52 = PROT_READ;
	}
	goto LA53;
	LA55: ;
	{
		LOC52 = (NI32)(PROT_READ | PROT_WRITE);
	}
	LA53: ;
	LOC58 = 0;
	{
		if (!readonly) goto LA61;
		LOC58 = MAP_PRIVATE;
	}
	goto LA59;
	LA61: ;
	{
		LOC58 = MAP_SHARED;
	}
	LA59: ;
	result.Mem = mmap(NIM_NIL, result.Size, LOC52, LOC58, result.Handle, ((off_t) (offset)));
	{
		NI32 LOC73;
		if (!(result.Mem == ((void*) (MAP_FAILED)))) goto LA66;
		result.Mem = NIM_NIL;
		result.Size = 0;
		{
			int LOC72;
			if (!!((result.Handle == ((NI32) 0)))) goto LA70;
			LOC72 = 0;
			LOC72 = close(result.Handle);
		}
		LA70: ;
		LOC73 = 0;
		LOC73 = oslasterror_106231();
		oserror_106205(LOC73);
	}
	LA66: ;
	return result;
}

N_NIMCALL(void, close_235243)(tmemfile234204* f) {
	NIM_BOOL error;
	NI32 lasterr;
	error = NIM_FALSE;
	lasterr = 0;
	{
		int LOC5;
		NIM_BOOL LOC6;
		int LOC7;
		if (!!(((*f).Handle == ((NI32) 0)))) goto LA3;
		LOC5 = 0;
		LOC5 = munmap((*f).Mem, (*f).Size);
		error = !((LOC5 == ((NI32) 0)));
		lasterr = oslasterror_106231();
		LOC6 = 0;
		LOC7 = 0;
		LOC7 = close((*f).Handle);
		LOC6 = !((LOC7 == ((NI32) 0)));
		if (LOC6) goto LA8;
		LOC6 = error;
		LA8: ;
		error = LOC6;
	}
	LA3: ;
	(*f).Size = 0;
	(*f).Mem = NIM_NIL;
	(*f).Handle = ((int) 0);
	{
		if (!error) goto LA11;
		oserror_106205(lasterr);
	}
	LA11: ;
}
N_NOINLINE(void, stdlibmemfilesInit)(void) {
}

N_NOINLINE(void, stdlibmemfilesDatInit)(void) {
static TNimNode* TMP821[3];
static TNimNode TMP805[4];
NTI234204.size = sizeof(tmemfile234204);
NTI234204.kind = 18;
NTI234204.base = 0;
NTI234204.flags = 3;
TMP821[0] = &TMP805[1];
TMP805[1].kind = 1;
TMP805[1].offset = offsetof(tmemfile234204, Mem);
TMP805[1].typ = (&NTI147);
TMP805[1].name = "mem";
TMP821[1] = &TMP805[2];
TMP805[2].kind = 1;
TMP805[2].offset = offsetof(tmemfile234204, Size);
TMP805[2].typ = (&NTI106);
TMP805[2].name = "size";
TMP821[2] = &TMP805[3];
TMP805[3].kind = 1;
TMP805[3].offset = offsetof(tmemfile234204, Handle);
TMP805[3].typ = (&NTI4409);
TMP805[3].name = "handle";
TMP805[0].len = 3; TMP805[0].kind = 2; TMP805[0].sons = &TMP821[0];
NTI234204.node = &TMP805[0];
}

