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

#include "nimbase.h"
#include <string.h>
#include <stdio.h>
#include <io.h>
#undef linux
typedef struct TGenericSeq TGenericSeq;
typedef struct NimStringDesc NimStringDesc;
typedef struct Consolescreenbufferinfo_r6d5R6U7A9aP1xHUbo2o04A Consolescreenbufferinfo_r6d5R6U7A9aP1xHUbo2o04A;
typedef struct Coord_38Oq1GgFPIYv2ksRi9c0fZA Coord_38Oq1GgFPIYv2ksRi9c0fZA;
typedef struct Smallrect_GSDbaE1SdabWptSZYJG3XA Smallrect_GSDbaE1SdabWptSZYJG3XA;
typedef N_STDCALL_PTR(NI, TY_zP2zGemcuVRvOUE82f9a0Pw) (NI32 nstdhandle0);
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
typedef N_STDCALL_PTR(NI32, TY_oew1DfaGl5XhHmnBK0HYXQ) (NI hsourceprocesshandle0, NI hsourcehandle0, NI htargetprocesshandle0, NI* lptargethandle0, NI32 dwdesiredaccess0, NI32 binherithandle0, NI32 dwoptions0);
typedef N_STDCALL_PTR(NI, TY_uryidYWkntM7ddjZSyxvyQ) (void);
struct  Coord_38Oq1GgFPIYv2ksRi9c0fZA  {
NI16 X;
NI16 Y;
};
struct  Smallrect_GSDbaE1SdabWptSZYJG3XA  {
NI16 Left;
NI16 Top;
NI16 Right;
NI16 Bottom;
};
struct  Consolescreenbufferinfo_r6d5R6U7A9aP1xHUbo2o04A  {
Coord_38Oq1GgFPIYv2ksRi9c0fZA dwsize;
Coord_38Oq1GgFPIYv2ksRi9c0fZA dwcursorposition;
NI16 wattributes;
Smallrect_GSDbaE1SdabWptSZYJG3XA srwindow;
Coord_38Oq1GgFPIYv2ksRi9c0fZA dwmaximumwindowsize;
};
typedef N_STDCALL_PTR(NI32, TY_vjGHiWUJwFzOgSxDffBb6Q) (NI hconsoleoutput0, Consolescreenbufferinfo_r6d5R6U7A9aP1xHUbo2o04A* lpconsolescreenbufferinfo0);
typedef NU8 Style_NXKayqTLMsAibFwNceBlpA_Set;
typedef NU8 Style_NXKayqTLMsAibFwNceBlpA;
typedef N_STDCALL_PTR(NI32, TY_oPCjp4xTgEl9a8fqSzOv8lw) (NI hconsoleoutput0, NI16 wattributes0);
typedef NU8 Foregroundcolor_ez9ah4IgQc0AbdW68acioDg;
typedef NI TY_e7dTfxuKO1YfQGLA3anCHg[8];
N_NIMCALL(void*, nimLoadLibrary)(NimStringDesc* path0);
N_NOINLINE(void, nimLoadLibraryError)(NimStringDesc* path0);
N_NIMCALL(void*, nimGetProcAddr)(void* lib0, NCSTRING name0);
N_NIMCALL(NI16, getattributes_PnaHeFPQ8QlJWgnEjaRyCg)(NI h0);
N_NIMCALL(int, getfilehandle_bZ9c2yojtXoDTUpfyl8h8Rg)(FILE* f0);
NI hstdout_Q6LBG8sK28zNgldP5VHBBA;
NI hstderr_6C9cOGHD37RMptVLFSz9ckEQ;
NI hstdouttemp_Frgc1hZhHtHmhQpuiOhypw;
extern TY_zP2zGemcuVRvOUE82f9a0Pw Dl_119820;
static void* T_CN3raB9atujkEiPpxYAdlgQ_2;
TY_oew1DfaGl5XhHmnBK0HYXQ Dl_190038;
TY_uryidYWkntM7ddjZSyxvyQ Dl_190048;
NI hstderrtemp_lkKJFEg9bMgFJ9bv6dAcceSQ;
NI16 oldstdoutattr_vkHOZle9bmebv77fjQpEGqw;
TY_vjGHiWUJwFzOgSxDffBb6Q Dl_190050;
NI16 oldstderrattr_ZXyw45dCi8lkCMLSUcaIBw;
TY_oPCjp4xTgEl9a8fqSzOv8lw Dl_190160;
STRING_LITERAL(T_CN3raB9atujkEiPpxYAdlgQ_4, "kernel32", 8);
STRING_LITERAL(T_CN3raB9atujkEiPpxYAdlgQ_5, "kernel32", 8);
NIM_CONST TY_e7dTfxuKO1YfQGLA3anCHg lookup_iBTamPHB6H8lJIzDyv8epw = {((NI) 0),
((NI) 4),
((NI) 2),
((NI) 6),
((NI) 1),
((NI) 5),
((NI) 3),
((NI) 7)}
;

