// File: /workspace/github_c_makefile_star2_O3/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 0x6674
movq %rsi, %rbx
movq %rdi, %r14
xorl %r15d, %r15d
movq 0x48(%rbx), %rax
movq (%rax,%r15,8), %rsi
movq %r14, %rdi
callq 0x65e4
testl %eax, %eax
jne 0x6864
incq %r15
cmpq 0x38(%rbx), %r15
jbe 0x6604
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x48(%rbx), %rax
movq (%rax,%r12,8), %rdi
movl $0x1, %r15d
testb $0x2, 0x1c(%rdi)
jne 0x665c
callq 0xa5e7
cmpq (%r14), %rax
jbe 0x665c
movq 0x48(%rbx), %rax
movq (%rax,%r12,8), %rsi
movq %r14, %rdi
callq 0x96c8
movq %rax, %r15
addq %r15, %r13
incq %r12
movq 0x38(%rbx), %rax
cmpq %rax, %r12
jbe 0x662b
leaq -0x1(%r13), %rdi
cmpq %rax, %rdi
jne 0x667b
xorl %eax, %eax
jmp 0x6869
jbe 0x6897
movq %rdi, %rax
shrq $0x3d, %rax
je 0x669a
callq 0x20a0
movl $0xc, (%rax)
jmp 0x6864
shlq $0x3, %rdi
callq 0x22a0
testq %rax, %rax
je 0x6864
movq %rax, %rbp
movabsq $0x1fffffffffffffff, %rax # imm = 0x1FFFFFFFFFFFFFFF
cmpq %rax, %r13
jbe 0x66ce
callq 0x20a0
movl $0xc, (%rax)
jmp 0x685c
leaq (,%r13,8), %rdi
callq 0x22a0
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x685c
movq $0x0, 0x8(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %rbp, 0x20(%rsp)
movq 0x48(%rbx), %rax
movq (%rax,%r12,8), %rsi
testb $0x2, 0x1c(%rsi)
jne 0x676e
movq %rsi, %rdi
callq 0xa5e7
movq 0x48(%rbx), %rcx
movq (%rcx,%r12,8), %rsi
cmpq (%r14), %rax
jbe 0x676e
leaq (,%r15,8), %rdx
addq %rbp, %rdx
movq 0x10(%rsp), %rax
leaq (%rax,%r15,8), %rbp
movq %r14, %rdi
movq %rbp, %rcx
leaq 0x18(%rsp), %r8
callq 0x97c7
testl %eax, %eax
je 0x6787
movq 0x48(%rbx), %rax
movq (%rax,%r12,8), %rax
movq %rax, (%rbp)
movq $0x1, 0x18(%rsp)
movl $0x1, %eax
movq %rax, 0x8(%rsp)
movl $0x1, %eax
jmp 0x678c
movq 0x10(%rsp), %rax
movq %rsi, (%rax,%r15,8)
movq $0x1, 0x18(%rsp)
movl $0x1, %eax
jmp 0x6791
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rbp
movq 0x38(%rbx), %rcx
cmpq %rcx, %r12
jae 0x67b2
movq 0x40(%rbx), %rdx
movq (%rdx,%r12,8), %rdx
leaq (,%r15,8), %rsi
addq %rbp, %rsi
movq %rdx, -0x8(%rsi,%rax,8)
incq %r12
addq %rax, %r15
cmpq %rcx, %r12
jbe 0x66fd
cmpl $0x0, 0x8(%rsp)
jne 0x67d1
cmpq %r15, %r13
jne 0x68b6
decq %r15
movq 0x40(%rbx), %rdi
callq 0x2040
movq 0x48(%rbx), %rdi
callq 0x2040
movq %rbp, 0x40(%rbx)
movq 0x10(%rsp), %rax
movq %rax, 0x48(%rbx)
movq %r15, 0x38(%rbx)
xorl %r12d, %r12d
movq 0x48(%rbx), %rax
movq (%rax,%r12,8), %rax
movq 0x28(%rax), %rcx
testq %rcx, %rcx
sete %dl
cmpq %rbx, %rcx
sete %sil
orb %dl, %sil
je 0x6878
testq %rcx, %rcx
jne 0x6848
movq %rbx, 0x28(%rax)
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 0x6848
movq %rbx, %rsi
callq 0x1070f
movq 0x38(%rbx), %r15
incq %r12
cmpq %r15, %r12
jbe 0x67fa
cmpl $0x0, 0x8(%rsp)
jne 0x6864
jmp 0x6674
movq %rbp, %rdi
callq 0x2040
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe1fa(%rip), %rdi      # 0x14a79
leaq 0xe19f(%rip), %rsi      # 0x14a25
leaq 0xe1a8(%rip), %rcx      # 0x14a35
movl $0x83, %edx
callq 0x21c0
leaq 0xe172(%rip), %rdi      # 0x14a10
leaq 0xe180(%rip), %rsi      # 0x14a25
leaq 0xe189(%rip), %rcx      # 0x14a35
movl $0x45, %edx
callq 0x21c0
leaq 0xe1a9(%rip), %rdi      # 0x14a66
leaq 0xe161(%rip), %rsi      # 0x14a25
leaq 0xe16a(%rip), %rcx      # 0x14a35
movl $0x71, %edx
callq 0x21c0