// File: /workspace/github_c_makefile_star2_O0/Tarsnap[P]kivaloo/kvlds/btree_node_split.c
// Function: split_leaf
static int
split_leaf(struct btree * T, struct node * N, const struct kvldskey ** keys,
    struct node ** parents, size_t * nparts, size_t breakat)
{
	size_t i;
	size_t cursize;
	size_t nkeys;

	/* This is a leaf. */
	assert(N->type == NODE_TYPE_LEAF);

	/* Scan through nodes. */
	*nparts = 0;
	cursize = SERIALIZE_OVERHEAD;
	nkeys = 0;
	for (i = 0; i < N->nkeys; i++) {
		/* Should we split before this next key-value pair? */
		if (cursize > breakat) {
			/* Create a new leaf node. */
			if ((parents[*nparts] =
			    makeleaf(T, nkeys, &N->u.pairs[i - nkeys])) == NULL)
				goto err1;

			/*
			 * Create a separator key which is greater than the
			 * previous key and less than or equal to the next
			 * key.
			 */
			keys[*nparts] = N->u.pairs[i].k;

			/* We've finished this part. */
			*nparts += 1;
			cursize = SERIALIZE_OVERHEAD;
			nkeys = 0;
		}

		/* Add the key size. */
		cursize += kvldskey_serial_size(N->u.pairs[i].k);

		/* Add the value size. */
		cursize += kvldskey_serial_size(N->u.pairs[i].v);

		/* We have a key in the node we're constructing. */
		nkeys += 1;
	}

	/* Create a leaf with whatever we've got left over. */
	if ((parents[*nparts] = makeleaf(T, nkeys,
	    &N->u.pairs[i - nkeys])) == NULL)
		goto err1;
	*nparts += 1;

	/* Destroy the old node. */
	btree_node_destroy(T, N);

	/* Success! */
	return (0);

err1:
	for (; *nparts > 0; *nparts -= 1)
		btree_node_destroy(T, parents[*nparts - 1]);

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x18(%rbp), %rax
movb 0x1c(%rax), %al
andb $0x3, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0xd403
jmp 0xd422
leaq 0x11cc9(%rip), %rdi     # 0x1f0d3
leaq 0x12311(%rip), %rsi     # 0x1f722
movl $0x87, %edx
leaq 0x123d6(%rip), %rcx     # 0x1f7f3
callq 0x21d0
movq -0x30(%rbp), %rax
movq $0x0, (%rax)
movq $0xa, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0xd54e
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
jbe 0xd4e6
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdx
movq -0x40(%rbp), %rax
subq -0x50(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdx
callq 0xd950
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq (%rdx), %rdx
movq %rax, (%rcx,%rdx,8)
cmpq $0x0, %rax
jne 0xd4a3
jmp 0xd5ad
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movq $0xa, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movzbl (%rax), %eax
addq $0x1, %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movzbl (%rax), %eax
addq $0x1, %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xd445
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdx
movq -0x40(%rbp), %rax
subq -0x50(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdx
callq 0xd950
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq (%rdx), %rdx
movq %rax, (%rcx,%rdx,8)
cmpq $0x0, %rax
jne 0xd589
jmp 0xd5ad
movq -0x30(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xc2e0
movl $0x0, -0x4(%rbp)
jmp 0xd5ec
jmp 0xd5af
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
jbe 0xd5e5
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rsi
callq 0xc2e0
movq -0x30(%rbp), %rax
movq (%rax), %rcx
subq $0x1, %rcx
movq %rcx, (%rax)
jmp 0xd5af
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)