// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movb 0x1c(%rax), %al
andb $0x3, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0xb97f
jmp 0xb99e
leaq 0x1374d(%rip), %rdi     # 0x1f0d3
leaq 0x139d7(%rip), %rsi     # 0x1f364
movl $0x65, %edx
leaq 0x13a4c(%rip), %rcx     # 0x1f3e5
callq 0x21d0
movq -0x10(%rbp), %rax
movb 0x1c(%rax), %al
shrb $0x2, %al
andb $0x3, %al
movzbl %al, %eax
cmpl $0x2, %eax
jne 0xb9b4
jmp 0xb9d3
leaq 0x139e0(%rip), %rdi     # 0x1f39b
leaq 0x139a2(%rip), %rsi     # 0x1f364
movl $0x66, %edx
leaq 0x13a17(%rip), %rcx     # 0x1f3e5
callq 0x21d0
movq -0x10(%rbp), %rax
cmpl $-0x1, 0x18(%rax)
jne 0xb9df
jmp 0xb9fe
leaq 0x139d2(%rip), %rdi     # 0x1f3b8
leaq 0x13977(%rip), %rsi     # 0x1f364
movl $0x67, %edx
leaq 0x139ec(%rip), %rcx     # 0x1f3e5
callq 0x21d0
movq $0x0, -0x38(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0xba52
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0xba42
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0xba44
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xba0e
movq $0x0, -0x38(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x48(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jae 0xbaad
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0xba9d
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0xba9f
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xba62
movq -0x18(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0xbacd
movq $0x0, -0x28(%rbp)
jmp 0xbd48
movq -0x30(%rbp), %rdi
movl $0x10, %esi
callq 0xbda0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0xbaf1
cmpq $0x0, -0x30(%rbp)
jbe 0xbaf1
jmp 0xbd8f
movq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x48(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jae 0xbbab
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0xbb98
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xbb9a
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xbb01
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rax
movzbl 0x1f(%rax), %eax
movl %eax, %edx
callq 0x18d80
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0xbd46
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
jne 0xbc16
jmp 0xbd35
jmp 0xbc18
movq -0x48(%rbp), %rcx
xorl %eax, %eax
cmpq -0x30(%rbp), %rcx
movb %al, -0x49(%rbp)
jae 0xbc67
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movzbl 0x1f(%rax), %eax
movl %eax, %edx
callq 0x18690
cmpl $0x0, %eax
setg %al
movb %al, -0x49(%rbp)
movb -0x49(%rbp), %al
testb $0x1, %al
jne 0xbc70
jmp 0xbcd7
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0xbc18
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xbbe5
jmp 0xbd48
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x2040
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x18cf0
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x48(%rax)
movl $0x0, -0x4(%rbp)
jmp 0xbd96
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop