// File: /workspace/github_cpp_makefile_star2_O1/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 0x5d62
movq %r8, %rbp
movl %ecx, %r14d
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r13
movq 0x10(%rdi), %rax
testq %rax, %rax
je 0x5d07
andb $-0x4, 0x44(%rax)
testb $0x1, 0x44(%r13)
jne 0x5d76
xorl %r15d, %r15d
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xa112
testl %eax, %eax
jne 0x5d67
movq %r12, 0x20(%rsp)
movq %r14, 0x18(%rsp)
movzwl 0x42(%r13), %eax
movq 0x48(%r13,%rax,8), %r12
testb $0x2, 0xa(%r12)
jne 0x5fb3
movq 0x18(%r13), %rax
movq 0x20(%rax), %rdi
leaq 0x9cec(%rip), %rsi      # 0xfa3d
leaq 0x9cf1(%rip), %rdx      # 0xfa49
movl $0x172f, %ecx           # imm = 0x172F
callq 0xc43e
movl $0xffff87c3, %eax       # imm = 0xFFFF87C3
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, 0x20(%rsp)
movq %r14, 0x18(%rsp)
movzwl 0x42(%r13), %eax
movq 0x48(%r13,%rax,8), %r12
movzwl 0xc(%r12), %ecx
andl $-0x2, %ecx
cmpw $0x10, %cx
jne 0x5db2
movw $0x0, 0x148(%r13,%rax,2)
movl $0x1, %r14d
xorl %r8d, %r8d
jmp 0x5f8a
testb $0x20, 0xa(%r12)
jne 0x5dda
movzwl 0x10(%r12), %eax
leaq (%r12,%rax), %rcx
movq %rcx, (%rsp)
addq %r12, %rax
addq $0x8, %rax
movzwl -0x2(%rax), %ecx
movq %rcx, 0x8(%rsp)
jmp 0x5df2
movq 0x28(%r13), %rax
movl (%rax), %eax
movq %rax, 0x8(%rsp)
leaq 0x10(%r12), %rax
movq $0x0, (%rsp)
leaq 0x8(%rsp), %rsi
movq %rax, 0x8(%rsi)
movq 0x30(%r13), %rax
movq %rbx, %rdi
callq *0x10(%rax)
testl %eax, %eax
je 0x5e5d
movl %eax, %r15d
jle 0x5e8b
movzwl 0xc(%r12), %eax
addl $-0x10, %eax
movl %eax, %r9d
shrl %r9d
cmpl $0x4, %eax
jb 0x5e94
leaq 0x10(%r12), %rcx
leal -0x1(%r9), %r14d
testb $0x20, 0xa(%r12)
movq %r9, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
jne 0x5e9a
movzwl 0x10(%r12,%r14,2), %eax
leaq (%r12,%rax), %rcx
movq %rcx, (%rsp)
addq %r12, %rax
addq $0x8, %rax
movzwl -0x2(%rax), %ecx
movq %rcx, 0x8(%rsp)
jmp 0x5ea6
movzwl 0x42(%r13), %eax
movw $0x0, 0x148(%r13,%rax,2)
xorl %r15d, %r15d
movl $0x2, %r14d
testq %rbp, %rbp
je 0x5e82
movl $0x1, (%rbp)
movq (%rsp), %r8
jmp 0x5f8a
movq (%rsp), %r8
jmp 0x5f51
movq (%rsp), %r8
jmp 0x5ecf
movq 0x8(%rsp), %rax
imulq %r14, %rax
addq %rcx, %rax
leaq 0x8(%rsp), %rsi
movq %rax, 0x8(%rsi)
movq 0x30(%r13), %rax
movq %rbx, %rdi
callq *0x10(%rax)
testl %eax, %eax
je 0x5f24
js 0x61b0
movl %eax, %r15d
movq (%rsp), %r8
movq 0x30(%rsp), %r9
movzwl 0x42(%r13), %eax
xorl %r14d, %r14d
movl $0x0, %edx
testl %eax, %eax
je 0x5f0b
movl %eax, %ecx
xorl %edx, %edx
movzwl 0x148(%r13,%rdx,2), %esi
movq 0x48(%r13,%rdx,8), %rdi
movzwl 0xc(%rdi), %edi
addl $-0x10, %edi
shrl %edi
decl %edi
cmpl %esi, %edi
ja 0x5f0b
incq %rdx
cmpq %rdx, %rcx
jne 0x5ee4
movl %eax, %edx
movb $0x1, %cl
cmpl %eax, %edx
jne 0x5f4d
movw %r9w, 0x148(%r13,%rax,2)
movl $0x1, %r14d
xorl %ecx, %ecx
jmp 0x5f4d
movzwl 0x42(%r13), %eax
movw %r14w, 0x148(%r13,%rax,2)
movl $0x2, %r14d
testq %rbp, %rbp
je 0x5f44
movl $0x1, (%rbp)
xorl %ecx, %ecx
xorl %r15d, %r15d
movq (%rsp), %r8
testb %cl, %cl
je 0x5f8a
xorl %r14d, %r14d
cmpw $0x0, 0x42(%r13)
jne 0x5f8a
movw $0x0, 0x148(%r13)
cmpl $0x11, 0x18(%rsp)
setne %al
testq %rbp, %rbp
setne %cl
orb %al, %cl
movzbl %cl, %eax
movl $0x2, %r14d
subl %eax, %r14d
xorl %ecx, %ecx
testb %al, %al
cmovel %ecx, %r15d
cmpl $0x3, %r14d
je 0x5fb3
cmpl $0x2, %r14d
je 0x5fdc
movl $0xffff87b2, %eax       # imm = 0xFFFF87B2
testl %r14d, %r14d
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %r12
je 0x5d0e
jmp 0x5d67
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0xa2ee
movq %rax, %r8
testq %rbp, %rbp
je 0x5fd3
cmpl $0x0, (%rbp)
je 0x60c4
testq %r8, %r8
je 0x60a9
movl 0x44(%r13), %eax
andl $-0x4, %eax
incl %eax
movl %eax, 0x44(%r13)
testb $0x20, 0xa(%r12)
jne 0x606a
testb $0x4, 0x4(%r8)
je 0x6009
movq %r13, %rdi
movq %r8, %rsi
movq %r8, %r14
callq 0x6663
movq %r14, %r8
movq 0x20(%rsp), %r12
testq %r12, %r12
movq 0x18(%rsp), %r14
je 0x618e
testb $0x4, 0x4(%r8)
jne 0x60ce
movl %r14d, %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0x611a
movq 0x18(%r13), %rdi
leaq 0x8(%rsp), %rdx
movq %r8, %rbp
movq %r8, %rsi
callq 0x6780
xorl %r15d, %r15d
testl %eax, %eax
je 0x620d
xorl %edx, %edx
movl %eax, %r15d
movq %rbp, %r8
testb %dl, %dl
je 0x5d67
jmp 0x618e
movq 0x18(%rsp), %rcx
andl $-0x2, %ecx
xorl %eax, %eax
cmpl $0x10, %ecx
jne 0x5d67
movq 0x28(%r13), %rcx
movl (%rcx), %ecx
movq %rcx, (%rbx)
movzwl 0x42(%r13), %edx
movzwl 0x148(%r13,%rdx,2), %edx
imulq %rcx, %rdx
leaq (%r12,%rdx), %rcx
addq $0x10, %rcx
movq %rcx, 0x8(%rbx)
jmp 0x5d67
movq %r13, %rdi
movl $0x1, %esi
callq 0xaab8
testl %eax, %eax
je 0x60f3
orb $0x2, 0x44(%r13)
jmp 0x5d67
movl $0xffff87b2, %eax       # imm = 0xFFFF87B2
jmp 0x5d67
leal -0xf(%r14), %eax
cmpl $0x2, %eax
ja 0x6149
movq 0x10(%r13), %rdi
movq %r12, %rsi
xorl %edx, %edx
movq %r8, %r15
callq 0x67c9
movq %r15, %r8
movl %eax, %r15d
jmp 0x618e
movzwl 0x42(%r13), %eax
movq 0x48(%r13,%rax,8), %r12
testb $0x2, 0xa(%r12)
je 0x62b0
movzwl 0x10(%r12), %r8d
addq %r12, %r8
xorl %r15d, %r15d
jmp 0x5fdc
movq 0x10(%r13), %rax
testq %rax, %rax
je 0x6127
andb $-0x4, 0x44(%rax)
movq 0x18(%r13), %rdi
movq %r8, %rsi
movq %r12, %rdx
movq %r8, %r12
callq 0x6780
testl %eax, %eax
jne 0x5d67
xorl %r15d, %r15d
movq %r12, %r8
jmp 0x618e
movq %r8, %rbp
cmpl $0x2, %r14d
jne 0x6160
leaq 0x8(%rsp), %r8
movl $0x0, (%r8)
jmp 0x6163
xorl %r8d, %r8d
movq 0x10(%r13), %rdi
movq %r12, %rsi
xorl %edx, %edx
movl $0x11, %ecx
callq 0x5cd7
movl %eax, %r15d
movl $0xffff87b2, %eax       # imm = 0xFFFF87B2
testl %r15d, %r15d
cmovnel %r15d, %eax
jne 0x5d67
movq %rbp, %r8
andl $-0x2, %r14d
cmpl $0x10, %r14d
jne 0x61a8
movzwl 0x6(%r8), %eax
movq %rax, (%rbx)
addq $0x8, %r8
movq %r8, 0x8(%rbx)
movl %r15d, %eax
jmp 0x5d67
movzwl 0x42(%r13), %eax
movzwl 0x148(%r13,%rax,2), %eax
movzwl 0xc(%r12), %edx
addl $-0x10, %edx
shrl %edx
xorl %ecx, %ecx
movl $0x3, %r14d
cmpl %eax, %edx
movq (%rsp), %r8
jbe 0x626e
testb $0x20, 0xa(%r12)
jne 0x6280
movzwl 0x10(%r12,%rax,2), %eax
leaq (%r12,%rax), %rcx
movq %rcx, (%rsp)
leaq (%r12,%rax), %rcx
addq $0x8, %rcx
movzwl -0x2(%rcx), %eax
movq %rax, 0x8(%rsp)
jmp 0x628e
movq 0x30(%r13), %rax
movq 0x18(%rax), %rax
leaq -0x31e3(%rip), %rcx     # 0x3039
xorq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq (%rsi), %rdx
xorq $0x8, %rdx
orq %rcx, %rdx
leaq -0x31e2(%rip), %rcx     # 0x3053
cmovneq %rax, %rcx
movq %r12, %rdi
callq *%rcx
movl %eax, %ecx
movl $0xffff87b2, %eax       # imm = 0xFFFF87B2
movb $0x1, %dl
testl %ecx, %ecx
je 0x605a
cmpl $0x2, %r14d
movq %rbp, %r8
je 0x6276
testl %ecx, %ecx
jg 0x6276
movups 0x8(%rsp), %xmm0
movups %xmm0, (%r12)
xorl %r15d, %r15d
jmp 0x605d
xorl %r15d, %r15d
jmp 0x5f4d
xorl %edx, %edx
movl %ecx, %r15d
jmp 0x605d
imulq 0x8(%rsp), %rax
movq 0x28(%rsp), %rcx
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, 0x8(%rsi)
movq 0x30(%r13), %rax
movq %rbx, %rdi
callq *0x10(%rax)
xorl %ecx, %ecx
testl %eax, %eax
jne 0x5f46
jmp 0x5f32
movq 0x18(%r13), %rax
movq 0x20(%rax), %rdi
leaq 0x977e(%rip), %rsi      # 0xfa3d
leaq 0x9783(%rip), %rdx      # 0xfa49
movl $0x173f, %ecx           # imm = 0x173F
callq 0xc43e