// File: /workspace/github_c_makefile_star2_O1/Tarsnap[P]kivaloo/kvlds/btree_mutate.c
// Function: btree_mutate_immutable
int
btree_mutate_immutable(struct node * N)
{
	size_t nkeys_list, nkeys_hash;
	struct kvpair_const * new_pairs;
	size_t new_nkeys;
	size_t i, j, k;

	/* Sanity check. */
	assert(N->type == NODE_TYPE_LEAF);
	assert(N->state == NODE_STATE_DIRTY);
	assert(N->pagesize == (uint32_t)(-1));

	/* Count keys with non-NULL values in the sorted list. */
	for (nkeys_list = i = 0; i < N->nkeys; i++)
		if (N->u.pairs[i].v != NULL)
			nkeys_list += 1;

	/* Count keys with non-NULL values in the hash table. */
	for (nkeys_hash = i = 0; i < N->v.H->nslots; i++)
		if (N->v.H->pairs[i].v != NULL)
			nkeys_hash += 1;

	/* How many key-value pairs do we have? */
	new_nkeys = nkeys_list + nkeys_hash;

	/* If there's nothing to do, do nothing. */
	if (new_nkeys == 0) {
		new_pairs = NULL;
		goto done;
	}

	/* Allocate new array of key-value pairs. */
	if (IMALLOC(new_pairs, new_nkeys, struct kvpair_const))
		goto err0;

	/* Copy pairs from the hash table to the end of the array. */
	for (j = nkeys_list, i = 0; i < N->v.H->nslots; i++) {
		if (N->v.H->pairs[i].v != NULL) {
			new_pairs[j].k = N->v.H->pairs[i].k;
			new_pairs[j].v = N->v.H->pairs[i].v;
			j++;
		}
	}

	/* Sort the pairs from the hash table in-place. */
	kvpair_sort((struct kvpair *)&new_pairs[nkeys_list],
	    nkeys_hash, N->mlen_n);

	/* Merge pairs from the sorted list into the new array. */
	for (j = i = 0, k = nkeys_list; i < N->nkeys; i++) {
		/* Skip keys for which the value was deleted. */
		if (N->u.pairs[i].v == NULL)
			continue;

		/* Move hashed pairs with smaller keys than this one. */
		while ((k < new_nkeys) && (kvldskey_cmp2(N->u.pairs[i].k,
		    new_pairs[k].k, N->mlen_n) > 0)) {
			new_pairs[j].k = new_pairs[k].k;
			new_pairs[j].v = new_pairs[k].v;
			j++;
			k++;
		}

		/* Copy in the pair. */
		new_pairs[j].k = N->u.pairs[i].k;
		new_pairs[j].v = N->u.pairs[i].v;
		j++;
	}

done:
	/* Free old array of key-value pairs. */
	free(N->u.pairs);

	/* Free the hash table. */
	kvhash_free(N->v.H);

	/* Update node. */
	N->u.pairs = new_pairs;
	N->nkeys = new_nkeys;
	N->v.H = NULL;

	/* Success! */
	return (0);

err0:
	/* Failure! */
	return (-1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movb 0x1c(%rdi), %al
movl %eax, %ecx
andb $0x3, %cl
cmpb $0x1, %cl
jne 0x8a68
andb $0xc, %al
cmpb $0x8, %al
jne 0x8a87
movq %rdi, %r8
cmpl $-0x1, 0x18(%rdi)
jne 0x8aa6
movq 0x38(%r8), %rax
testq %rax, %rax
je 0x8872
movq 0x40(%r8), %rcx
addq $0x8, %rcx
xorl %ebp, %ebp
cmpq $0x1, (%rcx)
sbbq $-0x1, %rbp
addq $0x10, %rcx
decq %rax
jne 0x885f
jmp 0x8874
xorl %ebp, %ebp
movq 0x48(%r8), %rcx
movq 0x18(%rcx), %rax
testq %rax, %rax
je 0x889e
movq (%rcx), %rcx
addq $0x8, %rcx
xorl %r12d, %r12d
cmpq $0x1, (%rcx)
sbbq $-0x1, %r12
addq $0x10, %rcx
decq %rax
jne 0x888b
jmp 0x88a1
xorl %r12d, %r12d
xorl %r14d, %r14d
movq %r12, %rcx
addq %rbp, %rcx
movq %rcx, 0x10(%rsp)
je 0x88cf
movq %r8, 0x8(%rsp)
movq %rcx, %rax
shrq $0x3c, %rax
je 0x88d7
callq 0x20a0
movl $0xc, (%rax)
xorl %r15d, %r15d
jmp 0x88e6
xorl %r15d, %r15d
jmp 0x8a24
movq %rcx, %rdi
shlq $0x4, %rdi
callq 0x22a0
movq %rax, %r15
testq %r15, %r15
movq 0x8(%rsp), %r8
je 0x8a50
movq 0x48(%r8), %rcx
movq 0x18(%rcx), %rax
testq %rax, %rax
je 0x8934
movq (%rcx), %rcx
addq $0x8, %rcx
movq %rbp, %rdx
cmpq $0x0, (%rcx)
je 0x892b
movq -0x8(%rcx), %rsi
movq %rdx, %rdi
shlq $0x4, %rdi
movq %rsi, (%r15,%rdi)
movq (%rcx), %rsi
movq %rsi, 0x8(%r15,%rdi)
incq %rdx
addq $0x10, %rcx
decq %rax
jne 0x890b
movq %rbp, %rdi
shlq $0x4, %rdi
addq %r15, %rdi
movzbl 0x1f(%r8), %edx
movq %r12, %rsi
callq 0x1069d
movq 0x8(%rsp), %r8
cmpq $0x0, 0x38(%r8)
je 0x8a24
xorl %r12d, %r12d
xorl %ecx, %ecx
movq 0x10(%rsp), %rsi
movq %r15, 0x18(%rsp)
movq 0x40(%r8), %rax
movq %rcx, %r14
shlq $0x4, %r14
cmpq $0x0, 0x8(%rax,%r14)
je 0x8a14
cmpq %rsi, %rbp
movq %rcx, 0x20(%rsp)
jae 0x89ea
movq %rbp, %r13
shlq $0x4, %r13
movq %r12, %rbx
shlq $0x4, %rbx
movq 0x40(%r8), %rax
movq (%rax,%r14), %rdi
movq (%r15,%r13), %rsi
movzbl 0x1f(%r8), %edx
callq 0x10377
testl %eax, %eax
jle 0x89e0
movq (%r15,%r13), %rax
movq %rax, (%r15,%rbx)
movq 0x8(%r15,%r13), %rax
movq %rax, 0x8(%r15,%rbx)
incq %r12
incq %rbp
addq $0x10, %r15
movq 0x10(%rsp), %rsi
cmpq %rsi, %rbp
movq 0x8(%rsp), %r8
jb 0x8999
jmp 0x89ea
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %rsi
movq 0x40(%r8), %rax
movq (%rax,%r14), %rcx
movq %r12, %rdx
shlq $0x4, %rdx
movq 0x18(%rsp), %r15
movq %rcx, (%r15,%rdx)
movq 0x8(%rax,%r14), %rax
movq %rax, 0x8(%r15,%rdx)
incq %r12
movq 0x20(%rsp), %rcx
incq %rcx
cmpq 0x38(%r8), %rcx
jb 0x896a
xorl %r14d, %r14d
movq 0x40(%r8), %rdi
movq %r8, %rbx
callq 0x2040
movq 0x48(%rbx), %rdi
callq 0x10666
movq %r15, 0x40(%rbx)
movq 0x10(%rsp), %rax
movq %rax, 0x38(%rbx)
movq $0x0, 0x48(%rbx)
jmp 0x8a56
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc294(%rip), %rdi      # 0x14d03
leaq 0xc51e(%rip), %rsi      # 0x14f94
leaq 0xc598(%rip), %rcx      # 0x15015
movl $0x65, %edx
callq 0x21c0
leaq 0xc53d(%rip), %rdi      # 0x14fcb
leaq 0xc4ff(%rip), %rsi      # 0x14f94
leaq 0xc579(%rip), %rcx      # 0x15015
movl $0x66, %edx
callq 0x21c0
leaq 0xc53b(%rip), %rdi      # 0x14fe8
leaq 0xc4e0(%rip), %rsi      # 0x14f94
leaq 0xc55a(%rip), %rcx      # 0x15015
movl $0x67, %edx
callq 0x21c0
nopl (%rax)