// File: /workspace/github_cpp_makefile_star2_O2/jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_cursor_set
static int
mdb_cursor_set(MDB_cursor *mc, MDB_val *key, MDB_val *data,
    MDB_cursor_op op, int *exactp)
{
	int		 rc;
	MDB_page	*mp;
	MDB_node	*leaf = NULL;
	DKBUF;

	if (key->mv_size == 0)
		return MDB_BAD_VALSIZE;

	if (mc->mc_xcursor)
		mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);

	/* See if we're already on the right page */
	if (mc->mc_flags & C_INITIALIZED) {
		MDB_val nodekey;

		mp = mc->mc_pg[mc->mc_top];
		if (!NUMKEYS(mp)) {
			mc->mc_ki[mc->mc_top] = 0;
			return MDB_NOTFOUND;
		}
		if (mp->mp_flags & P_LEAF2) {
			nodekey.mv_size = mc->mc_db->md_pad;
			nodekey.mv_data = LEAF2KEY(mp, 0, nodekey.mv_size);
		} else {
			leaf = NODEPTR(mp, 0);
			MDB_GET_KEY2(leaf, nodekey);
		}
		rc = mc->mc_dbx->md_cmp(key, &nodekey);
		if (rc == 0) {
			/* Probably happens rarely, but first node on the page
			 * was the one we wanted.
			 */
			mc->mc_ki[mc->mc_top] = 0;
			if (exactp)
				*exactp = 1;
			goto set1;
		}
		if (rc > 0) {
			unsigned int i;
			unsigned int nkeys = NUMKEYS(mp);
			if (nkeys > 1) {
				if (mp->mp_flags & P_LEAF2) {
					nodekey.mv_data = LEAF2KEY(mp,
						 nkeys-1, nodekey.mv_size);
				} else {
					leaf = NODEPTR(mp, nkeys-1);
					MDB_GET_KEY2(leaf, nodekey);
				}
				rc = mc->mc_dbx->md_cmp(key, &nodekey);
				if (rc == 0) {
					/* last node was the one we wanted */
					mc->mc_ki[mc->mc_top] = nkeys-1;
					if (exactp)
						*exactp = 1;
					goto set1;
				}
				if (rc < 0) {
					if (mc->mc_ki[mc->mc_top] < NUMKEYS(mp)) {
						/* This is definitely the right page, skip search_page */
						if (mp->mp_flags & P_LEAF2) {
							nodekey.mv_data = LEAF2KEY(mp,
								 mc->mc_ki[mc->mc_top], nodekey.mv_size);
						} else {
							leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
							MDB_GET_KEY2(leaf, nodekey);
						}
						rc = mc->mc_dbx->md_cmp(key, &nodekey);
						if (rc == 0) {
							/* current node was the one we wanted */
							if (exactp)
								*exactp = 1;
							goto set1;
						}
					}
					rc = 0;
					goto set2;
				}
			}
			/* If any parents have right-sibs, search.
			 * Otherwise, there's nothing further.
			 */
			for (i=0; i<mc->mc_top; i++)
				if (mc->mc_ki[i] <
					NUMKEYS(mc->mc_pg[i])-1)
					break;
			if (i == mc->mc_top) {
				/* There are no other pages */
				mc->mc_ki[mc->mc_top] = nkeys;
				return MDB_NOTFOUND;
			}
		}
		if (!mc->mc_top) {
			/* There are no other pages */
			mc->mc_ki[mc->mc_top] = 0;
			if (op == MDB_SET_RANGE && !exactp) {
				rc = 0;
				goto set1;
			} else
				return MDB_NOTFOUND;
		}
	}

	rc = mdb_page_search(mc, key, 0);
	if (rc != MDB_SUCCESS)
		return rc;

	mp = mc->mc_pg[mc->mc_top];
	mdb_cassert(mc, IS_LEAF(mp));

