// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_ovpage_free
static int
mdb_ovpage_free(MDB_cursor *mc, MDB_page *mp)
{
	MDB_txn *txn = mc->mc_txn;
	pgno_t pg = mp->mp_pgno;
	unsigned x = 0, ovpages = mp->mp_pages;
	MDB_env *env = txn->mt_env;
	MDB_IDL sl = txn->mt_spill_pgs;
	MDB_ID pn = pg << 1;
	int rc;

	DPRINTF(("free ov page %"Z"u (%d)", pg, ovpages));
	/* If the page is dirty or on the spill list we just acquired it,
	 * so we should give it back to our current free list, if any.
	 * Otherwise put it onto the list of pages we freed in this txn.
	 *
	 * Won't create me_pghead: me_pglast must be inited along with it.
	 * Unsupported in nested txns: They would need to hide the page
	 * range in ancestor txns' dirty and spilled lists.
	 */
	if (env->me_pghead &&
		!txn->mt_parent &&
		((mp->mp_flags & P_DIRTY) ||
		 (sl && (x = mdb_midl_search(sl, pn)) <= sl[0] && sl[x] == pn)))
	{
		unsigned i, j;
		pgno_t *mop;
		MDB_ID2 *dl, ix, iy;
		rc = mdb_midl_need(&env->me_pghead, ovpages);
		if (rc)
			return rc;
		if (!(mp->mp_flags & P_DIRTY)) {
			/* This page is no longer spilled */
			if (x == sl[0])
				sl[0]--;
			else
				sl[x] |= 1;
			goto release;
		}
		/* Remove from dirty list */
		dl = txn->mt_u.dirty_list;
		x = dl[0].mid--;
		for (ix = dl[x]; ix.mptr != mp; ix = iy) {
			if (x > 1) {
				x--;
				iy = dl[x];
				dl[x] = ix;
			} else {
				mdb_cassert(mc, x > 1);
				j = ++(dl[0].mid);
				dl[j] = ix;		/* Unsorted. OK when MDB_TXN_ERROR. */
				txn->mt_flags |= MDB_TXN_ERROR;
				return MDB_CORRUPTED;
			}
		}
		if (!(env->me_flags & MDB_WRITEMAP))
			mdb_dpage_free(env, mp);
release:
		/* Insert in me_pghead */
		mop = env->me_pghead;
		j = mop[0] + ovpages;
		for (i = mop[0]; i && mop[i] < pg; i--)
			mop[j--] = mop[i];
		while (j>i)
			mop[j--] = pg++;
		mop[0] += ovpages;
	} else {
		rc = mdb_midl_append_range(&txn->mt_free_pgs, pg, ovpages);
		if (rc)
			return rc;
	}
	mc->mc_db->md_overflow_pages -= ovpages;
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
shlq %rax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
cmpq $0x0, 0xb0(%rax)
je 0xaf2d
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jne 0xaf2d
movq -0x18(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0xacad
cmpq $0x0, -0x40(%rbp)
je 0xaf2d
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x125d0
movl %eax, -0x2c(%rbp)
movl %eax, %eax
movq -0x40(%rbp), %rcx
cmpq (%rcx), %rax
ja 0xaf2d
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
cmpq -0x48(%rbp), %rax
jne 0xaf2d
movq -0x38(%rbp), %rdi
addq $0xb0, %rdi
movl -0x30(%rbp), %esi
callq 0x127b0
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0xacd4
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xaf73
movq -0x18(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0xad18
movl -0x2c(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpq (%rcx), %rax
jne 0xad00
movq -0x40(%rbp), %rax
movq (%rax), %rcx
addq $-0x1, %rcx
movq %rcx, (%rax)
jmp 0xad13
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rdx
orq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0xae66
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rcx
movq (%rcx), %rax
movq %rax, %rdx
addq $-0x1, %rdx
movq %rdx, (%rcx)
movl %eax, -0x2c(%rbp)
movq -0x68(%rbp), %rax
movl -0x2c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x78(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x18(%rbp), %rax
je 0xae46
cmpl $0x1, -0x2c(%rbp)
jbe 0xadb1
movl -0x2c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x68(%rbp), %rax
movl -0x2c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x88(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x68(%rbp), %rax
movl -0x2c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x78(%rbp), %rcx
movq %rcx, (%rax)
movq -0x70(%rbp), %rcx
movq %rcx, 0x8(%rax)
jmp 0xae2c
cmpl $0x1, -0x2c(%rbp)
jbe 0xadb9
jmp 0xade5
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rdi
leaq 0xcd68(%rip), %rsi      # 0x17b34
leaq 0xcd67(%rip), %rdx      # 0x17b3a
leaq 0xc635(%rip), %rcx      # 0x1740f
movl $0x1597, %r8d           # imm = 0x1597
callq 0x133a0
movq -0x68(%rbp), %rcx
movq (%rcx), %rax
addq $0x1, %rax
movq %rax, (%rcx)
movl %eax, -0x54(%rbp)
movq -0x68(%rbp), %rax
movl -0x54(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x78(%rbp), %rcx
movq %rcx, (%rax)
movq -0x70(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movl 0x7c(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x7c(%rax)
movl $0xffff87b4, -0x4(%rbp) # imm = 0xFFFF87B4
jmp 0xaf73
jmp 0xae2e
movq -0x88(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0xad55
movq -0x38(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x80000, %eax          # imm = 0x80000
cmpl $0x0, %eax
jne 0xae64
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xee40
jmp 0xae66
movq -0x38(%rbp), %rax
movq 0xb0(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
addq %rcx, %rax
movl %eax, -0x54(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x50(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x50(%rbp)
movb %al, -0x89(%rbp)
je 0xaeb5
movq -0x60(%rbp), %rax
movl -0x50(%rbp), %ecx
movq (%rax,%rcx,8), %rax
cmpq -0x28(%rbp), %rax
setb %al
movb %al, -0x89(%rbp)
movb -0x89(%rbp), %al
testb $0x1, %al
jne 0xaec1
jmp 0xaeec
movq -0x60(%rbp), %rax
movl -0x50(%rbp), %ecx
movq (%rax,%rcx,8), %rdx
movq -0x60(%rbp), %rax
movl -0x54(%rbp), %ecx
movl %ecx, %esi
addl $-0x1, %esi
movl %esi, -0x54(%rbp)
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x50(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0xae8f
jmp 0xaeee
movl -0x54(%rbp), %eax
cmpl -0x50(%rbp), %eax
jbe 0xaf1c
movq -0x28(%rbp), %rdx
movq %rdx, %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x60(%rbp), %rax
movl -0x54(%rbp), %ecx
movl %ecx, %esi
addl $-0x1, %esi
movl %esi, -0x54(%rbp)
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
jmp 0xaeee
movl -0x30(%rbp), %eax
movl %eax, %ecx
movq -0x60(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
jmp 0xaf54
movq -0x20(%rbp), %rdi
addq $0x28, %rdi
movq -0x28(%rbp), %rsi
movl -0x30(%rbp), %edx
callq 0x12a30
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0xaf52
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xaf73
jmp 0xaf54
movl -0x30(%rbp), %eax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x18(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x18(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nop