// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_page_search
static int
mdb_page_search(MDB_cursor *mc, MDB_val *key, int flags)
{
	int		 rc;
	pgno_t		 root;

	/* Make sure the txn is still viable, then find the root from
	 * the txn's db table and set it as the root of the cursor's stack.
	 */
	if (mc->mc_txn->mt_flags & MDB_TXN_BLOCKED) {
		DPUTS("transaction may not be used now");
		return MDB_BAD_TXN;
	} else {
		/* Make sure we're using an up-to-date root */
		if (*mc->mc_dbflag & DB_STALE) {
				MDB_cursor mc2;
				if (TXN_DBI_CHANGED(mc->mc_txn, mc->mc_dbi))
					return MDB_BAD_DBI;
				mdb_cursor_init(&mc2, mc->mc_txn, MAIN_DBI, NULL);
				rc = mdb_page_search(&mc2, &mc->mc_dbx->md_name, 0);
				if (rc)
					return rc;
				{
					MDB_val data;
					int exact = 0;
					uint16_t flags;
					MDB_node *leaf = mdb_node_search(&mc2,
						&mc->mc_dbx->md_name, &exact);
					if (!exact)
						return MDB_NOTFOUND;
					if ((leaf->mn_flags & (F_DUPDATA|F_SUBDATA)) != F_SUBDATA)
						return MDB_INCOMPATIBLE; /* not a named DB */
					rc = mdb_node_read(mc->mc_txn, leaf, &data);
					if (rc)
						return rc;
					memcpy(&flags, ((char *) data.mv_data + offsetof(MDB_db, md_flags)),
						sizeof(uint16_t));
					/* The txn may not know this DBI, or another process may
					 * have dropped and recreated the DB with other flags.
					 */
					if ((mc->mc_db->md_flags & PERSISTENT_FLAGS) != flags)
						return MDB_INCOMPATIBLE;
					memcpy(mc->mc_db, data.mv_data, sizeof(MDB_db));
				}
				*mc->mc_dbflag &= ~DB_STALE;
		}
		root = mc->mc_db->md_root;

		if (root == P_INVALID) {		/* Tree is empty. */
			DPUTS("tree is empty");
			return MDB_NOTFOUND;
		}
	}

	mdb_cassert(mc, root > 1);
	if (!mc->mc_pg[0] || mc->mc_pg[0]->mp_pgno != root)
		if ((rc = mdb_page_get(mc->mc_txn, root, &mc->mc_pg[0], NULL)) != 0)
			return rc;

	mc->mc_snum = 1;
	mc->mc_top = 0;

	DPRINTF(("db %d root page %"Z"u has flags 0x%X",
		DDBI(mc), root, mc->mc_pg[0]->mp_flags));

	if (flags & MDB_PS_MODIFY) {
		if ((rc = mdb_page_touch(mc)))
			return rc;
	}

	if (flags & MDB_PS_ROOTONLY)
		return MDB_SUCCESS;

	return mdb_page_search_root(mc, key, flags);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1d0, %rsp            # imm = 0x1D0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x7c(%rax), %eax
andl $0x13, %eax
cmpl $0x0, %eax
je 0xeef5
movl $0xffff87c2, -0x4(%rbp) # imm = 0xFFFF87C2
jmp 0xf18c
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movzbl (%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xf086
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x60(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x20(%rcx), %ecx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x20(%rcx), %rcx
movq 0x98(%rcx), %rcx
movq -0x10(%rbp), %rdx
movl 0x20(%rdx), %edx
cmpl (%rcx,%rdx,4), %eax
je 0xef4d
movl $0xffff87c4, -0x4(%rbp) # imm = 0xFFFF87C4
jmp 0xf18c
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
leaq -0x1b0(%rbp), %rdi
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x4d40
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rsi
leaq -0x1b0(%rbp), %rdi
xorl %edx, %edx
callq 0xeec0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0xef94
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xf18c
movl $0x0, -0x1c4(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rsi
leaq -0x1b0(%rbp), %rdi
leaq -0x1c4(%rbp), %rdx
callq 0xf1a0
movq %rax, -0x1d0(%rbp)
cmpl $0x0, -0x1c4(%rbp)
jne 0xefd5
movl $0xffff87b2, -0x4(%rbp) # imm = 0xFFFF87B2
jmp 0xf18c
movq -0x1d0(%rbp), %rax
movzwl 0x4(%rax), %eax
andl $0x6, %eax
cmpl $0x2, %eax
je 0xeff4
movl $0xffff87c0, -0x4(%rbp) # imm = 0xFFFF87C0
jmp 0xf18c
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x1d0(%rbp), %rsi
leaq -0x1c0(%rbp), %rdx
callq 0x8a10
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0xf023
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xf18c
movq -0x1b8(%rbp), %rax
movw 0x4(%rax), %ax
movw %ax, -0x1c6(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movzwl 0x4(%rax), %eax
andl $0x7fff, %eax           # imm = 0x7FFF
movzwl -0x1c6(%rbp), %ecx
cmpl %ecx, %eax
je 0xf05d
movl $0xffff87c0, -0x4(%rbp) # imm = 0xFFFF87C0
jmp 0xf18c
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x1b8(%rbp), %rsi
movl $0x30, %edx
callq 0x2230
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movzbl (%rax), %ecx
andl $-0x3, %ecx
movb %cl, (%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, -0x28(%rbp)
cmpq $-0x1, -0x28(%rbp)
jne 0xf0a9
movl $0xffff87b2, -0x4(%rbp) # imm = 0xFFFF87B2
jmp 0xf18c
jmp 0xf0ab
cmpq $0x1, -0x28(%rbp)
jbe 0xf0b4
jmp 0xf0e0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rdi
leaq 0x88e8(%rip), %rsi      # 0x179af
leaq 0x88ea(%rip), %rdx      # 0x179b8
leaq 0x833a(%rip), %rcx      # 0x1740f
movl $0x1551, %r8d           # imm = 0x1551
callq 0x133a0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0xf0fc
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq (%rax), %rax
cmpq -0x28(%rbp), %rax
je 0xf12b
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x10(%rbp), %rdx
addq $0x48, %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0xa740
movl %eax, -0x20(%rbp)
cmpl $0x0, %eax
je 0xf129
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xf18c
jmp 0xf12b
movq -0x10(%rbp), %rax
movw $0x1, 0x40(%rax)
movq -0x10(%rbp), %rax
movw $0x0, 0x42(%rax)
movl -0x1c(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xf165
movq -0x10(%rbp), %rdi
callq 0xf430
movl %eax, -0x20(%rbp)
cmpl $0x0, %eax
je 0xf163
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xf18c
jmp 0xf165
movl -0x1c(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xf179
movl $0x0, -0x4(%rbp)
jmp 0xf18c
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0xf9e0
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
nopl (%rax,%rax)