// File: /workspace/github_c_makefile_star2_O3/Tarsnap[P]kivaloo/dynamodb-kv/dispatch.c
// Function: gotrequest
static int
gotrequest(void * cookie, int status)
{
	struct dispatch_state * D = cookie;
	struct request * R;
	struct dynamodb_request_queue * Q;
	const char * op;
	size_t maxrlen;
	int prio;

	/* 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 request))) == NULL)
			goto err0;
		R->D = D;

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

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

		/* Translate this to a DynamoDB request. */
		switch (R->R.type) {
		case PROTO_DDBKV_PUT:
			Q = D->QW;
			op = "PutItem";
			maxrlen = 1024;
			prio = 0;
			if ((R->body = dynamodb_kv_put(D->table, R->R.key,
			    R->R.buf, R->R.len)) == NULL)
				goto err1;
			break;
		case PROTO_DDBKV_ICAS:
			Q = D->QW;
			op = "PutItem";
			maxrlen = 1024;
			prio = 0;
			if ((R->body = dynamodb_kv_icas(D->table, R->R.key,
			    R->R.buf, R->R.len, R->R.buf2, R->R.len2)) == NULL)
				goto err1;
			break;
		case PROTO_DDBKV_CREATE:
			Q = D->QW;
			op = "PutItem";
			maxrlen = 1024;
			prio = 0;
			if ((R->body = dynamodb_kv_create(D->table, R->R.key,
			    R->R.buf, R->R.len)) == NULL)
				goto err1;
			break;
		case PROTO_DDBKV_GET:
			Q = D->QR;
			op = "GetItem";
			prio = 0;
			maxrlen = 1048576;
			if ((R->body =
			    dynamodb_kv_get(D->table, R->R.key)) == NULL)
				goto err1;
			break;
		case PROTO_DDBKV_GETC:
			Q = D->QR;
			op = "GetItem";
			prio = 0;
			maxrlen = 1048576;
			if ((R->body =
			    dynamodb_kv_getc(D->table, R->R.key)) == NULL)
				goto err1;
			break;
		case PROTO_DDBKV_DELETE:
			Q = D->QW;
			op = "DeleteItem";
			maxrlen = 1024;
			prio = 1;
			if ((R->body =
			    dynamodb_kv_delete(D->table, R->R.key)) == NULL)
				goto err1;
			break;
		default:
			/* proto_dynamodb_kv_request_read broke. */
			assert(0);
		}

		/* Add the request to the appropriate DynamoDB queue. */
		if (dynamodb_request_queue(Q, prio, op, R->body, maxrlen,
		    R->R.key, callback_response, R))
			goto err2;

		/* Add to the linked list. */
		if ((R->prev = D->ip_tail) == NULL) {
			D->ip_head = R;
		} else {
			R->prev->next = R;
		}
		R->next = NULL;
		D->ip_tail = R;
	} 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);

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

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

err2:
	free(R->body);
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, 0x40(%rdi)
testl %esi, %esi
je 0x50ce
movq %rbx, %rdi
callq 0x5057
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x58, %edi
callq 0x34d0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x50bd
movq %rax, %r14
leaq 0x18(%rbx), %r12
leaq 0x368(%rip), %r13       # 0x5458
movq %rbx, (%r14)
movq 0x38(%rbx), %rdi
leaq 0x18(%r14), %rsi
callq 0xdb74
testl %eax, %eax
jne 0x5294
movl 0x20(%r14), %eax
cmpl $0x1010f, %eax          # imm = 0x1010F
jg 0x5151
cmpl $0x10100, %eax          # imm = 0x10100
jle 0x51a2
cmpl $0x10101, %eax          # imm = 0x10101
je 0x51d5
cmpl $0x10102, %eax          # imm = 0x10102
jne 0x531e
movq (%rbx), %r15
movq 0x10(%rbx), %rdi
movq 0x28(%r14), %rsi
movq 0x38(%r14), %rdx
movl 0x30(%r14), %ecx
callq 0xbeda
jmp 0x51f5
cmpl $0x10110, %eax          # imm = 0x10110
je 0x51c2
cmpl $0x10111, %eax          # imm = 0x10111
je 0x5213
cmpl $0x10120, %eax          # imm = 0x10120
jne 0x531e
movq (%rbx), %r15
movq 0x10(%rbx), %rdi
movq 0x28(%r14), %rsi
callq 0xc274
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x52aa
movl $0x1, %esi
movl $0x400, %r8d            # imm = 0x400
leaq 0xf49d(%rip), %rdx      # 0x1463a
jmp 0x523c
jne 0x52bc
movq (%rbx), %r15
movq 0x10(%rbx), %rdi
movq 0x28(%r14), %rsi
movq 0x38(%r14), %rdx
movl 0x30(%r14), %ecx
callq 0xbb7c
jmp 0x51f5
movq 0x8(%rbx), %r15
movq 0x10(%rbx), %rdi
movq 0x28(%r14), %rsi
callq 0xc0ac
jmp 0x5224
movq (%rbx), %r15
movq 0x10(%rbx), %rdi
movq 0x28(%r14), %rsi
movq 0x38(%r14), %rdx
movl 0x30(%r14), %ecx
movq 0x48(%r14), %r8
movl 0x40(%r14), %r9d
callq 0xbccf
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x52aa
xorl %esi, %esi
movl $0x400, %r8d            # imm = 0x400
leaq 0xf419(%rip), %rdx      # 0x1462a
jmp 0x523c
movq 0x8(%rbx), %r15
movq 0x10(%rbx), %rdi
movq 0x28(%r14), %rsi
callq 0xc18e
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x52aa
xorl %esi, %esi
movl $0x100000, %r8d         # imm = 0x100000
leaq 0xf3f6(%rip), %rdx      # 0x14632
movq 0x28(%r14), %r9
movq %r15, %rdi
movq %rax, %rcx
pushq %r14
pushq %r13
callq 0xc930
addq $0x10, %rsp
testl %eax, %eax
jne 0x52a1
movq 0x20(%rbx), %rax
leaq 0x10(%rax), %rcx
testq %rax, %rax
cmoveq %r12, %rcx
movq %rax, 0x8(%r14)
movq %r14, (%rcx)
movq $0x0, 0x10(%r14)
movq %r14, 0x20(%rbx)
movl $0x58, %edi
callq 0x34d0
movq %rax, %r14
testq %rax, %rax
jne 0x50f0
jmp 0x50bd
movq %r14, %rdi
callq 0x3460
jmp 0x50b3
movq 0x50(%r14), %rdi
callq 0x3460
movq %r14, %rdi
callq 0x3460
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x50bd
cmpl $-0x1, %eax
jne 0x531e
movq %r14, %rdi
callq 0x3460
movq 0x38(%rbx), %rdi
leaq -0x23b(%rip), %rsi      # 0x5099
movq %rbx, %rdx
callq 0xe946
movq %rax, 0x40(%rbx)
xorl %ebp, %ebp
testq %rax, %rax
jne 0x50bd
callq 0x3040
cmpl $0x0, (%rax)
je 0x530e
movq %rax, %rbx
leaq 0xf27b(%rip), %rdi      # 0x1457a
xorl %eax, %eax
callq 0xb938
movl $0x0, (%rbx)
jmp 0x52b2
leaq 0xf265(%rip), %rdi      # 0x1457a
xorl %eax, %eax
callq 0xba57
jmp 0x52b2
leaq 0xfee2(%rip), %rdi      # 0x15207
leaq 0xf15b(%rip), %rsi      # 0x14487
leaq 0xf312(%rip), %rcx      # 0x14645
movl $0xcd, %edx
callq 0x3210