// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq $0x0, 0x18(%rax)
cmpl $0x0, -0x14(%rbp)
je 0x4bfe
jmp 0x4e6e
jmp 0x4c00
callq 0x19250
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0x4c14
jmp 0x4ea2
movq -0x20(%rbp), %rax
cmpq $0x1000, 0x20(%rax)     # imm = 0x1000
jne 0x4c27
jmp 0x4e2a
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x19280
cmpl $0x0, %eax
je 0x4c42
jmp 0x4e65
movq -0x28(%rbp), %rax
cmpl $-0x1, 0x8(%rax)
jne 0x4c51
jmp 0x4e2a
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x20(%rax)
callq 0x4ec0
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x4c75
jmp 0x4e89
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
addl $0xffffff00, %eax       # imm = 0xFFFFFF00
movl %eax, %ecx
movq %rcx, -0x38(%rbp)
subl $0x31, %eax
ja 0x4df8
movq -0x38(%rbp), %rax
leaq 0x199c6(%rip), %rcx     # 0x1e678
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rax
movl %eax, %edx
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rax
movl %eax, %ecx
callq 0x19860
cmpl $0x0, %eax
je 0x4cf0
jmp 0x4e80
movq -0x30(%rbp), %rdi
callq 0x4840
movq -0x28(%rbp), %rdi
callq 0x19810
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rcx
subq $0x1, %rcx
movq %rcx, 0x20(%rax)
jmp 0x4e1c
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movzbl (%rax), %eax
movq -0x20(%rbp), %rcx
cmpq 0x30(%rcx), %rax
ja 0x4d41
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movzbl (%rax), %eax
movq -0x20(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jbe 0x4d46
jmp 0x4e4c
jmp 0x4d48
movq -0x20(%rbp), %rax
cmpq $0x0, 0x60(%rax)
jne 0x4d61
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x60(%rax)
jmp 0x4d70
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rcx
addq $0x8, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x20(%rbp), %rax
movq 0x88(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x88(%rax)
movq -0x20(%rbp), %rdi
callq 0x4440
cmpl $0x0, %eax
je 0x4da9
jmp 0x4ea2
jmp 0x4e1c
movq -0x20(%rbp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x4dc4
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x40(%rax)
jmp 0x4dd3
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rcx
addq $0x8, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x20(%rbp), %rdi
callq 0x4ee0
cmpl $0x0, %eax
je 0x4df6
jmp 0x4ea2
jmp 0x4e1c
jmp 0x4dfa
movq -0x28(%rbp), %rax
movl 0x8(%rax), %esi
leaq 0x19af6(%rip), %rdi     # 0x1e8fe
movb $0x0, %al
callq 0x182f0
callq 0x20a0
movl $0x0, (%rax)
jmp 0x4e4c
jmp 0x4e1e
movb $0x1, %al
testb $0x1, %al
jne 0x4c00
jmp 0x4e2a
movq -0x28(%rbp), %rdi
callq 0x19810
movq -0x20(%rbp), %rdi
callq 0x4b10
cmpl $0x0, %eax
je 0x4e43
jmp 0x4ea2
movl $0x0, -0x4(%rbp)
jmp 0x4ea9
movq -0x30(%rbp), %rdi
callq 0x4840
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rcx
subq $0x1, %rcx
movq %rcx, 0x20(%rax)
movq -0x28(%rbp), %rdi
callq 0x19810
movq -0x20(%rbp), %rdi
callq 0x5010
movl $0x0, -0x4(%rbp)
jmp 0x4ea9
movq -0x30(%rbp), %rdi
callq 0x4840
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rcx
subq $0x1, %rcx
movq %rcx, 0x20(%rax)
movq -0x28(%rbp), %rdi
callq 0x19810
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)