// File: /workspace/github_c_makefile_star2_O3/Tarsnap[P]kivaloo/lbs-dynamodb/dispatch.c
// Function: gotrequest
static int
gotrequest(void * cookie, int status)
{
	struct dispatch_state * D = cookie;
	struct proto_lbs_request * R;
	uint32_t blklen;
	uint64_t lastblk;
	uint64_t nextblk;

	/* 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 or an error occurs. */
	do {
		/* Allocate space for a request. */
		if ((R = malloc(sizeof(struct proto_lbs_request))) == NULL)
			goto err0;

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

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

		/* Handle the request. */
		switch (R->type) {
		case PROTO_LBS_PARAMS:
			warn0("PROTO_LBS_PARAMS is not implemented");
			warn0("Update to a newer version of kvlds");
			goto drop1;
		case PROTO_LBS_PARAMS2:
			if (D->appendip != 0)
				goto drop1;
			state_params(D->S, &blklen, &lastblk, &nextblk);
			if (proto_lbs_response_params2(D->writeq, R->ID,
			    blklen, nextblk, lastblk))
				goto err1;
			free(R);
			break;
		case PROTO_LBS_GET:
			D->npending += 1;
			if (state_get(D->S, R, callback_get, D))
				goto err1;
			break;
		case PROTO_LBS_APPEND:
			state_params(D->S, &blklen, &lastblk, &nextblk);
			if (R->r.append.blklen != blklen)
				goto drop2;
			if ((R->r.append.blkno != nextblk) ||
			    (D->appendip != 0)) {
				if (proto_lbs_response_append(D->writeq,
				    R->ID, 1, 0))
					goto err2;
				free(R->r.append.buf);
				free(R);
				break;
			}
			D->npending += 1;
			D->appendip = 1;
			if (state_append(D->S, R, callback_append, D))
				goto err1;
			break;
		case PROTO_LBS_FREE:
			if (deleteto_deleteto(D->D, R->r.free.blkno))
				goto err1;
			if (proto_lbs_response_free(D->writeq, R->ID))
				goto err1;
			free(R);
			break;
		default:
			/* proto_lbs_request_read broke. */
			assert(0);
		}
	} while (1);

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

	/* Wait for more requests to arrive. */
	if ((D->read_cookie = wire_readpacket_wait(D->readq,
	    gotrequest, D)) == NULL) {
		warnp("Error reading request from connection");
		goto err0;
	}

	/* Success! */
	return (0);

drop2:
	free(R->r.append.buf);
drop1:
	free(R);
drop:
	/* We didn't get a valid request.  Drop the connection. */
	dropconnection(D);

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

err2:
	free(R->r.append.buf);
err1:
	free(R);
err0:
	/* Failure! */
	return (-1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq $0x0, 0x28(%rdi)
testl %esi, %esi
je 0x371b
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %r14
movl $0x28, %edi
callq 0x2270
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x370a
movq %rax, %r15
leaq 0x9b90(%rip), %rbp      # 0xd2cc
leaq 0x4(%rsp), %r13
leaq 0x10(%rsp), %r12
leaq 0x8(%rsp), %rbx
movq 0x20(%r14), %rdi
movq %r15, %rsi
callq 0xa1d8
testl %eax, %eax
jne 0x3947
movl 0x8(%r15), %eax
incl %eax
cmpl $0x5, %eax
ja 0x3983
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
incq 0x30(%r14)
movq (%r14), %rdi
movq %r15, %rsi
leaq 0x219(%rip), %rdx       # 0x39a2
movq %r14, %rcx
callq 0x3c2e
testl %eax, %eax
je 0x3876
jmp 0x38b4
cmpl $0x0, 0x38(%r14)
jne 0x3947
movq (%r14), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x3c1b
movq 0x18(%r14), %rdi
movq (%r15), %rsi
movl 0x4(%rsp), %edx
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %r8
callq 0xa38c
jmp 0x37f7
movq 0x8(%r14), %rdi
movq 0x10(%r15), %rsi
callq 0x4192
testl %eax, %eax
jne 0x38b4
movq 0x18(%r14), %rdi
movq (%r15), %rsi
callq 0xa59f
testl %eax, %eax
je 0x386e
jmp 0x38b4
movq (%r14), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x3c1b
movl 0x14(%r15), %eax
cmpl 0x4(%rsp), %eax
jne 0x393e
movq 0x18(%r15), %rax
cmpq 0x8(%rsp), %rax
jne 0x3831
cmpl $0x0, 0x38(%r14)
je 0x388e
movq 0x18(%r14), %rdi
movq (%r15), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0xa512
movq %rbx, %rbp
movq %r12, %rbx
movq %r13, %r12
movl %eax, %r13d
movq 0x20(%r15), %rdi
callq 0x2040
testl %r13d, %r13d
movq %r12, %r13
movq %rbx, %r12
movq %rbp, %rbx
leaq 0x9a60(%rip), %rbp      # 0xd2cc
jne 0x38b4
movq %r15, %rdi
callq 0x2040
movl $0x28, %edi
callq 0x2270
movq %rax, %r15
testq %rax, %rax
jne 0x374b
jmp 0x38bc
incq 0x30(%r14)
movl $0x1, 0x38(%r14)
movq (%r14), %rdi
movq %r15, %rsi
leaq 0x159(%rip), %rdx       # 0x3a00
movq %r14, %rcx
callq 0x3db7
jmp 0x3791
movq %r15, %rdi
callq 0x2040
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x370a
movq %r15, %rdi
callq 0x2040
movq 0x20(%r14), %rdi
leaq -0x1eb(%rip), %rsi      # 0x36ee
movq %r14, %rdx
callq 0xa706
movq %rax, 0x28(%r14)
testq %rax, %rax
jne 0x3708
callq 0x20a0
cmpl $0x0, (%rax)
je 0x3970
movq %rax, %r14
leaq 0x9ad2(%rip), %rdi      # 0xd3d4
xorl %eax, %eax
callq 0x95b0
movl $0x0, (%r14)
jmp 0x38bc
leaq 0x9ae1(%rip), %rdi      # 0xd3fa
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x96cf
callq 0x20a0
movq %rax, %rbx
movl %ebp, (%rax)
leaq 0x9aeb(%rip), %rdi      # 0xd41e
xorl %eax, %eax
callq 0x96cf
movl %ebp, (%rbx)
jmp 0x3947
movq 0x20(%r15), %rdi
callq 0x2040
movq %r15, %rdi
callq 0x2040
movq 0x28(%r14), %rdi
xorl %ebx, %ebx
testq %rdi, %rdi
je 0x370a
callq 0xa87e
movq $0x0, 0x28(%r14)
jmp 0x370a
leaq 0x9a5d(%rip), %rdi      # 0xd3d4
xorl %eax, %eax
callq 0x96cf
jmp 0x38bc
leaq 0xa6a9(%rip), %rdi      # 0xe033
leaq 0x9965(%rip), %rsi      # 0xd2f6
leaq 0x9aa9(%rip), %rcx      # 0xd441
movl $0x90, %edx
callq 0x21a0