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

	/* This is a parent. */
	assert(N->type == NODE_TYPE_PARENT);

	/* Scan through nodes. */
	*nparts = 0;
	cursize = SERIALIZE_OVERHEAD + SERIALIZE_PERCHILD;
	nkeys = 0;
	for (i = 1; i <= N->nkeys; i++) {
		/* Should we split before this next child? */
		if (cursize > breakat) {
			/* Create a new parent node. */
			if ((parents[*nparts] = makeparent(T, N->height,
			    nkeys, &N->u.keys[i - nkeys - 1],
			    &N->v.children[i - nkeys - 1])) == NULL)
				goto err1;

			/* Grab the separator key. */
			keys[*nparts] = N->u.keys[i - 1];

			/* We've finished this part. */
			*nparts += 1;
			cursize = SERIALIZE_OVERHEAD + SERIALIZE_PERCHILD;
			nkeys = 0;
		} else {
			/* Add the separator key size. */
			cursize += kvldskey_serial_size(N->u.keys[i-1]);
			nkeys += 1;

			/* Add the next child. */
			cursize += SERIALIZE_PERCHILD;
		}
	}

	/* Create a parent node with whatever we've got left over. */
	if ((parents[*nparts] = makeparent(T, N->height, nkeys,
	    &N->u.keys[i - nkeys - 1], &N->v.children[i - nkeys - 1])) == NULL)
		goto err1;
	*nparts += 1;

	/* Adjust parentage of children. */
	for (i = 0; i < *nparts; i++) {
		for (j = 0; j <= parents[i]->nkeys; j++) {
			/* Unlock old parent. */
			if (node_hasplock(parents[i]->v.children[j]))
				btree_node_unlock(T, N);

			/* Set dirty parent pointer. */
			parents[i]->v.children[j]->p_dirty = parents[i];

			/* Lock new parent. */
			if (node_hasplock(parents[i]->v.children[j]))
				btree_node_lock(T, parents[i]);
		}
	}

	/* Destroy the old node, but not its children. */
	destroy_parent_nokeys(T, N);

	/* Success! */
	return (0);

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

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %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 $0x0, %eax
jne 0xd633
jmp 0xd652
leaq 0x11c91(%rip), %rdi     # 0x1f2cb
leaq 0x120e1(%rip), %rsi     # 0x1f722
movl $0x100, %edx            # imm = 0x100
leaq 0x12210(%rip), %rcx     # 0x1f85d
callq 0x21d0
movq -0x30(%rbp), %rax
movq $0x0, (%rax)
movq $0x1e, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x1, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
ja 0xd78b
movq -0x50(%rbp), %rax
cmpq -0x38(%rbp), %rax
jbe 0xd73d
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movsbl 0x1d(%rax), %esi
movq -0x58(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rcx
movq -0x40(%rbp), %rax
subq -0x58(%rbp), %rax
subq $0x1, %rax
shlq $0x3, %rax
addq %rax, %rcx
movq -0x18(%rbp), %rax
movq 0x48(%rax), %r8
movq -0x40(%rbp), %rax
subq -0x58(%rbp), %rax
subq $0x1, %rax
shlq $0x3, %rax
addq %rax, %r8
callq 0xda90
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq (%rdx), %rdx
movq %rax, (%rcx,%rdx,8)
cmpq $0x0, %rax
jne 0xd6fa
jmp 0xd903
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x40(%rbp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %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 $0x1e, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
jmp 0xd778
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x40(%rbp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rax
movzbl (%rax), %eax
addq $0x1, %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
addq $0x14, %rax
movq %rax, -0x50(%rbp)
jmp 0xd77a
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xd675
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movsbl 0x1d(%rax), %esi
movq -0x58(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rcx
movq -0x40(%rbp), %rax
subq -0x58(%rbp), %rax
subq $0x1, %rax
shlq $0x3, %rax
addq %rax, %rcx
movq -0x18(%rbp), %rax
movq 0x48(%rax), %r8
movq -0x40(%rbp), %rax
subq -0x58(%rbp), %rax
subq $0x1, %rax
shlq $0x3, %rax
addq %rax, %r8
callq 0xda90
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq (%rdx), %rdx
movq %rax, (%rcx,%rdx,8)
cmpq $0x0, %rax
jne 0xd7f0
jmp 0xd903
movq -0x30(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq (%rcx), %rax
jae 0xd8ed
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x28(%rbp), %rcx
movq -0x40(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
cmpq 0x38(%rcx), %rax
ja 0xd8da
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x48(%rax), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xdb90
cmpl $0x0, %eax
je 0xd868
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xdbc0
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq 0x48(%rax), %rax
movq -0x48(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq %rcx, 0x28(%rax)
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x48(%rax), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xdb90
cmpl $0x0, %eax
je 0xd8c7
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0xdbf0
jmp 0xd8c9
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0xd81f
jmp 0xd8dc
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xd806
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xdc20
movl $0x0, -0x4(%rbp)
jmp 0xd942
jmp 0xd905
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
jbe 0xd93b
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rsi
callq 0xdc20
movq -0x30(%rbp), %rax
movq (%rax), %rcx
subq $0x1, %rcx
movq %rcx, (%rax)
jmp 0xd905
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)