// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x30, %rsp
movl %edi, %eax
andl $0xffff0000, %eax       # imm = 0xFFFF0000
xorl %ebx, %ebx
cmpl $0x10000, %eax          # imm = 0x10000
je 0x9198
movl %ebx, %eax
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rcx, %r14
movq %rsi, %r15
movl %edi, %edx
movq 0x6e31(%rip), %r12      # 0xffd8
movq (%r12), %rdi
leaq 0x313c(%rip), %rsi      # 0xc2ee
xorl %eax, %eax
callq 0x7610
movq %r15, 0x73d0(%rip)      # 0x10590
leaq 0x260(%rip), %rax       # 0x9427
movq %rax, (%r14)
leaq 0x25a(%rip), %rax       # 0x942b
movq %rax, 0x8(%r14)
leaq 0x2e7(%rip), %rax       # 0x94c3
movq %rax, 0x10(%r14)
leaq 0x2df(%rip), %rax       # 0x94c6
movq %rax, 0x18(%r14)
leaq 0x3129(%rip), %rdi      # 0xc31b
callq 0x74f0
testq %rax, %rax
je 0x9233
leaq 0x10(%rsp), %rbx
leaq 0xf(%rsp), %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x77e0
pushq $0xa
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x7910
testl %eax, %eax
setne 0x7371(%rip)            # 0x10598
leaq 0x10(%rsp), %rdi
callq 0x71e0
jmp 0x923a
movb $0x0, 0x735e(%rip)      # 0x10598
leaq 0x30e9(%rip), %rdi      # 0xc32a
pushq $0x1
popq %rsi
callq 0x75e0
testq %rax, %rax
je 0x93f4
leaq 0x3108(%rip), %rsi      # 0xc361
movq %rax, %rdi
callq 0x73c0
leaq 0x7338(%rip), %rsi      # 0x105a0
leaq 0x7391(%rip), %rcx      # 0x10600
pushq $0x1
popq %rbx
movl %ebx, %edi
xorl %edx, %edx
callq *%rax
cmpb $0x1, 0x7319(%rip)      # 0x10598
je 0x93b4
leaq 0x3109(%rip), %rdi      # 0xc395
callq *0x7386(%rip)           # 0x10618
movq %rax, 0x73bf(%rip)      # 0x10658
leaq 0x3103(%rip), %rdi      # 0xc3a3
callq *0x7372(%rip)           # 0x10618
movq %rax, 0x73b3(%rip)      # 0x10660
leaq 0x30fd(%rip), %rdi      # 0xc3b1
callq *0x735e(%rip)           # 0x10618
movq %rax, 0x73a7(%rip)      # 0x10668
leaq 0x30f9(%rip), %rdi      # 0xc3c1
callq *0x734a(%rip)           # 0x10618
movq %rax, 0x739b(%rip)      # 0x10670
leaq 0x30f8(%rip), %rdi      # 0xc3d4
callq *0x7336(%rip)           # 0x10618
movq %rax, 0x738f(%rip)      # 0x10678
leaq 0x30f5(%rip), %rdi      # 0xc3e5
callq *0x7322(%rip)           # 0x10618
movq %rax, 0x7383(%rip)      # 0x10680
leaq 0x30ed(%rip), %rdi      # 0xc3f1
callq *0x730e(%rip)           # 0x10618
movq %rax, 0x7377(%rip)      # 0x10688
leaq 0x30f1(%rip), %rdi      # 0xc409
callq *0x72fa(%rip)           # 0x10618
movq %rax, 0x736b(%rip)      # 0x10690
leaq 0x30ec(%rip), %rdi      # 0xc418
callq *0x72e6(%rip)           # 0x10618
movq %rax, 0x735f(%rip)      # 0x10698
leaq 0x30e7(%rip), %rdi      # 0xc427
callq *0x72d2(%rip)           # 0x10618
movq %rax, 0x7353(%rip)      # 0x106a0
leaq 0x30de(%rip), %rdi      # 0xc432
callq *0x72be(%rip)           # 0x10618
movq %rax, 0x7347(%rip)      # 0x106a8
leaq 0x30dc(%rip), %rdi      # 0xc444
callq *0x72aa(%rip)           # 0x10618
movq %rax, 0x733b(%rip)      # 0x106b0
leaq 0x30d5(%rip), %rdi      # 0xc451
callq *0x7296(%rip)           # 0x10618
movq %rax, 0x732f(%rip)      # 0x106b8
leaq 0x30ce(%rip), %rdi      # 0xc45e
callq *0x7282(%rip)           # 0x10618
movq %rax, 0x7323(%rip)      # 0x106c0
movq 0x72a4(%rip), %rax      # 0x10648
testq %rax, %rax
je 0x9189
callq *%rax
jmp 0x9189
movl %eax, %ebp
movq (%r12), %rdi
leaq 0x2fab(%rip), %rsi      # 0xc36c
leaq 0x2fb4(%rip), %rdx      # 0xc37c
xorl %eax, %eax
callq 0x7610
movq (%r12), %rdi
leaq 0x2fad(%rip), %rsi      # 0xc387
movl %ebp, %edx
xorl %eax, %eax
callq 0x7610
movq (%r12), %rsi
pushq $0xa
popq %rdi
callq 0x7540
jmp 0x9285
movq (%r12), %rcx
leaq 0x2f3e(%rip), %rdi      # 0xc33d
pushq $0x23
popq %rsi
pushq $0x1
popq %rdx
callq 0x7940
pushq $0x1
popq %rdi
callq 0x74e0
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x71e0
movq %rbx, %rdi
callq 0x7820