// File: /workspace/github_c_makefile_star2_O0/Tarsnap[P]kivaloo/kvlds/btree_node_merge.c
// Function: merge_leaf
static int
merge_leaf(struct btree * T, struct node ** c_in, struct node ** c_out,
    size_t nsep)
{
	size_t nkeys;
	size_t i, j;
	struct kvpair_const * pairs;
	struct node * N;

	/* The input nodes are dirty leaves. */
	for (i = 0; i <= nsep; i++) {
		assert(c_in[i]->type == NODE_TYPE_LEAF);
		assert(c_in[i]->state == NODE_STATE_DIRTY);
	}

	/* Figure out how big the new node needs to be. */
	for (nkeys = i = 0; i <= nsep; i++)
		nkeys += c_in[i]->nkeys;

	/* Allocate array of pair structures. */
	if (IMALLOC(pairs, nkeys, struct kvpair_const))
		goto err0;

	/* Copy in pointers to keys and values. */
	for (j = i = 0; i <= nsep; j += c_in[i]->nkeys, i++) {
		if (c_in[i]->nkeys > 0)
			memcpy(&pairs[j], c_in[i]->u.pairs,
			    c_in[i]->nkeys * sizeof(struct kvpair_const));
	}

	/* Create a node. */
	if ((N = btree_node_mkleaf(T, nkeys, pairs)) == NULL)
		goto err1;

	/* Assign a parent to this node. */
	N->p_dirty = c_in[0]->p_dirty;
	btree_node_lock(T, N->p_dirty);

	/* Destroy old nodes. */
	for (i = 0; i <= nsep; i++)
		btree_node_destroy(T, c_in[i]);

	/* Store created node. */
	c_out[0] = N;

	/* Success! */
	return (0);

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
ja 0xdf38
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movb 0x1c(%rax), %al
andb $0x3, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0xdec9
jmp 0xdee8
leaq 0x11ab0(%rip), %rdi     # 0x1f980
leaq 0x11a15(%rip), %rsi     # 0x1f8ec
movl $0x19, %edx
leaq 0x11abd(%rip), %rcx     # 0x1f9a0
callq 0x21d0
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movb 0x1c(%rax), %al
shrb $0x2, %al
andb $0x3, %al
movzbl %al, %eax
cmpl $0x2, %eax
jne 0xdf06
jmp 0xdf25
leaq 0x119bc(%rip), %rdi     # 0x1f8c9
leaq 0x119d8(%rip), %rsi     # 0x1f8ec
movl $0x1a, %edx
leaq 0x11a80(%rip), %rcx     # 0x1f9a0
callq 0x21d0
jmp 0xdf27
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xdea0
movq $0x0, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
ja 0xdf78
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x38(%rax), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xdf48
movq -0x30(%rbp), %rdi
movl $0x10, %esi
callq 0xe540
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0xdf9c
cmpq $0x0, -0x30(%rbp)
jbe 0xdf9c
jmp 0xe0d0
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
ja 0xe029
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq $0x0, 0x38(%rax)
jbe 0xe001
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x40(%rax), %rsi
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x38(%rax), %rdx
shlq $0x4, %rdx
callq 0x2280
jmp 0xe003
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x38(%rax), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xdfac
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rcx
movq -0x48(%rbp), %rax
movl $0x1, %esi
xorl %edx, %edx
xorl %r8d, %r8d
movl %r8d, %r9d
movq %r9, %r8
movq %rax, (%rsp)
callq 0xbe40
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
jne 0xe05a
jmp 0xe0c7
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x28(%rax), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rax
movq 0x28(%rax), %rsi
callq 0xe5e0
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
ja 0xe0b3
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0xc2e0
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xe086
movq -0x50(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0xe0d7
movq -0x48(%rbp), %rdi
callq 0x2040
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq