// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_page_spill
static int
mdb_page_spill(MDB_cursor *m0, MDB_val *key, MDB_val *data)
{
	MDB_txn *txn = m0->mc_txn;
	MDB_page *dp;
	MDB_ID2L dl = txn->mt_u.dirty_list;
	unsigned int i, j, need;
	int rc;

	if (m0->mc_flags & C_SUB)
		return MDB_SUCCESS;

	/* Estimate how much space this op will take */
	i = m0->mc_db->md_depth;
	/* Named DBs also dirty the main DB */
	if (m0->mc_dbi >= CORE_DBS)
		i += txn->mt_dbs[MAIN_DBI].md_depth;
	/* For puts, roughly factor in the key+data size */
	if (key)
		i += (LEAFSIZE(key, data) + txn->mt_env->me_psize) / txn->mt_env->me_psize;
	i += i;	/* double it for good measure */
	need = i;

	if (txn->mt_dirty_room > i)
		return MDB_SUCCESS;

	if (!txn->mt_spill_pgs) {
		txn->mt_spill_pgs = mdb_midl_alloc(MDB_IDL_UM_MAX);
		if (!txn->mt_spill_pgs)
			return ENOMEM;
	} else {
		/* purge deleted slots */
		MDB_IDL sl = txn->mt_spill_pgs;
		unsigned int num = sl[0];
		j=0;
		for (i=1; i<=num; i++) {
			if (!(sl[i] & 1))
				sl[++j] = sl[i];
		}
		sl[0] = j;
	}

	/* Preserve pages which may soon be dirtied again */
	if ((rc = mdb_pages_xkeep(m0, P_DIRTY, 1)) != MDB_SUCCESS)
		goto done;

	/* Less aggressive spill - we originally spilled the entire dirty list,
	 * with a few exceptions for cursor pages and DB root pages. But this
	 * turns out to be a lot of wasted effort because in a large txn many
	 * of those pages will need to be used again. So now we spill only 1/8th
	 * of the dirty pages. Testing revealed this to be a good tradeoff,
	 * better than 1/2, 1/4, or 1/10.
	 */
	if (need < MDB_IDL_UM_MAX / 8)
		need = MDB_IDL_UM_MAX / 8;

	/* Save the page IDs of all the pages we're flushing */
	/* flush from the tail forward, this saves a lot of shifting later on. */
	for (i=dl[0].mid; i && need; i--) {
		MDB_ID pn = dl[i].mid << 1;
		dp = dl[i].mptr;
		if (dp->mp_flags & (P_LOOSE|P_KEEP))
			continue;
		/* Can't spill twice, make sure it's not already in a parent's
		 * spill list.
		 */
		if (txn->mt_parent) {
			MDB_txn *tx2;
			for (tx2 = txn->mt_parent; tx2; tx2 = tx2->mt_parent) {
				if (tx2->mt_spill_pgs) {
					j = mdb_midl_search(tx2->mt_spill_pgs, pn);
					if (j <= tx2->mt_spill_pgs[0] && tx2->mt_spill_pgs[j] == pn) {
						dp->mp_flags |= P_KEEP;
						break;
					}
				}
			}
			if (tx2)
				continue;
		}
		if ((rc = mdb_midl_append(&txn->mt_spill_pgs, pn)))
			goto done;
		need--;
	}
	mdb_midl_sort(txn->mt_spill_pgs);

	/* Flush the spilled part of dirty list */
	if ((rc = mdb_page_flush(txn, i)) != MDB_SUCCESS)
		goto done;

	/* Reset any dirty pages we kept that page_flush didn't see */
	rc = mdb_pages_xkeep(m0, P_DIRTY|P_KEEP, i);

done:
	txn->mt_flags |= rc ? MDB_TXN_ERROR : MDB_TXN_SPILLS;
	return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl 0x44(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x9727
movl $0x0, -0x4(%rbp)
jmp 0x9a30
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movzwl 0x6(%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x2, 0x20(%rax)
jb 0x9752
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movzwl 0x36(%rax), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x9795
movq -0x18(%rbp), %rax
movq (%rax), %rax
addq $0x8, %rax
movq -0x20(%rbp), %rcx
addq (%rcx), %rax
movq -0x28(%rbp), %rcx
movq 0x20(%rcx), %rcx
movl 0x10(%rcx), %ecx
addq %rcx, %rax
movq -0x28(%rbp), %rcx
movq 0x20(%rcx), %rcx
movl 0x10(%rcx), %ecx
xorl %edx, %edx
divq %rcx
movq %rax, %rcx
movl -0x3c(%rbp), %eax
addq %rcx, %rax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x44(%rbp)
movq -0x28(%rbp), %rax
movl 0x80(%rax), %eax
cmpl -0x3c(%rbp), %eax
jbe 0x97bf
movl $0x0, -0x4(%rbp)
jmp 0x9a30
movq -0x28(%rbp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x97f8
movl $0x1ffff, %edi          # imm = 0x1FFFF
callq 0x126b0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x97f6
movl $0xc, -0x4(%rbp)
jmp 0x9a30
jmp 0x9870
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x54(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x1, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x54(%rbp), %eax
ja 0x9864
movq -0x50(%rbp), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
andq $0x1, %rax
cmpq $0x0, %rax
jne 0x9857
movq -0x50(%rbp), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rdx
movq -0x50(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x40(%rbp)
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
jmp 0x9859
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x981c
movl -0x40(%rbp), %eax
movl %eax, %ecx
movq -0x50(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rdi
movl $0x10, %esi
movl $0x1, %edx
callq 0x100d0
movl %eax, -0x48(%rbp)
cmpl $0x0, %eax
je 0x9890
jmp 0x9a0d
cmpl $0x3fff, -0x44(%rbp)    # imm = 0x3FFF
jae 0x98a0
movl $0x3fff, -0x44(%rbp)    # imm = 0x3FFF
movq -0x38(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x3c(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x3c(%rbp)
movb %al, -0x69(%rbp)
je 0x98bf
cmpl $0x0, -0x44(%rbp)
setne %al
movb %al, -0x69(%rbp)
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0x98cb
jmp 0x99d6
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
shlq %rax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0xc000, %eax           # imm = 0xC000
cmpl $0x0, %eax
je 0x9910
jmp 0x99c8
movq -0x28(%rbp), %rax
cmpq $0x0, (%rax)
je 0x99a4
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x9999
movq -0x68(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x998a
movq -0x68(%rbp), %rax
movq 0x40(%rax), %rdi
movq -0x60(%rbp), %rsi
callq 0x125d0
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x68(%rbp), %rcx
movq 0x40(%rcx), %rcx
cmpq (%rcx), %rax
ja 0x9988
movq -0x68(%rbp), %rax
movq 0x40(%rax), %rax
movl -0x40(%rbp), %ecx
movq (%rax,%rcx,8), %rax
cmpq -0x60(%rbp), %rax
jne 0x9988
movq -0x30(%rbp), %rax
movzwl 0xa(%rax), %ecx
orl $0x8000, %ecx           # imm = 0x8000
movw %cx, 0xa(%rax)
jmp 0x9999
jmp 0x998a
jmp 0x998c
movq -0x68(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x68(%rbp)
jmp 0x9929
cmpq $0x0, -0x68(%rbp)
je 0x99a2
jmp 0x99c8
jmp 0x99a4
movq -0x28(%rbp), %rdi
addq $0x40, %rdi
movq -0x60(%rbp), %rsi
callq 0x12860
movl %eax, -0x48(%rbp)
cmpl $0x0, %eax
je 0x99bf
jmp 0x9a0d
movl -0x44(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x44(%rbp)
movl -0x3c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x98aa
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x12bf0
movq -0x28(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x6f80
movl %eax, -0x48(%rbp)
cmpl $0x0, %eax
je 0x99f9
jmp 0x9a0d
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %edx
movl $0x8010, %esi           # imm = 0x8010
callq 0x100d0
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %edx
movl $0x8, %ecx
movl $0x2, %eax
cmpl $0x0, %edx
cmovnel %eax, %ecx
movq -0x28(%rbp), %rax
orl 0x7c(%rax), %ecx
movl %ecx, 0x7c(%rax)
movl -0x48(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)