// File: /workspace/github_c_makefile_star2_O3/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 0x3e95
movq %rbx, %rdi
callq 0x3e27
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 0x3e84
movq %rax, %r14
leaq 0x8(%rbx), %rax
movq %rax, (%rsp)
movq %rbx, (%r14)
movq 0x28(%rbx), %rdi
leaq 0x18(%r14), %rsi
callq 0xac80
testl %eax, %eax
jne 0x4088
cmpl $-0x1, 0x20(%r14)
je 0x4095
movq 0x28(%r14), %rax
movq 0x30(%r14), %rdx
movq %rax, 0x50(%r14)
leaq 0x80(%r14), %r13
movq %r13, %rdi
leaq 0xd628(%rip), %rsi      # 0x1151c
xorl %eax, %eax
callq 0x82e8
cmpl $-0x1, %eax
je 0x40f4
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 0x3f63
cmpl $0x10011, %eax          # imm = 0x10011
je 0x3f94
cmpl $0x10020, %eax          # imm = 0x10020
je 0x4004
cmpl $0x10030, %eax          # imm = 0x10030
jne 0x4116
leaq 0xd5e3(%rip), %rax      # 0x11541
jmp 0x400b
cmpl $0x10000, %eax          # imm = 0x10000
je 0x4010
cmpl $0x10010, %eax          # imm = 0x10010
jne 0x4116
leaq 0xd5a4(%rip), %rax      # 0x11524
movq %rax, 0x48(%r14)
movl 0x38(%r14), %edx
movq %rdx, 0x90(%r14)
jmp 0x402d
movl 0x38(%r14), %edx
movl 0x3c(%r14), %eax
leaq (%rdx,%rax), %rcx
decq %rcx
movq %r12, %rdi
leaq 0xd57b(%rip), %rsi      # 0x11528
xorl %eax, %eax
callq 0x82e8
cmpl $-0x1, %eax
je 0x40eb
leaq 0xd560(%rip), %rax      # 0x11524
movq %rax, 0x48(%r14)
movl 0x3c(%r14), %edx
movq %rdx, 0x90(%r14)
leaq 0x98(%r14), %rax
leaq 0xd555(%rip), %rcx      # 0x11536
movq %rcx, 0x98(%r14)
movq 0x88(%r14), %rcx
movq %rcx, 0xa0(%r14)
movq $0x1, 0x60(%r14)
movq %rax, 0x68(%r14)
jmp 0x402d
leaq 0xd531(%rip), %rax      # 0x1153c
movq %rax, (%r15)
jmp 0x402b
leaq 0xd509(%rip), %rax      # 0x11520
movq %rax, 0x48(%r14)
movl 0x38(%r14), %eax
movq %rax, 0x70(%r14)
movq 0x40(%r14), %rax
movq %rax, 0x78(%r14)
xorl %edx, %edx
movq (%rbx), %rdi
movq %r15, %rsi
leaq 0x229(%rip), %rcx       # 0x4263
movq %r14, %r8
callq 0xb159
testl %eax, %eax
jne 0x40e2
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 0x3eb4
jmp 0x3e84
movq %r14, %rdi
callq 0x2040
jmp 0x3e7a
movq %r14, %rdi
callq 0x2040
movq 0x28(%rbx), %rdi
leaq -0x248(%rip), %rsi      # 0x3e60
movq %rbx, %rdx
callq 0xb9ea
movq %rax, 0x30(%rbx)
xorl %ebp, %ebp
testq %rax, %rax
jne 0x3e84
callq 0x20a0
cmpl $0x0, (%rax)
je 0x4106
movq %rax, %rbx
leaq 0xd399(%rip), %rdi      # 0x1146c
xorl %eax, %eax
callq 0xa33c
movl $0x0, (%rbx)
jmp 0x40fc
movq (%r12), %rdi
callq 0x2040
movq (%r13), %rdi
callq 0x2040
movq %r14, %rdi
callq 0x2040
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x3e84
leaq 0xd35f(%rip), %rdi      # 0x1146c
xorl %eax, %eax
callq 0xa45b
jmp 0x40fc
leaq 0xe05a(%rip), %rdi      # 0x12177
leaq 0xd255(%rip), %rsi      # 0x11379
leaq 0xd41d(%rip), %rcx      # 0x11548
movl $0xc8, %edx
callq 0x21e0