set2:
	leaf = mdb_node_search(mc, key, exactp);
	if (exactp != NULL && !*exactp) {
		/* MDB_SET specified and not an exact match. */
		return MDB_NOTFOUND;
	}

	if (leaf == NULL) {
		DPUTS("===> inexact leaf not found, goto sibling");
		if ((rc = mdb_cursor_sibling(mc, 1)) != MDB_SUCCESS) {
			mc->mc_flags |= C_EOF;
			return rc;		/* no entries matched */
		}
		mp = mc->mc_pg[mc->mc_top];
		mdb_cassert(mc, IS_LEAF(mp));
		leaf = NODEPTR(mp, 0);
	}

set1:
	mc->mc_flags |= C_INITIALIZED;
	mc->mc_flags &= ~C_EOF;

	if (IS_LEAF2(mp)) {
		if (op == MDB_SET_RANGE || op == MDB_SET_KEY) {
			key->mv_size = mc->mc_db->md_pad;
			key->mv_data = LEAF2KEY(mp, mc->mc_ki[mc->mc_top], key->mv_size);
		}
		return MDB_SUCCESS;
	}

	if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
		mdb_xcursor_init1(mc, leaf);
	}
	if (data) {
		if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
			if (op == MDB_SET || op == MDB_SET_KEY || op == MDB_SET_RANGE) {
				rc = mdb_cursor_first(&mc->mc_xcursor->mx_cursor, data, NULL);
			} else {
				int ex2, *ex2p;
				if (op == MDB_GET_BOTH) {
					ex2p = &ex2;
					ex2 = 0;
				} else {
					ex2p = NULL;
				}
				rc = mdb_cursor_set(&mc->mc_xcursor->mx_cursor, data, NULL, MDB_SET_RANGE, ex2p);
				if (rc != MDB_SUCCESS)
					return rc;
			}
		} else if (op == MDB_GET_BOTH || op == MDB_GET_BOTH_RANGE) {
			MDB_val olddata;
			MDB_cmp_func *dcmp;
			if ((rc = mdb_node_read(mc->mc_txn, leaf, &olddata)) != MDB_SUCCESS)
				return rc;
			dcmp = mc->mc_dbx->md_dcmp;
#if UINT_MAX < SIZE_MAX
			if (dcmp == mdb_cmp_int && olddata.mv_size == sizeof(size_t))
				dcmp = mdb_cmp_clong;
#endif
			rc = dcmp(data, &olddata);
			if (rc) {
				if (op == MDB_GET_BOTH || rc > 0)
					return MDB_NOTFOUND;
				rc = 0;
				*data = olddata;
			}

		} else {
			if (mc->mc_xcursor)
				mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
			if ((rc = mdb_node_read(mc->mc_txn, leaf, data)) != MDB_SUCCESS)
				return rc;
		}
	}

	/* The key already matches in all other cases */
	if (op == MDB_SET_RANGE || op == MDB_SET_KEY)
		MDB_GET_KEY(leaf, key);
	DPRINTF(("==> cursor placed on key [%s]", DKEY(key)));

	return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
cmpq $0x0, (%rsi)
je 0x5667
movq %r8, %rbp
movl %ecx, %r14d
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r12
movq 0x10(%rdi), %rax
testq %rax, %rax
je 0x5649
andb $-0x4, 0x44(%rax)
testb $0x1, 0x44(%r12)
jne 0x566f
movq %r12, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x958a
testl %eax, %eax
je 0x56cd
movl %eax, %r13d
jmp 0x569d
movl $0xffff87c3, %r13d      # imm = 0xFFFF87C3
jmp 0x569d
movq %r15, 0x20(%rsp)
movzwl 0x42(%r12), %eax
movq 0x48(%r12,%rax,8), %r15
movzwl 0xc(%r15), %ecx
andl $-0x2, %ecx
cmpw $0x10, %cx
jne 0x56af
andw $0x0, 0x148(%r12,%rax,2)
movl $0xffff87b2, %r13d      # imm = 0xFFFF87B2
movl %r13d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb $0x20, 0xa(%r15)
jne 0x572e
movzwl 0x10(%r15), %eax
leaq (%r15,%rax), %r13
leaq (%r15,%rax), %rcx
addq $0x8, %rcx
movzwl -0x2(%rcx), %eax
jmp 0x573c
movq %r15, 0x20(%rsp)
movzwl 0x42(%r12), %eax
movq 0x48(%r12,%rax,8), %r15
testb $0x2, 0xa(%r15)
je 0x5ae5
movq %r12, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x9741
movq %rax, %r13
testq %rbp, %rbp
je 0x5704
cmpl $0x0, (%rbp)
je 0x5697
testq %r13, %r13
jne 0x58c6
pushq $0x1
popq %rsi
movq %r12, %rdi
callq 0x9e6c
testl %eax, %eax
je 0x5884
movl %eax, %r13d
orb $0x2, 0x44(%r12)
jmp 0x569d
movq 0x28(%r12), %rax
movl (%rax), %eax
leaq 0x10(%r15), %rcx
xorl %r13d, %r13d
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movq %rcx, 0x8(%rsi)
movq 0x30(%r12), %rax
movq %rbx, %rdi
callq *0x10(%rax)
testl %eax, %eax
je 0x57af
movq %r13, 0x18(%rsp)
jle 0x57d4
movzwl 0xc(%r15), %eax
addl $-0x10, %eax
movl %eax, %r13d
shrl %r13d
cmpl $0x4, %eax
jb 0x580a
leaq 0x10(%r15), %rcx
leal -0x1(%r13), %edx
testb $0x20, 0xa(%r15)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x30(%rsp)
jne 0x57dc
movzwl 0x10(%r15,%rdx,2), %eax
leaq (%r15,%rax), %rcx
movq %rcx, 0x18(%rsp)
addq %r15, %rax
addq $0x8, %rax
movzwl -0x2(%rax), %ecx
movq %rcx, 0x8(%rsp)
jmp 0x57e8
movzwl 0x42(%r12), %eax
andw $0x0, 0x148(%r12,%rax,2)
testq %rbp, %rbp
je 0x58c6
movl $0x1, (%rbp)
jmp 0x58c6
movzwl 0x42(%r12), %eax
jmp 0x584f
movq 0x8(%rsp), %rax
imulq %rdx, %rax
addq %rcx, %rax
leaq 0x8(%rsp), %rsi
movq %rax, 0x8(%rsi)
movq 0x30(%r12), %rax
movq %rbx, %rdi
callq *0x10(%rax)
testl %eax, %eax
je 0x58a4
js 0x5a2d
movzwl 0x42(%r12), %eax
movzwl %ax, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x5841
movzwl 0x148(%r12,%rdx,2), %esi
movq 0x48(%r12,%rdx,8), %rdi
movzwl 0xc(%rdi), %edi
addl $-0x10, %edi
shrl %edi
decl %edi
cmpl %esi, %edi
ja 0x583c
incq %rdx
jmp 0x5815
cmpq %rdx, %rcx
jne 0x584f
movw %r13w, 0x148(%r12,%rcx,2)
jmp 0x5697
testw %ax, %ax
je 0x585e
movq 0x20(%rsp), %r15
jmp 0x5651
andw $0x0, 0x148(%r12)
cmpl $0x11, %r14d
setne %al
testq %rbp, %rbp
setne %cl
orb %al, %cl
movq 0x18(%rsp), %r13
jne 0x5697
jmp 0x58c6
movzwl 0x42(%r12), %eax
movq 0x48(%r12,%rax,8), %r15
testb $0x2, 0xa(%r15)
je 0x5b06
movzwl 0x10(%r15), %r13d
addq %r15, %r13
jmp 0x58c6
movzwl 0x42(%r12), %eax
movq 0x30(%rsp), %rcx
movw %cx, 0x148(%r12,%rax,2)
testq %rbp, %rbp
movq 0x18(%rsp), %r13
jne 0x57c8
movl 0x44(%r12), %eax
andl $-0x4, %eax
incl %eax
movl %eax, 0x44(%r12)
testb $0x20, 0xa(%r15)
jne 0x599a
testb $0x4, 0x4(%r13)
je 0x58f2
movq %r12, %rdi
movq %r13, %rsi
callq 0x5e86
movq %r13, %rbp
xorl %r13d, %r13d
movq 0x20(%rsp), %r15
testq %r15, %r15
je 0x5a94
testb $0x4, 0x4(%rbp)
jne 0x59d9
movl %r14d, %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0x59f9
movq 0x18(%r12), %rdi
leaq 0x8(%rsp), %rdx
movq %rbp, %rsi
callq 0x5f9f
testl %eax, %eax
jne 0x5662
movq 0x30(%r12), %rax
movq 0x18(%rax), %rax
leaq -0x2bc0(%rip), %rcx     # 0x2d89
xorq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq (%rsi), %rdx
xorq $0x8, %rdx
orq %rcx, %rdx
leaq -0x2bc1(%rip), %rcx     # 0x2da1
cmovneq %rax, %rcx
movq %r15, %rdi
callq *%rcx
testl %eax, %eax
je 0x5a94
movl %eax, %ecx
movl $0xffff87b2, %eax       # imm = 0xFFFF87B2
cmpl $0x2, %r14d
je 0x5662
testl %ecx, %ecx
jg 0x5662
movups 0x8(%rsp), %xmm0
movups %xmm0, (%r15)
jmp 0x5a94
andl $-0x2, %r14d
xorl %r13d, %r13d
cmpl $0x10, %r14d
jne 0x569d
movq 0x28(%r12), %rax
movl (%rax), %eax
movq %rax, (%rbx)
movzwl 0x42(%r12), %ecx
movzwl 0x148(%r12,%rcx,2), %ecx
imulq %rax, %rcx
leaq (%r15,%rcx), %rax
addq $0x10, %rax
movq %rax, 0x8(%rbx)
jmp 0x569d
leal -0xf(%r14), %eax
cmpl $0x2, %eax
ja 0x5a19
movq 0x10(%r12), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x5fe8
movl %eax, %r13d
jmp 0x5a94
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x5a07
andb $-0x4, 0x44(%rax)
movq 0x18(%r12), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x5f9f
jmp 0x5a8c
xorl %r13d, %r13d
cmpl $0x2, %r14d
jne 0x5a77
leaq 0x8(%rsp), %r8
andl $0x0, (%r8)
jmp 0x5a7a
movzwl 0x42(%r12), %eax
movzwl 0x148(%r12,%rax,2), %eax
movzwl 0xc(%r15), %ecx
addl $-0x10, %ecx
shrl %ecx
cmpl %eax, %ecx
movq 0x18(%rsp), %r13
jbe 0x56e8
testb $0x20, 0xa(%r15)
jne 0x5ab6
movzwl 0x10(%r15,%rax,2), %eax
leaq (%r15,%rax), %r13
leaq (%r15,%rax), %rcx
addq $0x8, %rcx
movzwl -0x2(%rcx), %eax
movq %rax, 0x8(%rsp)
jmp 0x5ac4
xorl %r8d, %r8d
movq 0x10(%r12), %rdi
pushq $0x11
popq %rcx
movq %r15, %rsi
xorl %edx, %edx
callq 0x5619
testl %eax, %eax
jne 0x5662
andl $-0x2, %r14d
cmpl $0x10, %r14d
jne 0x569d
movzwl 0x6(%rbp), %eax
movq %rax, (%rbx)
addq $0x8, %rbp
movq %rbp, 0x8(%rbx)
jmp 0x569d
imulq 0x8(%rsp), %rax
movq 0x28(%rsp), %rcx
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, 0x8(%rsi)
movq 0x30(%r12), %rax
movq %rbx, %rdi
callq *0x10(%rax)
testl %eax, %eax
jne 0x56e8
jmp 0x57bf
movq 0x18(%r12), %rax
movq 0x20(%rax), %rdi
leaq 0x8f68(%rip), %rsi      # 0xea5d
leaq 0x8f6d(%rip), %rdx      # 0xea69
movl $0x172f, %ecx           # imm = 0x172F
callq 0xb672
movq 0x18(%r12), %rax
movq 0x20(%rax), %rdi
leaq 0x8f47(%rip), %rsi      # 0xea5d
leaq 0x8f4c(%rip), %rdx      # 0xea69
movl $0x173f, %ecx           # imm = 0x173F
callq 0xb672