// File: /workspace/github_cpp_makefile_star2_O3/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 0x67c4
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 0x680d(%rip), %rax      # 0xcfe0
movq (%rax), %rdi
leaq 0x2b08(%rip), %rsi      # 0x92e5
xorl %eax, %eax
callq 0x4390
movq %r15, 0x6ba5(%rip)      # 0xd390
leaq 0x2ae(%rip), %rax       # 0x6aa0
movq %rax, (%r14)
leaq 0x2aa(%rip), %rax       # 0x6aa6
movq %rax, 0x8(%r14)
leaq 0x359(%rip), %rax       # 0x6b60
movq %rax, 0x10(%r14)
leaq 0x351(%rip), %rax       # 0x6b63
movq %rax, 0x18(%r14)
leaq 0x2af5(%rip), %rdi      # 0x9312
callq 0x42e0
testq %rax, %rax
je 0x68c6
movq %rax, %rbx
leaq 0x18(%rsp), %r12
movq %r12, -0x10(%r12)
movq %rax, %rdi
callq 0x4140
movq %rbx, %rdx
addq %rax, %rdx
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4100
movq (%r14), %r14
callq 0x4040
movq %rax, %rbx
movl (%rax), %ebp
movl $0x0, (%rax)
leaq 0x28(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
movl $0xa, %edx
callq 0x4380
cmpq %r14, (%r15)
je 0x6a5f
movslq %eax, %rcx
cmpq %rax, %rcx
jne 0x6a6b
movl (%rbx), %ecx
cmpl $0x22, %ecx
je 0x6a6b
testl %ecx, %ecx
jne 0x68a4
movl %ebp, (%rbx)
testl %eax, %eax
setne 0x6aeb(%rip)            # 0xd398
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x68cd
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x42a0
jmp 0x68cd
movb $0x0, 0x6acb(%rip)      # 0xd398
leaq 0x2a4d(%rip), %rdi      # 0x9321
movl $0x1, %esi
callq 0x4370
testq %rax, %rax
je 0x6a5a
leaq 0x2a6a(%rip), %rsi      # 0x9358
movq %rax, %rdi
callq 0x4240
leaq 0x6aa3(%rip), %rsi      # 0xd3a0
leaq 0x6afc(%rip), %rcx      # 0xd400
movl $0x1, %ebx
movl $0x1, %edi
xorl %edx, %edx
callq *%rax
cmpb $0x1, 0x6a7f(%rip)      # 0xd398
je 0x6a4e
leaq 0x2a66(%rip), %rdi      # 0x938c
callq *0x6aec(%rip)           # 0xd418
movq %rax, 0x6b25(%rip)      # 0xd458
leaq 0x2a60(%rip), %rdi      # 0x939a
callq *0x6ad8(%rip)           # 0xd418
movq %rax, 0x6b19(%rip)      # 0xd460
leaq 0x2a5a(%rip), %rdi      # 0x93a8
callq *0x6ac4(%rip)           # 0xd418
movq %rax, 0x6b0d(%rip)      # 0xd468
leaq 0x2a56(%rip), %rdi      # 0x93b8
callq *0x6ab0(%rip)           # 0xd418
movq %rax, 0x6b01(%rip)      # 0xd470
leaq 0x2a55(%rip), %rdi      # 0x93cb
callq *0x6a9c(%rip)           # 0xd418
movq %rax, 0x6af5(%rip)      # 0xd478
leaq 0x2a52(%rip), %rdi      # 0x93dc
callq *0x6a88(%rip)           # 0xd418
movq %rax, 0x6ae9(%rip)      # 0xd480
leaq 0x2a4a(%rip), %rdi      # 0x93e8
callq *0x6a74(%rip)           # 0xd418
movq %rax, 0x6add(%rip)      # 0xd488
leaq 0x2a4e(%rip), %rdi      # 0x9400
callq *0x6a60(%rip)           # 0xd418
movq %rax, 0x6ad1(%rip)      # 0xd490
leaq 0x2a49(%rip), %rdi      # 0x940f
callq *0x6a4c(%rip)           # 0xd418
movq %rax, 0x6ac5(%rip)      # 0xd498
leaq 0x2a44(%rip), %rdi      # 0x941e
callq *0x6a38(%rip)           # 0xd418
movq %rax, 0x6ab9(%rip)      # 0xd4a0
leaq 0x2a3b(%rip), %rdi      # 0x9429
callq *0x6a24(%rip)           # 0xd418
movq %rax, 0x6aad(%rip)      # 0xd4a8
leaq 0x2a39(%rip), %rdi      # 0x943b
callq *0x6a10(%rip)           # 0xd418
movq %rax, 0x6aa1(%rip)      # 0xd4b0
leaq 0x2a32(%rip), %rdi      # 0x9448
callq *0x69fc(%rip)           # 0xd418
movq %rax, 0x6a95(%rip)      # 0xd4b8
leaq 0x2a2b(%rip), %rdi      # 0x9455
callq *0x69e8(%rip)           # 0xd418
movq %rax, 0x6a89(%rip)      # 0xd4c0
movq 0x6a0a(%rip), %rax      # 0xd448
testq %rax, %rax
je 0x67b5
callq *%rax
jmp 0x67b5
movl %eax, %edi
callq 0x4ce6
jmp 0x691f
callq 0x4d31
leaq 0x2ffb(%rip), %rdi      # 0x9a61
callq 0x41c0
leaq 0x2fef(%rip), %rdi      # 0x9a61
callq 0x43b0
movq %rax, %r14
cmpl $0x0, (%rbx)
jne 0x6a81
movl %ebp, (%rbx)
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x6a98
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x42a0
movq %r14, %rdi
callq 0x44e0