// File: /workspace/github_c_makefile_star2_O2/Tarsnap[P]kivaloo/kvlds/dispatch_nmr.c
// Function: callback_range_gotnode
static int
callback_range_gotnode(void * cookie, struct node * N,
    struct kvldskey * end)
{
	struct nmr_cookie * C = cookie;
	size_t start;
	size_t stop;
	size_t i;

	/* Record the end-of-range key. */
	C->end = end;

	/* Record how far all keys in the range must match. */
	C->mlen = N->mlen_t;

	/* Create a heap for holding key-value pairs. */
	if ((C->H = ptrheap_init(kvpair_cmp, NULL, (void *)&C->mlen)) == NULL)
		goto err1;

	/* We don't have any key-value pairs yet. */
	C->nkeys = 0;
	C->rlen = 0;

	/* Leaves and parents get handled differently. */
	switch (N->height) {
	case 0:
		/* Descend into a single leaf. */
		C->leavesleft = 1;
		if (btree_node_descend(C->T, N, callback_range_gotleaf, C))
			goto err2;
		break;
	case 1:
		/* Not descending into any leaves yet. */
		C->leavesleft = 0;

		/* Figure out which leaf to start with. */
		start = btree_find_child(N, C->R->range_start);

		/* Figure out the maximum number of leaves to process. */
		stop = start + C->R->range_max / C->T->pagelen;
		if (stop == start)
			stop = start + 1;

		/* Process leaf nodes. */
		for (i = start; (i <= N->nkeys) && (i < stop); i++) {
			/* Do this leaf. */
			C->leavesleft += 1;
			if (btree_node_descend(C->T, N->v.children[i],
			    callback_range_gotleaf, C))
				goto err0;

			/* Stop if we've gone too far. */
			if ((i < N->nkeys) && (kvldskey_cmp(C->R->range_end,
			    N->u.keys[i]) < 0)) {
				i++;
				break;
			}
		}

		/* Adjust our end pointer if we didn't do all the leaves. */
		if (i < N->nkeys + 1) {
			kvldskey_free(C->end);
			if ((C->end = kvldskey_dup(N->u.keys[i - 1])) == NULL)
				goto err0;
		}
		break;
	}

	/* Release the lock picked up by btree_find_range. */
	btree_node_unlock(C->T, N);

	/* Success! */
	return (0);

err2:
	ptrheap_free(C->H);
err1:
	kvldskey_free(C->end);
	proto_kvlds_request_free(C->R);
	free(C);
err0:
	/* Failure! */
	return (-1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
movq %rdx, 0x30(%rdi)
movzbl 0x1e(%rsi), %eax
leaq 0x38(%rdi), %rdx
movq %rax, 0x38(%rdi)
leaq 0x9e0e(%rip), %rdi      # 0xe618
xorl %esi, %esi
callq 0x96cc
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x4862
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rbx)
movzbl 0x1d(%r14), %eax
cmpl $0x1, %eax
je 0x4884
testl %eax, %eax
jne 0x4950
movq $0x1, 0x50(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x16f(%rip), %rdx       # 0x49b5
movq %r14, %rsi
movq %rbx, %rcx
callq 0x7e08
testl %eax, %eax
je 0x4950
movq 0x28(%rbx), %rdi
callq 0x9bdc
movq 0x30(%rbx), %rdi
callq 0x2040
movq 0x18(%rbx), %rdi
callq 0xeb7a
movq %rbx, %rdi
callq 0x2040
pushq $-0x1
popq %rbp
jmp 0x495e
andq $0x0, 0x50(%rbx)
movq 0x18(%rbx), %rax
movq 0x10(%rax), %rsi
movq %r14, %rdi
callq 0x6e00
movq %rax, %r15
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
movl 0xc(%rcx), %ecx
movq (%rax), %rsi
movq %rcx, %rax
xorl %edx, %edx
divq %rsi
cmpq %rcx, %rsi
pushq $0x1
popq %r13
cmovbeq %rax, %r13
addq %r15, %r13
leaq 0xee(%rip), %r12        # 0x49b5
pushq $-0x1
popq %rbp
movq 0x38(%r14), %rax
cmpq %rax, %r15
ja 0x4922
cmpq %r13, %r15
jae 0x4922
incq 0x50(%rbx)
movq 0x10(%rbx), %rdi
movq 0x48(%r14), %rax
movq (%rax,%r15,8), %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x7e08
testl %eax, %eax
jne 0x495e
cmpq 0x38(%r14), %r15
jae 0x4916
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rdi
movq 0x40(%r14), %rax
movq (%rax,%r15,8), %rsi
callq 0xe315
testl %eax, %eax
js 0x491b
incq %r15
jmp 0x48ca
incq %r15
movq 0x38(%r14), %rax
incq %rax
cmpq %rax, %r15
jae 0x4950
movq 0x30(%rbx), %rdi
callq 0x2040
movq 0x40(%r14), %rax
movq -0x8(%rax,%r15,8), %rdi
movzbl (%rdi), %esi
incq %rdi
callq 0xe268
movq %rax, 0x30(%rbx)
testq %rax, %rax
je 0x495e
movq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x496f
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq