// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x40(%rdi)
testl %esi, %esi
je 0x4ad7
movq %rbx, %rdi
callq 0x4a72
xorl %eax, %eax
jmp 0x4c90
leaq 0x18(%rbx), %r13
pushq $0x58
popq %r15
leaq 0x333(%rip), %rbp       # 0x4e19
movq %r15, %rdi
callq 0x34e0
testq %rax, %rax
je 0x4c8d
movq %rax, %r14
movq %rbx, (%rax)
movq 0x38(%rbx), %rdi
movq %rax, %rsi
addq $0x18, %rsi
callq 0xcae0
testl %eax, %eax
jne 0x4c6f
movl 0x20(%r14), %eax
cmpl $0x10100, %eax          # imm = 0x10100
je 0x4bba
cmpl $0x10101, %eax          # imm = 0x10101
je 0x4b98
cmpl $0x10102, %eax          # imm = 0x10102
je 0x4c03
cmpl $0x10110, %eax          # imm = 0x10110
je 0x4b85
cmpl $0x10111, %eax          # imm = 0x10111
je 0x4bd4
cmpl $0x10120, %eax          # imm = 0x10120
jne 0x4c9f
movq (%rbx), %r12
movq 0x10(%rbx), %rdi
movq 0x28(%r14), %rsi
callq 0xb301
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x4c85
movl $0x400, %r8d            # imm = 0x400
leaq 0xea95(%rip), %rdx      # 0x13612
pushq $0x1
popq %rsi
jmp 0x4c33
movq 0x8(%rbx), %r12
movq 0x10(%rbx), %rdi
movq 0x28(%r14), %rsi
callq 0xb139
jmp 0x4be5
movq (%rbx), %r12
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 0xad72
jmp 0x4c1b
movq (%rbx), %r12
movq 0x10(%rbx), %rdi
movq 0x28(%r14), %rsi
movq 0x38(%r14), %rdx
movl 0x30(%r14), %ecx
callq 0xac24
jmp 0x4c1b
movq 0x8(%rbx), %r12
movq 0x10(%rbx), %rdi
movq 0x28(%r14), %rsi
callq 0xb21b
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x4c85
xorl %esi, %esi
movl $0x100000, %r8d         # imm = 0x100000
leaq 0xea09(%rip), %rdx      # 0x1360a
jmp 0x4c33
movq (%rbx), %r12
movq 0x10(%rbx), %rdi
movq 0x28(%r14), %rsi
movq 0x38(%r14), %rdx
movl 0x30(%r14), %ecx
callq 0xaf72
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x4c85
xorl %esi, %esi
movl $0x400, %r8d            # imm = 0x400
leaq 0xe9cf(%rip), %rdx      # 0x13602
movq 0x28(%r14), %r9
movq %r12, %rdi
movq %rax, %rcx
pushq %r14
pushq %rbp
callq 0xb98b
popq %rcx
popq %rdx
testl %eax, %eax
jne 0x4c7c
movq 0x20(%rbx), %rax
movq %rax, 0x8(%r14)
leaq 0x10(%rax), %rcx
testq %rax, %rax
cmoveq %r13, %rcx
movq %r14, (%rcx)
andq $0x0, 0x10(%r14)
movq %r14, 0x20(%rbx)
jmp 0x4ae6
movq %r14, %rdi
callq 0x3470
jmp 0x4ac8
movq 0x50(%r14), %rdi
callq 0x3470
movq %r14, %rdi
callq 0x3470
pushq $-0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $-0x1, %eax
jne 0x4cfd
movq %r14, %rdi
callq 0x3470
movq 0x38(%rbx), %rdi
leaq -0x206(%rip), %rsi      # 0x4ab1
movq %rbx, %rdx
callq 0xd7e5
movq %rax, %rcx
movq %rax, 0x40(%rbx)
xorl %eax, %eax
testq %rcx, %rcx
jne 0x4c90
callq 0x3040
cmpl $0x0, (%rax)
je 0x4ced
movq %rax, %rbx
leaq 0xe871(%rip), %rdi      # 0x13552
xorl %eax, %eax
callq 0xa911
andl $0x0, (%rbx)
jmp 0x4c8d
leaq 0xe85e(%rip), %rdi      # 0x13552
xorl %eax, %eax
callq 0xaaa0
jmp 0x4c8d
leaq 0xf4b3(%rip), %rdi      # 0x141b7
leaq 0xe754(%rip), %rsi      # 0x1345f
leaq 0xe90b(%rip), %rcx      # 0x1361d
movl $0xcd, %edx
callq 0x3220