// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_page_get
static int
mdb_page_get(MDB_txn *txn, pgno_t pgno, MDB_page **ret, int *lvl)
{
	MDB_env *env = txn->mt_env;
	MDB_page *p = NULL;
	int level;

	if (! (txn->mt_flags & (MDB_TXN_RDONLY|MDB_TXN_WRITEMAP))) {
		MDB_txn *tx2 = txn;
		level = 1;
		do {
			MDB_ID2L dl = tx2->mt_u.dirty_list;
			unsigned x;
			/* Spilled pages were dirtied in this txn and flushed
			 * because the dirty list got full. Bring this page
			 * back in from the map (but don't unspill it here,
			 * leave that unless page_touch happens again).
			 */
			if (tx2->mt_spill_pgs) {
				MDB_ID pn = pgno << 1;
				x = mdb_midl_search(tx2->mt_spill_pgs, pn);
				if (x <= tx2->mt_spill_pgs[0] && tx2->mt_spill_pgs[x] == pn) {
					p = (MDB_page *)(env->me_map + env->me_psize * pgno);
					goto done;
				}
			}
			if (dl[0].mid) {
				unsigned x = mdb_mid2l_search(dl, pgno);
				if (x <= dl[0].mid && dl[x].mid == pgno) {
					p = dl[x].mptr;
					goto done;
				}
			}
			level++;
		} while ((tx2 = tx2->mt_parent) != NULL);
	}

	if (pgno < txn->mt_next_pgno) {
		level = 0;
		p = (MDB_page *)(env->me_map + env->me_psize * pgno);
	} else {
		DPRINTF(("page %"Z"u not found", pgno));
		txn->mt_flags |= MDB_TXN_ERROR;
		return MDB_PAGE_NOTFOUND;
	}

done:
	*ret = p;
	if (lvl)
		*lvl = level;
	return MDB_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl 0x7c(%rax), %eax
andl $0xa0000, %eax          # imm = 0xA0000
cmpl $0x0, %eax
jne 0xa884
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x1, -0x3c(%rbp)
movq -0x48(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0xa80d
movq -0x18(%rbp), %rax
shlq %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
movq 0x40(%rax), %rdi
movq -0x60(%rbp), %rsi
callq 0x125d0
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x48(%rbp), %rcx
movq 0x40(%rcx), %rcx
cmpq (%rcx), %rax
ja 0xa80b
movq -0x48(%rbp), %rax
movq 0x40(%rax), %rax
movl -0x54(%rbp), %ecx
movq (%rax,%rcx,8), %rax
cmpq -0x60(%rbp), %rax
jne 0xa80b
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %ecx
imulq -0x18(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
jmp 0xa8ce
jmp 0xa80d
movq -0x50(%rbp), %rax
cmpq $0x0, (%rax)
je 0xa864
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x130f0
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpq (%rcx), %rax
ja 0xa862
movq -0x50(%rbp), %rax
movl -0x64(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
cmpq -0x18(%rbp), %rax
jne 0xa862
movq -0x50(%rbp), %rax
movl -0x64(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0xa8ce
jmp 0xa864
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0xa790
jmp 0xa884
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0xa8b6
movl $0x0, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %ecx
imulq -0x18(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
jmp 0xa8cc
movq -0x10(%rbp), %rax
movl 0x7c(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x7c(%rax)
movl $0xffff87b3, -0x4(%rbp) # imm = 0xFFFF87B3
jmp 0xa8f0
jmp 0xa8ce
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x0, -0x28(%rbp)
je 0xa8e9
movl -0x3c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)