// File: 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:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x120(%rsp)
movq %rsi, 0x118(%rsp)
leaq 0x13002(%rip), %rdi     # 0x2b080
leaq 0x118(%rsp), %rsi
callq 0x1a810
cmpq $0x0, %rax
jne 0x18096
jmp 0x186f1
leaq 0x12fe3(%rip), %rdi     # 0x2b080
leaq 0x118(%rsp), %rsi
callq 0x1a840
movq %rax, 0x110(%rsp)
movq 0x110(%rsp), %rax
movl 0x8(%rax), %eax
shll $0x2, %eax
movq 0x110(%rsp), %rcx
imull 0xc(%rcx), %eax
movl %eax, %eax
movq %rax, 0x108(%rsp)
movq 0x108(%rsp), %rdi
callq 0x16450
movq %rax, 0x100(%rsp)
movq 0x13116(%rip), %rax     # 0x2b208
movq %rax, 0x98(%rsp)
callq 0x1a260
movq %rax, %rdi
movq 0x98(%rsp), %rax
movq 0x110(%rsp), %rcx
movq (%rcx), %rsi
movq 0x110(%rsp), %rcx
movq (%rcx), %rdx
movq 0x13139(%rip), %rcx     # 0x2b260
callq *%rax
movl %eax, 0xfc(%rsp)
cmpl $0x1, 0xfc(%rsp)
je 0x1818c
movq 0x11e97(%rip), %rax     # 0x29fd8
movq (%rax), %rax
movq %rax, 0x88(%rsp)
movl 0xfc(%rsp), %eax
movl %eax, 0x94(%rsp)
callq *0x13098(%rip)          # 0x2b1f8
movq 0x88(%rsp), %rdi
movl 0x94(%rsp), %ecx
movl %eax, %r8d
leaq 0xb318(%rip), %rsi      # 0x23491
leaq 0xb4e6(%rip), %rdx      # 0x23666
movb $0x0, %al
callq 0x16680
callq 0x153b0
jmp 0x1818e
callq *0x130a4(%rip)          # 0x2b238
movq 0x13095(%rip), %rax     # 0x2b230
movq 0x110(%rsp), %rcx
movl 0x8(%rcx), %edx
movq 0x110(%rsp), %rcx
movl 0xc(%rcx), %ecx
movq 0x100(%rsp), %r10
xorl %esi, %esi
movl $0x80e1, %r8d           # imm = 0x80E1
movl $0x1401, %r9d           # imm = 0x1401
movl %esi, %edi
movq %r10, (%rsp)
callq *%rax
movq 0x13032(%rip), %rax     # 0x2b208
movq %rax, 0x80(%rsp)
callq 0x1a260
movq %rax, %rdi
movq 0x80(%rsp), %rax
movq 0x13053(%rip), %rsi     # 0x2b248
movq 0x1304c(%rip), %rdx     # 0x2b248
movq 0x1304d(%rip), %rcx     # 0x2b250
callq *%rax
movl %eax, 0xf8(%rsp)
cmpl $0x1, 0xf8(%rsp)
je 0x1825c
movq 0x11dbb(%rip), %rax     # 0x29fd8
movq (%rax), %rax
movq %rax, 0x70(%rsp)
movl 0xf8(%rsp), %eax
movl %eax, 0x7c(%rsp)
callq *0x12fc2(%rip)          # 0x2b1f8
movq 0x70(%rsp), %rdi
movl 0x7c(%rsp), %ecx
movl %eax, %r8d
leaq 0xb248(%rip), %rsi      # 0x23491
leaq 0xb45e(%rip), %rdx      # 0x236ae
movb $0x0, %al
callq 0x16680
callq 0x153b0
jmp 0x1825e
movl $0x0, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
movq 0x110(%rsp), %rcx
movl 0xc(%rcx), %ecx
shrl %ecx
cmpl %ecx, %eax
jae 0x1831a
movq 0x100(%rsp), %rdi
movl 0xf4(%rsp), %eax
movq 0x110(%rsp), %rcx
imull 0x8(%rcx), %eax
shll $0x2, %eax
movl %eax, %eax
addq %rax, %rdi
movq 0x100(%rsp), %rsi
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movq 0x110(%rsp), %rcx
imull 0x8(%rcx), %eax
shll $0x2, %eax
movl %eax, %eax
addq %rax, %rsi
movq 0x100(%rsp), %rdx
movq 0x110(%rsp), %rax
movl 0xc(%rax), %eax
subl $0x1, %eax
subl 0xf4(%rsp), %eax
movq 0x110(%rsp), %rcx
imull 0x8(%rcx), %eax
shll $0x2, %eax
movl %eax, %eax
addq %rax, %rdx
callq 0x154c0
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0x18269
movq 0x120(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x120(%rsp), %rdi
xorl %esi, %esi
callq 0x1a060
movq 0x68(%rsp), %rdi
movq (%rax), %rsi
movq 0x100(%rsp), %r9
movq 0x110(%rsp), %rax
movl 0x8(%rax), %r10d
movq 0x110(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x18, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movl $0x20, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
callq 0x16330
movq %rax, 0xe8(%rsp)
cmpq $0x0, 0xe8(%rsp)
jne 0x183ca
movq 0x11c37(%rip), %rax     # 0x29fd8
movq (%rax), %rdi
leaq 0xb18b(%rip), %rsi      # 0x23536
leaq 0xb1a9(%rip), %rdx      # 0x2355b
movl $0x11f, %ecx            # imm = 0x11F
leaq 0xb334(%rip), %r8       # 0x236f2
movb $0x0, %al
callq 0x16680
callq 0x153b0
jmp 0x183cc
movq 0x120(%rsp), %rdi
movq 0x118(%rsp), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x157e0
movq %rax, 0xe0(%rsp)
movq 0x120(%rsp), %rdi
movq 0x118(%rsp), %rsi
movq 0xe0(%rsp), %rdx
movq 0xe8(%rsp), %rcx
movq 0x110(%rsp), %rax
movl 0x8(%rax), %r10d
movq 0x110(%rsp), %rax
movl 0xc(%rax), %eax
xorl %r9d, %r9d
movl %r9d, %r8d
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
callq 0x16fb0
movq 0x120(%rsp), %rdi
callq 0x16870
movq 0xe8(%rsp), %rax
movq 0x60(%rax), %rax
movq 0xe8(%rsp), %rdi
callq *%rax
movq 0x120(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1a870
movq %rax, 0xd0(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0xdc(%rsp), %rsi
leaq 0xd8(%rsp), %rdx
callq 0x165e0
leaq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x15e10
movl 0xdc(%rsp), %eax
movq 0x110(%rsp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x184e8
movl 0xd8(%rsp), %eax
movq 0x110(%rsp), %rcx
cmpl 0xc(%rcx), %eax
je 0x186f1
movq 0x12d69(%rip), %rax     # 0x2b258
cmpq 0x118(%rsp), %rax
jne 0x1857d
jmp 0x184ff
movq 0x12d02(%rip), %rax     # 0x2b208
movq %rax, 0x60(%rsp)
callq 0x1a260
movq %rax, %rdi
movq 0x60(%rsp), %rax
xorl %ecx, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq *%rax
movl %eax, 0xbc(%rsp)
cmpl $0x1, 0xbc(%rsp)
je 0x18579
movq 0x11a9e(%rip), %rax     # 0x29fd8
movq (%rax), %rax
movq %rax, 0x50(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0x5c(%rsp)
callq *0x12ca5(%rip)          # 0x2b1f8
movq 0x50(%rsp), %rdi
movl 0x5c(%rsp), %ecx
movl %eax, %r8d
leaq 0xaf2b(%rip), %rsi      # 0x23491
leaq 0xb194(%rip), %rdx      # 0x23701
movb $0x0, %al
callq 0x16680
callq 0x153b0
jmp 0x1857b
jmp 0x1857d
jmp 0x1857f
movq 0x12c9a(%rip), %rax     # 0x2b220
movq %rax, 0x48(%rsp)
callq 0x1a260
movq %rax, %rdi
movq 0x48(%rsp), %rax
movq 0x110(%rsp), %rcx
movq (%rcx), %rsi
callq *%rax
movl %eax, 0xb8(%rsp)
cmpl $0x1, 0xb8(%rsp)
je 0x185fc
movq 0x11a1b(%rip), %rax     # 0x29fd8
movq (%rax), %rax
movq %rax, 0x38(%rsp)
movl 0xb8(%rsp), %eax
movl %eax, 0x44(%rsp)
callq *0x12c22(%rip)          # 0x2b1f8
movq 0x38(%rsp), %rdi
movl 0x44(%rsp), %ecx
movl %eax, %r8d
leaq 0xaea8(%rip), %rsi      # 0x23491
leaq 0xb15b(%rip), %rdx      # 0x2374b
movb $0x0, %al
callq 0x16680
callq 0x153b0
jmp 0x185fe
movq 0x110(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0xb0(%rsp)
leaq 0x12a67(%rip), %rdi     # 0x2b080
leaq 0x118(%rsp), %rsi
callq 0x1b8f0
movq 0x120(%rsp), %rdi
movq 0x118(%rsp), %rsi
movq 0xb0(%rsp), %rdx
callq 0x1a4e0
movq %rax, 0xa8(%rsp)
movq 0x12c06(%rip), %rax     # 0x2b258
cmpq 0x118(%rsp), %rax
jne 0x186ef
jmp 0x18662
movq 0x12b9f(%rip), %rax     # 0x2b208
movq %rax, 0x30(%rsp)
callq 0x1a260
movq %rax, %rdi
movq 0x30(%rsp), %rax
movq 0xa8(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq 0x12bbe(%rip), %rcx     # 0x2b250
callq *%rax
movl %eax, 0xa4(%rsp)
cmpl $0x1, 0xa4(%rsp)
je 0x186eb
movq 0x1192c(%rip), %rax     # 0x29fd8
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x2c(%rsp)
callq *0x12b33(%rip)          # 0x2b1f8
movq 0x20(%rsp), %rdi
movl 0x2c(%rsp), %ecx
movl %eax, %r8d
leaq 0xadb9(%rip), %rsi      # 0x23491
leaq 0xb096(%rip), %rdx      # 0x23775
movb $0x0, %al
callq 0x16680
callq 0x153b0
jmp 0x186ed
jmp 0x186ef
jmp 0x186f1
addq $0x128, %rsp            # imm = 0x128
retq
nopl (%rax)