// File: /workspace/github_c_makefile_star2_O1/Tarsnap[P]kivaloo/s3/dispatch.c
// Function: gotrequest
static int
gotrequest(void * cookie, int status)
{
	struct dispatch_state * D = cookie;
	struct 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 request))) == NULL)
			goto err0;
		R->D = D;

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

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

		/* Fill in the bucket and path fields. */
		R->req.bucket = R->R.bucket;
		if (asprintf(&R->path, "/%s", R->R.object) == -1)
			goto err1;
		R->req.path = R->path;

		/* Fill in default S3 request parameters. */
		R->req.nheaders = 0;
		R->req.headers = NULL;
		R->req.bodylen = 0;
		R->req.body = NULL;
		R->maxrlen = 0;
		R->range = NULL;

		/* Construct S3 request. */
		switch (R->R.type) {
		case PROTO_S3_PUT:
			/* PUT has a body and body length. */
			R->req.method = "PUT";
			R->req.bodylen = R->R.r.put.len;
			R->req.body = R->R.r.put.buf;
			break;
		case PROTO_S3_GET:
			/* GET has a maximum read length. */
			R->req.method = "GET";
			R->maxrlen = R->R.r.get.maxlen;
			break;
		case PROTO_S3_RANGE:
			/* Construct a Range header. */
			if (asprintf(&R->range, "bytes=%" PRIu64 "-%" PRIu64,
			    (uint64_t)(R->R.r.range.offset),
			    (uint64_t)(R->R.r.range.offset) +
				(uint64_t)(R->R.r.range.len) - 1) == -1)
				goto err2;

			/* RANGE is a GET with a Range header. */
			R->req.method = "GET";
			R->maxrlen = R->R.r.range.len;
			R->hdr.header = "Range";
			R->hdr.value = R->range;
			R->req.nheaders = 1;
			R->req.headers = &R->hdr;
			break;
		case PROTO_S3_HEAD:
			/* HEAD has no parameters. */
			R->req.method = "HEAD";
			break;
		case PROTO_S3_DELETE:
			/* DELETE has no parameters. */
			R->req.method = "DELETE";
			break;
		default:
			/* proto_s3_request_read broke. */
			assert(0);
		}

		/* Add the request to the S3 queue. */
		if (s3_request_queue(D->Q, &R->req, R->maxrlen,
		    callback_response, R))
			goto err3;

		/* 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);

err3:
	free(R->range);
err2:
	free(R->path);
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, 0x30(%rdi)
testl %esi, %esi
je 0x3ed5
movq %rbx, %rdi
callq 0x3e67
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xa8, %edi
callq 0x2330
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x3ec4
movq %rax, %r14
leaq 0x8(%rbx), %rax
movq %rax, (%rsp)
movq %rbx, (%r14)
movq 0x28(%rbx), %rdi
leaq 0x18(%r14), %rsi
callq 0xac38
testl %eax, %eax
jne 0x40cd
cmpl $-0x1, 0x20(%r14)
je 0x40da
movq 0x28(%r14), %rax
movq 0x30(%r14), %rdx
movq %rax, 0x50(%r14)
leaq 0x80(%r14), %r13
movq %r13, %rdi
leaq 0xd65c(%rip), %rsi      # 0x11590
xorl %eax, %eax
callq 0x82e8
cmpl $-0x1, %eax
je 0x4139
leaq 0x48(%r14), %r15
movq 0x80(%r14), %rax
movq %rax, 0x58(%r14)
leaq 0x88(%r14), %r12
xorps %xmm0, %xmm0
movups %xmm0, 0x60(%r14)
movups %xmm0, 0x70(%r14)
movups %xmm0, 0x88(%r14)
movl 0x20(%r14), %eax
cmpl $0x10010, %eax          # imm = 0x10010
jle 0x3fa3
cmpl $0x10011, %eax          # imm = 0x10011
je 0x3fd4
cmpl $0x10020, %eax          # imm = 0x10020
je 0x4044
cmpl $0x10030, %eax          # imm = 0x10030
jne 0x415b
leaq 0xd617(%rip), %rax      # 0x115b5
jmp 0x404b
cmpl $0x10000, %eax          # imm = 0x10000
je 0x4050
cmpl $0x10010, %eax          # imm = 0x10010
jne 0x415b
leaq 0xd5d8(%rip), %rax      # 0x11598
movq %rax, 0x48(%r14)
movl 0x38(%r14), %eax
movq %rax, 0x90(%r14)
jmp 0x406b
movl 0x38(%r14), %edx
movl 0x3c(%r14), %eax
leaq (%rdx,%rax), %rcx
decq %rcx
movq %r12, %rdi
leaq 0xd5af(%rip), %rsi      # 0x1159c
xorl %eax, %eax
callq 0x82e8
cmpl $-0x1, %eax
je 0x4130
leaq 0xd594(%rip), %rax      # 0x11598
movq %rax, 0x48(%r14)
movl 0x3c(%r14), %eax
movq %rax, 0x90(%r14)
leaq 0x98(%r14), %rax
leaq 0xd589(%rip), %rcx      # 0x115aa
movq %rcx, 0x98(%r14)
movq 0x88(%r14), %rcx
movq %rcx, 0xa0(%r14)
movq $0x1, 0x60(%r14)
movq %rax, 0x68(%r14)
jmp 0x406b
leaq 0xd565(%rip), %rax      # 0x115b0
movq %rax, (%r15)
jmp 0x406b
leaq 0xd53d(%rip), %rax      # 0x11594
movq %rax, 0x48(%r14)
movl 0x38(%r14), %eax
movq %rax, 0x70(%r14)
movq 0x40(%r14), %rax
movq %rax, 0x78(%r14)
movq (%rbx), %rdi
movq 0x90(%r14), %rdx
movq %r15, %rsi
leaq 0x22d(%rip), %rcx       # 0x42ac
movq %r14, %r8
callq 0xb11d
testl %eax, %eax
jne 0x4127
movq 0x10(%rbx), %rax
leaq 0x10(%rax), %rcx
testq %rax, %rax
cmoveq (%rsp), %rcx
movq %rax, 0x8(%r14)
movq %r14, (%rcx)
movq $0x0, 0x10(%r14)
movq %r14, 0x10(%rbx)
movl $0xa8, %edi
callq 0x2330
movq %rax, %r14
testq %rax, %rax
jne 0x3ef4
jmp 0x3ec4
movq %r14, %rdi
callq 0x2040
jmp 0x3eba
movq %r14, %rdi
callq 0x2040
movq 0x28(%rbx), %rdi
leaq -0x24d(%rip), %rsi      # 0x3ea0
movq %rbx, %rdx
callq 0xb9bd
movq %rax, 0x30(%rbx)
xorl %ebp, %ebp
testq %rax, %rax
jne 0x3ec4
callq 0x20a0
cmpl $0x0, (%rax)
je 0x414b
movq %rax, %rbx
leaq 0xd3c8(%rip), %rdi      # 0x114e0
xorl %eax, %eax
callq 0xa2f4
movl $0x0, (%rbx)
jmp 0x4141
movq (%r12), %rdi
callq 0x2040
movq (%r13), %rdi
callq 0x2040
movq %r14, %rdi
callq 0x2040
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x3ec4
leaq 0xd38e(%rip), %rdi      # 0x114e0
xorl %eax, %eax
callq 0xa413
jmp 0x4141
leaq 0xe001(%rip), %rdi      # 0x12163
leaq 0xd284(%rip), %rsi      # 0x113ed
leaq 0xd44c(%rip), %rcx      # 0x115bc
movl $0xc8, %edx
callq 0x21e0