// File: /workspace/github_cpp_makefile_star2_O1/jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_update_key
static int
mdb_update_key(MDB_cursor *mc, MDB_val *key)
{
	MDB_page		*mp;
	MDB_node		*node;
	char			*base;
	size_t			 len;
	int				 delta, ksize, oksize;
	indx_t			 ptr, i, numkeys, indx;
	DKBUF;

	indx = mc->mc_ki[mc->mc_top];
	mp = mc->mc_pg[mc->mc_top];
	node = NODEPTR(mp, indx);
	ptr = mp->mp_ptrs[indx];
#if MDB_DEBUG
	{
		MDB_val	k2;
		char kbuf2[DKBUF_MAXKEYSIZE*2+1];
		k2.mv_data = NODEKEY(node);
		k2.mv_size = node->mn_ksize;
		DPRINTF(("update key %u (ofs %u) [%s] to [%s] on page %"Z"u",
			indx, ptr,
			mdb_dkey(&k2, kbuf2),
			DKEY(key),
			mp->mp_pgno));
	}
#endif

	/* Sizes must be 2-byte aligned. */
	ksize = EVEN(key->mv_size);
	oksize = EVEN(node->mn_ksize);
	delta = ksize - oksize;

	/* Shift node contents if EVEN(key length) changed. */
	if (delta) {
		if (delta > 0 && SIZELEFT(mp) < delta) {
			pgno_t pgno;
			/* not enough space left, do a delete and split */
			DPRINTF(("Not enough room, delta = %d, splitting...", delta));
			pgno = NODEPGNO(node);
			mdb_node_del(mc, 0);
			return mdb_page_split(mc, key, NULL, pgno, MDB_SPLIT_REPLACE);
		}

		numkeys = NUMKEYS(mp);
		for (i = 0; i < numkeys; i++) {
			if (mp->mp_ptrs[i] <= ptr)
				mp->mp_ptrs[i] -= delta;
		}

		base = (char *)mp + mp->mp_upper + PAGEBASE;
		len = ptr - mp->mp_upper + NODESIZE;
		memmove(base - delta, base, len);
		mp->mp_upper -= delta;

		node = NODEPTR(mp, indx);
	}

	/* But even if no shift was needed, update ksize */
	if (node->mn_ksize != key->mv_size)
		node->mn_ksize = key->mv_size;

	if (key->mv_size)
		memcpy(NODEKEY(node), key->mv_data, key->mv_size);

	return MDB_SUCCESS;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rsi, %rbx
movq %rdi, %r14
movzwl 0x42(%rdi), %eax
movzwl 0x148(%rdi,%rax,2), %r12d
movq 0x48(%rdi,%rax,8), %r15
movzwl 0x10(%r15,%r12,2), %edx
leaq (%r15,%rdx), %rdi
movl (%rsi), %eax
incl %eax
movslq %eax, %r13
andq $-0x2, %r13
movzwl 0x6(%r15,%rdx), %eax
incl %eax
andl $-0x2, %eax
cmpl %eax, %r13d
je 0x72e1
subq %rax, %r13
testl %r13d, %r13d
jle 0x7287
movzwl 0xe(%r15), %eax
subw 0xc(%r15), %ax
movzwl %ax, %eax
cmpl %eax, %r13d
jle 0x7287
movl (%rdi), %eax
movzwl 0x4(%rdi), %r15d
shlq $0x20, %r15
orq %rax, %r15
movq %r14, %rdi
xorl %esi, %esi
callq 0x776d
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %r15, %rcx
movl $0x40000, %r8d          # imm = 0x40000
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x7d7b
movzwl 0xc(%r15), %eax
addl $0x1fff0, %eax          # imm = 0x1FFF0
shrl %eax
movzwl %ax, %eax
testl %eax, %eax
je 0x72b8
xorl %ecx, %ecx
movzwl 0x10(%r15,%rcx,2), %esi
cmpw %dx, %si
ja 0x72b0
subl %r13d, %esi
movw %si, 0x10(%r15,%rcx,2)
incq %rcx
cmpq %rcx, %rax
jne 0x729c
movzwl 0xe(%r15), %eax
leaq (%r15,%rax), %rsi
subq %rax, %rdx
addq $0x8, %rdx
movq %rsi, %rdi
subq %r13, %rdi
callq 0x22d0
subw %r13w, 0xe(%r15)
movzwl 0x10(%r15,%r12,2), %edi
addq %r15, %rdi
movzwl 0x6(%rdi), %eax
movq (%rbx), %rdx
cmpq %rax, %rdx
je 0x72f1
movw %dx, 0x6(%rdi)
testq %rdx, %rdx
je 0x7303
addq $0x8, %rdi
movq 0x8(%rbx), %rsi
callq 0x2240
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq