// File: /workspace/github_c_makefile_star2_O1/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 0x509a
movq %rbx, %rdi
callq 0x5023
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 0x5089
movq %rax, %r14
leaq 0x18(%rbx), %r12
leaq 0x36d(%rip), %r13       # 0x5429
movq %rbx, (%r14)
movq 0x38(%rbx), %rdi
leaq 0x18(%r14), %rsi
callq 0xdc60
testl %eax, %eax
jne 0x5261
movl 0x20(%r14), %eax
cmpl $0x1010f, %eax          # imm = 0x1010F
jg 0x511d
cmpl $0x10100, %eax          # imm = 0x10100
jle 0x516e
cmpl $0x10101, %eax          # imm = 0x10101
je 0x51a1
cmpl $0x10102, %eax          # imm = 0x10102
jne 0x52eb
movq (%rbx), %r15
movq 0x10(%rbx), %rdi
movq 0x28(%r14), %rsi
movq 0x38(%r14), %rdx
movl 0x30(%r14), %ecx
callq 0xbea2
jmp 0x51c1
cmpl $0x10110, %eax          # imm = 0x10110
je 0x518e
cmpl $0x10111, %eax          # imm = 0x10111
je 0x51df
cmpl $0x10120, %eax          # imm = 0x10120
jne 0x52eb
movq (%rbx), %r15
movq 0x10(%rbx), %rdi
movq 0x28(%r14), %rsi
callq 0xc2e6
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x5277
movl $0x1, %esi
movl $0x400, %r8d            # imm = 0x400
leaq 0xf4d1(%rip), %rdx      # 0x1463a
jmp 0x5208
jne 0x5289
movq (%rbx), %r15
movq 0x10(%rbx), %rdi
movq 0x28(%r14), %rsi
movq 0x38(%r14), %rdx
movl 0x30(%r14), %ecx
callq 0xbac8
jmp 0x51c1
movq 0x8(%rbx), %r15
movq 0x10(%rbx), %rdi
movq 0x28(%r14), %rsi
callq 0xc0b0
jmp 0x51f0
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 0xbc5b
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x5277
xorl %esi, %esi
movl $0x400, %r8d            # imm = 0x400
leaq 0xf44d(%rip), %rdx      # 0x1462a
jmp 0x5208
movq 0x8(%rbx), %r15
movq 0x10(%rbx), %rdi
movq 0x28(%r14), %rsi
callq 0xc1c9
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x5277
xorl %esi, %esi
movl $0x100000, %r8d         # imm = 0x100000
leaq 0xf42a(%rip), %rdx      # 0x14632
movq 0x28(%r14), %r9
movq 0x50(%r14), %rcx
movq %r15, %rdi
pushq %r14
pushq %r13
callq 0xca27
addq $0x10, %rsp
testl %eax, %eax
jne 0x526e
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 0x50bc
jmp 0x5089
movq %r14, %rdi
callq 0x3460
jmp 0x507f
movq 0x50(%r14), %rdi
callq 0x3460
movq %r14, %rdi
callq 0x3460
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x5089
cmpl $-0x1, %eax
jne 0x52eb
movq %r14, %rdi
callq 0x3460
movq 0x38(%rbx), %rdi
leaq -0x23c(%rip), %rsi      # 0x5065
movq %rbx, %rdx
callq 0xea25
movq %rax, 0x40(%rbx)
xorl %ebp, %ebp
testq %rax, %rax
jne 0x5089
callq 0x3040
cmpl $0x0, (%rax)
je 0x52db
movq %rax, %rbx
leaq 0xf2ae(%rip), %rdi      # 0x1457a
xorl %eax, %eax
callq 0xb884
movl $0x0, (%rbx)
jmp 0x527f
leaq 0xf298(%rip), %rdi      # 0x1457a
xorl %eax, %eax
callq 0xb9a3
jmp 0x527f
leaq 0xff05(%rip), %rdi      # 0x151f7
leaq 0xf18e(%rip), %rsi      # 0x14487
leaq 0xf345(%rip), %rcx      # 0x14645
movl $0xcd, %edx
callq 0x3210