// File: /workspace/github_cpp_makefile_star2_O2/gatecat[P]subprime/subprime.cpp
// Function: (anonymous namespace)::glx_swap_buffers(_XDisplay*, unsigned long)
void glx_swap_buffers(Display *dpy, GLXDrawable drawable) {
	if (!drawable2surface.count(drawable))
		return;
	auto &sfc = drawable2surface.at(drawable);
	size_t buf_size = 4 * sfc.width * sfc.height;
	uint8_t *pixel_buf = reinterpret_cast<uint8_t*>(malloc(buf_size));
	// Make sure the surface is current
	SP_CHECK(fn_eglMakeCurrent(disp(), sfc.egl_sfc, sfc.egl_sfc, last_valid_context));
	fn_glFinish();
	fn_glReadPixels(0, 0, sfc.width, sfc.height, GL_BGRA, GL_UNSIGNED_BYTE, pixel_buf);
	SP_CHECK(fn_eglMakeCurrent(disp(), curr_surface, curr_surface, curr_context));
	// Unmirror
	for (int y = 0; y < (sfc.height / 2); y++) {
		std::swap_ranges(pixel_buf + (y * sfc.width * 4), pixel_buf + ((y + 1) * sfc.width * 4),
			pixel_buf + (((sfc.height - 1) - y) * sfc.width * 4));
	}
	XImage *img = XCreateImage(dpy, get_visual(dpy, 0)->visual, 24, ZPixmap, 0,
		reinterpret_cast<char*>(pixel_buf), sfc.width, sfc.height, 32, 0);
	SP_ASSERT(img != nullptr);
	GC gc = XCreateGC(dpy, drawable, 0, NULL);
	XPutImage(dpy, drawable, gc, img, 0, 0, 0, 0, sfc.width, sfc.height);
	XFlush(dpy);
	XDestroyImage(img);
	// Also, check if the buffer was resized
	unsigned new_width, new_height;
	std::tie(new_width, new_height) = get_drawable_dims(dpy, drawable);
	if (new_width != sfc.width || new_height != sfc.height) {
		// Destroy old surface and recreate with new size
		if (curr_drawable == drawable)
			SP_CHECK(fn_eglMakeCurrent(disp(), EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
		SP_CHECK(fn_eglDestroySurface(disp(), sfc.egl_sfc));
		GLXFBConfig cfg = sfc.cfg;
		drawable2surface.erase(drawable);
		EGLSurface new_sfc = lookup_drawable(dpy, drawable, cfg);
		// If it was current before, make it current again
		if (curr_drawable == drawable)
			SP_CHECK(fn_eglMakeCurrent(disp(), new_sfc, new_sfc, curr_context));
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x8(%rsp), %rdi
movq %rsi, (%rdi)
callq 0x985e
testq %rax, %rax
je 0x87d4
movq %rbx, 0x10(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x9884
movq %rax, %r15
movl 0x8(%rax), %edi
imull 0xc(%rax), %edi
shll $0x2, %edi
callq 0x7570
movq %rax, %r12
movq 0x8160(%rip), %rbx      # 0x10690
callq 0x95e6
movq (%r15), %rdx
movq 0x81a9(%rip), %rcx      # 0x106e8
movq %rax, %rdi
movq %rdx, %rsi
callq *%rbx
cmpl $0x1, %eax
jne 0x87e3
callq *0x816a(%rip)           # 0x106c0
movl 0x8(%r15), %edx
movl 0xc(%r15), %ecx
subq $0x8, %rsp
xorl %r13d, %r13d
xorl %edi, %edi
xorl %esi, %esi
movl $0x80e1, %r8d           # imm = 0x80E1
movl $0x1401, %r9d           # imm = 0x1401
pushq %r12
callq *0x813b(%rip)           # 0x106b8
addq $0x10, %rsp
movq 0x8108(%rip), %rbx      # 0x10690
callq 0x95e6
movq 0x813c(%rip), %rdx      # 0x106d0
movq 0x813d(%rip), %rcx      # 0x106d8
movq %rax, %rdi
movq %rdx, %rsi
callq *%rbx
cmpl $0x1, %eax
jne 0x8808
pushq $-0x1
popq %rbp
xorl %ebx, %ebx
movl 0xc(%r15), %edx
movl %edx, %eax
shrl %eax
cmpl %eax, %ebx
jae 0x85ed
movl 0x8(%r15), %eax
movl %r13d, %edi
imull %eax, %edi
addq %r12, %rdi
incl %ebx
addl $0x4, %r13d
movl %r13d, %esi
imull %eax, %esi
addq %r12, %rsi
addl %ebp, %edx
imull %eax, %edx
shll $0x2, %edx
addq %r12, %rdx
callq 0x7140
decl %ebp
jmp 0x85b1
movq %r14, %rdi
xorl %esi, %esi
callq 0x94e5
movq (%rax), %rsi
movl 0x8(%r15), %eax
movl 0xc(%r15), %r10d
pushq $0x18
popq %rdx
pushq $0x2
popq %rcx
movq %r14, %rdi
xorl %r8d, %r8d
movq %r12, %r9
pushq $0x0
pushq $0x20
pushq %r10
pushq %rax
callq 0x7520
addq $0x20, %rsp
testq %rax, %rax
je 0x882a
movq %rax, %r12
movq %r14, %rdi
movq 0x10(%rsp), %r13
movq %r13, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x7240
movl 0x8(%r15), %r10d
movl 0xc(%r15), %r11d
movq %r14, %rdi
movq %r13, %rsi
movq %rax, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r11
pushq %r10
pushq $0x0
pushq $0x0
callq 0x7950
addq $0x20, %rsp
movq %r14, %rdi
callq 0x7700
movq %r12, %rdi
callq *0x60(%r12)
movq %r14, %rdi
movq %r13, %rsi
callq 0x98b3
cmpl %eax, 0x8(%r15)
jne 0x869b
shrq $0x20, %rax
cmpl %eax, 0xc(%r15)
je 0x87d4
cmpq %r13, 0x803e(%rip)      # 0x106e0
jne 0x86c4
movq 0x7fe5(%rip), %rbx      # 0x10690
callq 0x95e6
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq *%rbx
cmpl $0x1, %eax
jne 0x887c
movq 0x7fdd(%rip), %rbx      # 0x106a8
callq 0x95e6
movq (%r15), %rsi
movq %rax, %rdi
callq *%rbx
cmpl $0x1, %eax
jne 0x885a
movq 0x10(%r15), %r15
movq 0x7e24(%rip), %rbx      # 0x10510
movq %r13, %rax
xorl %edx, %edx
divq %rbx
movq %rdx, %r12
leaq 0x8(%rsp), %rsi
movq %rdx, %rdi
callq 0x9b50
testq %rax, %rax
je 0x8793
movq %rax, %rcx
movq (%rax), %rdi
movq 0x7dee(%rip), %rsi      # 0x10508
movq (%rsi,%r12,8), %r9
movq (%rdi), %r8
cmpq %rax, %r9
je 0x873f
testq %r8, %r8
je 0x877e
movq 0x8(%r8), %rax
xorl %edx, %edx
divq %rbx
cmpq %r12, %rdx
je 0x877e
movq %rcx, (%rsi,%rdx,8)
jmp 0x877e
testq %r8, %r8
je 0x8763
movq 0x8(%r8), %rax
xorl %edx, %edx
divq %rbx
cmpq %r12, %rdx
je 0x877e
movq %r9, (%rsi,%rdx,8)
movq 0x7dab(%rip), %rsi      # 0x10508
movq (%rsi,%r12,8), %rax
jmp 0x8766
movq %rcx, %rax
leaq 0x7dab(%rip), %rdx      # 0x10518
cmpq %rdx, %rax
jne 0x8779
movq %r8, 0x7d9f(%rip)       # 0x10518
andq $0x0, (%rsi,%r12,8)
movq (%rdi), %rax
movq %rax, (%rcx)
pushq $0x28
popq %rsi
callq 0x7450
decq 0x7d8d(%rip)            # 0x10520
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x9739
cmpq %r13, 0x7f38(%rip)      # 0x106e0
jne 0x87d4
movq %rax, %r14
movq 0x7edc(%rip), %rbx      # 0x10690
callq 0x95e6
movq 0x7f18(%rip), %rcx      # 0x106d8
movq %rax, %rdi
movq %r14, %rsi
movq %r14, %rdx
callq *%rbx
cmpl $0x1, %eax
jne 0x88b0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %ebp
movq 0x77ec(%rip), %rax      # 0xffd8
movq (%rax), %rbx
callq *0x7e8b(%rip)           # 0x10680
leaq 0x3ca1(%rip), %rsi      # 0xc49d
leaq 0x3e6f(%rip), %rdx      # 0xc672
jmp 0x889c
movl %eax, %ebp
movq 0x77c7(%rip), %rax      # 0xffd8
movq (%rax), %rbx
callq *0x7e66(%rip)           # 0x10680
leaq 0x3c7c(%rip), %rsi      # 0xc49d
leaq 0x3e92(%rip), %rdx      # 0xc6ba
jmp 0x889c
movq 0x77a7(%rip), %rax      # 0xffd8
movq (%rax), %rdi
leaq 0x3d07(%rip), %rsi      # 0xc542
leaq 0x3d25(%rip), %rdx      # 0xc567
leaq 0x3eb5(%rip), %r8       # 0xc6fe
movl $0x11f, %ecx            # imm = 0x11F
xorl %eax, %eax
callq 0x7610
callq 0x70f0
movl %eax, %ebp
movq 0x7775(%rip), %rax      # 0xffd8
movq (%rax), %rbx
callq *0x7e14(%rip)           # 0x10680
leaq 0x3c2a(%rip), %rsi      # 0xc49d
leaq 0x3edd(%rip), %rdx      # 0xc757
jmp 0x889c
movl %eax, %ebp
movq 0x7753(%rip), %rax      # 0xffd8
movq (%rax), %rbx
callq *0x7df2(%rip)           # 0x10680
leaq 0x3c08(%rip), %rsi      # 0xc49d
leaq 0x3e71(%rip), %rdx      # 0xc70d
movq %rbx, %rdi
movl %ebp, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x7610
callq 0x70f0
movl %eax, %ebx
movq 0x771f(%rip), %rax      # 0xffd8
movq (%rax), %r14
callq *0x7dbe(%rip)           # 0x10680
leaq 0x3bd4(%rip), %rsi      # 0xc49d
leaq 0x3eb1(%rip), %rdx      # 0xc781
movq %r14, %rdi
movl %ebx, %ecx
jmp 0x88a1