/* Generated by Nim Compiler v2.2.10 */
/* Compiled for: MacOSX, arm64, clang */
/* Command for C compiler:
   clang -c  -w -ferror-limit=3 -fno-strict-aliasing -pthread -isysroot /Applications/Xcode_16.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -target arm64-apple-darwin24.2.0 -O3   -I/Users/runner/work/nightlies/nightlies/nim-2.2.10/lib -I/Users/runner/work/nightlies/nightlies/nim-2.2.10/dist/atlas/src -o /Users/runner/work/nightlies/nightlies/nim-2.2.10/dist/atlas/.nimcache/@pterminal.nim.c.o /Users/runner/work/nightlies/nightlies/nim-2.2.10/dist/atlas/.nimcache/@pterminal.nim.c */
#define NIM_INTBITS 64
#define NIM_EmulateOverflowChecks

#include "nimbase.h"
#include <unistd.h>
#include <string.h>
#include <stdio.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
typedef struct tyObject_PTerminalcolonObjectType___RhpH3E2E8nN9cg4jvP7ixag tyObject_PTerminalcolonObjectType___RhpH3E2E8nN9cg4jvP7ixag;
typedef struct NimStrPayload NimStrPayload;
typedef struct NimStringV2 NimStringV2;
typedef NU8 tyEnum_ForegroundColor__ratYsRpCv8ahCFl8K1T9aJg;
struct NimStrPayload {
	NI cap;
	NIM_CHAR data[SEQ_DECL_SIZE];
};
struct NimStringV2 {
	NI len;
	NimStrPayload* p;
};
typedef NU16 tySet_tyEnum_Style__NVzGhXKu9bTv2XNbUEnVW3A;
typedef NU8 tyEnum_Style__NVzGhXKu9bTv2XNbUEnVW3A;
N_LIB_PRIVATE N_NIMCALL(int, getFileHandle__stdZsyncio_u280)(FILE* f_p0);
static N_INLINE(NIM_BOOL*, nimErrorFlag)(void);
static N_INLINE(NIM_BOOL, nimAddInt)(NI a_p0, NI b_p1, NI* res_p2);
N_LIB_PRIVATE N_NOINLINE(void, raiseOverflow)(void);
N_LIB_PRIVATE N_NIMCALL(void, write__stdZsyncio_u254)(FILE* f_p0, NimStringV2 s_p1);
N_LIB_PRIVATE N_NIMCALL(NimStringV2, ansiStyleCode__pureZterminal_u389)(NI style_p0);
N_NIMCALL(NimStringV2, rawNewString)(NI cap_p0);
N_LIB_PRIVATE N_NIMCALL(void, formatValue__pureZterminal_u264)(NimStringV2* result_p0, NimStringV2 value_p1);
N_LIB_PRIVATE N_NIMCALL(void, formatValue__pureZterminal_u277)(NimStringV2* result_p0, NI value_p1);
static N_INLINE(void, appendString)(NimStringV2* dest_p0, NimStringV2 src_p1);
static N_INLINE(void, copyMem__system_u1755)(void* dest_p0, void* source_p1, NI size_p2);
static N_INLINE(void, nimCopyMem)(void* dest_p0, void* source_p1, NI size_p2);
N_LIB_PRIVATE N_NIMCALL(void, prepareAdd)(NimStringV2* s_p0, NI addLen_p1);
N_LIB_PRIVATE N_NOCONV(void, deallocShared)(void* p_p0);
static const struct {
  NI cap; NIM_CHAR data[2+1];
} TM__CN3raB9atujkEiPpxYAdlgQ_3 = { 2 | NIM_STRLIT_FLAG, "\033[" };
static const NimStringV2 TM__CN3raB9atujkEiPpxYAdlgQ_4 = {2, (NimStrPayload*)&TM__CN3raB9atujkEiPpxYAdlgQ_3};
static const struct {
  NI cap; NIM_CHAR data[1+1];
} TM__CN3raB9atujkEiPpxYAdlgQ_5 = { 1 | NIM_STRLIT_FLAG, "m" };
static const NimStringV2 TM__CN3raB9atujkEiPpxYAdlgQ_6 = {1, (NimStrPayload*)&TM__CN3raB9atujkEiPpxYAdlgQ_5};
static const struct {
  NI cap; NIM_CHAR data[4+1];
} TM__CN3raB9atujkEiPpxYAdlgQ_7 = { 4 | NIM_STRLIT_FLAG, "\033[0m" };
static const NimStringV2 TM__CN3raB9atujkEiPpxYAdlgQ_8 = {4, (NimStrPayload*)&TM__CN3raB9atujkEiPpxYAdlgQ_7};
N_LIB_PRIVATE NIM_THREADVAR tyObject_PTerminalcolonObjectType___RhpH3E2E8nN9cg4jvP7ixag* gTerm__pureZterminal_u10;
N_LIB_PRIVATE NIM_THREADVAR NI gFG__pureZterminal_u372;
N_LIB_PRIVATE NIM_THREADVAR NI gBG__pureZterminal_u373;
extern NIM_THREADVAR NIM_BOOL nimInErrorMode__system_u4198;
static N_INLINE(NIM_BOOL*, nimErrorFlag)(void) {
	NIM_BOOL* result;
	result = (&nimInErrorMode__system_u4198);
	return result;
}
N_LIB_PRIVATE N_NIMCALL(NIM_BOOL, isatty__pureZterminal_u635)(FILE* f_p0) {
	NIM_BOOL result;
	int T1_;
	int T2_;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result = (NIM_BOOL)0;
	T1_ = (int)0;
	T1_ = getFileHandle__stdZsyncio_u280(f_p0);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	T2_ = (int)0;
	T2_ = isatty(T1_);
	result = !((T2_ == ((NI32)0)));
	}BeforeRet_: ;
	return result;
}
static N_INLINE(NIM_BOOL, nimAddInt)(NI a_p0, NI b_p1, NI* res_p2) {
	NIM_BOOL result;
	NI r_1;
	result = (NIM_BOOL)0;
	r_1 = ((NI) ((NU)((NU64)(((NU) (a_p0))) + (NU64)(((NU) (b_p1))))));
	{
		NIM_BOOL T3_;
		T3_ = (NIM_BOOL)0;
		T3_ = (((NI)0) <= (NI)(r_1 ^ a_p0));
		if (T3_) goto LA4_;
		T3_ = (((NI)0) <= (NI)(r_1 ^ b_p1));
LA4_: ;
		if (!T3_) goto LA5_;
		(*res_p2) = r_1;
	}
	goto LA1_;
LA5_: ;
	{
		result = NIM_TRUE;
	}
LA1_: ;
	return result;
}
static N_INLINE(void, nimCopyMem)(void* dest_p0, void* source_p1, NI size_p2) {
	void* T1_;
	T1_ = (void*)0;
	T1_ = memcpy(dest_p0, source_p1, ((size_t) (size_p2)));
}
static N_INLINE(void, copyMem__system_u1755)(void* dest_p0, void* source_p1, NI size_p2) {
	nimCopyMem(dest_p0, source_p1, size_p2);
}
static N_INLINE(void, appendString)(NimStringV2* dest_p0, NimStringV2 src_p1) {
	{
		if (!(((NI)0) < src_p1.len)) goto LA3_;
		copyMem__system_u1755(((void*) ((&(*(*dest_p0).p).data[(*dest_p0).len]))), ((void*) ((&(*src_p1.p).data[((NI)0)]))), (src_p1.len));
		(*dest_p0).len += src_p1.len;
		(*(*dest_p0).p).data[(*dest_p0).len] = 0;
	}
LA3_: ;
}
N_LIB_PRIVATE N_NIMCALL(NimStringV2, ansiStyleCode__pureZterminal_u389)(NI style_p0) {
	NimStringV2 result;
	NimStringV2 T1_;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result.len = 0; result.p = NIM_NIL;
	T1_.len = 0; T1_.p = NIM_NIL;
	{
		NimStringV2 fmtRes_1;
		fmtRes_1.len = 0; fmtRes_1.p = NIM_NIL;
		fmtRes_1 = rawNewString(((NI)41));
		formatValue__pureZterminal_u264((&fmtRes_1), TM__CN3raB9atujkEiPpxYAdlgQ_4);
		if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
		formatValue__pureZterminal_u277((&fmtRes_1), style_p0);
		if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
		prepareAdd((&fmtRes_1), 1);
appendString((&fmtRes_1), TM__CN3raB9atujkEiPpxYAdlgQ_6);
		result = fmtRes_1;
	}
	}BeforeRet_: ;
	return result;
}
N_LIB_PRIVATE N_NIMCALL(void, setForegroundColor__pureZterminal_u509)(FILE* f_p0, tyEnum_ForegroundColor__ratYsRpCv8ahCFl8K1T9aJg fg_p1, NIM_BOOL bright_p2) {
	NimStringV2 colontmpD_;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	colontmpD_.len = 0; colontmpD_.p = NIM_NIL;
	gFG__pureZterminal_u372 = fg_p1;
	{
		NI TM__CN3raB9atujkEiPpxYAdlgQ_2;
		if (!bright_p2) goto LA4_;
		if (nimAddInt(gFG__pureZterminal_u372, ((NI)60), &TM__CN3raB9atujkEiPpxYAdlgQ_2)) { raiseOverflow(); goto LA1_;
		};
		gFG__pureZterminal_u372 = (NI)(TM__CN3raB9atujkEiPpxYAdlgQ_2);
	}
LA4_: ;
	colontmpD_ = ansiStyleCode__pureZterminal_u389(gFG__pureZterminal_u372);
	if (NIM_UNLIKELY(*nimErr_)) goto LA1_;
	write__stdZsyncio_u254(f_p0, colontmpD_);
	if (NIM_UNLIKELY(*nimErr_)) goto LA1_;
	{
		LA1_:;
	}
	{
		if (colontmpD_.p && !(colontmpD_.p->cap & NIM_STRLIT_FLAG)) {
 deallocShared(colontmpD_.p);
}
	}
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	}BeforeRet_: ;
}
N_LIB_PRIVATE N_NIMCALL(void, setStyle__pureZterminal_u413)(FILE* f_p0, tySet_tyEnum_Style__NVzGhXKu9bTv2XNbUEnVW3A style_p1) {
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	{
		tyEnum_Style__NVzGhXKu9bTv2XNbUEnVW3A s_1;
		NI i_1;
		s_1 = (tyEnum_Style__NVzGhXKu9bTv2XNbUEnVW3A)0;
		i_1 = ((NI)1);
		{
			while (1) {
				if (!(i_1 <= ((NI)9))) goto LA3;
				{
					NimStringV2 colontmpD_;
					if (!((style_p1 &((NU16)1<<((NU)((((tyEnum_Style__NVzGhXKu9bTv2XNbUEnVW3A) (i_1)) - 1))&15U)))!=0)) goto LA6_;
					colontmpD_.len = 0; colontmpD_.p = NIM_NIL;
					s_1 = ((tyEnum_Style__NVzGhXKu9bTv2XNbUEnVW3A) (i_1));
					colontmpD_ = ansiStyleCode__pureZterminal_u389(((NI) (s_1)));
					if (NIM_UNLIKELY(*nimErr_)) goto LA8_;
					write__stdZsyncio_u254(f_p0, colontmpD_);
					if (NIM_UNLIKELY(*nimErr_)) goto LA8_;
					{
						LA8_:;
					}
					{
						if (colontmpD_.p && !(colontmpD_.p->cap & NIM_STRLIT_FLAG)) {
 deallocShared(colontmpD_.p);
}
					}
					if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
				}
LA6_: ;
				i_1 += ((NI)1);
			} LA3: ;
		}
	}
	}BeforeRet_: ;
}
N_LIB_PRIVATE N_NIMCALL(void, resetAttributes__pureZterminal_u374)(FILE* f_p0) {
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	write__stdZsyncio_u254(f_p0, TM__CN3raB9atujkEiPpxYAdlgQ_8);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	gFG__pureZterminal_u372 = ((NI)0);
	gBG__pureZterminal_u373 = ((NI)0);
	}BeforeRet_: ;
}
