// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_page_search_root
static int
mdb_page_search_root(MDB_cursor *mc, MDB_val *key, int flags)
{
	MDB_page	*mp = mc->mc_pg[mc->mc_top];
	int rc;
	DKBUF;

	while (IS_BRANCH(mp)) {
		MDB_node	*node;
		indx_t		i;

		DPRINTF(("branch page %"Z"u has %u keys", mp->mp_pgno, NUMKEYS(mp)));
		mdb_cassert(mc, NUMKEYS(mp) > 1);
		DPRINTF(("found index 0 to page %"Z"u", NODEPGNO(NODEPTR(mp, 0))));

		if (flags & (MDB_PS_FIRST|MDB_PS_LAST)) {
			i = 0;
			if (flags & MDB_PS_LAST)
				i = NUMKEYS(mp) - 1;
		} else {
			int	 exact;
			node = mdb_node_search(mc, key, &exact);
			if (node == NULL)
				i = NUMKEYS(mp) - 1;
			else {
				i = mc->mc_ki[mc->mc_top];
				if (!exact) {
					mdb_cassert(mc, i > 0);
					i--;
				}
			}
			DPRINTF(("following index %u for key [%s]", i, DKEY(key)));
		}

		mdb_cassert(mc, i < NUMKEYS(mp));
		node = NODEPTR(mp, i);

		if ((rc = mdb_page_get(mc->mc_txn, NODEPGNO(node), &mp, NULL)) != 0)
			return rc;

		mc->mc_ki[mc->mc_top] = i;
		if ((rc = mdb_cursor_push(mc, mp)))
			return rc;

		if (flags & MDB_PS_MODIFY) {
			if ((rc = mdb_page_touch(mc)) != 0)
				return rc;
			mp = mc->mc_pg[mc->mc_top];
		}
	}

	if (!IS_LEAF(mp)) {
		DPRINTF(("internal error, index points to a %02X page!?",
		    mp->mp_flags));
		mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
		return MDB_CORRUPTED;
	}

	DPRINTF(("found leaf page %"Z"u for key [%s]", mp->mp_pgno,
	    key ? DKEY(key) : "null"));
	mc->mc_flags |= C_INITIALIZED;
	mc->mc_flags &= ~C_EOF;

	return MDB_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movq 0x48(%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x1, %eax
cmpl $0x1, %eax
jne 0xfc4b
movq -0x28(%rbp), %rax
movzwl 0xc(%rax), %eax
subl $0x10, %eax
shrl %eax
cmpl $0x1, %eax
jbe 0xfa30
jmp 0xfa5c
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rdi
leaq 0x7fcb(%rip), %rsi      # 0x17a0e
leaq 0x7fd4(%rip), %rdx      # 0x17a1e
leaq 0x79be(%rip), %rcx      # 0x1740f
movl $0x14c4, %r8d           # imm = 0x14C4
callq 0x133a0
movl -0x1c(%rbp), %eax
andl $0xc, %eax
cmpl $0x0, %eax
je 0xfa91
movw $0x0, -0x3a(%rbp)
movl -0x1c(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0xfa8c
movq -0x28(%rbp), %rax
movzwl 0xc(%rax), %eax
subl $0x10, %eax
shrl %eax
subl $0x1, %eax
movw %ax, -0x3a(%rbp)
jmp 0xfb28
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0xf1a0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0xfac3
movq -0x28(%rbp), %rax
movzwl 0xc(%rax), %eax
subl $0x10, %eax
shrl %eax
subl $0x1, %eax
movw %ax, -0x3a(%rbp)
jmp 0xfb26
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movw 0x148(%rax,%rcx,2), %ax
movw %ax, -0x3a(%rbp)
cmpl $0x0, -0x40(%rbp)
jne 0xfb24
movzwl -0x3a(%rbp), %eax
cmpl $0x0, %eax
jle 0xfaec
jmp 0xfb18
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rdi
leaq 0x7f34(%rip), %rsi      # 0x17a33
leaq 0x7f18(%rip), %rdx      # 0x17a1e
leaq 0x7902(%rip), %rcx      # 0x1740f
movl $0x14d3, %r8d           # imm = 0x14D3
callq 0x133a0
movw -0x3a(%rbp), %ax
addw $-0x1, %ax
movw %ax, -0x3a(%rbp)
jmp 0xfb26
jmp 0xfb28
movzwl -0x3a(%rbp), %eax
movq -0x28(%rbp), %rcx
movzwl 0xc(%rcx), %ecx
subl $0x10, %ecx
shrl %ecx
cmpl %ecx, %eax
jae 0xfb3f
jmp 0xfb6b
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rdi
leaq 0x7ee7(%rip), %rsi      # 0x17a39
leaq 0x7ec5(%rip), %rdx      # 0x17a1e
leaq 0x78af(%rip), %rcx      # 0x1740f
movl $0x14da, %r8d           # imm = 0x14DA
callq 0x133a0
movq -0x28(%rbp), %rax
movq -0x28(%rbp), %rcx
movzwl -0x3a(%rbp), %edx
movzwl 0x10(%rcx,%rdx,2), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rax
movzwl (%rax), %eax
movl %eax, %esi
movq -0x38(%rbp), %rax
movzwl 0x2(%rax), %eax
shlq $0x10, %rax
orq %rax, %rsi
movq -0x38(%rbp), %rax
movzwl 0x4(%rax), %eax
shlq $0x20, %rax
orq %rax, %rsi
leaq -0x28(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0xa740
movl %eax, -0x2c(%rbp)
cmpl $0x0, %eax
je 0xfbd5
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xfc96
movw -0x3a(%rbp), %dx
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movw %dx, 0x148(%rax,%rcx,2)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x9b50
movl %eax, -0x2c(%rbp)
cmpl $0x0, %eax
je 0xfc0d
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xfc96
movl -0x1c(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xfc46
movq -0x10(%rbp), %rdi
callq 0xf430
movl %eax, -0x2c(%rbp)
cmpl $0x0, %eax
je 0xfc31
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xfc96
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movq 0x48(%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
jmp 0xfa08
movq -0x28(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x2, %eax
cmpl $0x2, %eax
je 0xfc75
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x7c(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x7c(%rax)
movl $0xffff87b4, -0x4(%rbp) # imm = 0xFFFF87B4
jmp 0xfc96
movq -0x10(%rbp), %rax
movl 0x44(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0x44(%rax)
movq -0x10(%rbp), %rax
movl 0x44(%rax), %ecx
andl $-0x3, %ecx
movl %ecx, 0x44(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nop