// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_pages_xkeep
static int
mdb_pages_xkeep(MDB_cursor *mc, unsigned pflags, int all)
{
	enum { Mask = P_SUBP|P_DIRTY|P_LOOSE|P_KEEP };
	MDB_txn *txn = mc->mc_txn;
	MDB_cursor *m3;
	MDB_xcursor *mx;
	MDB_page *dp, *mp;
	MDB_node *leaf;
	unsigned i, j;
	int rc = MDB_SUCCESS, level;

	/* Mark pages seen by cursors */
	if (mc->mc_flags & C_UNTRACK)
		mc = NULL;				/* will find mc in mt_cursors */
	for (i = txn->mt_numdbs;; mc = txn->mt_cursors[--i]) {
		for (; mc; mc=mc->mc_next) {
			if (!(mc->mc_flags & C_INITIALIZED))
				continue;
			for (m3 = mc;; m3 = &mx->mx_cursor) {
				mp = NULL;
				for (j=0; j<m3->mc_snum; j++) {
					mp = m3->mc_pg[j];
					if ((mp->mp_flags & Mask) == pflags)
						mp->mp_flags ^= P_KEEP;
				}
				mx = m3->mc_xcursor;
				/* Proceed to mx if it is at a sub-database */
				if (! (mx && (mx->mx_cursor.mc_flags & C_INITIALIZED)))
					break;
				if (! (mp && (mp->mp_flags & P_LEAF)))
					break;
				leaf = NODEPTR(mp, m3->mc_ki[j-1]);
				if (!(leaf->mn_flags & F_SUBDATA))
					break;
			}
		}
		if (i == 0)
			break;
	}

	if (all) {
		/* Mark dirty root pages */
		for (i=0; i<txn->mt_numdbs; i++) {
			if (txn->mt_dbflags[i] & DB_DIRTY) {
				pgno_t pgno = txn->mt_dbs[i].md_root;
				if (pgno == P_INVALID)
					continue;
				if ((rc = mdb_page_get(txn, pgno, &dp, &level)) != MDB_SUCCESS)
					break;
				if ((dp->mp_flags & Mask) == pflags && level <= 1)
					dp->mp_flags ^= P_KEEP;
			}
		}
	}

	return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x8(%rbp), %rax
movl 0x44(%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x1010c
movq $0x0, -0x8(%rbp)
movq -0x18(%rbp), %rax
movl 0x78(%rax), %eax
movl %eax, -0x44(%rbp)
jmp 0x10118
cmpq $0x0, -0x8(%rbp)
je 0x10239
movq -0x8(%rbp), %rax
movl 0x44(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x10137
jmp 0x10229
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movq -0x20(%rbp), %rcx
movzwl 0x40(%rcx), %ecx
cmpl %ecx, %eax
jae 0x1019e
movq -0x20(%rbp), %rax
movl -0x48(%rbp), %ecx
movq 0x48(%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0xc050, %eax           # imm = 0xC050
cmpl -0xc(%rbp), %eax
jne 0x10191
movq -0x38(%rbp), %rax
movzwl 0xa(%rax), %ecx
xorl $0x8000, %ecx           # imm = 0x8000
movw %cx, 0xa(%rax)
jmp 0x10193
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x1014e
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x101c0
movq -0x28(%rbp), %rax
movl 0x44(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x101c2
jmp 0x10227
cmpq $0x0, -0x38(%rbp)
je 0x101d9
movq -0x38(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x101db
jmp 0x10227
movq -0x38(%rbp), %rax
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rdx
movl -0x48(%rbp), %esi
subl $0x1, %esi
movl %esi, %esi
movzwl 0x148(%rdx,%rsi,2), %edx
movzwl 0x10(%rcx,%rdx,2), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movzwl 0x4(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x10218
jmp 0x10227
jmp 0x1021a
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0x1013f
jmp 0x10229
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x10118
cmpl $0x0, -0x44(%rbp)
jne 0x10241
jmp 0x10263
jmp 0x10243
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rax
movl -0x44(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x44(%rbp)
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x8(%rbp)
jmp 0x10116
cmpl $0x0, -0x10(%rbp)
je 0x1031b
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x78(%rcx), %eax
jae 0x10319
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rax
movl -0x44(%rbp), %ecx
movzbl (%rax,%rcx), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x10309
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x44(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq 0x28(%rax), %rax
movq %rax, -0x58(%rbp)
cmpq $-0x1, -0x58(%rbp)
jne 0x102be
jmp 0x1030b
movq -0x18(%rbp), %rdi
movq -0x58(%rbp), %rsi
leaq -0x30(%rbp), %rdx
leaq -0x50(%rbp), %rcx
callq 0xa740
movl %eax, -0x4c(%rbp)
cmpl $0x0, %eax
je 0x102dd
jmp 0x10319
movq -0x30(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0xc050, %eax           # imm = 0xC050
cmpl -0xc(%rbp), %eax
jne 0x10307
cmpl $0x1, -0x50(%rbp)
jg 0x10307
movq -0x30(%rbp), %rax
movzwl 0xa(%rax), %ecx
xorl $0x8000, %ecx           # imm = 0x8000
movw %cx, 0xa(%rax)
jmp 0x10309
jmp 0x1030b
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x10274
jmp 0x1031b
movl -0x4c(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)