// File: /workspace/github_cpp_makefile_star2_O3/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 0x6d08
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
movq 0x18(%rdi), %rbx
movq 0x28(%rdi), %rax
movzwl 0x6(%rax), %r13d
cmpl $0x2, 0x20(%rdi)
jb 0x6b60
movq 0x58(%rbx), %rax
movzwl 0x36(%rax), %eax
addl %eax, %r13d
movq 0x48(%rbx), %r15
testq %rsi, %rsi
je 0x6b85
movq (%rsi), %rax
addq (%rdx), %rax
movq 0x20(%rbx), %rdx
movl 0x10(%rdx), %esi
addq %rsi, %rax
addq $0x8, %rax
xorl %edx, %edx
divq %rsi
addl %eax, %r13d
addl %r13d, %r13d
cmpl %r13d, 0x80(%rbx)
ja 0x6cfa
leaq 0x40(%rbx), %r12
movq 0x40(%rbx), %rax
testq %rax, %rax
je 0x6bdb
movl (%rax), %esi
xorl %ecx, %ecx
testl %esi, %esi
je 0x6bd6
incl %esi
cmpl $0x3, %esi
movl $0x2, %edx
cmovael %esi, %edx
movl $0x1, %esi
movq (%rax,%rsi,8), %rdi
testb $0x1, %dil
jne 0x6bcc
incl %ecx
movq %rdi, (%rax,%rcx,8)
incq %rsi
cmpq %rsi, %rdx
jne 0x6bbc
movl %ecx, %ecx
movq %rcx, (%rax)
jmp 0x6bf2
movl $0x1ffff, %edi          # imm = 0x1FFFF
callq 0xb9fb
movq %rax, (%r12)
testq %rax, %rax
je 0x6cb2
movq %r14, %rdi
movl $0x10, %esi
movl $0x1, %edx
callq 0xa7e6
testl %eax, %eax
jne 0x6ce6
movq %r12, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movl (%r15), %r12d
testq %r12, %r12
je 0x6cb9
movq %r15, %rcx
cmpl $0x4000, %r13d          # imm = 0x4000
movl $0x3fff, %r15d          # imm = 0x3FFF
cmovael %r13d, %r15d
movq %rcx, (%rsp)
movq %r12, %rax
shlq $0x4, %rax
movq 0x8(%rcx,%rax), %rbp
cmpw $0x3fff, 0xa(%rbp)      # imm = 0x3FFF
ja 0x6ca3
movq (%rcx,%rax), %r13
addq %r13, %r13
movq %rbx, %r14
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x6c88
movq 0x40(%rbx), %rdi
testq %rdi, %rdi
je 0x6c58
movq %r13, %rsi
callq 0xb9bc
movl %eax, %eax
movq 0x40(%rbx), %rcx
cmpq %rax, (%rcx)
jb 0x6c58
cmpq %r13, (%rcx,%rax,8)
jne 0x6c58
orb $-0x80, 0xb(%rbp)
jmp 0x6c9c
movq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0xbad1
testl %eax, %eax
jne 0x6d0b
decl %r15d
movq %r14, %rbx
movq (%rsp), %rcx
decq %r12
testl %r12d, %r12d
je 0x6cbc
testl %r15d, %r15d
jne 0x6c3a
jmp 0x6cbc
movl $0xc, %ecx
jmp 0x6cfa
xorl %r12d, %r12d
movq 0x40(%rbx), %rdi
callq 0xbd0b
movq %rbx, %rdi
movl %r12d, %esi
callq 0x54a6
testl %eax, %eax
jne 0x6ce6
movq 0x8(%rsp), %rdi
movl $0x8010, %esi           # imm = 0x8010
movl %r12d, %edx
callq 0xa7e6
movl %eax, %ecx
testl %ecx, %ecx
movl $0x8, %eax
movl $0x2, %edx
cmovel %eax, %edx
orl %edx, 0x7c(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
movl %ecx, %eax
retq
movl %eax, %ecx
movq %r14, %rbx
jmp 0x6ce8