// File: /workspace/github_c_makefile_star2_O2/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 0x762c
andb $0xc, %al
cmpb $0x8, %al
jne 0x7645
movq %rdi, %r8
cmpl $-0x1, 0x18(%rdi)
jne 0x765e
movq 0x38(%r8), %rax
pushq $0x8
popq %rcx
xorl %ebx, %ebx
subq $0x1, %rax
jb 0x7472
movq 0x40(%r8), %rdx
cmpq $0x1, (%rdx,%rcx)
sbbq $-0x1, %rbx
addq $0x10, %rcx
jmp 0x7459
movq 0x48(%r8), %r14
movq 0x18(%r14), %r15
pushq $0x8
popq %rax
xorl %r12d, %r12d
movq %r15, %rcx
subq $0x1, %rcx
jb 0x749b
movq (%r14), %rdx
cmpq $0x1, (%rdx,%rax)
sbbq $-0x1, %r12
addq $0x10, %rax
jmp 0x7483
movq %r12, %rcx
addq %rbx, %rcx
movq %rcx, 0x8(%rsp)
je 0x74be
movq %rcx, %rax
shrq $0x3c, %rax
je 0x74eb
callq 0x20a0
movl $0xc, (%rax)
jmp 0x753c
xorl %ebp, %ebp
movq 0x40(%r8), %rdi
movq %r8, %rbx
callq 0x2040
movq 0x48(%rbx), %rdi
callq 0xe5f1
movq %rbp, 0x40(%rbx)
movq 0x8(%rsp), %rax
movq %rax, 0x38(%rbx)
andq $0x0, 0x48(%rbx)
xorl %eax, %eax
jmp 0x753f
movq %r8, %r13
movq %rcx, %rdi
shlq $0x4, %rdi
callq 0x22a0
testq %rax, %rax
je 0x753c
movq %rax, %rbp
pushq $0x8
popq %rax
movq %rbx, %rcx
movq %r13, %r8
subq $0x1, %r15
jb 0x754e
movq (%r14), %rsi
movq (%rsi,%rax), %rdx
testq %rdx, %rdx
je 0x7536
movq -0x8(%rsi,%rax), %rsi
movq %rcx, %rdi
shlq $0x4, %rdi
movq %rsi, (%rbp,%rdi)
movq %rdx, 0x8(%rbp,%rdi)
incq %rcx
addq $0x10, %rax
jmp 0x750b
pushq $-0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rax
shlq $0x4, %rax
movq %rbp, %rdi
addq %rax, %rdi
movzbl 0x1f(%r8), %edx
movq %r12, %rsi
callq 0xe629
movq %r13, %r8
xorl %edx, %edx
xorl %r14d, %r14d
movq 0x8(%rsp), %rcx
movq %rbp, 0x18(%rsp)
cmpq 0x38(%r8), %rdx
jae 0x74c0
movq 0x40(%r8), %rax
movq %rdx, %r15
shlq $0x4, %r15
cmpq $0x0, 0x8(%rax,%r15)
je 0x7624
movq %rdx, 0x20(%rsp)
movq %r14, %rdx
incq %r14
shlq $0x4, %rdx
movq %rdx, 0x10(%rsp)
movq %rbx, %r13
shlq $0x4, %r13
cmpq %rcx, %rbx
jae 0x760b
movq (%rax,%r15), %rdi
movq (%rbp,%r13), %rsi
movzbl 0x1f(%r8), %edx
movq %r8, %r12
callq 0xe31c
testl %eax, %eax
jle 0x75fe
movups (%rbp,%r13), %xmm0
movq 0x10(%rsp), %rax
movups %xmm0, (%rbp,%rax)
incq %rbx
movq 0x40(%r12), %rax
incq %r14
addq $0x10, %rbp
movq %r12, %r8
movq 0x8(%rsp), %rcx
jmp 0x75b6
movq 0x40(%r12), %rax
movq %r12, %r8
movq 0x8(%rsp), %rcx
movups (%rax,%r15), %xmm0
movq 0x10(%rsp), %rax
movups %xmm0, (%rbp,%rax)
movq 0x18(%rsp), %rbp
movq 0x20(%rsp), %rdx
incq %rdx
jmp 0x757a
leaq 0xa6c0(%rip), %rdi      # 0x11cf3
leaq 0xa935(%rip), %rsi      # 0x11f6f
leaq 0xa9af(%rip), %rcx      # 0x11ff0
pushq $0x65
jmp 0x7675
leaq 0xa95a(%rip), %rdi      # 0x11fa6
leaq 0xa91c(%rip), %rsi      # 0x11f6f
leaq 0xa996(%rip), %rcx      # 0x11ff0
pushq $0x66
jmp 0x7675
leaq 0xa95e(%rip), %rdi      # 0x11fc3
leaq 0xa903(%rip), %rsi      # 0x11f6f
leaq 0xa97d(%rip), %rcx      # 0x11ff0
pushq $0x67
popq %rdx
callq 0x21c0
nop