/* Generated by Nim Compiler v2.2.10 */
/* Compiled for: Linux, amd64, gcc */
/* Command for C compiler:
   gcc -c  -w -fmax-errors=3 -fno-strict-aliasing -pthread -O3 -fno-ident -fno-math-errno   -I/home/runner/work/nightlies/nightlies/nim-2.2.10/lib -I/home/runner/work/nightlies/nightlies/nim-2.2.10/dist/atlas/src -o /home/runner/work/nightlies/nightlies/nim-2.2.10/dist/atlas/.nimcache/@pnativesockets.nim.c.o /home/runner/work/nightlies/nightlies/nim-2.2.10/dist/atlas/.nimcache/@pnativesockets.nim.c */
#define NIM_INTBITS 64

#include "nimbase.h"
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <string.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 NimStrPayload NimStrPayload;
typedef struct NimStringV2 NimStringV2;
typedef struct tyObject_Option__lf3iZ9bSOr0IZPtb2xh5Ylg tyObject_Option__lf3iZ9bSOr0IZPtb2xh5Ylg;
typedef NU8 tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q;
typedef NU8 tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA;
struct NimStrPayload {
	NI cap;
	NIM_CHAR data[SEQ_DECL_SIZE];
};
struct NimStringV2 {
	NI len;
	NimStrPayload* p;
};
typedef NU8 tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA;
struct tyObject_Option__lf3iZ9bSOr0IZPtb2xh5Ylg {
	tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA val;
	NIM_BOOL has;
};
typedef NIM_CHAR tyArray__AY9b9cftHbCDp9awOr5wvXtwg[14];
static N_INLINE(void, nimZeroMem)(void* p_p0, NI size_p1);
static N_INLINE(void, nimSetMem__system_u2188)(void* a_p0, int v_p1, NI size_p2);
N_LIB_PRIVATE N_NIMCALL(int, toInt__pureZnativesockets_u60)(tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA domain_p0);
N_LIB_PRIVATE N_NIMCALL(int, toInt__pureZnativesockets_u62)(tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q typ_p0);
N_LIB_PRIVATE N_NIMCALL(int, toInt__pureZnativesockets_u64)(tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA p_p0);
N_LIB_PRIVATE N_NIMCALL(struct addrinfo*, getAddrInfo__pureZnativesockets_u250)(NimStringV2 address_p0, NU16 port_p1, struct addrinfo hints_p2);
N_LIB_PRIVATE N_NIMCALL(NimStringV2, dollar___systemZdollars_u49)(NU16 xX60gensym7__p0);
static N_INLINE(NCSTRING, nimToCStringConv)(NimStringV2 s_p0);
N_LIB_PRIVATE N_NOINLINE(void, raiseOSError__stdZoserrors_u119)(NI32 errorCode_p0, NimStringV2 additionalInfo_p1);
N_LIB_PRIVATE N_NIMCALL(NI32, osLastError__stdZoserrors_u122)(void);
N_LIB_PRIVATE N_NIMCALL(NimStringV2, cstrToNimstr)(NCSTRING str_p0);
N_LIB_PRIVATE N_NOCONV(void, deallocShared)(void* p_p0);
static N_INLINE(NIM_BOOL*, nimErrorFlag)(void);
static N_INLINE(tyObject_Option__lf3iZ9bSOr0IZPtb2xh5Ylg, some__pureZnativesockets_u77)(tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA val_p0);
static N_INLINE(tyObject_Option__lf3iZ9bSOr0IZPtb2xh5Ylg, none__pureZnativesockets_u131)(void);
N_LIB_PRIVATE N_NIMCALL(int, createNativeSocket__pureZnativesockets_u228)(int domain_p0, int sockType_p1, int protocol_p2, NIM_BOOL inheritable_p3);
static const struct {
  NI cap; NIM_CHAR data[0+1];
} TM__f9bP3LqjpgpB9cXL8Nnak7tQ_2 = { 0 | NIM_STRLIT_FLAG, "" };
static const NimStringV2 TM__f9bP3LqjpgpB9cXL8Nnak7tQ_3 = {0, (NimStrPayload*)&TM__f9bP3LqjpgpB9cXL8Nnak7tQ_2};
N_LIB_PRIVATE NIM_CONST int osInvalidSocket__pureZnativesockets_u48 = ((int)-1);
N_LIB_PRIVATE NIM_CONST int nativeAfInet__pureZnativesockets_u49 = ((int)2);
N_LIB_PRIVATE NIM_CONST int nativeAfInet6__pureZnativesockets_u50 = ((int)10);
N_LIB_PRIVATE NIM_CONST int nativeAfUnix__pureZnativesockets_u51 = ((int)1);
extern NIM_THREADVAR NIM_BOOL nimInErrorMode__system_u4216;
N_LIB_PRIVATE N_NIMCALL(void, close__pureZnativesockets_u201)(int socket_p0) {
	int T1_;
	T1_ = (int)0;
	T1_ = close(socket_p0);
	(void)(T1_);
}
N_LIB_PRIVATE N_NIMCALL(tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q, toSockType__pureZnativesockets_u147)(tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA protocol_p0) {
	tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q result;
	tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q T1_;
	T1_ = (tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q)0;
	switch (protocol_p0) {
	case ((tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA)6):
	{
		result = ((tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q)1);
	}
	break;
	case ((tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA)17):
	{
		result = ((tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q)2);
	}
	break;
	case ((tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA)18):
	case ((tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA)19):
	case ((tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA)20):
	case ((tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA)21):
	case ((tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA)22):
	{
		result = ((tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q)3);
	}
	break;
	default: __builtin_unreachable();
	}
	return result;
}
static N_INLINE(void, nimSetMem__system_u2188)(void* a_p0, int v_p1, NI size_p2) {
	void* T1_;
	T1_ = (void*)0;
	T1_ = memset(a_p0, v_p1, ((size_t) (size_p2)));
}
static N_INLINE(void, nimZeroMem)(void* p_p0, NI size_p1) {
	nimSetMem__system_u2188(p_p0, ((int)0), size_p1);
}
N_LIB_PRIVATE N_NIMCALL(int, toInt__pureZnativesockets_u60)(tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA domain_p0) {
	int result;
	switch (domain_p0) {
	case ((tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA)0):
	{
		result = ((int)0);
	}
	break;
	case ((tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA)1):
	{
		result = ((int)1);
	}
	break;
	case ((tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA)2):
	{
		result = ((int)2);
	}
	break;
	case ((tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA)10):
	{
		result = ((int)10);
	}
	break;
	default: __builtin_unreachable();
	}
	return result;
}
N_LIB_PRIVATE N_NIMCALL(int, toInt__pureZnativesockets_u62)(tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q typ_p0) {
	int result;
	switch (typ_p0) {
	case ((tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q)1):
	{
		result = ((int)1);
	}
	break;
	case ((tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q)2):
	{
		result = ((int)2);
	}
	break;
	case ((tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q)5):
	{
		result = ((int)5);
	}
	break;
	case ((tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q)3):
	{
		result = ((int)3);
	}
	break;
	default: __builtin_unreachable();
	}
	return result;
}
N_LIB_PRIVATE N_NIMCALL(int, toInt__pureZnativesockets_u64)(tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA p_p0) {
	int result;
	switch (p_p0) {
	case ((tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA)6):
	{
		result = ((int)6);
	}
	break;
	case ((tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA)17):
	{
		result = ((int)17);
	}
	break;
	case ((tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA)18):
	{
		result = ((int)0);
	}
	break;
	case ((tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA)19):
	{
		result = ((int)41);
	}
	break;
	case ((tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA)20):
	{
		result = ((int)255);
	}
	break;
	case ((tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA)21):
	{
		result = ((int)1);
	}
	break;
	case ((tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA)22):
	{
		result = ((int)58);
	}
	break;
	default: __builtin_unreachable();
	}
	return result;
}
static N_INLINE(NCSTRING, nimToCStringConv)(NimStringV2 s_p0) {
	NCSTRING result;
	{
		if (!(s_p0.len == ((NI)0))) goto LA3_;
		result = "";
	}
	goto LA1_;
LA3_: ;
	{
		result = ((NCSTRING) ((*s_p0.p).data));
	}
LA1_: ;
	return result;
}
static N_INLINE(NIM_BOOL*, nimErrorFlag)(void) {
	NIM_BOOL* result;
	result = (&nimInErrorMode__system_u4216);
	return result;
}
N_LIB_PRIVATE N_NIMCALL(struct addrinfo*, getAddrInfo__pureZnativesockets_u250)(NimStringV2 address_p0, NU16 port_p1, struct addrinfo hints_p2) {
	struct addrinfo* result;
	NimStringV2 socketPort_1;
	NimStringV2 T2_;
	int gaiResult_1;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result = (struct addrinfo*)0;
	socketPort_1.len = 0; socketPort_1.p = NIM_NIL;
	result = ((struct addrinfo*) NIM_NIL);
	T2_.len = 0; T2_.p = NIM_NIL;
	{
		int T5_;
		T5_ = (int)0;
		T5_ = toInt__pureZnativesockets_u62(((tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q)3));
		if (NIM_UNLIKELY(*nimErr_)) goto LA1_;
		if (!(hints_p2.ai_socktype == T5_)) goto LA6_;
		socketPort_1 = TM__f9bP3LqjpgpB9cXL8Nnak7tQ_3;
	}
	goto LA3_;
LA6_: ;
	{
		socketPort_1 = dollar___systemZdollars_u49(port_p1);
		if (NIM_UNLIKELY(*nimErr_)) goto LA1_;
	}
LA3_: ;
	gaiResult_1 = getaddrinfo(nimToCStringConv(address_p0), nimToCStringConv(socketPort_1), (&hints_p2), &result);
	{
		NimStringV2 colontmpD_;
		NI32 T14_;
		NCSTRING T15_;
		if (!!((gaiResult_1 == ((NI32)0)))) goto LA11_;
		colontmpD_.len = 0; colontmpD_.p = NIM_NIL;
		T14_ = (NI32)0;
		T14_ = osLastError__stdZoserrors_u122();
		if (NIM_UNLIKELY(*nimErr_)) goto LA13_;
		T15_ = (NCSTRING)0;
		T15_ = (char *)gai_strerror(gaiResult_1);
		colontmpD_ = cstrToNimstr(T15_);
		raiseOSError__stdZoserrors_u119(T14_, colontmpD_);
		if (NIM_UNLIKELY(*nimErr_)) goto LA13_;
		{
			LA13_:;
		}
		{
			if (colontmpD_.p && !(colontmpD_.p->cap & NIM_STRLIT_FLAG)) {
 deallocShared(colontmpD_.p);
}
		}
		if (NIM_UNLIKELY(*nimErr_)) goto LA1_;
	}
LA11_: ;
	{
		LA1_:;
	}
	{
		if (socketPort_1.p && !(socketPort_1.p->cap & NIM_STRLIT_FLAG)) {
 deallocShared(socketPort_1.p);
}
	}
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	}BeforeRet_: ;
	return result;
}
N_LIB_PRIVATE N_NIMCALL(struct addrinfo*, getAddrInfo__pureZnativesockets_u276)(NimStringV2 address_p0, NU16 port_p1, tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA domain_p2, tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q sockType_p3, tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA protocol_p4) {
	struct addrinfo* result;
	struct addrinfo hints_1;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result = (struct addrinfo*)0;
	nimZeroMem((void*)(&hints_1), sizeof(struct addrinfo));
	hints_1.ai_family = toInt__pureZnativesockets_u60(domain_p2);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	hints_1.ai_socktype = toInt__pureZnativesockets_u62(sockType_p3);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	hints_1.ai_protocol = toInt__pureZnativesockets_u64(protocol_p4);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	{
		if (!(domain_p2 == ((tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA)10))) goto LA3_;
		hints_1.ai_flags = ((int)8);
	}
LA3_: ;
	result = getAddrInfo__pureZnativesockets_u250(address_p0, port_p1, hints_1);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	}BeforeRet_: ;
	return result;
}
static N_INLINE(tyObject_Option__lf3iZ9bSOr0IZPtb2xh5Ylg, some__pureZnativesockets_u77)(tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA val_p0) {
	tyObject_Option__lf3iZ9bSOr0IZPtb2xh5Ylg result;
	nimZeroMem((void*)(&result), sizeof(tyObject_Option__lf3iZ9bSOr0IZPtb2xh5Ylg));
	result.has = NIM_TRUE;
	result.val = val_p0;
	return result;
}
static N_INLINE(tyObject_Option__lf3iZ9bSOr0IZPtb2xh5Ylg, none__pureZnativesockets_u131)(void) {
	tyObject_Option__lf3iZ9bSOr0IZPtb2xh5Ylg result;
	nimZeroMem((void*)(&result), sizeof(tyObject_Option__lf3iZ9bSOr0IZPtb2xh5Ylg));
	return result;
}
N_LIB_PRIVATE N_NIMCALL(tyObject_Option__lf3iZ9bSOr0IZPtb2xh5Ylg, toKnownDomain__pureZnativesockets_u69)(int family_p0) {
	tyObject_Option__lf3iZ9bSOr0IZPtb2xh5Ylg result;
	tyObject_Option__lf3iZ9bSOr0IZPtb2xh5Ylg T1_;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	nimZeroMem((void*)(&result), sizeof(tyObject_Option__lf3iZ9bSOr0IZPtb2xh5Ylg));
	{
		if (!(family_p0 == ((int)0))) goto LA4_;
		result = some__pureZnativesockets_u77(((tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA)0));
		if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	}
	goto LA2_;
LA4_: ;
	{
		if (!(family_p0 == ((int)1))) goto LA7_;
		result = some__pureZnativesockets_u77(((tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA)1));
		if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	}
	goto LA2_;
LA7_: ;
	{
		if (!(family_p0 == ((int)2))) goto LA10_;
		result = some__pureZnativesockets_u77(((tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA)2));
		if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	}
	goto LA2_;
LA10_: ;
	{
		if (!(family_p0 == ((int)10))) goto LA13_;
		result = some__pureZnativesockets_u77(((tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA)10));
		if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	}
	goto LA2_;
LA13_: ;
	{
		result = none__pureZnativesockets_u131();
		if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	}
LA2_: ;
	}BeforeRet_: ;
	return result;
}
N_LIB_PRIVATE N_NIMCALL(int, createNativeSocket__pureZnativesockets_u228)(int domain_p0, int sockType_p1, int protocol_p2, NIM_BOOL inheritable_p3) {
	int result;
	NI32 colontmpD_;
	NI32 colontmpD__2;
	NI32 sockType_2;
	colontmpD_ = (NI32)0;
	colontmpD__2 = (NI32)0;
	{
		if (!inheritable_p3) goto LA3_;
		colontmpD_ = (NI32)(sockType_p1 & ((NI32)-524289));
		sockType_2 = colontmpD_;
	}
	goto LA1_;
LA3_: ;
	{
		colontmpD__2 = (NI32)(sockType_p1 | ((int)524288));
		sockType_2 = colontmpD__2;
	}
LA1_: ;
	result = socket(domain_p0, sockType_2, protocol_p2);
	return result;
}
N_LIB_PRIVATE N_NIMCALL(int, createNativeSocket__pureZnativesockets_u235)(tyEnum_Domain__MfMl870GtGGt9bzO9cicdWqA domain_p0, tyEnum_SockType__wyAiWULDFVysRokh4hHr7Q sockType_p1, tyEnum_Protocol__x11L2EqCXuQVMDp2SGyCyA protocol_p2, NIM_BOOL inheritable_p3) {
	int result;
	int T1_;
	int T2_;
	int T3_;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result = (int)0;
	T1_ = (int)0;
	T1_ = toInt__pureZnativesockets_u60(domain_p0);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	T2_ = (int)0;
	T2_ = toInt__pureZnativesockets_u62(sockType_p1);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	T3_ = (int)0;
	T3_ = toInt__pureZnativesockets_u64(protocol_p2);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	result = createNativeSocket__pureZnativesockets_u228(T1_, T2_, T3_, inheritable_p3);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	}BeforeRet_: ;
	return result;
}
