// File: /workspace/github_c_makefile_star2_O3/Tarsnap[P]kivaloo/liball/../lib/s3/s3_request.c
// Function: s3_request
void *
s3_request(struct sock_addr * const * addrs, const char * key_id,
    const char * key_secret, const char * region,
    struct s3_request * request, size_t maxrlen,
    int (* callback)(void *, struct http_response *), void * cookie)
{
	struct http_request RH;
	char * host;
	char * x_amz_content_sha256;
	char * x_amz_date;
	char * authorization;
	char content_length[sizeof(size_t) * 3 + 1];
	void * http_cookie;

	/* Construct headers needed for authorization. */
	if (aws_sign_s3_headers(key_id, key_secret, region,
	    request->method, request->bucket, request->path, request->body,
	    request->bodylen, &x_amz_content_sha256, &x_amz_date,
	    &authorization))
		goto err0;

	/* Construct Host header. */
	if (asprintf(&host, "%s.s3.amazonaws.com", request->bucket) == -1)
		goto err1;

	/* If we have a message body, construct Content-Length. */
	if (request->body)
		sprintf(content_length, "%zu", request->bodylen);

	/* Construct HTTP request structure. */
	RH.method = request->method;
	RH.path = request->path;
	RH.bodylen = request->bodylen;
	RH.body = request->body;

	/* We have 4 or 5 extra headers. */
	if (request->body)
		RH.nheaders = request->nheaders + 5;
	else
		RH.nheaders = request->nheaders + 4;
	if ((RH.headers =
	    malloc(RH.nheaders * sizeof(struct http_header))) == NULL)
		goto err2;

	/* Construct headers. */
	if (request->nheaders)
		memcpy(RH.headers, request->headers,
		    request->nheaders * sizeof(struct http_header));
	RH.headers[request->nheaders].header = "Host";
	RH.headers[request->nheaders].value = host;
	RH.headers[request->nheaders + 1].header = "X-Amz-Content-SHA256";
	RH.headers[request->nheaders + 1].value = x_amz_content_sha256;
	RH.headers[request->nheaders + 2].header = "X-Amz-Date";
	RH.headers[request->nheaders + 2].value = x_amz_date;
	RH.headers[request->nheaders + 3].header = "Authorization";
	RH.headers[request->nheaders + 3].value = authorization;
	if (request->body) {
		RH.headers[request->nheaders + 4].header = "Content-Length";
		RH.headers[request->nheaders + 4].value = content_length;
	}

	/* Send the request. */
	if ((http_cookie = http_request(addrs, &RH, maxrlen,
	    callback, cookie)) == NULL)
		goto err3;

	/* Free array of headers. */
	free(RH.headers);

	/* Free string allocated by asprintf. */
	free(host);

	/* Free headers used for authorization. */
	free(authorization);
	free(x_amz_date);
	free(x_amz_content_sha256);

	/* Success! */
	return (http_cookie);

err3:
	free(RH.headers);
err2:
	free(host);
err1:
	free(authorization);
	free(x_amz_date);
	free(x_amz_content_sha256);
err0:
	/* Failure! */
	return (NULL);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, %rbx
movq %r8, %r15
movq %rdi, %r14
movq (%r8), %rax
movq 0x8(%r8), %r8
movq 0x10(%r15), %r9
subq $0x8, %rsp
leaq 0x8(%rsp), %r10
leaq 0x18(%rsp), %r11
leaq 0x20(%rsp), %r12
movq %rsi, %rdi
movq %rdx, %rsi
movq %rcx, %rdx
movq %rax, %rcx
pushq %r10
pushq %r11
pushq %r12
pushq 0x28(%r15)
pushq 0x30(%r15)
callq 0xd71c
addq $0x30, %rsp
testl %eax, %eax
je 0xd56f
xorl %ebx, %ebx
jmp 0xd70a
movq 0x8(%r15), %rdx
leaq 0x5a3d(%rip), %rsi      # 0x12fb7
leaq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0x82e8
cmpl $-0x1, %eax
je 0xd6eb
cmpq $0x0, 0x30(%r15)
je 0xd5b3
movq 0x28(%r15), %rdx
leaq 0x56f3(%rip), %rsi      # 0x12c94
leaq 0x50(%rsp), %rdi
xorl %eax, %eax
callq 0x2450
movq 0x30(%r15), %r13
jmp 0xd5b6
xorl %r13d, %r13d
movq (%r15), %rax
movq %rax, 0x20(%rsp)
movq 0x10(%r15), %rax
movq %rax, 0x28(%rsp)
movq 0x28(%r15), %rax
movq %rax, 0x40(%rsp)
movq %r13, 0x48(%rsp)
cmpq $0x1, %r13
movl $0x5, %edi
sbbq $0x0, %rdi
movq 0x18(%r15), %rbp
addq %rbp, %rdi
movq %rdi, 0x30(%rsp)
shlq $0x4, %rdi
callq 0x2330
movq %rax, 0x38(%rsp)
testq %rax, %rax
je 0xd6e1
movq %rax, %r12
testq %rbp, %rbp
je 0xd620
movq 0x20(%r15), %rsi
movq %rbp, %rdx
shlq $0x4, %rdx
movq %r12, %rdi
callq 0x22e0
movq 0xb8(%rsp), %r8
shlq $0x4, %rbp
leaq 0x5998(%rip), %rax      # 0x12fcb
movq %rax, (%r12,%rbp)
movq 0x8(%rsp), %rax
movq %rax, 0x8(%r12,%rbp)
leaq 0x5988(%rip), %rax      # 0x12fd0
movq %rax, 0x10(%r12,%rbp)
movq 0x18(%rsp), %rax
movq %rax, 0x18(%r12,%rbp)
leaq 0x5987(%rip), %rax      # 0x12fe5
movq %rax, 0x20(%r12,%rbp)
movq 0x10(%rsp), %rax
movq %rax, 0x28(%r12,%rbp)
leaq 0x597c(%rip), %rax      # 0x12ff0
movq %rax, 0x30(%r12,%rbp)
movq (%rsp), %rax
movq %rax, 0x38(%r12,%rbp)
movq 0xb0(%rsp), %rcx
testq %r13, %r13
je 0xd6a8
addq %rbp, %r12
leaq 0x3f3e(%rip), %rax      # 0x115d7
movq %rax, 0x40(%r12)
leaq 0x50(%rsp), %rax
movq %rax, 0x48(%r12)
leaq 0x20(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x5b8c
movq %rax, %rbx
movq 0x18(%r15), %rdi
callq 0x2040
testq %rbx, %rbx
je 0xd6e1
movq 0x8(%rsp), %rdi
callq 0x2040
movq (%rsp), %rdi
callq 0x2040
jmp 0xd6f6
movq 0x8(%rsp), %rdi
callq 0x2040
movq (%rsp), %rdi
callq 0x2040
xorl %ebx, %ebx
movq 0x10(%rsp), %rdi
callq 0x2040
movq 0x18(%rsp), %rdi
callq 0x2040
movq %rbx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq