// File: /workspace/github_cpp_makefile_star2_O3/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 0x5b07
movq %r8, %rbp
movl %ecx, %r14d
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r12
movq 0x10(%rdi), %rax
testq %rax, %rax
je 0x5ae9
andb $-0x4, 0x44(%rax)
testb $0x1, 0x44(%r12)
jne 0x5b0f
movq %r12, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x9d28
testl %eax, %eax
je 0x5b6e
movl %eax, %r13d
jmp 0x5b3e
movl $0xffff87c3, %r13d      # imm = 0xFFFF87C3
jmp 0x5b3e
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 0x5b50
movw $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 0x5bd1
movzwl 0x10(%r15), %eax
leaq (%r15,%rax), %r13
leaq (%r15,%rax), %rcx
addq $0x8, %rcx
movzwl -0x2(%rcx), %eax
jmp 0x5bdf
movq %r15, 0x20(%rsp)
movzwl 0x42(%r12), %eax
movq 0x48(%r12,%rax,8), %r15
testb $0x2, 0xa(%r15)
je 0x5f8f
movq %r12, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x9ee4
movq %rax, %r13
testq %rbp, %rbp
je 0x5ba5
cmpl $0x0, (%rbp)
je 0x5b38
testq %r13, %r13
jne 0x5d6b
movq %r12, %rdi
movl $0x1, %esi
callq 0xa6a4
testl %eax, %eax
je 0x5d29
movl %eax, %r13d
orb $0x2, 0x44(%r12)
jmp 0x5b3e
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 0x5c52
movq %r13, 0x18(%rsp)
jle 0x5c78
movzwl 0xc(%r15), %eax
addl $-0x10, %eax
movl %eax, %r13d
shrl %r13d
cmpl $0x4, %eax
jb 0x5cae
leaq 0x10(%r15), %rcx
leal -0x1(%r13), %edx
testb $0x20, 0xa(%r15)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x30(%rsp)
jne 0x5c80
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 0x5c8c
movzwl 0x42(%r12), %eax
movw $0x0, 0x148(%r12,%rax,2)
testq %rbp, %rbp
je 0x5d6b
movl $0x1, (%rbp)
jmp 0x5d6b
movzwl 0x42(%r12), %eax
jmp 0x5cf3
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 0x5d49
js 0x5ed5
movzwl 0x42(%r12), %eax
xorl %ecx, %ecx
testl %eax, %eax
je 0x5ce1
movzwl 0x148(%r12,%rcx,2), %edx
movq 0x48(%r12,%rcx,8), %rsi
movzwl 0xc(%rsi), %esi
addl $-0x10, %esi
shrl %esi
decl %esi
cmpl %edx, %esi
ja 0x5ce1
incq %rcx
cmpq %rcx, %rax
jne 0x5cba
jmp 0x5ce5
cmpl %eax, %ecx
jne 0x5cf3
movw %r13w, 0x148(%r12,%rax,2)
jmp 0x5b38
testw %ax, %ax
je 0x5d02
movq 0x20(%rsp), %r15
jmp 0x5af1
movw $0x0, 0x148(%r12)
cmpl $0x11, %r14d
setne %al
testq %rbp, %rbp
setne %cl
orb %al, %cl
movq 0x18(%rsp), %r13
jne 0x5b38
jmp 0x5d6b
movzwl 0x42(%r12), %eax
movq 0x48(%r12,%rax,8), %r15
testb $0x2, 0xa(%r15)
je 0x5fb0
movzwl 0x10(%r15), %r13d
addq %r15, %r13
jmp 0x5d6b
movzwl 0x42(%r12), %eax
movq 0x30(%rsp), %rcx
movw %cx, 0x148(%r12,%rax,2)
testq %rbp, %rbp
movq 0x18(%rsp), %r13
jne 0x5c6c
movl 0x44(%r12), %eax
andl $-0x4, %eax
incl %eax
movl %eax, 0x44(%r12)
testb $0x20, 0xa(%r15)
jne 0x5e3f
testb $0x4, 0x4(%r13)
je 0x5d97
movq %r12, %rdi
movq %r13, %rsi
callq 0x633e
movq %r13, %rbp
xorl %r13d, %r13d
movq 0x20(%rsp), %r15
testq %r15, %r15
je 0x5f3e
testb $0x4, 0x4(%rbp)
jne 0x5e7e
movl %r14d, %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0x5e9e
movq 0x18(%r12), %rdi
leaq 0x8(%rsp), %rdx
movq %rbp, %rsi
callq 0x645b
testl %eax, %eax
jne 0x5b02
movq 0x30(%r12), %rax
movq 0x18(%rax), %rax
leaq -0x2dc5(%rip), %rcx     # 0x3029
xorq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq (%rsi), %rdx
xorq $0x8, %rdx
orq %rcx, %rdx
leaq -0x2dc4(%rip), %rcx     # 0x3043
cmovneq %rax, %rcx
movq %r15, %rdi
callq *%rcx
testl %eax, %eax
je 0x5f3e
movl %eax, %ecx
movl $0xffff87b2, %eax       # imm = 0xFFFF87B2
cmpl $0x2, %r14d
je 0x5b02
testl %ecx, %ecx
jg 0x5b02
movups 0x8(%rsp), %xmm0
movups %xmm0, (%r15)
jmp 0x5f3e
andl $-0x2, %r14d
xorl %r13d, %r13d
cmpl $0x10, %r14d
jne 0x5b3e
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 0x5b3e
leal -0xf(%r14), %eax
cmpl $0x2, %eax
ja 0x5ebe
movq 0x10(%r12), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x64a4
movl %eax, %r13d
jmp 0x5f3e
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x5eac
andb $-0x4, 0x44(%rax)
movq 0x18(%r12), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x645b
jmp 0x5f36
xorl %r13d, %r13d
cmpl $0x2, %r14d
jne 0x5f1f
leaq 0x8(%rsp), %r8
movl $0x0, (%r8)
jmp 0x5f22
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 0x5b89
testb $0x20, 0xa(%r15)
jne 0x5f60
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 0x5f6e
xorl %r8d, %r8d
movq 0x10(%r12), %rdi
movq %r15, %rsi
xorl %edx, %edx
movl $0x11, %ecx
callq 0x5ab9
testl %eax, %eax
jne 0x5b02
andl $-0x2, %r14d
cmpl $0x10, %r14d
jne 0x5b3e
movzwl 0x6(%rbp), %eax
movq %rax, (%rbx)
addq $0x8, %rbp
movq %rbp, 0x8(%rbx)
jmp 0x5b3e
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 0x5b89
jmp 0x5c63
movq 0x18(%r12), %rax
movq 0x20(%rax), %rdi
leaq 0x9ade(%rip), %rsi      # 0xfa7d
leaq 0x9ae3(%rip), %rdx      # 0xfa89
movl $0x172f, %ecx           # imm = 0x172F
callq 0xbfbd
movq 0x18(%r12), %rax
movq 0x20(%rax), %rdi
leaq 0x9abd(%rip), %rsi      # 0xfa7d
leaq 0x9ac2(%rip), %rdx      # 0xfa89
movl $0x173f, %ecx           # imm = 0x173F
callq 0xbfbd