// File: gatecat[P]subprime/subprime.cpp
// Function: _glx_Main
Bool __glx_Main(
	uint32_t version,
	const __GLXapiExports *exports,
	__GLXvendorInfo *vendor,
	__GLXapiImports *imports
) {
	if (GLX_VENDOR_ABI_GET_MAJOR_VERSION(version) == GLX_VENDOR_ABI_MAJOR_VERSION
		&& GLX_VENDOR_ABI_GET_MINOR_VERSION(version) >= GLX_VENDOR_ABI_MINOR_VERSION) {
		fprintf(stderr, "subprime vendor initialised (version=%08x).\n", version);
		api_exports = exports;
		imports->isScreenSupported = is_screen_supported;
		imports->getProcAddress = get_proc_address;
		imports->getDispatchAddress = get_dispatch_address;
		imports->setDispatchIndex = set_dispatch_index;

		const char *trc_env = getenv("SUBPRIME_TRACE");
		trace_en = (trc_env && std::stoi(trc_env));

		void *egl_lib = dlopen("libEGL_nvidia.so.0", RTLD_LOCAL | RTLD_LAZY);
		if (!egl_lib)
		{
			fprintf(stderr, "failed to open EGL vendor library!\n");
			exit(1);
			return False;
		}
		auto egl_main = reinterpret_cast<egl_main_t>(dlsym(egl_lib, "__egl_Main"));
		EGLBoolean egl_result = egl_main(0x0001, &egl_exports, nullptr, &egl_imports);
		SP_TRACE("egl_result=%d", egl_result);

		LOAD_FN(eglInitialize);
		LOAD_FN(eglGetDisplay);
		LOAD_FN(eglChooseConfig);
		LOAD_FN(eglGetConfigAttrib);
		LOAD_FN(eglCreateContext);
		LOAD_FN(eglGetError);
		LOAD_FN(eglCreatePbufferSurface);
		LOAD_FN(eglMakeCurrent);
		LOAD_FN(eglSwapBuffers);
		LOAD_FN(eglBindAPI);
		LOAD_FN(eglDestroySurface);
		LOAD_FN(glReadBuffer);
		LOAD_FN(glReadPixels);
		LOAD_FN(glFinish);

		if (egl_imports.patchThreadAttach) {
			egl_imports.patchThreadAttach();
		}

		return True;
	}
	return False;
}

