// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
xorl %ebp, %ebp
testb $0x4, 0x44(%rdi)
jne 0x707c
movq %rdi, %r14
movq 0x18(%rdi), %rbx
movq 0x28(%rdi), %rax
movzwl 0x6(%rax), %r13d
cmpl $0x2, 0x20(%rdi)
jb 0x6e96
movq 0x58(%rbx), %rax
movzwl 0x36(%rax), %eax
addl %eax, %r13d
movq 0x48(%rbx), %rax
movq %rax, 0x8(%rsp)
testq %rsi, %rsi
je 0x6ec0
movq (%rsi), %rax
addq (%rdx), %rax
movq 0x20(%rbx), %rcx
movl 0x10(%rcx), %ecx
addq %rcx, %rax
addq $0x8, %rax
xorl %edx, %edx
divq %rcx
addl %eax, %r13d
addl %r13d, %r13d
cmpl %r13d, 0x80(%rbx)
ja 0x707c
leaq 0x40(%rbx), %r15
movq 0x40(%rbx), %rax
testq %rax, %rax
je 0x6f16
movl (%rax), %esi
xorl %ecx, %ecx
testl %esi, %esi
je 0x6f11
incl %esi
cmpl $0x3, %esi
movl $0x2, %edx
cmovael %esi, %edx
movl $0x1, %esi
movq (%rax,%rsi,8), %rdi
testb $0x1, %dil
jne 0x6f07
incl %ecx
movq %rdi, (%rax,%rcx,8)
incq %rsi
cmpq %rsi, %rdx
jne 0x6ef7
movl %ecx, %ecx
movq %rcx, (%rax)
jmp 0x6f2c
movl $0x1ffff, %edi          # imm = 0x1FFFF
callq 0xbeff
movq %rax, (%r15)
testq %rax, %rax
je 0x708d
movq %r14, %rdi
movl $0x10, %esi
movl $0x1, %edx
callq 0xac00
testl %eax, %eax
jne 0x7068
movq %r15, 0x20(%rsp)
movq %r14, 0x10(%rsp)
movq 0x8(%rsp), %rdx
movq (%rdx), %r12
testl %r12d, %r12d
je 0x703e
movl %r12d, %r12d
cmpl $0x4000, %r13d          # imm = 0x4000
movl $0x3fff, %esi           # imm = 0x3FFF
cmovael %r13d, %esi
xorl %ebp, %ebp
movq %rbx, 0x18(%rsp)
movq %r12, %rcx
shlq $0x4, %rcx
movq 0x8(%rdx,%rcx), %r15
movl $0x8, %eax
cmpw $0x3fff, 0xa(%r15)      # imm = 0x3FFF
ja 0x7025
movl %esi, 0x4(%rsp)
movq (%rdx,%rcx), %r13
addq %r13, %r13
movq (%rbx), %r14
testq %r14, %r14
je 0x6ff3
xorl %ebx, %ebx
movq 0x40(%r14), %rdi
testq %rdi, %rdi
je 0x6fd0
movq %r13, %rsi
callq 0xbeac
movl %eax, %eax
movq 0x40(%r14), %rcx
cmpq %rax, (%rcx)
jb 0x6fd0
cmpq %r13, (%rcx,%rax,8)
je 0x6fdf
movq (%r14), %r14
testq %r14, %r14
sete %bl
jne 0x6fae
xorl %eax, %eax
jmp 0x6fe9
orb $-0x80, 0xb(%r15)
movl $0x8, %eax
testb $0x1, %bl
movq 0x18(%rsp), %rbx
je 0x700b
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0xbfd5
testl %eax, %eax
je 0x7016
movl %eax, %ebp
movl $0x5, %eax
movq 0x8(%rsp), %rdx
movl 0x4(%rsp), %esi
jmp 0x7025
movl 0x4(%rsp), %esi
decl %esi
xorl %ebp, %ebp
xorl %eax, %eax
movq 0x8(%rsp), %rdx
testl %eax, %eax
je 0x702e
cmpl $0x8, %eax
jne 0x7094
decq %r12
testl %r12d, %r12d
je 0x703e
testl %esi, %esi
jne 0x6f7b
movq 0x40(%rbx), %rdi
callq 0xc17a
movq %rbx, %rdi
movl %r12d, %esi
callq 0x56d7
testl %eax, %eax
jne 0x7068
movq 0x10(%rsp), %rdi
movl $0x8010, %esi           # imm = 0x8010
movl %r12d, %edx
callq 0xac00
movl %eax, %ebp
testl %ebp, %ebp
movl $0x8, %eax
movl $0x2, %ecx
cmovel %eax, %ecx
orl %ecx, 0x7c(%rbx)
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xc, %ebp
jmp 0x707c
cmpl $0x5, %eax
je 0x706a
jmp 0x707c