N_NIMCALL(NI16, getattributes_PnaHeFPQ8QlJWgnEjaRyCg)(NI h0) {
	NI16 result0;
	Consolescreenbufferinfo_r6d5R6U7A9aP1xHUbo2o04A c0;
{	result0 = (NI16)0;
	memset((void*)(&c0), 0, sizeof(c0));
	{
		NI32 LOC3;
		LOC3 = (NI32)0;
		LOC3 = Dl_190050(h0, (&c0));
		if (!!((LOC3 == ((NI32) 0)))) goto LA4;
		result0 = c0.wattributes;
		goto BeforeRet;
	}
	LA4: ;
	result0 = ((NI16) 112);
	goto BeforeRet;
	}BeforeRet: ;
	return result0;
}

N_NIMCALL(void, setstyle_GclWadYUouE9aXrU2KZQtoQ)(FILE* f0, Style_NXKayqTLMsAibFwNceBlpA_Set style0) {
	NI h0;
	NI16 old0;
	NI16 LOC6;
	NI16 a0;
	NI32 LOC23;
	{
		if (!(f0 == stderr)) goto LA3;
		h0 = hstderr_6C9cOGHD37RMptVLFSz9ckEQ;
	}
	goto LA1;
	LA3: ;
	{
		h0 = hstdout_Q6LBG8sK28zNgldP5VHBBA;
	}
	LA1: ;
	LOC6 = (NI16)0;
	LOC6 = getattributes_PnaHeFPQ8QlJWgnEjaRyCg(h0);
	old0 = (NI16)(LOC6 & ((NI16) 119));
	a0 = ((NI16) 0);
	{
		if (!((style0 &(1U<<((NU)((((Style_NXKayqTLMsAibFwNceBlpA) 1)- 1))&7U)))!=0)) goto LA9;
		a0 = (NI16)(a0 | ((NI16) 8));
	}
	LA9: ;
	{
		if (!((style0 &(1U<<((NU)((((Style_NXKayqTLMsAibFwNceBlpA) 5)- 1))&7U)))!=0)) goto LA13;
		a0 = (NI16)(a0 | ((NI16) 128));
	}
	LA13: ;
	{
		if (!((style0 &(1U<<((NU)((((Style_NXKayqTLMsAibFwNceBlpA) 7)- 1))&7U)))!=0)) goto LA17;
		a0 = (NI16)(a0 | ((NI16) 16384));
	}
	LA17: ;
	{
		if (!((style0 &(1U<<((NU)((((Style_NXKayqTLMsAibFwNceBlpA) 4)- 1))&7U)))!=0)) goto LA21;
		a0 = (NI16)(a0 | ((NI16) -32768));
	}
	LA21: ;
	LOC23 = (NI32)0;
	LOC23 = Dl_190160(h0, (NI16)(old0 | a0));
}

N_NIMCALL(void, resetattributes_6DYwjcHkAz0BG9bT24wH4dw)(FILE* f0) {
	{
		NI32 LOC5;
		if (!(f0 == stderr)) goto LA3;
		LOC5 = (NI32)0;
		LOC5 = Dl_190160(hstderr_6C9cOGHD37RMptVLFSz9ckEQ, oldstderrattr_ZXyw45dCi8lkCMLSUcaIBw);
	}
	goto LA1;
	LA3: ;
	{
		NI32 LOC7;
		LOC7 = (NI32)0;
		LOC7 = Dl_190160(hstdout_Q6LBG8sK28zNgldP5VHBBA, oldstdoutattr_vkHOZle9bmebv77fjQpEGqw);
	}
	LA1: ;
}

