// File: /workspace/github_cpp_makefile_star2_O1/jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_node_search
static MDB_node *
mdb_node_search(MDB_cursor *mc, MDB_val *key, int *exactp)
{
	unsigned int	 i = 0, nkeys;
	int		 low, high;
	int		 rc = 0;
	MDB_page *mp = mc->mc_pg[mc->mc_top];
	MDB_node	*node = NULL;
	MDB_val	 nodekey;
	MDB_cmp_func *cmp;
	DKBUF;

	nkeys = NUMKEYS(mp);

	DPRINTF(("searching %u keys in %s %spage %"Z"u",
	    nkeys, IS_LEAF(mp) ? "leaf" : "branch", IS_SUBP(mp) ? "sub-" : "",
	    mdb_dbg_pgno(mp)));

	low = IS_LEAF(mp) ? 0 : 1;
	high = nkeys - 1;
	cmp = mc->mc_dbx->md_cmp;

	/* Branch pages have no data, so if using integer keys,
	 * alignment is guaranteed. Use faster mdb_cmp_int.
	 */
	if (cmp == mdb_cmp_cint && IS_BRANCH(mp)) {
		if (NODEPTR(mp, 1)->mn_ksize == sizeof(size_t))
			cmp = mdb_cmp_long;
		else
			cmp = mdb_cmp_int;
	}

	if (IS_LEAF2(mp)) {
		nodekey.mv_size = mc->mc_db->md_pad;
		node = NODEPTR(mp, 0);	/* fake */
		while (low <= high) {
			i = (low + high) >> 1;
			nodekey.mv_data = LEAF2KEY(mp, i, nodekey.mv_size);
			rc = cmp(key, &nodekey);
			DPRINTF(("found leaf index %u [%s], rc = %i",
			    i, DKEY(&nodekey), rc));
			if (rc == 0)
				break;
			if (rc > 0)
				low = i + 1;
			else
				high = i - 1;
		}
	} else {
		while (low <= high) {
			i = (low + high) >> 1;

			node = NODEPTR(mp, i);
			nodekey.mv_size = NODEKSZ(node);
			nodekey.mv_data = NODEKEY(node);

			rc = cmp(key, &nodekey);
#if MDB_DEBUG
			if (IS_LEAF(mp))
				DPRINTF(("found leaf index %u [%s], rc = %i",
				    i, DKEY(&nodekey), rc));
			else
				DPRINTF(("found branch index %u [%s -> %"Z"u], rc = %i",
				    i, DKEY(&nodekey), NODEPGNO(node), rc));
#endif
			if (rc == 0)
				break;
			if (rc > 0)
				low = i + 1;
			else
				high = i - 1;
		}
	}

	if (rc > 0) {	/* Found entry is less than the key. */
		i++;	/* Skip to get the smallest entry larger than key. */
		if (!IS_LEAF2(mp))
			node = NODEPTR(mp, i);
	}
	if (exactp)
		*exactp = (rc == 0 && nkeys > 0);
	/* store the key index */
	mc->mc_ki[mc->mc_top] = i;
	if (i >= nkeys)
		/* There is no entry larger or equal to the key. */
		return NULL;

	/* nodeptr is fake for LEAF2 */
	return node;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, 0x38(%rsp)
movzwl 0x42(%rdi), %eax
movq 0x48(%rdi,%rax,8), %r14
movzwl 0xc(%r14), %r9d
addl $-0x10, %r9d
movl %r9d, %r8d
shrl %r8d
movzwl 0xa(%r14), %eax
xorl %ebx, %ebx
testb $0x2, %al
sete %cl
movq 0x30(%rdi), %rsi
movq 0x10(%rsi), %r12
leaq 0x6e0(%rip), %rsi       # 0xaa14
cmpq %rsi, %r12
jne 0xa35e
movq %rsi, %r12
testb $0x1, %al
je 0xa35e
movzwl 0x12(%r14), %esi
cmpw $0x8, 0x6(%r14,%rsi)
leaq -0x7300(%rip), %rsi     # 0x3053
leaq -0x7321(%rip), %r12     # 0x3039
cmoveq %rsi, %r12
movb %cl, %bl
leal -0x1(%r8), %r15d
testb $0x20, %al
jne 0xa3e2
xorl %ecx, %ecx
cmpl %r8d, %ebx
jae 0xa490
movl %r9d, 0xc(%rsp)
movq %r8, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
leal (%r15,%rbx), %eax
movl %eax, %r13d
shrl %r13d
andl $-0x2, %eax
movzwl 0x10(%r14,%rax), %eax
leaq (%r14,%rax), %rbp
addq %r14, %rax
addq $0x8, %rax
movzwl -0x2(%rax), %ecx
movq %rcx, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq *%r12
testl %eax, %eax
je 0xa478
leal 0x1(%r13), %ecx
leal -0x1(%r13), %edx
testl %eax, %eax
cmovgl %ecx, %ebx
cmovgl %r15d, %edx
movl %edx, %r15d
cmpl %edx, %ebx
jle 0xa387
jmp 0xa460
movq 0x28(%rdi), %rax
movl (%rax), %eax
movq %rax, 0x28(%rsp)
movzwl 0x10(%r14), %ebp
addq %r14, %rbp
xorl %ecx, %ecx
cmpl %r8d, %ebx
jae 0xa497
movl %r9d, 0xc(%rsp)
movq %r8, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
leaq 0x10(%r14), %rax
movq %rax, 0x40(%rsp)
leal (%r15,%rbx), %r13d
shrl %r13d
movq 0x28(%rsp), %rax
imulq %r13, %rax
addq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq *%r12
testl %eax, %eax
je 0xa478
leal 0x1(%r13), %ecx
leal -0x1(%r13), %edx
testl %eax, %eax
cmovgl %ecx, %ebx
cmovgl %r15d, %edx
movl %edx, %r15d
cmpl %edx, %ebx
jle 0xa41d
movl %eax, %ecx
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %r8
movl 0xc(%rsp), %r9d
jmp 0xa49a
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %r8
movl 0xc(%rsp), %r9d
xorl %ecx, %ecx
jmp 0xa49a
xorl %r13d, %r13d
xorl %ebp, %ebp
jmp 0xa49a
xorl %r13d, %r13d
testl %ecx, %ecx
jle 0xa4b4
incl %r13d
testb $0x20, 0xa(%r14)
jne 0xa4b4
movl %r13d, %eax
movzwl 0x10(%r14,%rax,2), %ebp
addq %r14, %rbp
testq %rdx, %rdx
je 0xa4cc
testl %ecx, %ecx
sete %al
cmpl $0x2, %r9d
setae %cl
andb %al, %cl
movzbl %cl, %eax
movl %eax, (%rdx)
movzwl 0x42(%rdi), %eax
movw %r13w, 0x148(%rdi,%rax,2)
xorl %eax, %eax
cmpl %r8d, %r13d
cmovbq %rbp, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq