// File: /workspace/github_c_makefile_star2_O1/Tarsnap[P]kivaloo/kvlds/btree_balance.c
// Function: splitchildren
static int
splitchildren(struct btree * T, struct node * N)
{
	size_t i, j, nparts;
	size_t new_nkeys;
	const struct kvldskey ** new_keys;
	struct node ** new_children;
	int failed = 0;		/* We haven't failed yet. */

	/* If this node has no children, do nothing. */
	if (N->type != NODE_TYPE_PARENT)
		goto done;

	/* If this node is not dirty, it doesn't need splitting. */
	if (N->state != NODE_STATE_DIRTY)
		goto done;

	/* Recurse down. */
	for (i = 0; i <= N->nkeys; i++) {
		if (splitchildren(T, N->v.children[i]))
			goto err0;
	}

	/* Figure out how many children we'll have after splitting them. */
	for (new_nkeys = i = 0; i <= N->nkeys; i++) {
		if (node_present(N->v.children[i]) &&
		    (serialize_size(N->v.children[i]) > T->pagelen))
			new_nkeys +=
			    btree_node_split_nparts(T, N->v.children[i]);
		else
			new_nkeys += 1;
	}
	new_nkeys -= 1;		/* Last child doesn't have a separator key. */

	/*
	 * If the number of children won't change, we must not need to split
	 * any nodes; so we have nothing to do.
	 */
	if (new_nkeys == N->nkeys)
		goto done;

	/* This must be more than we had before. */
	assert(new_nkeys > N->nkeys);

	/* Allocate new child and key arrays. */
	if (IMALLOC(new_keys, new_nkeys, const struct kvldskey *))
		goto err0;
	if (IMALLOC(new_children, new_nkeys + 1, struct node *))
		goto err1;

	/*
	 * We're going to walk through the list of children, splitting them
	 * as required, and placing nodes onto the new_children list.  While
	 * doing this, we're also going to place keys onto the new_keys list.
	 * If we fail to split a node, we'll just copy it intact (thus ending
	 * up with an intact tree, albeit one with an overlarge node).
	 */
	for (i = 0, j = 0; i <= N->nkeys; i++, j += nparts) {
		/* If a node is present and overlarge, split it. */
		if (node_present(N->v.children[i]) &&
		    (serialize_size(N->v.children[i]) > T->pagelen)) {
			if (btree_node_split(T, N->v.children[i],
			    &new_keys[j], &new_children[j], &nparts)) {
				/*
				 * Splitting failed.  Just grab the unsplit
				 * node and record that we failed.
				 */
				new_children[j] = N->v.children[i];
				nparts = 1;
				failed = 1;
			}
		} else {
			new_children[j] = N->v.children[i];
			nparts = 1;
		}

		/* Handle the separator, if one exists. */
		if (i < N->nkeys)
			new_keys[j + nparts - 1] = N->u.keys[i];
	}

	/* If we failed, we will have less keys than expected. */
	if (failed)
		new_nkeys = j - 1;

	/* Check that we got the right number of keys. */
	assert(new_nkeys == j - 1);

	/* Free old separator and child vectors. */
	free(N->u.keys);
	free(N->v.children);

	/* Attach new vectors. */
	N->u.keys = new_keys;
	N->v.children = new_children;
	N->nkeys = new_nkeys;

	/* Hook up parent pointers. */
	for (i = 0; i <= N->nkeys; i++) {
		/*
		 * The dirty parent should be either N (non-split node) or
		 * NULL (split parts).
		 */
		assert((N->v.children[i]->p_dirty == NULL) ||
		    (N->v.children[i]->p_dirty == N));

		/* We don't need to do anything if p_dirty is correct. */
		if (N->v.children[i]->p_dirty)
			continue;

		/* If it's NULL, we need to hook it up. */
		N->v.children[i]->p_dirty = N;

		/* ... and add a child lock on N. */
		btree_node_lock(T, N);
	}

#ifdef SANITY_CHECKS
	/* Sanity check the B+Tree. */
	btree_sanity(T);
#endif

	/* If we failed to split a node, exit with an error response. */
	if (failed)
		goto err0;

done:
	/* Success! */
	return (0);

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movb 0x1c(%rsi), %al
andb $0xf, %al
cmpb $0x8, %al
jne 0x6675
movq %rsi, %rbx
movq %rdi, %r14
xorl %r15d, %r15d
movq 0x48(%rbx), %rax
movq (%rax,%r15,8), %rsi
movq %r14, %rdi
callq 0x65e6
testl %eax, %eax
jne 0x6870
incq %r15
cmpq 0x38(%rbx), %r15
jbe 0x6606
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x48(%rbx), %rax
movq (%rax,%r12,8), %rdi
movl $0x1, %r15d
testb $0x2, 0x1c(%rdi)
jne 0x665d
callq 0xa703
cmpq (%r14), %rax
jbe 0x665d
movq 0x48(%rbx), %rax
movq (%rax,%r12,8), %rsi
movq %r14, %rdi
callq 0x9730
movq %rax, %r15
addq %r15, %rbp
incq %r12
movq 0x38(%rbx), %rax
cmpq %rax, %r12
jbe 0x662c
leaq -0x1(%rbp), %r15
cmpq %rax, %r15
jne 0x667c
xorl %eax, %eax
jmp 0x6875
jbe 0x68a3
movq %r15, %rax
shrq $0x3d, %rax
je 0x669b
callq 0x20a0
movl $0xc, (%rax)
xorl %r13d, %r13d
jmp 0x66ab
leaq (,%r15,8), %rdi
callq 0x22a0
movq %rax, %r13
testq %r13, %r13
je 0x6870
testq %rbp, %rbp
je 0x66d3
movabsq $0x1fffffffffffffff, %rax # imm = 0x1FFFFFFFFFFFFFFF
cmpq %rax, %rbp
jbe 0x66d8
callq 0x20a0
movl $0xc, (%rax)
xorl %r12d, %r12d
jmp 0x66e8
leaq (,%rbp,8), %rdi
callq 0x22a0
movq %rax, %r12
testq %rbp, %rbp
setne %al
testq %r12, %r12
sete %cl
testb %cl, %al
jne 0x685b
movq %r13, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movq $0x0, 0x10(%rsp)
xorl %r15d, %r15d
xorl %r13d, %r13d
movq 0x48(%rbx), %rax
movq (%rax,%r15,8), %rdi
testb $0x2, 0x1c(%rdi)
jne 0x6777
callq 0xa703
cmpq (%r14), %rax
jbe 0x6777
movq 0x48(%rbx), %rax
movq (%rax,%r15,8), %rsi
movq 0x18(%rsp), %rax
leaq (%rax,%r13,8), %rdx
leaq (%r12,%r13,8), %rbp
movq %r14, %rdi
movq %rbp, %rcx
leaq 0x8(%rsp), %r8
callq 0x982f
testl %eax, %eax
je 0x678c
movq 0x48(%rbx), %rax
movq (%rax,%r15,8), %rax
movq %rax, (%rbp)
movq $0x1, 0x8(%rsp)
movl $0x1, %eax
movq %rax, 0x10(%rsp)
jmp 0x678c
movq 0x48(%rbx), %rax
movq (%rax,%r15,8), %rax
movq %rax, (%r12,%r13,8)
movq $0x1, 0x8(%rsp)
cmpq 0x38(%rbx), %r15
jae 0x67ad
movq 0x40(%rbx), %rax
movq (%rax,%r15,8), %rax
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %rdx
leaq (%rdx,%r13,8), %rdx
movq %rax, -0x8(%rdx,%rcx,8)
incq %r15
addq 0x8(%rsp), %r13
cmpq 0x38(%rbx), %r15
jbe 0x6715
decq %r13
cmpl $0x0, 0x10(%rsp)
movq 0x20(%rsp), %r15
cmovneq %r13, %r15
cmpq %r13, %r15
jne 0x68c2
movq 0x40(%rbx), %rdi
callq 0x2040
movq 0x48(%rbx), %rdi
callq 0x2040
movq 0x18(%rsp), %rax
movq %rax, 0x40(%rbx)
movq %r12, 0x48(%rbx)
movq %r15, 0x38(%rbx)
xorl %r15d, %r15d
movq 0x48(%rbx), %rax
movq (%rax,%r15,8), %rax
movq 0x28(%rax), %rcx
testq %rcx, %rcx
sete %dl
cmpq %rbx, %rcx
sete %sil
orb %dl, %sil
je 0x6884
testq %rcx, %rcx
je 0x682d
incq %r15
cmpq 0x38(%rbx), %r15
jbe 0x67ff
jmp 0x6865
movq %rbx, 0x28(%rax)
testq %rbx, %rbx
je 0x6822
movq 0x30(%r14), %rdi
movq 0x20(%rdi), %rax
movq (%rbx,%rax), %rax
movq (%rax), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rax)
testq %rcx, %rcx
jne 0x6822
movq %rbx, %rsi
callq 0x107a3
jmp 0x6822
movq %r13, %rdi
callq 0x2040
jmp 0x6870
cmpl $0x0, 0x10(%rsp)
je 0x6675
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe21e(%rip), %rdi      # 0x14aa9
leaq 0xe1c3(%rip), %rsi      # 0x14a55
leaq 0xe1cc(%rip), %rcx      # 0x14a65
movl $0x83, %edx
callq 0x21c0
leaq 0xe196(%rip), %rdi      # 0x14a40
leaq 0xe1a4(%rip), %rsi      # 0x14a55
leaq 0xe1ad(%rip), %rcx      # 0x14a65
movl $0x45, %edx
callq 0x21c0
leaq 0xe1cd(%rip), %rdi      # 0x14a96
leaq 0xe185(%rip), %rsi      # 0x14a55
leaq 0xe18e(%rip), %rcx      # 0x14a65
movl $0x71, %edx
callq 0x21c0