// File: /workspace/github_cpp_makefile_star2_O2/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 %rdx, 0x10(%rsp)
movq %rsi, 0x30(%rsp)
movzwl 0x42(%rdi), %eax
movq 0x48(%rdi,%rax,8), %r14
movzwl 0xc(%r14), %eax
addl $-0x10, %eax
movl %eax, 0x1c(%rsp)
movl %eax, %esi
shrl %esi
movzwl 0xa(%r14), %eax
xorl %ebx, %ebx
testb $0x2, %al
sete %cl
movq 0x30(%rdi), %rdx
movq 0x10(%rdx), %r12
leaq 0x63b(%rip), %rdx       # 0x9dc8
cmpq %rdx, %r12
jne 0x97b7
movq %rdx, %r12
testb $0x1, %al
je 0x97b7
movzwl 0x12(%r14), %edx
cmpw $0x8, 0x6(%r14,%rdx)
leaq -0x6a0b(%rip), %rdx     # 0x2da1
leaq -0x6a2a(%rip), %r12     # 0x2d89
cmoveq %rdx, %r12
movb %cl, %bl
movq %rsi, 0x8(%rsp)
leal -0x1(%rsi), %r15d
testb $0x20, %al
movq %rdi, 0x38(%rsp)
jne 0x982e
xorl %ebp, %ebp
xorl %eax, %eax
xorl %r13d, %r13d
cmpl %r15d, %ebx
jg 0x9894
leal (%r15,%rbx), %eax
movl %eax, %ebp
shrl %ebp
andl $-0x2, %eax
movzwl 0x10(%r14,%rax), %eax
leaq (%r14,%rax), %r13
addq %r14, %rax
addq $0x8, %rax
movzwl -0x2(%rax), %ecx
movq %rcx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq *%r12
testl %eax, %eax
je 0x98bb
leal 0x1(%rbp), %ecx
testl %eax, %eax
cmovgl %ecx, %ebx
leal -0x1(%rbp), %ecx
cmovgl %r15d, %ecx
movl %ecx, %r15d
jmp 0x97d2
movq 0x28(%rdi), %rax
movl (%rax), %eax
leaq 0x20(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x10(%r14), %rax
movq %rax, 0x40(%rsp)
movzwl 0x10(%r14), %r13d
addq %r14, %r13
xorl %ebp, %ebp
xorl %eax, %eax
cmpl %r15d, %ebx
jg 0x9894
leal (%r15,%rbx), %ebp
shrl %ebp
movq 0x20(%rsp), %rax
imulq %rbp, %rax
addq 0x40(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq *%r12
testl %eax, %eax
je 0x98bb
leal 0x1(%rbp), %ecx
testl %eax, %eax
cmovgl %ecx, %ebx
leal -0x1(%rbp), %ecx
cmovgl %r15d, %ecx
movl %ecx, %r15d
jmp 0x9851
testl %eax, %eax
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rsi
jle 0x98c7
incl %ebp
pushq $0x1
popq %rax
testb $0x20, 0xa(%r14)
jne 0x98c7
movl %ebp, %ecx
movzwl 0x10(%r14,%rcx,2), %r13d
addq %r14, %r13
jmp 0x98c7
xorl %eax, %eax
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rsi
testq %rdx, %rdx
je 0x98e0
testl %eax, %eax
sete %al
cmpl $0x2, 0x1c(%rsp)
setae %cl
andb %al, %cl
movzbl %cl, %eax
movl %eax, (%rdx)
movq 0x38(%rsp), %rcx
movzwl 0x42(%rcx), %eax
movw %bp, 0x148(%rcx,%rax,2)
xorl %eax, %eax
cmpl %esi, %ebp
cmovbq %r13, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq