// File: /workspace/github_cpp_makefile_star2_O1/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 %rsp, %rdi
movq %rsi, (%rdi)
callq 0x6e80
testq %rax, %rax
je 0x60e7
movq %rsp, %rdi
callq 0x6eae
movq %rax, %r15
movl 0x8(%rax), %edi
imull 0xc(%rax), %edi
shll $0x2, %edi
callq 0x4330
movq %rax, %r12
movq 0x76f5(%rip), %r13      # 0xd490
callq 0x6ca2
movq (%r15), %rdx
movq 0x773e(%rip), %rcx      # 0xd4e8
movq %rax, %rdi
movq %rdx, %rsi
callq *%r13
cmpl $0x1, %eax
jne 0x60f6
callq *0x76fe(%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 *0x76d2(%rip)           # 0xd4b8
addq $0x10, %rsp
movq 0x769f(%rip), %r13      # 0xd490
callq 0x6ca2
movq 0x76d3(%rip), %rdx      # 0xd4d0
movq 0x76d4(%rip), %rcx      # 0xd4d8
movq %rax, %rdi
movq %rdx, %rsi
callq *%r13
cmpl $0x1, %eax
jne 0x60fd
movl 0xc(%r15), %eax
cmpl $0x2, %eax
jb 0x5e9d
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 0x5e86
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 0x5e6e
movl 0xc(%r15), %eax
movl %eax, %r8d
shrl %r8d
decl %ecx
addl $0x4, %edx
addl $0x4, %esi
cmpl %r8d, %edi
jb 0x5e2d
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 0x4310
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 0x42f0
addq $0x20, %rsp
testq %rax, %rax
je 0x6104
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 0x4410
movq %r12, %rdi
callq *0x60(%r12)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6eea
cmpl %eax, 0x8(%r15)
jne 0x5f6a
shrq $0x20, %rax
cmpl %eax, 0xc(%r15)
je 0x60e7
cmpq %rbx, 0x756f(%rip)      # 0xd4e0
jne 0x5f94
movq 0x7516(%rip), %r12      # 0xd490
callq 0x6ca2
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq *%r12
cmpl $0x1, %eax
jne 0x6110
movq 0x750d(%rip), %r12      # 0xd4a8
callq 0x6ca2
movq (%r15), %rsi
movq %rax, %rdi
callq *%r12
cmpl $0x1, %eax
jne 0x6109
movq 0x10(%r15), %r15
movq 0x7353(%rip), %r11      # 0xd310
xorl %r9d, %r9d
movq %rbx, %rax
xorl %edx, %edx
divq %r11
movq %rdx, %rcx
movq 0x7336(%rip), %r8       # 0xd308
movq (%r8,%rdx,8), %r10
testq %r10, %r10
je 0x6016
movq (%r10), %rdi
movq %r10, %r9
cmpq %rbx, 0x8(%rdi)
je 0x6016
movq %rdi, %r12
movq (%rdi), %rdi
testq %rdi, %rdi
je 0x6013
movq 0x8(%rdi), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %r11
movl $0x0, %r9d
cmpq %rcx, %rdx
jne 0x6016
movq %r12, %r9
cmpq %rbx, %rsi
jne 0x5fe7
jmp 0x6016
xorl %r9d, %r9d
testq %r9, %r9
je 0x60aa
movq (%r9), %rdi
movq (%rdi), %rsi
cmpq %r9, %r10
je 0x6043
testq %rsi, %rsi
je 0x6093
movq 0x8(%rsi), %rax
xorl %edx, %edx
divq %r11
cmpq %rcx, %rdx
je 0x6093
movq %r9, (%r8,%rdx,8)
jmp 0x6093
testq %rsi, %rsi
je 0x6053
movq 0x8(%rsi), %rax
xorl %edx, %edx
divq %r11
jmp 0x6055
xorl %edx, %edx
testq %rsi, %rsi
setne %al
cmpq %rcx, %rdx
sete %r11b
testb %r11b, %al
jne 0x6093
testq %rsi, %rsi
je 0x6070
movq %r10, (%r8,%rdx,8)
movq 0x7291(%rip), %rax      # 0xd308
leaq 0x729a(%rip), %rdx      # 0xd318
cmpq %rdx, (%rax,%rcx,8)
jne 0x608b
movq %rsi, 0x728d(%rip)      # 0xd318
movq $0x0, (%rax,%rcx,8)
movq (%rdi), %rax
movq %rax, (%r9)
movl $0x28, %esi
callq 0x42a0
decq 0x7276(%rip)            # 0xd320
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x6d3f
cmpq %rbx, 0x7421(%rip)      # 0xd4e0
jne 0x60e7
movq %rax, %r14
movq 0x73c5(%rip), %rbx      # 0xd490
callq 0x6ca2
movq 0x7401(%rip), %rcx      # 0xd4d8
movq %rax, %rdi
movq %r14, %rsi
movq %r14, %rdx
callq *%rbx
cmpl $0x1, %eax
jne 0x6117
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
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