/* Generated by Nim Compiler v0.13.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 Memfile251202 Memfile251202;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
struct  Memfile251202  {
void* mem;
NI size;
int handle;
};
typedef N_NIMCALL_PTR(void, TY3489) (void* p, NI op);
typedef N_NIMCALL_PTR(void*, TY3494) (void* p);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY3489 marker;
TY3494 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_120803)(NI32 errorcode, NimStringDesc* additionalinfo);
N_NIMCALL(NI32, oslasterror_120819)(void);
STRING_LITERAL(TMP1570, "", 0);
TNimType NTI251202; /* MemFile */
extern TNimType NTI142; /* pointer */
extern TNimType NTI104; /* int */
extern TNimType NTI7208; /* cint */

N_NIMCALL(Memfile251202, open_251801)(NimStringDesc* filename, NU8 mode, NI mappedsize, NI offset, NI newfilesize) {
	Memfile251202 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_120819();
		raiseoserror_120803(LOC20, ((NimStringDesc*) &TMP1570));
	}
	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_120819();
			raiseoserror_120803(LOC35, ((NimStringDesc*) &TMP1570));
		}
		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_120819();
			raiseoserror_120803(LOC52, ((NimStringDesc*) &TMP1570));
		}
		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_120819();
		raiseoserror_120803(LOC74, ((NimStringDesc*) &TMP1570));
	}
	LA67: ;
	return result;
}

N_NIMCALL(void, close_252243)(Memfile251202* 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_120819();
		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_120803(lasterr, ((NimStringDesc*) &TMP1570));
	}
	LA11: ;
}
NIM_EXTERNC N_NOINLINE(void, stdlib_memfilesInit000)(void) {
}

NIM_EXTERNC N_NOINLINE(void, stdlib_memfilesDatInit000)(void) {
static TNimNode* TMP914[3];
static TNimNode TMP900[4];
NTI251202.size = sizeof(Memfile251202);
NTI251202.kind = 18;
NTI251202.base = 0;
NTI251202.flags = 3;
TMP914[0] = &TMP900[1];
TMP900[1].kind = 1;
TMP900[1].offset = offsetof(Memfile251202, mem);
TMP900[1].typ = (&NTI142);
TMP900[1].name = "mem";
TMP914[1] = &TMP900[2];
TMP900[2].kind = 1;
TMP900[2].offset = offsetof(Memfile251202, size);
TMP900[2].typ = (&NTI104);
TMP900[2].name = "size";
TMP914[2] = &TMP900[3];
TMP900[3].kind = 1;
TMP900[3].offset = offsetof(Memfile251202, handle);
TMP900[3].typ = (&NTI7208);
TMP900[3].name = "handle";
TMP900[0].len = 3; TMP900[0].kind = 2; TMP900[0].sons = &TMP914[0];
NTI251202.node = &TMP900[0];
}

