// File: /workspace/github_c_makefile_star2_O1/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 0x4203
movq %rbx, %rdi
callq 0x46aa
xorl %eax, %eax
jmp 0x45b6
callq 0x108c0
testq %rax, %rax
je 0x45b1
movq %rax, %r14
leaq 0x40(%rbx), %rbp
leaq 0x60(%rbx), %r13
movl $0xf, %r12d
cmpq $0x1000, 0x20(%rbx)     # imm = 0x1000
je 0x4494
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x10920
testl %eax, %eax
jne 0x4590
cmpl $-0x1, 0x8(%r14)
je 0x4494
incq 0x20(%rbx)
incq 0x18fde(%rip)           # 0x1d238
movq 0x18fbf(%rip), %rax     # 0x1d220
testq %rax, %rax
je 0x427f
movq 0x18fc3(%rip), %rcx     # 0x1d230
leaq -0x1(%rax), %rdx
movq %rdx, 0x18fa8(%rip)     # 0x1d220
movq -0x8(%rcx,%rax,8), %r15
jmp 0x42b2
incq 0x18fba(%rip)           # 0x1d240
cmpl $0x0, 0x18fbb(%rip)     # 0x1d248
jne 0x42a5
movq 0x18fc2(%rip), %rdi     # 0x1d258
callq 0x130a0
movl $0x1, 0x18fa3(%rip)     # 0x1d248
movl $0x20, %edi
callq 0x22a0
movq %rax, %r15
testq %r15, %r15
je 0x45a5
movq %r14, (%r15)
movq $0x0, 0x8(%r15)
movl 0x8(%r14), %esi
leal -0x110(%rsi), %ecx
cmpl $0x21, %ecx
ja 0x4385
leaq 0x8(%r15), %rax
btq %rcx, %r12
jb 0x4321
movl $0x30000, %edx          # imm = 0x30000
btq %rcx, %rdx
jb 0x4343
movabsq $0x300000000, %rdx      # imm = 0x300000000
btq %rcx, %rdx
jae 0x4385
cmpq $0x0, (%rbp)
movq %rbp, %rcx
je 0x4310
movq 0x48(%rbx), %rcx
movq %r15, (%rcx)
movq %rax, 0x48(%rbx)
movq %rbx, %rdi
callq 0x45e4
jmp 0x4367
movq 0x10(%r14), %rcx
movzbl (%rcx), %ecx
cmpq %rcx, 0x30(%rbx)
jb 0x44ca
movq 0x18(%r14), %rcx
movzbl (%rcx), %ecx
cmpq %rcx, 0x38(%rbx)
jb 0x44ca
cmpq $0x0, (%r13)
movq %r13, %rcx
je 0x4351
movq 0x68(%rbx), %rcx
movq %r15, (%rcx)
movq %rax, 0x68(%rbx)
incq 0x88(%rbx)
movq %rbx, %rdi
callq 0x3c2d
testl %eax, %eax
jne 0x45b1
callq 0x108c0
movq %rax, %r14
testq %rax, %rax
jne 0x4222
jmp 0x45b1
cmpl $0x100, %esi            # imm = 0x100
jne 0x44b1
movq 0x10(%rbx), %rdi
movq (%r14), %rsi
movl 0x30(%rbx), %edx
movl 0x38(%rbx), %ecx
callq 0x10cd2
testl %eax, %eax
jne 0x459d
movq 0x18e6e(%rip), %rax     # 0x1d220
movq 0x18e6f(%rip), %r12     # 0x1d228
cmpq %r12, %rax
jae 0x43df
movq 0x18e6b(%rip), %rcx     # 0x1d230
leaq 0x1(%rax), %rdx
movq %rdx, 0x18e50(%rip)     # 0x1d220
movq %r15, (%rcx,%rax,8)
movl $0xf, %r12d
jmp 0x4483
movq 0x18e52(%rip), %rax     # 0x1d238
shrq $0x8, %rax
cmpq %rax, 0x18e4f(%rip)     # 0x1d240
jbe 0x446b
testq %r12, %r12
je 0x45c5
movq %r12, %rdi
shlq $0x4, %rdi
callq 0x22a0
testq %rax, %rax
je 0x446b
movq 0x18e1c(%rip), %rsi     # 0x1d230
movq %rsi, 0x8(%rsp)
shlq $0x3, %r12
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movq %r12, %rdx
callq 0x2280
movq 0x8(%rsp), %rdi
cmpq 0x18e17(%rip), %rdi     # 0x1d250
je 0x4440
callq 0x2040
movq 0x10(%rsp), %rdx
movq %rdx, 0x18de4(%rip)     # 0x1d230
shlq 0x18dd5(%rip)           # 0x1d228
movq 0x18dc6(%rip), %rax     # 0x1d220
leaq 0x1(%rax), %rcx
movq %rcx, 0x18dbb(%rip)     # 0x1d220
movq %r15, (%rdx,%rax,8)
jmp 0x4473
movq %r15, %rdi
callq 0x2040
movl $0xf, %r12d
xorps %xmm0, %xmm0
movups %xmm0, 0x18db5(%rip)    # 0x1d238
movq %r14, %rdi
callq 0x10bd2
decq 0x20(%rbx)
jmp 0x436f
movq %r14, %rdi
callq 0x10bd2
movq %rbx, %rdi
callq 0x4155
testl %eax, %eax
jne 0x45b1
jmp 0x41fc
leaq 0x100fe(%rip), %rdi     # 0x145b6
xorl %eax, %eax
callq 0x10167
callq 0x20a0
movl $0x0, (%rax)
movq 0x18d4f(%rip), %rax     # 0x1d220
movq 0x18d50(%rip), %r12     # 0x1d228
cmpq %r12, %rax
jae 0x44f8
movq 0x18d4c(%rip), %rcx     # 0x1d230
leaq 0x1(%rax), %rdx
movq %rdx, 0x18d31(%rip)     # 0x1d220
movq %r15, (%rcx,%rax,8)
jmp 0x458c
movq 0x18d39(%rip), %rax     # 0x1d238
shrq $0x8, %rax
cmpq %rax, 0x18d36(%rip)     # 0x1d240
jbe 0x457a
testq %r12, %r12
je 0x45c5
movq %r12, %rdi
shlq $0x4, %rdi
callq 0x22a0
testq %rax, %rax
je 0x457a
movq %rax, %r13
movq 0x18d00(%rip), %rbp     # 0x1d230
shlq $0x3, %r12
movq %rax, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x2280
cmpq 0x18d07(%rip), %rbp     # 0x1d250
je 0x4553
movq %rbp, %rdi
callq 0x2040
movq %r13, 0x18cd6(%rip)     # 0x1d230
shlq 0x18cc7(%rip)           # 0x1d228
movq 0x18cb8(%rip), %rax     # 0x1d220
leaq 0x1(%rax), %rcx
movq %rcx, 0x18cad(%rip)     # 0x1d220
movq %r15, (%r13,%rax,8)
jmp 0x4582
movq %r15, %rdi
callq 0x2040
xorps %xmm0, %xmm0
movups %xmm0, 0x18cac(%rip)    # 0x1d238
decq 0x20(%rbx)
movq %r14, %rdi
callq 0x10bd2
jmp 0x41f4
movq %r15, %rdi
callq 0x3fb8
decq 0x20(%rbx)
movq %r14, %rdi
callq 0x10bd2
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xff69(%rip), %rdi      # 0x14535
leaq 0xff73(%rip), %rsi      # 0x14546
leaq 0xff8e(%rip), %rcx      # 0x14568
movl $0x5a, %edx
callq 0x21c0