// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_page_unspill
static int
mdb_page_unspill(MDB_txn *txn, MDB_page *mp, MDB_page **ret)
{
	MDB_env *env = txn->mt_env;
	const MDB_txn *tx2;
	unsigned x;
	pgno_t pgno = mp->mp_pgno, pn = pgno << 1;

	for (tx2 = txn; tx2; tx2=tx2->mt_parent) {
		if (!tx2->mt_spill_pgs)
			continue;
		x = mdb_midl_search(tx2->mt_spill_pgs, pn);
		if (x <= tx2->mt_spill_pgs[0] && tx2->mt_spill_pgs[x] == pn) {
			MDB_page *np;
			int num;
			if (txn->mt_dirty_room == 0)
				return MDB_TXN_FULL;
			if (IS_OVERFLOW(mp))
				num = mp->mp_pages;
			else
				num = 1;
			if (env->me_flags & MDB_WRITEMAP) {
				np = mp;
			} else {
				np = mdb_page_malloc(txn, num);
				if (!np)
					return ENOMEM;
				if (num > 1)
					memcpy(np, mp, num * env->me_psize);
				else
					mdb_page_copy(np, mp, env->me_psize);
			}
			if (tx2 == txn) {
				/* If in current txn, this page is no longer spilled.
				 * If it happens to be the last page, truncate the spill list.
				 * Otherwise mark it as deleted by setting the LSB.
				 */
				if (x == txn->mt_spill_pgs[0])
					txn->mt_spill_pgs[0]--;
				else
					txn->mt_spill_pgs[x] |= 1;
			}	/* otherwise, if belonging to a parent txn, the
				 * page remains spilled until child commits
				 */

			mdb_page_dirty(txn, np);
			np->mp_flags |= P_DIRTY;
			*ret = np;
			break;
		}
	}
	return MDB_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
shlq %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xaad0
movq -0x30(%rbp), %rax
cmpq $0x0, 0x40(%rax)
jne 0xa959
jmp 0xaac0
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rdi
movq -0x48(%rbp), %rsi
callq 0x125d0
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x30(%rbp), %rcx
movq 0x40(%rcx), %rcx
cmpq (%rcx), %rax
ja 0xaabe
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
cmpq -0x48(%rbp), %rax
jne 0xaabe
movq -0x10(%rbp), %rax
cmpl $0x0, 0x80(%rax)
jne 0xa9b3
movl $0xffff87bc, -0x4(%rbp) # imm = 0xFFFF87BC
jmp 0xaad7
movq -0x18(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x4, %eax
cmpl $0x4, %eax
jne 0xa9cf
movq -0x18(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x54(%rbp)
jmp 0xa9d6
movl $0x1, -0x54(%rbp)
movq -0x28(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x80000, %eax          # imm = 0x80000
cmpl $0x0, %eax
je 0xa9f1
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0xaa4e
movq -0x10(%rbp), %rdi
movl -0x54(%rbp), %esi
callq 0xaae0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0xaa14
movl $0xc, -0x4(%rbp)
jmp 0xaad7
cmpl $0x1, -0x54(%rbp)
jle 0xaa38
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x54(%rbp), %eax
movq -0x28(%rbp), %rcx
imull 0x10(%rcx), %eax
movl %eax, %eax
movl %eax, %edx
callq 0x2230
jmp 0xaa4c
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rax
movl 0x10(%rax), %edx
callq 0xfd30
jmp 0xaa4e
movq -0x30(%rbp), %rax
cmpq -0x10(%rbp), %rax
jne 0xaa95
movl -0x34(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x40(%rcx), %rcx
cmpq (%rcx), %rax
jne 0xaa7c
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq (%rax), %rcx
addq $-0x1, %rcx
movq %rcx, (%rax)
jmp 0xaa93
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rdx
orq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0xaa95
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x103e0
movq -0x50(%rbp), %rax
movzwl 0xa(%rax), %ecx
orl $0x10, %ecx
movw %cx, 0xa(%rax)
movq -0x50(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
jmp 0xaad0
jmp 0xaac0
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0xa93e
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq