// File: /workspace/github_c_makefile_star2_O1/Tarsnap[P]kivaloo/kvlds/btree_balance.c
// Function: planmergenode
static int
planmergenode(struct balance_cookie * B, struct node * N)
{
	size_t maxplen = (B->T->pagelen * 2) / 3;
	size_t i;
	size_t plen;
	int gotdirty;
	int leafchild;
	int merging;

	/* 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;

	/* Plan merges under our children first. */
	for (i = 0; i <= N->nkeys; i++) {
		if (planmergenode(B, N->v.children[i]))
			goto err0;
	}

	/* Scan this node to see if we can merge any of its children. */
	/*
	 * Child number N->nkeys can't be merged into a higher-numbered child
	 * so we jump directly to nomerge.  This could be done using an
	 * "are we on the first pass through the loop" test inside the loop,
	 * but this confuses gcc.
	 */
	i = N->nkeys;
	goto nomerge;
	for (/* i = N->nkeys; */; i <= N->nkeys; i--) {
		/* Have we seen a dirty node yet? */
		if (N->v.children[i]->state == NODE_STATE_DIRTY) {
			gotdirty = 1;
			leafchild = (N->v.children[i]->type == NODE_TYPE_LEAF);
		}

		/*
		 * If we haven't seen a dirty node, we can't possibly need to
		 * merge this into the previous node.
		 */
		if (!gotdirty)
			goto nomerge;

		/*
		 * Figure out how large a node we'll produce if we merge this
		 * node into the next one.
		 */
		if (!leafchild)
			plen += kvldskey_serial_size(N->u.keys[i]);
		plen += serialize_merge_size(N->v.children[i]);

		/* Would the resulting node be too big? */
		if (plen > maxplen)
			goto nomerge;

		/* Mark this node as requiring merging. */
		N->v.children[i]->merging = 1;

		/* Move on to considering the next child. */
		continue;

nomerge:
		/*
		 * We're not merging this node into the next one.  Reset the
		 * state we're tracking so we can check if other nodes should
		 * be merged into this one.
		 */
		plen = serialize_size(N->v.children[i]);
		gotdirty = (N->v.children[i]->state == NODE_STATE_DIRTY);
		leafchild = (N->v.children[i]->type == NODE_TYPE_LEAF);
	}

	/* Page in any nodes which are needed for merging. */
	for (merging = i = 0; i <= N->nkeys; i++) {
		/* Do we need this node? */
		if (merging || N->v.children[i]->merging) {
			/* Is it present? */
			if (node_present(N->v.children[i])) {
				/* Make sure it stays present. */
				btree_node_lock(B->T, N->v.children[i]);
			} else {
				/* Fetch it. */
				B->nmergefetch += 1;
				if (btree_node_fetch(B->T, N->v.children[i],
				    merge_fetch, B))
					goto err0;
			}
		}

		/* Are we in the middle of a merging range? */
		merging = N->v.children[i]->merging;
	}

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

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq 0x10(%rdi), %rax
movq (%rax), %rax
addq %rax, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
movb 0x1c(%rsi), %al
andb $0xf, %al
cmpb $0x8, %al
jne 0x69f7
movq %rsi, %rbx
movq %rdi, %r14
movq %rdx, %r15
shrq %r15
xorl %r12d, %r12d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0x48(%rbx), %rax
movq (%rax,%r12,8), %rsi
movq %r14, %rdi
callq 0x68e1
testl %eax, %eax
jne 0x69f9
incq %r12
movq 0x38(%rbx), %r13
cmpq %r13, %r12
jbe 0x6924
movq 0x48(%rbx), %rax
movq (%rax,%r13,8), %rdi
callq 0xa703
leaq -0x1(%r13), %r12
cmpq 0x38(%rbx), %r12
jbe 0x6a0a
xorl %eax, %eax
leaq 0x328(%rip), %r15       # 0x6c94
xorl %r12d, %r12d
testl %eax, %eax
jne 0x6981
movq 0x48(%rbx), %rax
movq (%rax,%r12,8), %rax
testb $0x20, 0x1c(%rax)
je 0x69d9
movq 0x48(%rbx), %rax
movq (%rax,%r12,8), %rsi
testb $0x2, 0x1c(%rsi)
jne 0x69ba
testq %rsi, %rsi
je 0x69d9
movq 0x10(%r14), %rax
movq 0x30(%rax), %rdi
movq 0x20(%rdi), %rax
movq (%rsi,%rax), %rax
movq (%rax), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rax)
testq %rcx, %rcx
jne 0x69d9
callq 0x107a3
jmp 0x69d9
incq 0x18(%r14)
movq 0x10(%r14), %rdi
movq 0x48(%rbx), %rax
movq (%rax,%r12,8), %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x8c20
testl %eax, %eax
jne 0x69f9
movq 0x48(%rbx), %rax
movq (%rax,%r12,8), %rax
movl 0x1c(%rax), %eax
shrl $0x5, %eax
andl $0x1, %eax
incq %r12
cmpq 0x38(%rbx), %r12
jbe 0x696f
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
shlq $0x3, %r13
addq 0x48(%rbx), %r13
movq %r15, (%rsp)
movq (%r13), %rcx
movb 0x1c(%rcx), %dl
movl %edx, %ecx
andb $0xc, %cl
cmpb $0x8, %cl
setne %cl
andb $0x3, %dl
xorl %r13d, %r13d
cmpb $0x1, %dl
sete %r13b
movl %r13d, %edx
movq 0x48(%rbx), %rsi
movq (%rsi,%r12,8), %rdi
movb 0x1c(%rdi), %sil
movl %esi, %r8d
andb $0xc, %r8b
andb $0x3, %sil
xorl %r13d, %r13d
cmpb $0x1, %sil
sete %r13b
cmpb $0x8, %r8b
cmovnel %edx, %r13d
je 0x6a69
testb $0x1, %cl
jne 0x6aac
testl %r13d, %r13d
jne 0x6a7f
movq 0x40(%rbx), %rcx
movq (%rcx,%r12,8), %rcx
movzbl (%rcx), %ecx
addq %rcx, %rax
incq %rax
movq %rax, %r15
callq 0xac61
addq %r15, %rax
cmpq (%rsp), %rax
ja 0x6aac
movq 0x48(%rbx), %rcx
movq (%rcx,%r12,8), %rcx
orb $0x20, 0x1c(%rcx)
decq %r12
xorl %ecx, %ecx
cmpq 0x38(%rbx), %r12
jbe 0x6a35
jmp 0x6963
movq 0x48(%rbx), %rax
leaq (,%r12,8), %r13
movq (%rax,%r12,8), %rdi
callq 0xa703
addq 0x48(%rbx), %r13
decq %r12
cmpq 0x38(%rbx), %r12
jbe 0x6a16
jmp 0x6963