// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq $0x0, 0x30(%rax)
cmpl $0x0, -0x14(%rbp)
je 0x44be
jmp 0x482c
jmp 0x44c0
movl $0xa8, %edi
callq 0x2350
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0x44d9
jmp 0x4867
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x28(%rbp), %rsi
addq $0x18, %rsi
callq 0xf470
cmpl $0x0, %eax
je 0x4503
jmp 0x4823
movq -0x28(%rbp), %rax
cmpl $-0x1, 0x20(%rax)
jne 0x4512
jmp 0x47b2
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x28(%rbp), %rdi
addq $0x80, %rdi
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rdx
leaq 0x1a345(%rip), %rsi     # 0x1e881
movb $0x0, %al
callq 0xafe0
cmpl $-0x1, %eax
jne 0x454d
jmp 0x485e
movq -0x28(%rbp), %rax
movq 0x80(%rax), %rcx
movq %rcx, 0x58(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x60(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x68(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x70(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x78(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x90(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x88(%rax)
movq -0x28(%rbp), %rax
movl 0x20(%rax), %eax
addl $0xffff0000, %eax       # imm = 0xFFFF0000
movl %eax, %ecx
movq %rcx, -0x30(%rbp)
subl $0x30, %eax
ja 0x4707
movq -0x30(%rbp), %rax
leaq 0x19f74(%rip), %rcx     # 0x1e544
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x28(%rbp), %rax
leaq 0x1a2a1(%rip), %rcx     # 0x1e885
movq %rcx, 0x48(%rax)
movq -0x28(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x78(%rax)
jmp 0x4726
movq -0x28(%rbp), %rax
leaq 0x1a270(%rip), %rcx     # 0x1e889
movq %rcx, 0x48(%rax)
movq -0x28(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movq %rcx, 0x90(%rax)
jmp 0x4726
movq -0x28(%rbp), %rdi
addq $0x88, %rdi
movq -0x28(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, %edx
movq -0x28(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl 0x3c(%rax), %eax
addq %rax, %rcx
subq $0x1, %rcx
leaq 0x1a225(%rip), %rsi     # 0x1e88d
movb $0x0, %al
callq 0xafe0
cmpl $-0x1, %eax
jne 0x4679
jmp 0x484e
movq -0x28(%rbp), %rax
leaq 0x1a205(%rip), %rcx     # 0x1e889
movq %rcx, 0x48(%rax)
movq -0x28(%rbp), %rax
movl 0x3c(%rax), %eax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movq %rcx, 0x90(%rax)
movq -0x28(%rbp), %rax
leaq 0x1a1f4(%rip), %rcx     # 0x1e89b
movq %rcx, 0x98(%rax)
movq -0x28(%rbp), %rax
movq 0x88(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0xa0(%rax)
movq -0x28(%rbp), %rax
movq $0x1, 0x60(%rax)
movq -0x28(%rbp), %rcx
addq $0x98, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x68(%rax)
jmp 0x4726
movq -0x28(%rbp), %rax
leaq 0x1a1b1(%rip), %rcx     # 0x1e8a1
movq %rcx, 0x48(%rax)
jmp 0x4726
movq -0x28(%rbp), %rax
leaq 0x1a1a5(%rip), %rcx     # 0x1e8a6
movq %rcx, 0x48(%rax)
jmp 0x4726
leaq 0x1a911(%rip), %rdi     # 0x1f01f
leaq 0x19fc9(%rip), %rsi     # 0x1e6de
movl $0xc8, %edx
leaq 0x1a18c(%rip), %rcx     # 0x1e8ad
callq 0x2200
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq -0x28(%rbp), %rsi
addq $0x48, %rsi
movq -0x28(%rbp), %rax
movq 0x90(%rax), %rdx
movq -0x28(%rbp), %r8
leaq 0x2d5(%rip), %rcx       # 0x4a20
callq 0xfd10
cmpl $0x0, %eax
je 0x475a
jmp 0x483e
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
movq %rax, 0x8(%rcx)
cmpq $0x0, %rax
jne 0x477e
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x478e
movq -0x28(%rbp), %rcx
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movb $0x1, %al
testb $0x1, %al
jne 0x44c0
jmp 0x47b2
movq -0x28(%rbp), %rdi
callq 0x2040
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x20(%rbp), %rdx
leaq -0x33e(%rip), %rsi      # 0x4490
callq 0x10cd0
movq -0x20(%rbp), %rcx
movq %rax, 0x30(%rcx)
cmpq $0x0, %rax
jne 0x481a
jmp 0x47e3
callq 0x20a0
cmpl $0x0, (%rax)
je 0x4808
leaq 0x19fdd(%rip), %rdi     # 0x1e7d1
movb $0x0, %al
callq 0xe670
callq 0x20a0
movl $0x0, (%rax)
jmp 0x4816
leaq 0x19fc2(%rip), %rdi     # 0x1e7d1
movb $0x0, %al
callq 0xe8c0
jmp 0x4818
jmp 0x4867
movl $0x0, -0x4(%rbp)
jmp 0x486e
movq -0x28(%rbp), %rdi
callq 0x2040
movq -0x20(%rbp), %rdi
callq 0x4420
movl $0x0, -0x4(%rbp)
jmp 0x486e
movq -0x28(%rbp), %rax
movq 0x88(%rax), %rdi
callq 0x2040
movq -0x28(%rbp), %rax
movq 0x80(%rax), %rdi
callq 0x2040
movq -0x28(%rbp), %rdi
callq 0x2040
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)