// File: /workspace/github_c_makefile_star2_O3/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 0x89e8
andb $0xc, %al
cmpb $0x8, %al
jne 0x8a07
movq %rdi, %r8
cmpl $-0x1, 0x18(%rdi)
jne 0x8a26
movq 0x38(%r8), %rax
testq %rax, %rax
je 0x880b
movq 0x40(%r8), %rcx
addq $0x8, %rcx
xorl %r13d, %r13d
cmpq $0x1, (%rcx)
sbbq $-0x1, %r13
addq $0x10, %rcx
decq %rax
jne 0x87f8
jmp 0x880e
xorl %r13d, %r13d
movq 0x48(%r8), %r15
movq 0x18(%r15), %r14
testq %r14, %r14
je 0x883b
movq (%r15), %rax
addq $0x8, %rax
xorl %r12d, %r12d
movq %r14, %rcx
cmpq $0x1, (%rax)
sbbq $-0x1, %r12
addq $0x10, %rax
decq %rcx
jne 0x8828
jmp 0x883e
xorl %r12d, %r12d
movq %r12, %rbp
addq %r13, %rbp
je 0x885f
movq %rbp, %rax
shrq $0x3c, %rax
je 0x8867
callq 0x20a0
movl $0xc, (%rax)
jmp 0x89a6
xorl %r15d, %r15d
jmp 0x89b2
movq %r8, 0x8(%rsp)
movq %rbp, %rdi
shlq $0x4, %rdi
callq 0x22a0
testq %rax, %rax
je 0x89a6
testq %r14, %r14
movq 0x8(%rsp), %r8
je 0x88bd
movq (%r15), %r9
addq $0x8, %r9
movq %r13, %rcx
movq (%r9), %rdx
testq %rdx, %rdx
je 0x88b4
movq -0x8(%r9), %rsi
movq %rcx, %rdi
shlq $0x4, %rdi
movq %rsi, (%rax,%rdi)
movq %rdx, 0x8(%rax,%rdi)
incq %rcx
addq $0x10, %r9
decq %r14
jne 0x8895
movq %r13, %rcx
shlq $0x4, %rcx
movq %rax, 0x10(%rsp)
movq %rax, %rdi
addq %rcx, %rdi
movzbl 0x1f(%r8), %edx
movq %r12, %rsi
callq 0x1060d
movq 0x8(%rsp), %r8
movq 0x38(%r8), %rax
testq %rax, %rax
je 0x89ad
movq 0x40(%r8), %rcx
xorl %r12d, %r12d
xorl %esi, %esi
movq 0x10(%rsp), %r15
movq %rbp, 0x18(%rsp)
movq %rsi, %r14
shlq $0x4, %r14
cmpq $0x0, 0x8(%rcx,%r14)
je 0x8998
cmpq %rbp, %r13
jae 0x8984
movq %rsi, 0x20(%rsp)
movq %r13, %rbp
shlq $0x4, %rbp
movq %r12, %rbx
shlq $0x4, %rbx
movq 0x40(%r8), %rax
movq (%rax,%r14), %rdi
movq (%r15,%rbp), %rsi
movzbl 0x1f(%r8), %edx
callq 0x102fb
testl %eax, %eax
jle 0x8968
movups (%r15,%rbp), %xmm0
movups %xmm0, (%r15,%rbx)
incq %r12
incq %r13
addq $0x10, %r15
cmpq 0x18(%rsp), %r13
movq 0x8(%rsp), %r8
jb 0x892c
jmp 0x896d
movq 0x8(%rsp), %r8
movq 0x38(%r8), %rax
movq 0x40(%r8), %rcx
movq 0x18(%rsp), %rbp
movq 0x10(%rsp), %r15
movq 0x20(%rsp), %rsi
movq %r12, %rdx
shlq $0x4, %rdx
movups (%rcx,%r14), %xmm0
movups %xmm0, (%r15,%rdx)
incq %r12
incq %rsi
cmpq %rax, %rsi
jb 0x8901
jmp 0x89b2
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x89d9
movq 0x10(%rsp), %r15
movq 0x40(%r8), %rdi
movq %r8, %rbx
callq 0x2040
movq 0x48(%rbx), %rdi
callq 0x105d7
movq %r15, 0x40(%rbx)
movq %rbp, 0x38(%rbx)
movq $0x0, 0x48(%rbx)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc2e4(%rip), %rdi      # 0x14cd3
leaq 0xc559(%rip), %rsi      # 0x14f4f
leaq 0xc5d3(%rip), %rcx      # 0x14fd0
movl $0x65, %edx
callq 0x21c0
leaq 0xc578(%rip), %rdi      # 0x14f86
leaq 0xc53a(%rip), %rsi      # 0x14f4f
leaq 0xc5b4(%rip), %rcx      # 0x14fd0
movl $0x66, %edx
callq 0x21c0
leaq 0xc576(%rip), %rdi      # 0x14fa3
leaq 0xc51b(%rip), %rsi      # 0x14f4f
leaq 0xc595(%rip), %rcx      # 0x14fd0
movl $0x67, %edx
callq 0x21c0
nopl (%rax)