// File: /workspace/github_c_makefile_star2_O1/Tarsnap[P]kivaloo/lbs-s3/dispatch.c
// Function: gotrequest
static int
gotrequest(void * cookie, int status)
{
	struct dispatch_state * D = cookie;
	struct proto_lbs_request * R;

	/* 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 in lbs-s3");
			warn0("Update to a newer version of kvlds");
			goto drop1;
		case PROTO_LBS_PARAMS2:
			if (proto_lbs_response_params2(D->writeq, R->ID,
			    D->S->blklen, D->S->nextblk, D->S->lastblk))
				goto err1;
			free(R);
			break;
		case PROTO_LBS_GET:
			D->npending += 1;
			if (s3state_get(D->S, R, callback_get, D))
				goto err1;
			break;
		case PROTO_LBS_APPEND:
			if (R->r.append.blklen != D->S->blklen)
				goto drop2;
			if ((R->r.append.blkno != D->S->nextblk) ||
			    (D->appendip != 0)) {
				if (proto_lbs_response_append(D->writeq,
				    R->ID, 1, 0))
					goto err2;
				break;
			}
			D->npending += 1;
			D->appendip = 1;
			if (s3state_append(D->S, R, callback_append, D))
				goto err1;
			break;
		case PROTO_LBS_FREE:
			if (s3state_gc(D->S, 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
pushq %rax
movq %rdi, %rbx
movq $0x0, 0x20(%rdi)
testl %esi, %esi
je 0x36e9
movq 0x20(%rbx), %rdi
xorl %ebp, %ebp
testq %rdi, %rdi
je 0x38d2
callq 0xad35
movq $0x0, 0x20(%rbx)
jmp 0x38d2
movl $0x28, %edi
callq 0x2270
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x38d2
movq %rax, %r14
leaq 0x9bf1(%rip), %r13      # 0xd2fc
leaq 0x200(%rip), %r15       # 0x3912
leaq 0x257(%rip), %r12       # 0x3970
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0x9abc
testl %eax, %eax
jne 0x38af
movl 0x8(%r14), %eax
incl %eax
cmpl $0x5, %eax
ja 0x38f3
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
incq 0x28(%rbx)
movq (%rbx), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x3b36
testl %eax, %eax
je 0x37ef
jmp 0x38c5
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
movq (%r14), %rsi
movl (%rax), %edx
movq 0x8(%rax), %rcx
movq 0x10(%rax), %r8
callq 0x9c74
jmp 0x37a3
movq (%rbx), %rdi
movq 0x10(%r14), %rsi
callq 0x3d36
testl %eax, %eax
jne 0x38c5
movq 0x10(%rbx), %rdi
movq (%r14), %rsi
callq 0x9e87
testl %eax, %eax
jne 0x38c5
movq %r14, %rdi
callq 0x2040
jmp 0x37ef
movl 0x14(%r14), %eax
movq (%rbx), %rdi
cmpl (%rdi), %eax
jne 0x38a6
movq 0x18(%r14), %rax
cmpq 0x8(%rdi), %rax
jne 0x37d4
cmpl $0x0, 0x30(%rbx)
je 0x380a
movq 0x10(%rbx), %rdi
movq (%r14), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x9dfa
testl %eax, %eax
jne 0x38bc
movl $0x28, %edi
callq 0x2270
movq %rax, %r14
testq %rax, %rax
jne 0x3719
jmp 0x38d2
incq 0x28(%rbx)
movl $0x1, 0x30(%rbx)
movq %r14, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x3c1c
jmp 0x375b
movq %r14, %rdi
callq 0x2040
movq 0x18(%rbx), %rdi
leaq -0x18d(%rip), %rsi      # 0x36ae
movq %rbx, %rdx
callq 0xabbd
movq %rax, 0x20(%rbx)
xorl %ebp, %ebp
testq %rax, %rax
jne 0x38d2
callq 0x20a0
cmpl $0x0, (%rax)
je 0x38e3
movq %rax, %rbx
leaq 0x9b9a(%rip), %rdi      # 0xd404
xorl %eax, %eax
callq 0x9878
movl $0x0, (%rbx)
jmp 0x38cd
leaq 0x9baa(%rip), %rdi      # 0xd42a
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x9997
callq 0x20a0
movq %rax, %r15
movl %ebp, (%rax)
leaq 0x9bbe(%rip), %rdi      # 0xd458
xorl %eax, %eax
callq 0x9997
movl %ebp, (%r15)
jmp 0x38af
movq 0x20(%r14), %rdi
callq 0x2040
movq %r14, %rdi
callq 0x2040
jmp 0x36c8
movq 0x20(%r14), %rdi
callq 0x2040
movq %r14, %rdi
callq 0x2040
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9b1a(%rip), %rdi      # 0xd404
xorl %eax, %eax
callq 0x9997
jmp 0x38cd
leaq 0xa4a9(%rip), %rdi      # 0xdda3
leaq 0x9a25(%rip), %rsi      # 0xd326
leaq 0x9b73(%rip), %rcx      # 0xd47b
movl $0x84, %edx
callq 0x21a0