// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
movq %rsi, %rdi
callq 0x6dcc
testq %rax, %rax
je 0x60c6
movq %rbx, %rdi
callq 0x6e34
movq %rax, %r15
movl 0x8(%rax), %edi
imull 0xc(%rax), %edi
shll $0x2, %edi
callq 0x4340
movq %rax, %r12
movq 0x76f8(%rip), %r13      # 0xd490
callq 0x6bef
movq (%r15), %rdx
movq 0x7741(%rip), %rcx      # 0xd4e8
movq %rax, %rdi
movq %rdx, %rsi
callq *%r13
cmpl $0x1, %eax
jne 0x60e5
callq *0x7701(%rip)           # 0xd4c0
movl 0x8(%r15), %edx
movl 0xc(%r15), %ecx
subq $0x8, %rsp
xorl %edi, %edi
xorl %esi, %esi
movl $0x80e1, %r8d           # imm = 0x80E1
movl $0x1401, %r9d           # imm = 0x1401
pushq %r12
callq *0x76d5(%rip)           # 0xd4b8
addq $0x10, %rsp
movq 0x76a2(%rip), %r13      # 0xd490
callq 0x6bef
movq 0x76d6(%rip), %rdx      # 0xd4d0
movq 0x76d7(%rip), %rcx      # 0xd4d8
movq %rax, %rdi
movq %rdx, %rsi
callq *%r13
cmpl $0x1, %eax
jne 0x60ec
movl 0xc(%r15), %eax
cmpl $0x2, %eax
jb 0x5e9a
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0x4, %edx
xorl %esi, %esi
xorl %edi, %edi
movl 0x8(%r15), %r8d
movl %edi, %r9d
imull %r8d, %r9d
shll $0x2, %r9d
incl %edi
movl %edi, %r10d
imull %r8d, %r10d
shll $0x2, %r10d
cmpl %r10d, %r9d
je 0x5e87
addl %ecx, %eax
imull %r8d, %eax
shll $0x2, %eax
addq %r12, %rax
movl %r8d, %r9d
imull %edx, %r9d
imull %esi, %r8d
subq %r8, %r9
addq %r12, %r8
xorl %r10d, %r10d
movb (%r8,%r10), %r11b
movb (%rax,%r10), %bpl
movb %bpl, (%r8,%r10)
movb %r11b, (%rax,%r10)
incq %r10
cmpq %r10, %r9
jne 0x5e6b
movl 0xc(%r15), %eax
movl %eax, %r8d
shrl %r8d
decl %ecx
addl $0x4, %edx
addl $0x4, %esi
cmpl %r8d, %edi
jb 0x5e2a
movl $0x40, %edi
callq 0x4280
movq %rax, %r13
movq 0xe8(%r14), %rax
movl 0x38(%rax), %edx
xorl %ebp, %ebp
movq %r14, %rdi
xorl %esi, %esi
movl $0x4, %ecx
movq %r13, %r8
callq 0x4320
movq (%r13), %rsi
movl 0x8(%r15), %eax
movl 0xc(%r15), %r10d
movq %r14, %rdi
movl $0x18, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
movq %r12, %r9
pushq $0x0
pushq $0x20
pushq %r10
pushq %rax
callq 0x4300
addq $0x20, %rsp
testq %rax, %rax
je 0x60f3
movq %rax, %r12
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4190
movl 0x8(%r15), %r10d
movl 0xc(%r15), %r11d
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r11
pushq %r10
pushq %rbp
pushq %rbp
callq 0x4560
addq $0x20, %rsp
movq %r14, %rdi
callq 0x4420
movq %r12, %rdi
callq *0x60(%r12)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6e93
cmpl %eax, 0x8(%r15)
jne 0x5f67
shrq $0x20, %rax
cmpl %eax, 0xc(%r15)
je 0x60c6
cmpq %rbx, 0x7572(%rip)      # 0xd4e0
jne 0x5f91
movq 0x7519(%rip), %r12      # 0xd490
callq 0x6bef
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq *%r12
cmpl $0x1, %eax
jne 0x60ff
movq 0x7510(%rip), %r12      # 0xd4a8
callq 0x6bef
movq (%r15), %rsi
movq %rax, %rdi
callq *%r12
cmpl $0x1, %eax
jne 0x60f8
movq 0x10(%r15), %r15
movq 0x7356(%rip), %r11      # 0xd310
movq %rbx, %rax
xorl %edx, %edx
divq %r11
movq 0x733f(%rip), %r8       # 0xd308
movq (%r8,%rdx,8), %r10
testq %r10, %r10
je 0x6089
movq %rdx, %rcx
movq (%r10), %rdi
cmpq %rbx, 0x8(%rdi)
jne 0x600c
movq (%rdi), %rsi
testq %rsi, %rsi
je 0x6052
movq %r10, %r9
movq 0x8(%rsi), %rax
xorl %edx, %edx
divq %r11
cmpq %rcx, %rdx
je 0x6072
movq %r10, (%r8,%rdx,8)
movq 0x7302(%rip), %r8       # 0xd308
movq (%r8,%rcx,8), %r10
jmp 0x6057
movq %rdi, %r9
movq (%rdi), %rdi
testq %rdi, %rdi
je 0x6089
movq 0x8(%rdi), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %r11
cmpq %rcx, %rdx
jne 0x6089
cmpq %rbx, %rsi
jne 0x600c
movq (%rdi), %rsi
cmpq %r9, %r10
je 0x60d5
testq %rsi, %rsi
je 0x6072
movq 0x8(%rsi), %rax
xorl %edx, %edx
divq %r11
cmpq %rcx, %rdx
je 0x6072
movq %r9, (%r8,%rdx,8)
jmp 0x6072
xorl %esi, %esi
movq %r10, %r9
leaq 0x72ba(%rip), %rax      # 0xd318
cmpq %rax, %r10
jne 0x606a
movq %rsi, 0x72ae(%rip)      # 0xd318
movq $0x0, (%r8,%rcx,8)
movq (%rdi), %rax
movq %rax, (%r9)
movl $0x28, %esi
callq 0x42a0
decq 0x7297(%rip)            # 0xd320
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x6c8c
cmpq %rbx, 0x7442(%rip)      # 0xd4e0
jne 0x60c6
movq %rax, %r14
movq 0x73e6(%rip), %rbx      # 0xd490
callq 0x6bef
movq 0x7422(%rip), %rcx      # 0xd4d8
movq %rax, %rdi
movq %r14, %rsi
movq %r14, %rdx
callq *%rbx
cmpl $0x1, %eax
jne 0x6106
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %rsi, %rsi
jne 0x5fed
xorl %esi, %esi
jmp 0x6057
movl %eax, %edi
callq 0x4782
movl %eax, %edi
callq 0x47ba
callq 0x489a
movl %eax, %edi
callq 0x482a
movl %eax, %edi
callq 0x47f2
movl %eax, %edi
callq 0x4862