/* 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/@prandom.nim.c.o /Users/runner/work/nightlies/nightlies/nim-2.2.10/dist/atlas/.nimcache/@prandom.nim.c */
#define NIM_INTBITS 64
#define NIM_EmulateOverflowChecks

#include "nimbase.h"
#include <sys/types.h>
                          #include <pthread.h>
#include <pthread.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_Rand__RKGKOUprONAbkVAKotpsfw tyObject_Rand__RKGKOUprONAbkVAKotpsfw;
struct tyObject_Rand__RKGKOUprONAbkVAKotpsfw {
	NU64 a0;
	NU64 a1;
};
static N_INLINE(void, initLock__coreZlocks_u7)(pthread_mutex_t* lock_p0);
static N_INLINE(NIM_BOOL*, nimErrorFlag)(void);
N_LIB_PRIVATE N_NIMCALL(NI, rand__pureZrandom_u95)(tyObject_Rand__RKGKOUprONAbkVAKotpsfw* r_p0, NI max_p1);
N_LIB_PRIVATE N_NIMCALL(NU64, rand__pureZrandom_u99)(tyObject_Rand__RKGKOUprONAbkVAKotpsfw* r_p0, NU64 max_p1);
N_LIB_PRIVATE N_NIMCALL(NU64, next__pureZrandom_u36)(tyObject_Rand__RKGKOUprONAbkVAKotpsfw* r_p0);
N_LIB_PRIVATE N_NIMCALL(NU64, rotl__pureZrandom_u24)(NU64 x_p0, NU64 k_p1);
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_NOINLINE(void, raiseRangeErrorI)(NI64 i_p0, NI64 a_p1, NI64 b_p2);
N_LIB_PRIVATE N_NIMCALL(void, nimTestErrorFlag)(void);
N_LIB_PRIVATE NIM_CONST tyObject_Rand__RKGKOUprONAbkVAKotpsfw DefaultRandSeed__pureZrandom_u14 = {7616934476015405061ULL, 15122295919272093642ULL}
;
N_LIB_PRIVATE tyObject_Rand__RKGKOUprONAbkVAKotpsfw state__pureZrandom_u15;
N_LIB_PRIVATE pthread_mutex_t baseSeedLock__pureZrandom_u294;
N_LIB_PRIVATE tyObject_Rand__RKGKOUprONAbkVAKotpsfw baseState__pureZrandom_u295;
extern NIM_THREADVAR NIM_BOOL nimInErrorMode__system_u4198;
static N_INLINE(void, initLock__coreZlocks_u7)(pthread_mutex_t* lock_p0) {
	pthread_mutex_init(lock_p0, ((pthread_mutexattr_t*) NIM_NIL));
}
static N_INLINE(NIM_BOOL*, nimErrorFlag)(void) {
	NIM_BOOL* result;
	result = (&nimInErrorMode__system_u4198);
	return result;
}
N_LIB_PRIVATE N_NIMCALL(NU64, rotl__pureZrandom_u24)(NU64 x_p0, NU64 k_p1) {
	NU64 result;
	result = (NU64)((NU64)((NU64)(x_p0) << (NU64)(k_p1 & (64 - 1))) | (NU64)((NU64)(x_p0) >> (NU64)((NU64)((NU64)(64ULL) - (NU64)(k_p1)) & (64 - 1))));
	return result;
}
N_LIB_PRIVATE N_NIMCALL(NU64, next__pureZrandom_u36)(tyObject_Rand__RKGKOUprONAbkVAKotpsfw* r_p0) {
	NU64 result;
	NU64 s0_1;
	NU64 s1_1;
	NU64 T1_;
	NU64 T2_;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	s0_1 = (*r_p0).a0;
	s1_1 = (*r_p0).a1;
	result = (NU64)((NU64)(s0_1) + (NU64)(s1_1));
	s1_1 = (NU64)(s1_1 ^ s0_1);
	T1_ = (NU64)0;
	T1_ = rotl__pureZrandom_u24(s0_1, 55ULL);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	(*r_p0).a0 = (NU64)((NU64)(T1_ ^ s1_1) ^ (NU64)((NU64)(s1_1) << (NU64)(((NI)14) & (64 - 1))));
	T2_ = (NU64)0;
	T2_ = rotl__pureZrandom_u24(s1_1, 36ULL);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	(*r_p0).a1 = T2_;
	}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;
}
N_LIB_PRIVATE N_NIMCALL(NU64, rand__pureZrandom_u99)(tyObject_Rand__RKGKOUprONAbkVAKotpsfw* r_p0, NU64 max_p1) {
	NU64 result;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result = (NU64)0;
	{
		if (!(max_p1 == 0ULL)) goto LA3_;
		goto BeforeRet_;
	}
	goto LA1_;
LA3_: ;
	{
		NU64 max_2;
		NI iters_1;
		max_2 = max_p1;
		{
			if (!(max_2 == 18446744073709551615ULL)) goto LA8_;
			result = next__pureZrandom_u36(r_p0);
			if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
			goto BeforeRet_;
		}
LA8_: ;
		iters_1 = ((NI)0);
		{
			while (1) {
				NU64 x_1;
				x_1 = next__pureZrandom_u36(r_p0);
				if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
				{
					NIM_BOOL T14_;
					T14_ = (NIM_BOOL)0;
					T14_ = ((NU64)(x_1) <= (NU64)((NU64)((NU64)(18446744073709551615ULL) - (NU64)((NU64)((NU64)(18446744073709551615ULL) % (NU64)(max_2))))));
					if (T14_) goto LA15_;
					T14_ = (((NI)20) < iters_1);
LA15_: ;
					if (!T14_) goto LA16_;
					result = (NU64)((NU64)(x_1) % (NU64)((NU64)((NU64)(max_2) + (NU64)(1ULL))));
					goto BeforeRet_;
				}
				goto LA12_;
LA16_: ;
				{
					NI TM__AIKkgex03Z1j45JceakCTA_2;
					if (nimAddInt(iters_1, ((NI)1), &TM__AIKkgex03Z1j45JceakCTA_2)) { raiseOverflow(); goto BeforeRet_;
					};
					iters_1 = (NI)(TM__AIKkgex03Z1j45JceakCTA_2);
				}
LA12_: ;
			}
		}
	}
LA1_: ;
	}BeforeRet_: ;
	return result;
}
N_LIB_PRIVATE N_NIMCALL(NI, rand__pureZrandom_u95)(tyObject_Rand__RKGKOUprONAbkVAKotpsfw* r_p0, NI max_p1) {
	NI result;
	NU64 T1_;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result = (NI)0;
	T1_ = (NU64)0;
	T1_ = rand__pureZrandom_u99(r_p0, ((NU64) (max_p1)));
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	result = ((NI) (T1_));
	}BeforeRet_: ;
	return result;
}
N_LIB_PRIVATE N_NIMCALL(NI, rand__pureZrandom_u124)(NI max_p0) {
	NI result;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result = (NI)0;
	if ((max_p0) < ((NI)0) || (max_p0) > ((NI)IL64(9223372036854775807))){ raiseRangeErrorI(max_p0, ((NI)0), ((NI)IL64(9223372036854775807))); goto BeforeRet_;
	}
	result = rand__pureZrandom_u95((&state__pureZrandom_u15), (max_p0));
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	}BeforeRet_: ;
	return result;
}
N_LIB_PRIVATE N_NIMCALL(void, atprandomdotnim_Init000)(void) {
{
NIM_BOOL* nimErr_;
nimErr_ = nimErrorFlag();
	state__pureZrandom_u15 = DefaultRandSeed__pureZrandom_u14;
	initLock__coreZlocks_u7((&baseSeedLock__pureZrandom_u294));
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	BeforeRet_: ;
	nimTestErrorFlag();
}
}

