// File: /workspace/github_cpp_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edi, %eax
andl $0xffff0000, %eax       # imm = 0xFFFF0000
xorl %ebx, %ebx
cmpl $0x10000, %eax          # imm = 0x10000
je 0x6876
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rcx, %r14
movq %rsi, %r15
movl %edi, %edx
movq 0x675b(%rip), %rax      # 0xcfe0
movq (%rax), %rdi
leaq 0x2a56(%rip), %rsi      # 0x92e5
xorl %eax, %eax
callq 0x4380
movq %r15, 0x6af3(%rip)      # 0xd390
leaq 0x2af(%rip), %rax       # 0x6b53
movq %rax, (%r14)
leaq 0x2ab(%rip), %rax       # 0x6b59
movq %rax, 0x8(%r14)
leaq 0x35a(%rip), %rax       # 0x6c13
movq %rax, 0x10(%r14)
leaq 0x352(%rip), %rax       # 0x6c16
movq %rax, 0x18(%r14)
leaq 0x2a43(%rip), %rdi      # 0x9312
callq 0x42e0
movq %rax, %rbx
testq %rax, %rax
je 0x6975
leaq 0x10(%rsp), %r13
movq %r13, -0x10(%r13)
movq %rbx, %rdi
callq 0x4140
leaq (%rax,%rbx), %rdx
movq %rsp, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4100
movq (%r14), %r15
callq 0x4040
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
leaq 0x20(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
movl $0xa, %edx
callq 0x4370
cmpq %r15, (%r12)
je 0x6b11
movslq %eax, %rcx
cmpq %rax, %rcx
jne 0x6b1d
movl (%r14), %ecx
cmpl $0x22, %ecx
je 0x6b1d
testl %ecx, %ecx
jne 0x6954
movl %ebp, (%r14)
testl %eax, %eax
setne 0x6a3b(%rip)            # 0xd398
movq (%rsp), %rdi
cmpq %r13, %rdi
je 0x697c
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x42a0
jmp 0x697c
movb $0x0, 0x6a1c(%rip)      # 0xd398
testq %rbx, %rbx
leaq 0x299b(%rip), %rdi      # 0x9321
movl $0x1, %esi
callq 0x4360
testq %rax, %rax
je 0x6b0c
leaq 0x29b8(%rip), %rsi      # 0x9358
movq %rax, %rdi
callq 0x4240
leaq 0x69f1(%rip), %rsi      # 0xd3a0
leaq 0x6a4a(%rip), %rcx      # 0xd400
movl $0x1, %ebx
movl $0x1, %edi
xorl %edx, %edx
callq *%rax
cmpb $0x1, 0x69cd(%rip)      # 0xd398
je 0x6b00
leaq 0x29b4(%rip), %rdi      # 0x938c
callq *0x6a3a(%rip)           # 0xd418
movq %rax, 0x6a73(%rip)      # 0xd458
leaq 0x29ae(%rip), %rdi      # 0x939a
callq *0x6a26(%rip)           # 0xd418
movq %rax, 0x6a67(%rip)      # 0xd460
leaq 0x29a8(%rip), %rdi      # 0x93a8
callq *0x6a12(%rip)           # 0xd418
movq %rax, 0x6a5b(%rip)      # 0xd468
leaq 0x29a4(%rip), %rdi      # 0x93b8
callq *0x69fe(%rip)           # 0xd418
movq %rax, 0x6a4f(%rip)      # 0xd470
leaq 0x29a3(%rip), %rdi      # 0x93cb
callq *0x69ea(%rip)           # 0xd418
movq %rax, 0x6a43(%rip)      # 0xd478
leaq 0x29a0(%rip), %rdi      # 0x93dc
callq *0x69d6(%rip)           # 0xd418
movq %rax, 0x6a37(%rip)      # 0xd480
leaq 0x2998(%rip), %rdi      # 0x93e8
callq *0x69c2(%rip)           # 0xd418
movq %rax, 0x6a2b(%rip)      # 0xd488
leaq 0x299c(%rip), %rdi      # 0x9400
callq *0x69ae(%rip)           # 0xd418
movq %rax, 0x6a1f(%rip)      # 0xd490
leaq 0x2997(%rip), %rdi      # 0x940f
callq *0x699a(%rip)           # 0xd418
movq %rax, 0x6a13(%rip)      # 0xd498
leaq 0x2992(%rip), %rdi      # 0x941e
callq *0x6986(%rip)           # 0xd418
movq %rax, 0x6a07(%rip)      # 0xd4a0
leaq 0x2989(%rip), %rdi      # 0x9429
callq *0x6972(%rip)           # 0xd418
movq %rax, 0x69fb(%rip)      # 0xd4a8
leaq 0x2987(%rip), %rdi      # 0x943b
callq *0x695e(%rip)           # 0xd418
movq %rax, 0x69ef(%rip)      # 0xd4b0
leaq 0x2980(%rip), %rdi      # 0x9448
callq *0x694a(%rip)           # 0xd418
movq %rax, 0x69e3(%rip)      # 0xd4b8
leaq 0x2979(%rip), %rdi      # 0x9455
callq *0x6936(%rip)           # 0xd418
movq %rax, 0x69d7(%rip)      # 0xd4c0
movq 0x6958(%rip), %rax      # 0xd448
testq %rax, %rax
je 0x6865
callq *%rax
jmp 0x6865
movl %eax, %edi
callq 0x4cdc
jmp 0x69d1
callq 0x4d27
leaq 0x2f49(%rip), %rdi      # 0x9a61
callq 0x41c0
leaq 0x2f3d(%rip), %rdi      # 0x9a61
callq 0x43a0
movq %rax, %rbx
cmpl $0x0, (%r14)
jne 0x6b35
movl %ebp, (%r14)
movq (%rsp), %rdi
cmpq %r13, %rdi
je 0x6b4b
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x42a0
movq %rbx, %rdi
callq 0x44d0