/* Generated by Nim Compiler v0.12.0 */
/*   (c) 2015 Andreas Rumpf */
/* The generated code is subject to the original license. */
#define NIM_INTBITS 64

#include "nimbase.h"
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <sys/mman.h>
typedef struct Memfile266208 Memfile266208;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
struct  Memfile266208  {
void* mem;
NI size;
int handle;
};
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  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_NOINLINE(void, raiseoserror_126009)(NI32 errorcode, NimStringDesc* additionalinfo);
N_NIMCALL(NI32, oslasterror_126034)(void);
STRING_LITERAL(TMP1536, "", 0);
TNimType NTI266208; /* MemFile */
extern TNimType NTI153; /* pointer */
extern TNimType NTI108; /* int */
extern TNimType NTI8211; /* cint */

N_NIMCALL(Memfile266208, open_266807)(NimStringDesc* filename, NU8 mode, NI mappedsize, NI offset, NI newfilesize) {
	Memfile266208 result;
	NIM_BOOL readonly;
	int flags;
	int LOC53;
	NI32 LOC59;
	memset((void*)(&result), 0, sizeof(result));
	readonly = (mode == ((NU8) 0));
	{
		if (!readonly) goto LA3;
		flags = O_RDONLY;
	}
	goto LA1;
	LA3: ;
	{
		flags = O_RDWR;
	}
	LA1: ;
	{
		NI32 permissionsmode;
		if (!!((newfilesize == ((NI) -1)))) goto LA8;
		flags = (NI32)((NI32)(flags | O_CREAT) | O_TRUNC);
		permissionsmode = (NI32)(S_IRUSR | S_IWUSR);
		result.handle = open(filename->data, flags, permissionsmode);
	}
	goto LA6;
	LA8: ;
	{
		result.handle = open(filename->data, flags);
	}
	LA6: ;
	{
		NI32 LOC20;
		if (!(result.handle == ((NI32) -1))) goto LA13;
		result.mem = NIM_NIL;
		result.size = ((NI) 0);
		{
			int LOC19;
			if (!!((result.handle == ((NI32) 0)))) goto LA17;
			LOC19 = 0;
			LOC19 = close(result.handle);
		}
		LA17: ;
		LOC20 = 0;
		LOC20 = oslasterror_126034();
		raiseoserror_126009(LOC20, ((NimStringDesc*) &TMP1536));
	}
	LA13: ;
	{
		if (!!((newfilesize == ((NI) -1)))) goto LA23;
		{
			int LOC27;
			NI32 LOC35;
			LOC27 = 0;
			LOC27 = ftruncate(result.handle, ((off_t) (newfilesize)));
			if (!(LOC27 == ((NI32) -1))) goto LA28;
			result.mem = NIM_NIL;
			result.size = ((NI) 0);
			{
				int LOC34;
				if (!!((result.handle == ((NI32) 0)))) goto LA32;
				LOC34 = 0;
				LOC34 = close(result.handle);
			}
			LA32: ;
			LOC35 = 0;
			LOC35 = oslasterror_126034();
			raiseoserror_126009(LOC35, ((NimStringDesc*) &TMP1536));
		}
		LA28: ;
	}
	LA23: ;
	{
		if (!!((mappedsize == ((NI) -1)))) goto LA38;
		result.size = mappedsize;
	}
	goto LA36;
	LA38: ;
	{
		struct stat stat;
		memset((void*)(&stat), 0, sizeof(stat));
		{
			int LOC43;
			LOC43 = 0;
			LOC43 = fstat(result.handle, (&stat));
			if (!!((LOC43 == ((NI32) -1)))) goto LA44;
			result.size = ((NI) (stat.st_size));
		}
		goto LA41;
		LA44: ;
		{
			NI32 LOC52;
			result.mem = NIM_NIL;
			result.size = ((NI) 0);
			{
				int LOC51;
				if (!!((result.handle == ((NI32) 0)))) goto LA49;
				LOC51 = 0;
				LOC51 = close(result.handle);
			}
			LA49: ;
			LOC52 = 0;
			LOC52 = oslasterror_126034();
			raiseoserror_126009(LOC52, ((NimStringDesc*) &TMP1536));
		}
		LA41: ;
	}
	LA36: ;
	LOC53 = 0;
	{
		if (!readonly) goto LA56;
		LOC53 = PROT_READ;
	}
	goto LA54;
	LA56: ;
	{
		LOC53 = (NI32)(PROT_READ | PROT_WRITE);
	}
	LA54: ;
	LOC59 = 0;
	{
		if (!readonly) goto LA62;
		LOC59 = (NI32)(MAP_PRIVATE | MAP_POPULATE);
	}
	goto LA60;
	LA62: ;
	{
		LOC59 = (NI32)(MAP_SHARED | MAP_POPULATE);
	}
	LA60: ;
	result.mem = mmap(NIM_NIL, result.size, LOC53, LOC59, result.handle, ((off_t) (offset)));
	{
		NI32 LOC74;
		if (!(result.mem == ((void*) (MAP_FAILED)))) goto LA67;
		result.mem = NIM_NIL;
		result.size = ((NI) 0);
		{
			int LOC73;
			if (!!((result.handle == ((NI32) 0)))) goto LA71;
			LOC73 = 0;
			LOC73 = close(result.handle);
		}
		LA71: ;
		LOC74 = 0;
		LOC74 = oslasterror_126034();
		raiseoserror_126009(LOC74, ((NimStringDesc*) &TMP1536));
	}
	LA67: ;
	return result;
}

N_NIMCALL(void, close_267249)(Memfile266208* 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_126034();
		LOC6 = 0;
		LOC7 = 0;
		LOC7 = close((*f).handle);
		LOC6 = !((LOC7 == ((NI32) 0)));
		if (LOC6) goto LA8;
		LOC6 = error;
		LA8: ;
		error = LOC6;
	}
	LA3: ;
	(*f).size = ((NI) 0);
	(*f).mem = NIM_NIL;
	(*f).handle = ((int) 0);
	{
		if (!error) goto LA11;
		raiseoserror_126009(lasterr, ((NimStringDesc*) &TMP1536));
	}
	LA11: ;
}
NIM_EXTERNC N_NOINLINE(void, stdlib_memfilesInit000)(void) {
}

NIM_EXTERNC N_NOINLINE(void, stdlib_memfilesDatInit000)(void) {
static TNimNode* TMP910[3];
static TNimNode TMP896[4];
NTI266208.size = sizeof(Memfile266208);
NTI266208.kind = 18;
NTI266208.base = 0;
NTI266208.flags = 3;
TMP910[0] = &TMP896[1];
TMP896[1].kind = 1;
TMP896[1].offset = offsetof(Memfile266208, mem);
TMP896[1].typ = (&NTI153);
TMP896[1].name = "mem";
TMP910[1] = &TMP896[2];
TMP896[2].kind = 1;
TMP896[2].offset = offsetof(Memfile266208, size);
TMP896[2].typ = (&NTI108);
TMP896[2].name = "size";
TMP910[2] = &TMP896[3];
TMP896[3].kind = 1;
TMP896[3].offset = offsetof(Memfile266208, handle);
TMP896[3].typ = (&NTI8211);
TMP896[3].name = "handle";
TMP896[0].len = 3; TMP896[0].kind = 2; TMP896[0].sons = &TMP910[0];
NTI266208.node = &TMP896[0];
}