// Assembly:
subq $0x98, %rsp
movl %edi, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
movq %rdx, 0x80(%rsp)
movq %rcx, 0x78(%rsp)
movl 0x90(%rsp), %edi
callq 0x19900
cmpl $0x1, %eax
jne 0x198d7
movl 0x90(%rsp), %edi
callq 0x19910
cmpl $0x0, %eax
jb 0x198d7
movq 0x10a94(%rip), %rax     # 0x29fd8
movq (%rax), %rdi
movl 0x90(%rsp), %edx
leaq 0x9d8d(%rip), %rsi      # 0x232e2
movb $0x0, %al
callq 0x16680
movq 0x88(%rsp), %rax
movq %rax, 0x11b9d(%rip)     # 0x2b108
movq 0x78(%rsp), %rax
leaq 0x3a9(%rip), %rcx       # 0x19920
movq %rcx, (%rax)
movq 0x78(%rsp), %rax
leaq 0x3aa(%rip), %rcx       # 0x19930
movq %rcx, 0x8(%rax)
movq 0x78(%rsp), %rax
leaq 0x54a(%rip), %rcx       # 0x19ae0
movq %rcx, 0x10(%rax)
movq 0x78(%rsp), %rax
leaq 0x54a(%rip), %rcx       # 0x19af0
movq %rcx, 0x18(%rax)
leaq 0x9d5e(%rip), %rdi      # 0x2330f
callq 0x16290
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rcx
movb $0x0, 0x4e(%rsp)
movb $0x0, 0x3b(%rsp)
xorl %eax, %eax
cmpq $0x0, %rcx
movb %al, 0x23(%rsp)
je 0x19636
movq 0x70(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x4f(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x16be0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movb $0x1, 0x4e(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x16af0
jmp 0x1960a
movb $0x1, 0x3b(%rsp)
xorl %eax, %eax
movl %eax, %esi
leaq 0x50(%rsp), %rdi
movl $0xa, %edx
callq 0x16ef0
movl %eax, 0xc(%rsp)
jmp 0x19628
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
setne %al
movb %al, 0x23(%rsp)
movb 0x23(%rsp), %al
andb $0x1, %al
movb %al, 0x11ace(%rip)      # 0x2b110
testb $0x1, 0x3b(%rsp)
jne 0x1964b
jmp 0x19655
leaq 0x50(%rsp), %rdi
callq 0x158f0
testb $0x1, 0x4e(%rsp)
jne 0x1965e
jmp 0x19668
leaq 0x4f(%rsp), %rdi
callq 0x16080
leaq 0x9caf(%rip), %rdi      # 0x2331e
movl $0x1, %esi
callq 0x165c0
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0x196f3
movq 0x1094b(%rip), %rax     # 0x29fd8
movq (%rax), %rdi
leaq 0x9c9a(%rip), %rsi      # 0x23331
movb $0x0, %al
callq 0x16680
movl $0x1, %edi
callq 0x16280
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
jmp 0x196db
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
testb $0x1, 0x3b(%rsp)
jne 0x196cf
jmp 0x196d9
leaq 0x50(%rsp), %rdi
callq 0x158f0
jmp 0x196db
testb $0x1, 0x4e(%rsp)
jne 0x196e4
jmp 0x196ee
leaq 0x4f(%rsp), %rdi
callq 0x16080
jmp 0x198f1
movq 0x30(%rsp), %rdi
leaq 0x9c56(%rip), %rsi      # 0x23355
callq 0x15df0
movq %rax, 0x28(%rsp)
movl $0x1, %edi
leaq 0x11a03(%rip), %rsi     # 0x2b118
xorl %eax, %eax
movl %eax, %edx
leaq 0x11a58(%rip), %rcx     # 0x2b178
callq *0x28(%rsp)
movl %eax, 0x24(%rsp)
testb $0x1, 0x119e1(%rip)     # 0x2b110
je 0x19784
movq 0x108a0(%rip), %rax     # 0x29fd8
movq (%rax), %rdi
leaq 0x9c1e(%rip), %rsi      # 0x23360
leaq 0x9c27(%rip), %rdx      # 0x23370
movb $0x0, %al
callq 0x16680
movq 0x10881(%rip), %rax     # 0x29fd8
movq (%rax), %rdi
movl 0x24(%rsp), %edx
leaq 0x9c16(%rip), %rsi      # 0x2337b
movb $0x0, %al
callq 0x16680
movq 0x10865(%rip), %rax     # 0x29fd8
movq (%rax), %rdi
leaq 0x9bd6(%rip), %rsi      # 0x23353
movb $0x0, %al
callq 0x16680
jmp 0x19786
jmp 0x19788
leaq 0x9bfa(%rip), %rdi      # 0x23389
callq *0x119fb(%rip)          # 0x2b190
movq %rax, 0x11a34(%rip)     # 0x2b1d0
jmp 0x1979e
leaq 0x9bf2(%rip), %rdi      # 0x23397
callq *0x119e5(%rip)          # 0x2b190
movq %rax, 0x11a26(%rip)     # 0x2b1d8
jmp 0x197b4
leaq 0x9bea(%rip), %rdi      # 0x233a5
callq *0x119cf(%rip)          # 0x2b190
movq %rax, 0x11a18(%rip)     # 0x2b1e0
jmp 0x197ca
leaq 0x9be4(%rip), %rdi      # 0x233b5
callq *0x119b9(%rip)          # 0x2b190
movq %rax, 0x11a0a(%rip)     # 0x2b1e8
jmp 0x197e0
leaq 0x9be1(%rip), %rdi      # 0x233c8
callq *0x119a3(%rip)          # 0x2b190
movq %rax, 0x119fc(%rip)     # 0x2b1f0
jmp 0x197f6
leaq 0x9bdc(%rip), %rdi      # 0x233d9
callq *0x1198d(%rip)          # 0x2b190
movq %rax, 0x119ee(%rip)     # 0x2b1f8
jmp 0x1980c
leaq 0x9bd2(%rip), %rdi      # 0x233e5
callq *0x11977(%rip)          # 0x2b190
movq %rax, 0x119e0(%rip)     # 0x2b200
jmp 0x19822
leaq 0x9bd4(%rip), %rdi      # 0x233fd
callq *0x11961(%rip)          # 0x2b190
movq %rax, 0x119d2(%rip)     # 0x2b208
jmp 0x19838
leaq 0x9bcd(%rip), %rdi      # 0x2340c
callq *0x1194b(%rip)          # 0x2b190
movq %rax, 0x119c4(%rip)     # 0x2b210
jmp 0x1984e
leaq 0x9bc6(%rip), %rdi      # 0x2341b
callq *0x11935(%rip)          # 0x2b190
movq %rax, 0x119b6(%rip)     # 0x2b218
jmp 0x19864
leaq 0x9bbb(%rip), %rdi      # 0x23426
callq *0x1191f(%rip)          # 0x2b190
movq %rax, 0x119a8(%rip)     # 0x2b220
jmp 0x1987a
leaq 0x9bb7(%rip), %rdi      # 0x23438
callq *0x11909(%rip)          # 0x2b190
movq %rax, 0x1199a(%rip)     # 0x2b228
jmp 0x19890
leaq 0x9bae(%rip), %rdi      # 0x23445
callq *0x118f3(%rip)          # 0x2b190
movq %rax, 0x1198c(%rip)     # 0x2b230
jmp 0x198a6
leaq 0x9ba5(%rip), %rdi      # 0x23452
callq *0x118dd(%rip)          # 0x2b190
movq %rax, 0x1197e(%rip)     # 0x2b238
cmpq $0x0, 0x118fe(%rip)     # 0x2b1c0
je 0x198ca
callq *0x118f6(%rip)          # 0x2b1c0
movl $0x1, 0x94(%rsp)
jmp 0x198e2
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
addq $0x98, %rsp
retq
movq 0x40(%rsp), %rdi
callq 0x16bc0
nopl (%rax,%rax)