/* Generated by Nim Compiler v1.6.6 */
#define NIM_INTBITS 32

#include "nimbase.h"
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#undef LANGUAGE_C
#undef MIPSEB
#undef MIPSEL
#undef PPC
#undef R3000
#undef R4000
#undef i386
#undef linux
#undef mips
#undef near
#undef far
#undef powerpc
#undef unix
#define nimfr_(x, y)
#define nimln_(x, y)
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct tySequence__9aKpvUh0ca6Bp9cKAwrowqUg tySequence__9aKpvUh0ca6Bp9cKAwrowqUg;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
struct TGenericSeq {
NI len;
NI reserved;
};
struct NimStringDesc {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
typedef NU8 tyEnum_TNimKind__jIBKr1ejBgsfM33Kxw4j7A;
typedef NU8 tySet_tyEnum_TNimTypeFlag__v8QUszD1sWlSIWZz7mC4bQ;
typedef N_NIMCALL_PTR(void, tyProc__ojoeKfW4VYIm36I9cpDTQIg) (void* p, NI op);
typedef N_NIMCALL_PTR(void*, tyProc__WSm2xU5ARYv9aAR4l0z9c9auQ) (void* p);
struct TNimType {
NI size;
NI align;
tyEnum_TNimKind__jIBKr1ejBgsfM33Kxw4j7A kind;
tySet_tyEnum_TNimTypeFlag__v8QUszD1sWlSIWZz7mC4bQ flags;
TNimType* base;
TNimNode* node;
void* finalizer;
tyProc__ojoeKfW4VYIm36I9cpDTQIg marker;
tyProc__WSm2xU5ARYv9aAR4l0z9c9auQ deepcopy;
};
typedef NU8 tyEnum_TNimNodeKind__unfNsxrcATrufDZmpBq4HQ;
struct TNimNode {
tyEnum_TNimNodeKind__unfNsxrcATrufDZmpBq4HQ kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
typedef NI16 tyUncheckedArray__Tyd4y3haUOOHTj71TPIRag[1];
typedef N_STDCALL_PTR(NI32, tyProc__3pVglHKolpBa6UoJifpHkA) (NCSTRING lpName, NCSTRING lpValue);
struct tySequence__9aKpvUh0ca6Bp9cKAwrowqUg {
  TGenericSeq Sup;
  NI16 data[SEQ_DECL_SIZE];
};
static N_INLINE(NIM_BOOL, eqeq___system_7778)(NCSTRING x, NCSTRING y);
static N_INLINE(NCSTRING, nimToCStringConv)(NimStringDesc* s);
static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src);
static N_INLINE(void, copyMem__system_1727)(void* dest, void* source, NI size);
static N_INLINE(void, nimCopyMem)(void* dest, void* source, NI size);
N_LIB_PRIVATE N_NIMCALL(NimStringDesc*, rawNewString)(NI space);
N_LIB_PRIVATE N_NIMCALL(tySequence__9aKpvUh0ca6Bp9cKAwrowqUg*, newSeq__stdZprivateZwin95setenv_36)(NI len);
static N_NIMCALL(void, Marker_tySequence__9aKpvUh0ca6Bp9cKAwrowqUg)(void* p, NI op);
N_LIB_PRIVATE N_NIMCALL(void*, nimLoadLibrary)(NimStringDesc* path);
N_LIB_PRIVATE N_NIMCALL(void, nimLoadLibraryError)(NimStringDesc* path);
N_LIB_PRIVATE N_NIMCALL(void*, nimGetProcAddr)(void* lib, NCSTRING name);
STRING_LITERAL(TM__UOYsq9cbb0VE8vr3Xt181HA_2, "=", 1);
STRING_LITERAL(TM__UOYsq9cbb0VE8vr3Xt181HA_3, "=  ", 3);
extern TNimType NTIint4954__kDPg4wXyR8DDyA0MeEjIsw_;
N_LIB_PRIVATE TNimType NTIseqLutf4954charT__9aKpvUh0ca6Bp9cKAwrowqUg_;
STRING_LITERAL(TM__UOYsq9cbb0VE8vr3Xt181HA_6, "kernel32", 8);
STRING_LITERAL(TM__UOYsq9cbb0VE8vr3Xt181HA_7, "kernel32", 8);
extern wchar_t** _wenviron;
static void* TM__UOYsq9cbb0VE8vr3Xt181HA_4;
tyProc__3pVglHKolpBa6UoJifpHkA Dl_1426063362_;
static N_INLINE(NIM_BOOL, eqeq___system_7778)(NCSTRING x, NCSTRING y) {
	NIM_BOOL result;
	result = (NIM_BOOL)0;
	{
		if (!(((void*) (x)) == ((void*) (y)))) goto LA3_;
		result = NIM_TRUE;
	}
	goto LA1_;
	LA3_: ;
	{
		NIM_BOOL T6_;
		T6_ = (NIM_BOOL)0;
		T6_ = (x == 0);
		if (T6_) goto LA7_;
		T6_ = (y == 0);
		LA7_: ;
		if (!T6_) goto LA8_;
		result = NIM_FALSE;
	}
	goto LA1_;
	LA8_: ;
	{
		int T11_;
		T11_ = (int)0;
		T11_ = strcmp(x, y);
		result = (T11_ == ((NI32) 0));
	}
	LA1_: ;
	return result;
}
static N_INLINE(NCSTRING, nimToCStringConv)(NimStringDesc* s) {
	NCSTRING result;
	result = (NCSTRING)0;
	{
		NIM_BOOL T3_;
		T3_ = (NIM_BOOL)0;
		T3_ = (s == ((NimStringDesc*) NIM_NIL));
		if (T3_) goto LA4_;
		T3_ = ((*s).Sup.len == ((NI) 0));
		LA4_: ;
		if (!T3_) goto LA5_;
		result = "";
	}
	goto LA1_;
	LA5_: ;
	{
		result = ((NCSTRING) ((*s).data));
	}
	LA1_: ;
	return result;
}
static N_INLINE(void, nimCopyMem)(void* dest, void* source, NI size) {
	void* T1_;
	T1_ = (void*)0;
	T1_ = memcpy(dest, source, ((size_t) (size)));
}
static N_INLINE(void, copyMem__system_1727)(void* dest, void* source, NI size) {
	nimCopyMem(dest, source, size);
}
static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src) {
	{
		if (!!((src == ((NimStringDesc*) NIM_NIL)))) goto LA3_;
		copyMem__system_1727(((void*) ((&(*dest).data[(*dest).Sup.len]))), ((void*) ((*src).data)), ((NI) ((NI)((*src).Sup.len + ((NI) 1)))));
		(*dest).Sup.len += (*src).Sup.len;
	}
	LA3_: ;
}
static N_NIMCALL(void, Marker_tySequence__9aKpvUh0ca6Bp9cKAwrowqUg)(void* p, NI op) {
	tySequence__9aKpvUh0ca6Bp9cKAwrowqUg* a;
	NI T1_;
	a = (tySequence__9aKpvUh0ca6Bp9cKAwrowqUg*)p;
	T1_ = (NI)0;
}
N_LIB_PRIVATE N_NIMCALL(int, setEnvImpl__stdZprivateZwin95setenv_17)(NimStringDesc* name, NimStringDesc* value, int overwrite) {
	int result;
	NimStringDesc* envstring_2;
	NimStringDesc* T18_;
	NCSTRING s;
{	result = (int)0;
	{
		NIM_BOOL T3_;
		NCSTRING T5_;
		NIM_BOOL T6_;
		T3_ = (NIM_BOOL)0;
		T3_ = (overwrite == ((NI32) 0));
		if (!(T3_)) goto LA4_;
		T5_ = (NCSTRING)0;
		T5_ = getenv(nimToCStringConv(name));
		T6_ = (NIM_BOOL)0;
		T6_ = eqeq___system_7778(T5_, ((NCSTRING) NIM_NIL));
		T3_ = !(T6_);
		LA4_: ;
		if (!T3_) goto LA7_;
		result = ((int) 0);
		goto BeforeRet_;
	}
	LA7_: ;
	{
		NimStringDesc* envstring;
		NimStringDesc* T13_;
		int e;
		if (!!(((value ? value->Sup.len : 0) == 0))) goto LA11_;
		T13_ = NIM_NIL;
		T13_ = rawNewString((name ? name->Sup.len : 0) + (value ? value->Sup.len : 0) + 1);
appendString(T13_, name);
appendString(T13_, ((NimStringDesc*) &TM__UOYsq9cbb0VE8vr3Xt181HA_2));
appendString(T13_, value);
		envstring = T13_;
		e = _putenv(nimToCStringConv(envstring));
		{
			if (!!((e == ((NI32) 0)))) goto LA16_;
			errno = ((int) 22);
			result = ((int) -1);
			goto BeforeRet_;
		}
		LA16_: ;
		result = ((int) 0);
		goto BeforeRet_;
	}
	LA11_: ;
	T18_ = NIM_NIL;
	T18_ = rawNewString((name ? name->Sup.len : 0) + 3);
appendString(T18_, name);
appendString(T18_, ((NimStringDesc*) &TM__UOYsq9cbb0VE8vr3Xt181HA_3));
	envstring_2 = T18_;
	{
		int T21_;
		T21_ = (int)0;
		T21_ = _putenv(nimToCStringConv(envstring_2));
		if (!!((T21_ == ((NI32) 0)))) goto LA22_;
		errno = ((int) 22);
		result = ((int) -1);
		goto BeforeRet_;
	}
	LA22_: ;
	s = getenv(nimToCStringConv(name));
	s[((NI) 0)] = 0;
	s = getenv(nimToCStringConv(name));
	s[((NI) 1)] = 61;
	{
		NI requiredSize;
		size_t T28_;
		tySequence__9aKpvUh0ca6Bp9cKAwrowqUg* buf;
		wchar_t* buf2;
		NI16* ptrToEnv;
		wchar_t* T34_;
		wchar_t* T35_;
		if (!!((_wenviron == ((wchar_t**) NIM_NIL)))) goto LA26_;
		T28_ = (size_t)0;
		T28_ = mbstowcs(((wchar_t*) NIM_NIL), nimToCStringConv(name), ((size_t) 0));
		requiredSize = ((NI) (T28_));
		buf = newSeq__stdZprivateZwin95setenv_36(((NI) ((NI)(requiredSize + ((NI) 1)))));
		buf2 = ((wchar_t*) ((&buf->data[((NI) 0)])));
		{
			size_t T31_;
			T31_ = (size_t)0;
			T31_ = mbstowcs(buf2, nimToCStringConv(name), ((size_t) ((NI)(requiredSize + ((NI) 1)))));
			if (!(T31_ == ((size_t) IL64(4294967295)))) goto LA32_;
			errno = ((int) 22);
			result = ((int) -1);
			goto BeforeRet_;
		}
		LA32_: ;
		T34_ = (wchar_t*)0;
		T34_ = _wgetenv(buf2);
		ptrToEnv = ((NI16*) (T34_));
		ptrToEnv[((NI) 0)] = ((NI16) 0);
		T35_ = (wchar_t*)0;
		T35_ = _wgetenv(buf2);
		ptrToEnv = ((NI16*) (T35_));
		ptrToEnv[((NI) 1)] = ((NI16) 61);
	}
	LA26_: ;
	{
		NI32 T38_;
		T38_ = (NI32)0;
		T38_ = Dl_1426063362_(nimToCStringConv(name), nimToCStringConv(value));
		if (!(T38_ == ((NI32) 0))) goto LA39_;
		errno = ((int) 22);
		result = ((int) -1);
		goto BeforeRet_;
	}
	LA39_: ;
	result = ((int) 0);
	goto BeforeRet_;
	}BeforeRet_: ;
	return result;
}
N_LIB_PRIVATE N_NIMCALL(void, stdlib_win_setenvDatInit000)(void) {
NTIseqLutf4954charT__9aKpvUh0ca6Bp9cKAwrowqUg_.size = sizeof(tySequence__9aKpvUh0ca6Bp9cKAwrowqUg*);
NTIseqLutf4954charT__9aKpvUh0ca6Bp9cKAwrowqUg_.align = NIM_ALIGNOF(tySequence__9aKpvUh0ca6Bp9cKAwrowqUg*);
NTIseqLutf4954charT__9aKpvUh0ca6Bp9cKAwrowqUg_.kind = 24;
NTIseqLutf4954charT__9aKpvUh0ca6Bp9cKAwrowqUg_.base = (&NTIint4954__kDPg4wXyR8DDyA0MeEjIsw_);
NTIseqLutf4954charT__9aKpvUh0ca6Bp9cKAwrowqUg_.marker = Marker_tySequence__9aKpvUh0ca6Bp9cKAwrowqUg;
if (!((TM__UOYsq9cbb0VE8vr3Xt181HA_4 = nimLoadLibrary(((NimStringDesc*) &TM__UOYsq9cbb0VE8vr3Xt181HA_6)))
)) nimLoadLibraryError(((NimStringDesc*) &TM__UOYsq9cbb0VE8vr3Xt181HA_7));
	Dl_1426063362_ = (tyProc__3pVglHKolpBa6UoJifpHkA) nimGetProcAddr(TM__UOYsq9cbb0VE8vr3Xt181HA_4, "SetEnvironmentVariableA");
}

