// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movb 0x1c(%rax), %al
andb $0x3, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x7e9d
jmp 0x82bf
movq -0x18(%rbp), %rax
movb 0x1c(%rax), %al
shrb $0x2, %al
andb $0x3, %al
movzbl %al, %eax
cmpl $0x2, %eax
je 0x7eb6
jmp 0x82bf
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
ja 0x7eff
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x7e70
cmpl $0x0, %eax
je 0x7eef
jmp 0x82d1
jmp 0x7ef1
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x7ebe
movq $0x0, -0x20(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
ja 0x7f97
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x8570
cmpl $0x0, %eax
je 0x7f78
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xecb0
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jbe 0x7f78
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0xd0a0
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x7f84
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x7f86
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x7f0f
movq -0x38(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jne 0x7fb6
jmp 0x82bf
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jbe 0x7fc6
jmp 0x7fe5
leaq 0x16e73(%rip), %rdi     # 0x1ee40
leaq 0x16e81(%rip), %rsi     # 0x1ee55
movl $0x45, %edx
leaq 0x16e85(%rip), %rcx     # 0x1ee65
callq 0x21d0
movq -0x38(%rbp), %rdi
movl $0x8, %esi
callq 0x85b0
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jne 0x8009
cmpq $0x0, -0x38(%rbp)
jbe 0x8009
jmp 0x82d1
movq -0x38(%rbp), %rdi
addq $0x1, %rdi
movl $0x8, %esi
callq 0x85b0
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x8038
movq -0x38(%rbp), %rax
addq $0x1, %rax
cmpq $0x0, %rax
jbe 0x8038
jmp 0x82c8
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
ja 0x8178
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x8570
cmpl $0x0, %eax
je 0x8103
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xecb0
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jbe 0x8103
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x40(%rbp), %rdx
movq -0x28(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdx
movq -0x48(%rbp), %rcx
movq -0x28(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rcx
leaq -0x30(%rbp), %r8
callq 0xd2e0
cmpl $0x0, %eax
je 0x8101
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x48(%rbp), %rax
movq -0x28(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq $0x1, -0x30(%rbp)
movl $0x1, -0x4c(%rbp)
jmp 0x8127
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x48(%rbp), %rax
movq -0x28(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq $0x1, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0x8159
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x40(%rbp), %rax
movq -0x28(%rbp), %rcx
addq -0x30(%rbp), %rcx
subq $0x1, %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x815b
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x30(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x8048
cmpl $0x0, -0x4c(%rbp)
je 0x818a
movq -0x28(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0x819d
jmp 0x81bc
leaq 0x16cf2(%rip), %rdi     # 0x1ee96
leaq 0x16caa(%rip), %rsi     # 0x1ee55
movl $0x71, %edx
leaq 0x16cae(%rip), %rcx     # 0x1ee65
callq 0x21d0
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x2040
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x2040
movq -0x40(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x48(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x38(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
ja 0x82b5
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq $0x0, 0x28(%rax)
je 0x8245
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x28(%rax), %rax
cmpq -0x18(%rbp), %rax
jne 0x8247
jmp 0x8266
leaq 0x16c5b(%rip), %rdi     # 0x1eea9
leaq 0x16c00(%rip), %rsi     # 0x1ee55
movl $0x83, %edx
leaq 0x16c04(%rip), %rcx     # 0x1ee65
callq 0x21d0
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq $0x0, 0x28(%rax)
je 0x827f
jmp 0x82a4
movq -0x18(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x8650
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x8202
cmpl $0x0, -0x4c(%rbp)
je 0x82bd
jmp 0x82d1
jmp 0x82bf
movl $0x0, -0x4(%rbp)
jmp 0x82d8
movq -0x40(%rbp), %rdi
callq 0x2040
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)