// File: /workspace/github_cpp_makefile_star2_O2/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:
xorl %ecx, %ecx
testb $0x4, 0x44(%rdi)
jne 0x681c
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq 0x18(%rdi), %r14
movq 0x28(%rdi), %rax
movzwl 0x6(%rax), %ebp
cmpl $0x2, 0x20(%rdi)
jb 0x6697
movq 0x58(%r14), %rax
movzwl 0x36(%rax), %eax
addl %eax, %ebp
movq 0x48(%r14), %r13
testq %rsi, %rsi
je 0x66bb
movq (%rsi), %rax
addq (%rdx), %rax
movq 0x20(%r14), %rdx
movl 0x10(%rdx), %esi
addq %rsi, %rax
addq $0x8, %rax
xorl %edx, %edx
divq %rsi
addl %eax, %ebp
addl %ebp, %ebp
cmpl %ebp, 0x80(%r14)
ja 0x680e
leaq 0x40(%r14), %r15
movq 0x40(%r14), %rax
testq %rax, %rax
je 0x66ff
movl (%rax), %ecx
xorl %edx, %edx
pushq $0x1
popq %rsi
cmpq %rcx, %rsi
ja 0x66f8
movq (%rax,%rsi,8), %rdi
testb $0x1, %dil
jne 0x66f3
incl %edx
movq %rdi, (%rax,%rdx,8)
incq %rsi
jmp 0x66de
movl %edx, %ecx
movq %rcx, (%rax)
jmp 0x6715
movl $0x1ffff, %edi          # imm = 0x1FFFF
callq 0xb17a
movq %rax, (%r15)
testq %rax, %rax
je 0x681f
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x9f9d
testl %eax, %eax
jne 0x67fd
movq %r15, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
cmpl $0x4000, %ebp           # imm = 0x4000
movl $0x3fff, %r15d          # imm = 0x3FFF
cmovael %ebp, %r15d
movl (%r13), %r12d
movl %r12d, %ebp
movq %r13, (%rsp)
addl $-0x1, %ebp
jae 0x67d3
testl %r15d, %r15d
je 0x67d3
movl %r12d, %eax
shlq $0x4, %rax
movq 0x8(%r13,%rax), %rbx
cmpw $0x3fff, 0xa(%rbx)      # imm = 0x3FFF
ja 0x67c8
movq (%r13,%rax), %r12
addq %r12, %r12
movq %r14, %r13
movq (%r14), %r14
testq %r14, %r14
je 0x67ad
testq %r14, %r14
je 0x67ad
movq 0x40(%r14), %rdi
testq %rdi, %rdi
je 0x67a8
movq %r12, %rsi
callq 0xb128
movl %eax, %eax
movq 0x40(%r14), %rcx
cmpq %rax, (%rcx)
jb 0x67a8
cmpq %r12, (%rcx,%rax,8)
je 0x67cd
movq (%r14), %r14
jmp 0x6781
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0xb24b
testl %eax, %eax
jne 0x6824
decl %r15d
movq %r13, %r14
movq (%rsp), %r13
movl %ebp, %r12d
jmp 0x6750
orb $-0x80, 0xb(%rbx)
jmp 0x67c1
movq 0x40(%r14), %rdi
callq 0xb3d7
movq %r14, %rdi
movl %r12d, %esi
callq 0x52b1
testl %eax, %eax
jne 0x67fd
movq 0x8(%rsp), %rdi
movl $0x8010, %esi           # imm = 0x8010
movl %r12d, %edx
callq 0x9f9d
movl %eax, %ecx
testl %ecx, %ecx
pushq $0x8
popq %rax
pushq $0x2
popq %rdx
cmovel %eax, %edx
orl %edx, 0x7c(%r14)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
movl %ecx, %eax
retq
pushq $0xc
popq %rcx
jmp 0x680e
movl %eax, %ecx
movq %r13, %r14
jmp 0x67ff