// File: /workspace/github_c_makefile_star2_O2/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 0x57a1
movq %rsi, %rbx
movq %rdi, %r14
xorl %r15d, %r15d
movq 0x38(%rbx), %rax
cmpq %rax, %r15
ja 0x574c
movq 0x48(%rbx), %rax
movq (%rax,%r15,8), %rsi
movq %r14, %rdi
callq 0x5703
incq %r15
testl %eax, %eax
je 0x5727
pushq $-0x1
popq %rax
jmp 0x57a3
xorl %r15d, %r15d
pushq $0x1
popq %r12
xorl %r13d, %r13d
cmpq %rax, %r13
ja 0x5798
movq 0x48(%rbx), %rax
movq (%rax,%r13,8), %rdi
testb $0x2, 0x1c(%rdi)
movq %r12, %rax
jne 0x578c
callq 0x8e4b
movq %rax, %rcx
movq %r12, %rax
cmpq (%r14), %rcx
jbe 0x578c
movq 0x48(%rbx), %rax
movq (%rax,%r13,8), %rsi
movq %r14, %rdi
callq 0x8040
addq %rax, %r15
incq %r13
movq 0x38(%rbx), %rax
jmp 0x5756
leaq -0x1(%r15), %rdi
cmpq %rax, %rdi
jne 0x57b2
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jbe 0x597a
callq 0x59b0
testq %rax, %rax
je 0x5747
movq %rax, %rbp
movq %r15, %rdi
callq 0x59b0
movq %rax, 0x18(%rsp)
testq %rax, %rax
setne %al
testq %r15, %r15
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x58c2
movq 0x38(%rbx), %rax
xorl %r12d, %r12d
xorl %r13d, %r13d
movl $0x0, 0xc(%rsp)
movq %rbp, 0x10(%rsp)
cmpq %rax, %r13
ja 0x58cf
movq 0x48(%rbx), %rax
movq (%rax,%r13,8), %rsi
testb $0x2, 0x1c(%rsi)
jne 0x5875
movq %rsi, %rdi
callq 0x8e4b
movq 0x48(%rbx), %rcx
movq (%rcx,%r13,8), %rsi
cmpq (%r14), %rax
jbe 0x5875
leaq (,%r12,8), %rdx
addq %rbp, %rdx
movq 0x18(%rsp), %rax
leaq (%rax,%r12,8), %rbp
movq %r14, %rdi
movq %rbp, %rcx
leaq 0x20(%rsp), %r8
callq 0x810c
testl %eax, %eax
je 0x588c
movq 0x48(%rbx), %rax
movq (%rax,%r13,8), %rax
movq %rax, (%rbp)
movq $0x1, 0x20(%rsp)
pushq $0x1
popq %rcx
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
jmp 0x5891
movq 0x18(%rsp), %rax
movq %rsi, (%rax,%r12,8)
movq $0x1, 0x20(%rsp)
pushq $0x1
popq %rcx
jmp 0x5896
movq 0x20(%rsp), %rcx
movq 0x10(%rsp), %rbp
movq 0x38(%rbx), %rax
cmpq %rax, %r13
jae 0x58b7
movq 0x40(%rbx), %rdx
movq (%rdx,%r13,8), %rdx
leaq (,%r12,8), %rsi
addq %rbp, %rsi
movq %rdx, -0x8(%rsi,%rcx,8)
incq %r13
addq %rcx, %r12
jmp 0x5800
movq %rbp, %rdi
callq 0x2040
jmp 0x5747
movl 0xc(%rsp), %ebp
testl %ebp, %ebp
jne 0x58e0
cmpq %r12, %r15
jne 0x5993
decq %r12
movq 0x40(%rbx), %rdi
callq 0x2040
movq 0x48(%rbx), %rdi
callq 0x2040
movq 0x10(%rsp), %rax
movq %rax, 0x40(%rbx)
movq 0x18(%rsp), %rax
movq %rax, 0x48(%rbx)
movq %r12, 0x38(%rbx)
xorl %r15d, %r15d
cmpq %r12, %r15
ja 0x594e
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 0x595b
testq %rcx, %rcx
jne 0x5949
movq %rbx, 0x28(%rax)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x59da
movq 0x38(%rbx), %r12
incq %r15
jmp 0x590e
testl %ebp, %ebp
jne 0x5747
jmp 0x57a1
leaq 0xc12f(%rip), %rdi      # 0x11a91
leaq 0xc0d4(%rip), %rsi      # 0x11a3d
leaq 0xc0dd(%rip), %rcx      # 0x11a4d
movl $0x83, %edx
callq 0x21c0
leaq 0xc0a7(%rip), %rdi      # 0x11a28
leaq 0xc0b5(%rip), %rsi      # 0x11a3d
leaq 0xc0be(%rip), %rcx      # 0x11a4d
pushq $0x45
jmp 0x59aa
leaq 0xc0e4(%rip), %rdi      # 0x11a7e
leaq 0xc09c(%rip), %rsi      # 0x11a3d
leaq 0xc0a5(%rip), %rcx      # 0x11a4d
pushq $0x71
popq %rdx
callq 0x21c0