// File: /workspace/github_c_makefile_star2_O3/Tarsnap[P]kivaloo/kvlds/dispatch.c
// Function: gotrequest
static int
gotrequest(void * cookie, int status)
{
	struct dispatch_state * D = cookie;
	struct proto_kvlds_request * R;
	struct requestq * RQ;

	/* We're no longer waiting for a packet to arrive. */
	D->read_cookie = NULL;

	/* If the wait failed, the connection is dead. */
	if (status)
		goto drop;

	/*
	 * Read packets until there are no more to read, we hit MAXREQS, or
	 * an error occurs.
	 */
	do {
		/* Allocate space for a request. */
		if ((R = proto_kvlds_request_alloc()) == NULL)
			goto err0;

		/* If we have MAXREQS requests, stop looping. */
		if (D->nrequests == MAXREQS)
			break;

		/* Attempt to read a request. */
		if (proto_kvlds_request_read(D->readq, R))
			goto drop1;

		/* If we have no request, stop looping. */
		if (R->type == PROTO_KVLDS_NONE)
			break;

		/* We owe a response to the client. */
		D->nrequests += 1;

		/* Construct a linked list node. */
		if ((RQ = mpool_requestq_malloc()) == NULL)
			goto err1;
		RQ->R = R;
		RQ->next = NULL;

		/* Add to the modifying or non-modifying queue, and poke it. */
		switch (R->type) {
		case PROTO_KVLDS_PARAMS:
			/* Send the response immediately. */
			if (proto_kvlds_response_params(D->writeq, RQ->R->ID,
			    (uint32_t)D->kmax, (uint32_t)D->vmax))
				goto err2;

			/* Free the linked list node. */
			mpool_requestq_free(RQ);

			/* Free the request packet. */
			proto_kvlds_request_free(R);

			/* This request has been handled. */
			D->nrequests -= 1;
			break;
		case PROTO_KVLDS_CAS:
		case PROTO_KVLDS_SET:
		case PROTO_KVLDS_ADD:
		case PROTO_KVLDS_MODIFY:
			/*
			 * We can't add or modify a key-value pair if the key
			 * is too long or the value we're setting is too long.
			 */
			if ((R->key->len > D->kmax) ||
			    (R->value->len > D->vmax))
				goto drop2;

			/* FALLTHROUGH */

		case PROTO_KVLDS_DELETE:
		case PROTO_KVLDS_CAD:
			/* Add to modifying request queue. */
			if (D->mr_head == NULL)
				D->mr_head = RQ;
			else
				*(D->mr_tail) = RQ;
			D->mr_tail = &RQ->next;

			/* The MR queue has gained an element. */
			D->mr_qlen += 1;

			/* Poke the queue. */
			if (poke_mr(D))
				goto err0;
			break;
		case PROTO_KVLDS_GET:
		case PROTO_KVLDS_RANGE:
			/* Add to non-modifying request queue. */
			if (D->nmr_head == NULL)
				D->nmr_head = RQ;
			else
				*(D->nmr_tail) = RQ;
			D->nmr_tail = &RQ->next;

			/* Poke the queue. */
			if (poke_nmr(D))
				goto err0;
			break;
		default:
			/* Don't recognize this packet... */
			warn0("Received unrecognized packet type: 0x%08" PRIx32,
			    R->type);
			goto drop2;
		}
	} while (1);

	/* Free the (unused) request structure. */
	proto_kvlds_request_free(R);

	/* Wait for more requests to arrive. */
	if (readreqs(D))
		goto err0;

	/* Success! */
	return (0);

drop2:
	mpool_requestq_free(RQ);
	D->nrequests -= 1;
drop1:
	proto_kvlds_request_free(R);
drop:
	/* We didn't get a valid request.  Drop the connection. */
	dropconnection(D);

	/* All is good. */
	return (0);

err2:
	mpool_requestq_free(RQ);
err1:
	D->nrequests -= 1;
	proto_kvlds_request_free(R);
err0:
	/* Failure! */
	return (-1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq $0x0, 0x18(%rdi)
testl %esi, %esi
je 0x41b8
movq %rbx, %rdi
callq 0x4666
xorl %eax, %eax
jmp 0x4588
callq 0x10824
testq %rax, %rax
je 0x4583
movq %rax, %r14
leaq 0x40(%rbx), %r12
leaq 0x60(%rbx), %r13
movl $0xf, %ebp
cmpq $0x1000, 0x20(%rbx)     # imm = 0x1000
je 0x445a
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x10884
testl %eax, %eax
jne 0x4562
cmpl $-0x1, 0x8(%r14)
je 0x445a
incq 0x20(%rbx)
incq 0x1902a(%rip)           # 0x1d238
movq 0x1900b(%rip), %rax     # 0x1d220
testq %rax, %rax
je 0x4233
movq 0x1900f(%rip), %rcx     # 0x1d230
leaq -0x1(%rax), %rdx
movq %rdx, 0x18ff4(%rip)     # 0x1d220
movq -0x8(%rcx,%rax,8), %r15
jmp 0x4266
incq 0x19006(%rip)           # 0x1d240
cmpl $0x0, 0x19007(%rip)     # 0x1d248
jne 0x4259
movq 0x1900e(%rip), %rdi     # 0x1d258
callq 0x13090
movl $0x1, 0x18fef(%rip)     # 0x1d248
movl $0x20, %edi
callq 0x22a0
movq %rax, %r15
testq %r15, %r15
je 0x4577
movq %r14, (%r15)
movq $0x0, 0x8(%r15)
movl 0x8(%r14), %esi
leal -0x110(%rsi), %ecx
cmpl $0x21, %ecx
ja 0x4339
leaq 0x8(%r15), %rax
btq %rcx, %rbp
jb 0x42d5
movl $0x30000, %edx          # imm = 0x30000
btq %rcx, %rdx
jb 0x42f7
movabsq $0x300000000, %rdx      # imm = 0x300000000
btq %rcx, %rdx
jae 0x4339
cmpq $0x0, (%r12)
movq %r12, %rcx
je 0x42c4
movq 0x48(%rbx), %rcx
movq %r15, (%rcx)
movq %rax, 0x48(%rbx)
movq %rbx, %rdi
callq 0x45b6
jmp 0x431b
movq 0x10(%r14), %rcx
movzbl (%rcx), %ecx
cmpq %rcx, 0x30(%rbx)
jb 0x4490
movq 0x18(%r14), %rcx
movzbl (%rcx), %ecx
cmpq %rcx, 0x38(%rbx)
jb 0x4490
cmpq $0x0, (%r13)
movq %r13, %rcx
je 0x4305
movq 0x68(%rbx), %rcx
movq %r15, (%rcx)
movq %rax, 0x68(%rbx)
incq 0x88(%rbx)
movq %rbx, %rdi
callq 0x3bd8
testl %eax, %eax
jne 0x4583
callq 0x10824
movq %rax, %r14
testq %rax, %rax
jne 0x41d6
jmp 0x4583
cmpl $0x100, %esi            # imm = 0x100
jne 0x4477
movq 0x10(%rbx), %rdi
movq (%r14), %rsi
movl 0x30(%rbx), %edx
movl 0x38(%rbx), %ecx
callq 0x10c7f
testl %eax, %eax
jne 0x456f
movq %r12, (%rsp)
movq 0x18eb6(%rip), %r12     # 0x1d220
movq 0x18eb7(%rip), %rbp     # 0x1d228
cmpq %rbp, %r12
jae 0x4392
movq 0x18eb3(%rip), %rax     # 0x1d230
leaq 0x1(%r12), %rcx
movq %rcx, 0x18e97(%rip)     # 0x1d220
movq %r15, (%rax,%r12,8)
jmp 0x4440
movq 0x18e9f(%rip), %rax     # 0x1d238
shrq $0x8, %rax
cmpq %rax, 0x18e9c(%rip)     # 0x1d240
jbe 0x442e
testq %rbp, %rbp
je 0x4597
movq %rbp, %rdi
shlq $0x4, %rdi
callq 0x22a0
testq %rax, %rax
je 0x442e
movq 0x18e65(%rip), %rsi     # 0x1d230
movq %rsi, 0x8(%rsp)
leaq (,%rbp,8), %rdx
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0x2280
movq 0x8(%rsp), %rdi
cmpq 0x18e5f(%rip), %rdi     # 0x1d250
je 0x4406
callq 0x2040
movq 0x18e29(%rip), %rbp     # 0x1d228
movq 0x18e1a(%rip), %r12     # 0x1d220
movq 0x10(%rsp), %rcx
movq %rcx, 0x18e1e(%rip)     # 0x1d230
addq %rbp, %rbp
movq %rbp, 0x18e0c(%rip)     # 0x1d228
leaq 0x1(%r12), %rax
movq %rax, 0x18df8(%rip)     # 0x1d220
movq %r15, (%rcx,%r12,8)
jmp 0x4436
movq %r15, %rdi
callq 0x2040
xorps %xmm0, %xmm0
movups %xmm0, 0x18df8(%rip)    # 0x1d238
movq (%rsp), %r12
movl $0xf, %ebp
movq %r14, %rdi
callq 0x10b73
decq 0x20(%rbx)
jmp 0x4323
movq %r14, %rdi
callq 0x10b73
movq %rbx, %rdi
callq 0x410a
testl %eax, %eax
jne 0x4583
jmp 0x41b1
leaq 0x100d8(%rip), %rdi     # 0x14556
xorl %eax, %eax
callq 0x100ef
callq 0x20a0
movl $0x0, (%rax)
movq 0x18d89(%rip), %rbp     # 0x1d220
movq 0x18d8a(%rip), %r13     # 0x1d228
cmpq %r13, %rbp
jae 0x44be
movq 0x18d86(%rip), %rax     # 0x1d230
leaq 0x1(%rbp), %rcx
movq %rcx, 0x18d6b(%rip)     # 0x1d220
movq %r15, (%rax,%rbp,8)
jmp 0x455e
movq 0x18d73(%rip), %rax     # 0x1d238
shrq $0x8, %rax
cmpq %rax, 0x18d70(%rip)     # 0x1d240
jbe 0x454c
testq %r13, %r13
je 0x4597
movq %r13, %rdi
shlq $0x4, %rdi
callq 0x22a0
testq %rax, %rax
je 0x454c
movq %rax, %r12
movq 0x18d3a(%rip), %rsi     # 0x1d230
movq %rsi, (%rsp)
leaq (,%r13,8), %rdx
movq %rax, %rdi
callq 0x2280
movq (%rsp), %rdi
cmpq 0x18d3b(%rip), %rdi     # 0x1d250
je 0x452a
callq 0x2040
movq 0x18d05(%rip), %r13     # 0x1d228
movq 0x18cf6(%rip), %rbp     # 0x1d220
movq %r12, 0x18cff(%rip)     # 0x1d230
addq %r13, %r13
movq %r13, 0x18ced(%rip)     # 0x1d228
leaq 0x1(%rbp), %rax
movq %rax, 0x18cda(%rip)     # 0x1d220
movq %r15, (%r12,%rbp,8)
jmp 0x4554
movq %r15, %rdi
callq 0x2040
xorps %xmm0, %xmm0
movups %xmm0, 0x18cda(%rip)    # 0x1d238
decq 0x20(%rbx)
movq %r14, %rdi
callq 0x10b73
jmp 0x41a9
movq %r15, %rdi
callq 0x3f61
decq 0x20(%rbx)
movq %r14, %rdi
callq 0x10b73
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xff37(%rip), %rdi      # 0x144d5
leaq 0xff41(%rip), %rsi      # 0x144e6
leaq 0xff5c(%rip), %rcx      # 0x14508
movl $0x5a, %edx
callq 0x21c0