// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x30(%rdi)
testl %esi, %esi
je 0x38bc
movq %rbx, %rdi
callq 0x3860
xorl %eax, %eax
jmp 0x3af3
leaq 0x8(%rbx), %r15
movl $0xa8, %edi
callq 0x2340
testq %rax, %rax
je 0x3af0
movq %rax, %r14
movq %rbx, (%rax)
movq 0x28(%rbx), %rdi
leaq 0x18(%rax), %rsi
callq 0x9e38
testl %eax, %eax
jne 0x3a80
cmpl $-0x1, 0x20(%r14)
je 0x3a8d
movq 0x28(%r14), %rax
movq 0x30(%r14), %rdx
movq %rax, 0x50(%r14)
leaq 0x80(%r14), %rbp
movq %rbp, %rdi
leaq 0xcc06(%rip), %rsi      # 0x1051c
xorl %eax, %eax
callq 0x74a4
cmpl $-0x1, %eax
je 0x3ae8
leaq 0x48(%r14), %r12
movq 0x80(%r14), %rax
movq %rax, 0x58(%r14)
leaq 0x88(%r14), %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x60(%r14)
movups %xmm0, 0x70(%r14)
movups %xmm0, 0x88(%r14)
movl 0x20(%r14), %eax
cmpl $0x10030, %eax          # imm = 0x10030
je 0x39bb
cmpl $0x10010, %eax          # imm = 0x10010
je 0x39a0
cmpl $0x10011, %eax          # imm = 0x10011
je 0x39c4
cmpl $0x10020, %eax          # imm = 0x10020
je 0x3a36
cmpl $0x10000, %eax          # imm = 0x10000
jne 0x3b12
leaq 0xcb99(%rip), %rax      # 0x10520
movq %rax, 0x48(%r14)
movl 0x38(%r14), %eax
movq %rax, 0x70(%r14)
movq 0x40(%r14), %rax
movq %rax, 0x78(%r14)
jmp 0x3a41
leaq 0xcb7d(%rip), %rax      # 0x10524
movq %rax, 0x48(%r14)
movl 0x38(%r14), %edx
movq %rdx, 0x90(%r14)
jmp 0x3a43
leaq 0xcb7f(%rip), %rax      # 0x10541
jmp 0x3a3d
movl 0x38(%r14), %edx
movl 0x3c(%r14), %eax
leaq (%rdx,%rax), %rcx
decq %rcx
movq %r13, %rdi
leaq 0xcb4b(%rip), %rsi      # 0x10528
xorl %eax, %eax
callq 0x74a4
cmpl $-0x1, %eax
je 0x3adf
leaq 0xcb30(%rip), %rax      # 0x10524
movq %rax, 0x48(%r14)
movl 0x3c(%r14), %edx
movq %rdx, 0x90(%r14)
movq %r14, %rax
addq $0x98, %rax
leaq 0xcb23(%rip), %rcx      # 0x10536
movq %rcx, 0x98(%r14)
movq 0x88(%r14), %rcx
movq %rcx, 0xa0(%r14)
movq $0x1, 0x60(%r14)
movq %rax, 0x68(%r14)
jmp 0x3a43
leaq 0xcaff(%rip), %rax      # 0x1053c
movq %rax, (%r12)
xorl %edx, %edx
movq (%rbx), %rdi
movq %r12, %rsi
leaq 0x1f1(%rip), %rcx       # 0x3c41
movq %r14, %r8
callq 0xa307
testl %eax, %eax
jne 0x3ad6
movq 0x10(%rbx), %rax
movq %rax, 0x8(%r14)
leaq 0x10(%rax), %rcx
testq %rax, %rax
cmoveq %r15, %rcx
movq %r14, (%rcx)
andq $0x0, 0x10(%r14)
movq %r14, 0x10(%rbx)
jmp 0x38c0
movq %r14, %rdi
callq 0x2040
jmp 0x38ad
movq %r14, %rdi
callq 0x2040
movq 0x28(%rbx), %rdi
leaq -0x20a(%rip), %rsi      # 0x3896
movq %rbx, %rdx
callq 0xab0d
movq %rax, %rcx
movq %rax, 0x30(%rbx)
xorl %eax, %eax
testq %rcx, %rcx
jne 0x3af3
callq 0x20a0
cmpl $0x0, (%rax)
je 0x3b02
movq %rax, %rbx
leaq 0xc9a2(%rip), %rdi      # 0x1046c
xorl %eax, %eax
callq 0x945d
andl $0x0, (%rbx)
jmp 0x3af0
movq (%r13), %rdi
callq 0x2040
movq (%rbp), %rdi
callq 0x2040
movq %r14, %rdi
callq 0x2040
pushq $-0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc963(%rip), %rdi      # 0x1046c
xorl %eax, %eax
callq 0x95ec
jmp 0x3af0
leaq 0xd62e(%rip), %rdi      # 0x11147
leaq 0xc859(%rip), %rsi      # 0x10379
leaq 0xca21(%rip), %rcx      # 0x10548
movl $0xc8, %edx
callq 0x21e0