N_NIMCALL(void, setforegroundcolor_Tw7yxMupneOmAicqiYt3NA)(FILE* f0, Foregroundcolor_ez9ah4IgQc0AbdW68acioDg fg0, NIM_BOOL bright0) {
	NI h0;
	NI16 old0;
	NI16 LOC6;
	NI32 LOC11;
	{
		if (!(f0 == stderr)) goto LA3;
		h0 = hstderr_6C9cOGHD37RMptVLFSz9ckEQ;
	}
	goto LA1;
	LA3: ;
	{
		h0 = hstdout_Q6LBG8sK28zNgldP5VHBBA;
	}
	LA1: ;
	LOC6 = (NI16)0;
	LOC6 = getattributes_PnaHeFPQ8QlJWgnEjaRyCg(h0);
	old0 = (NI16)(LOC6 & ((NI16) -8));
	{
		if (!bright0) goto LA9;
		old0 = (NI16)(old0 | ((NI16) 8));
	}
	LA9: ;
	LOC11 = (NI32)0;
	LOC11 = Dl_190160(h0, ((NI16)(NU16)(NU)((NI)(((NI) (old0)) | lookup_iBTamPHB6H8lJIzDyv8epw[(fg0)- 30]))));
}

N_NIMCALL(NIM_BOOL, isatty_POoHvosCWg3Yl5ieYeLHrg)(FILE* f0) {
	NIM_BOOL result0;
	int LOC1;
	int LOC2;
	result0 = (NIM_BOOL)0;
	LOC1 = (int)0;
	LOC1 = getfilehandle_bZ9c2yojtXoDTUpfyl8h8Rg(f0);
	LOC2 = (int)0;
	LOC2 = _isatty(LOC1);
	result0 = !((LOC2 == ((NI32) 0)));
	return result0;
}
NIM_EXTERNC N_NOINLINE(void, stdlib_terminalInit000)(void) {
	{
		hstdouttemp_Frgc1hZhHtHmhQpuiOhypw = Dl_119820(((NI32) -11));
		{
			NI LOC4;
			NI LOC5;
			NI32 LOC6;
			LOC4 = (NI)0;
			LOC4 = Dl_190048();
			LOC5 = (NI)0;
			LOC5 = Dl_190048();
			LOC6 = (NI32)0;
			LOC6 = Dl_190038(LOC4, hstdouttemp_Frgc1hZhHtHmhQpuiOhypw, LOC5, (&hstdout_Q6LBG8sK28zNgldP5VHBBA), ((NI32) 0), ((NI32) 1), ((NI32) 2));
			if (!(LOC6 == ((NI32) 0))) goto LA7;
		}
		LA7: ;
		hstderrtemp_lkKJFEg9bMgFJ9bv6dAcceSQ = Dl_119820(((NI32) -12));
		{
			NI LOC11;
			NI LOC12;
			NI32 LOC13;
			LOC11 = (NI)0;
			LOC11 = Dl_190048();
			LOC12 = (NI)0;
			LOC12 = Dl_190048();
			LOC13 = (NI32)0;
			LOC13 = Dl_190038(LOC11, hstderrtemp_lkKJFEg9bMgFJ9bv6dAcceSQ, LOC12, (&hstderr_6C9cOGHD37RMptVLFSz9ckEQ), ((NI32) 0), ((NI32) 1), ((NI32) 2));
			if (!(LOC13 == ((NI32) 0))) goto LA14;
		}
		LA14: ;
	}
	oldstdoutattr_vkHOZle9bmebv77fjQpEGqw = getattributes_PnaHeFPQ8QlJWgnEjaRyCg(hstdout_Q6LBG8sK28zNgldP5VHBBA);
	oldstderrattr_ZXyw45dCi8lkCMLSUcaIBw = getattributes_PnaHeFPQ8QlJWgnEjaRyCg(hstderr_6C9cOGHD37RMptVLFSz9ckEQ);
}

NIM_EXTERNC N_NOINLINE(void, stdlib_terminalDatInit000)(void) {
if (!((T_CN3raB9atujkEiPpxYAdlgQ_2 = nimLoadLibrary((NimStringDesc*) &T_CN3raB9atujkEiPpxYAdlgQ_4))
)) nimLoadLibraryError((NimStringDesc*) &T_CN3raB9atujkEiPpxYAdlgQ_5);
	Dl_190038 = (TY_oew1DfaGl5XhHmnBK0HYXQ) nimGetProcAddr(T_CN3raB9atujkEiPpxYAdlgQ_2, "DuplicateHandle");
	Dl_190048 = (TY_uryidYWkntM7ddjZSyxvyQ) nimGetProcAddr(T_CN3raB9atujkEiPpxYAdlgQ_2, "GetCurrentProcess");
	Dl_190050 = (TY_vjGHiWUJwFzOgSxDffBb6Q) nimGetProcAddr(T_CN3raB9atujkEiPpxYAdlgQ_2, "GetConsoleScreenBufferInfo");
	Dl_190160 = (TY_oPCjp4xTgEl9a8fqSzOv8lw) nimGetProcAddr(T_CN3raB9atujkEiPpxYAdlgQ_2, "SetConsoleTextAttribute");
}

