// File: /workspace/github_c_makefile_star2_O1/Tarsnap[P]kivaloo/liball/../libcperciva/aws/aws_sign.c
// Function: aws_sign_s3_headers
int
aws_sign_s3_headers(const char * key_id, const char * key_secret,
    const char * region, const char * method, const char * bucket,
    const char * path, const uint8_t * body, size_t bodylen,
    char ** x_amz_content_sha256, char ** x_amz_date, char ** authorization)
{
	time_t t_now;
	struct tm r_result;
	char date[9];
	char datetime[17];
	uint8_t hbuf[32];
	char content_sha256[65];
	char * canonical_request;
	char sigbuf[65];

	/* Get the current time. */
	if (time(&t_now) == (time_t)(-1)) {
		warnp("time");
		goto err0;
	}

	/* Construct date string <yyyymmdd>. */
	if (strftime(date, 9, "%Y%m%d", gmtime_r(&t_now, &r_result)) == 0) {
		warnp("strftime");
		goto err0;
	}

	/* Construct date-and-time string <yyyymmddThhmmssZ>. */
	if (strftime(datetime, 17, "%Y%m%dT%H%M%SZ",
	    gmtime_r(&t_now, &r_result)) == 0) {
		warnp("strftime");
		goto err0;
	}

	/* Compute the hexified SHA256 of the payload. */
	SHA256_Buf(body, body ? bodylen : 0, hbuf);
	hexify(hbuf, content_sha256, 32);

	/* Construct Canonical Request. */
	if (asprintf(&canonical_request,
	    "%s\n"
	    "%s\n"
	    "\n"
	    "host:%s.s3.amazonaws.com\n"
	    "x-amz-content-sha256:%s\n"
	    "x-amz-date:%s\n"
	    "\n"
	    "host;x-amz-content-sha256;x-amz-date\n"
	    "%s",
	    method, path, bucket, content_sha256, datetime,
	    content_sha256) == -1)
		goto err0;

	/* Compute request signature. */
	if (aws_sign(key_secret, date, datetime, region,
	    "s3", canonical_request, sigbuf))
		goto err1;

	/* Construct Authorization header. */
	if (asprintf(authorization,
	    "AWS4-HMAC-SHA256 "
	    "Credential=%s/%s/%s/s3/aws4_request,"
	    "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
	    "Signature=%s",
	    key_id, date, region, sigbuf) == -1)
		goto err1;

	/* Duplicate X-Amz-Content-SHA256 and X-Amz-Date headers. */
	if ((*x_amz_content_sha256 = strdup(content_sha256)) == NULL)
		goto err2;
	if ((*x_amz_date = strdup(datetime)) == NULL)
		goto err3;

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

	/* Success! */
	return (0);

err3:
	free(*x_amz_content_sha256);
err2:
	free(*authorization);
err1:
	free(canonical_request);
err0:
	/* Failure! */
	return (-1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %r9, %r13
movq %r8, %r12
movq %rcx, %rbp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
leaq 0x8(%rsp), %rdi
callq 0x3320
cmpq $-0x1, %rax
je 0xce5c
leaq 0x8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x3660
leaq 0x5f5f(%rip), %rdx      # 0x12c94
leaq 0x17(%rsp), %rdi
movl $0x9, %esi
movq %rax, %rcx
callq 0x3270
testq %rax, %rax
je 0xce46
leaq 0x8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x3660
leaq 0x5f35(%rip), %rdx      # 0x12c9b
leaq 0x30(%rsp), %rdi
movl $0x11, %esi
movq %rax, %rcx
callq 0x3270
testq %rax, %rax
je 0xce46
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq 0x170(%rsp), %rdi
testq %rdi, %rdi
movq 0x178(%rsp), %rsi
cmoveq %rdi, %rsi
leaq 0x80(%rsp), %r14
movq %r14, %rdx
callq 0xe971
leaq 0xf0(%rsp), %rbx
movl $0x20, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xe3e4
leaq 0x5ed9(%rip), %rsi      # 0x12caa
movq %rsp, %rdi
movq %rbp, %rdx
movq %r13, %rcx
movq %r12, %r8
movq %rbx, %r9
xorl %eax, %eax
pushq %rbx
leaq 0x38(%rsp), %r10
pushq %r10
callq 0xe2e0
addq $0x10, %rsp
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
je 0xce9b
movq (%rsp), %r9
subq $0x8, %rsp
leaq 0xa8(%rsp), %rax
leaq 0x5f01(%rip), %r8       # 0x12d19
leaq 0x1f(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movq %r15, %rdi
movq 0x30(%rsp), %rbx
movq %rbx, %rcx
pushq %rax
callq 0xcf42
addq $0x10, %rsp
testl %eax, %eax
je 0xceaf
movq (%rsp), %rdi
callq 0x3450
jmp 0xce9b
callq 0x3040
cmpl $0x0, (%rax)
je 0xce7f
movq %rax, %rbx
leaq 0x4bab(%rip), %rdi      # 0x11a05
jmp 0xce70
callq 0x3040
cmpl $0x0, (%rax)
je 0xce88
movq %rax, %rbx
leaq 0x4b99(%rip), %rdi      # 0x11a09
xorl %eax, %eax
callq 0x578c
movl $0x0, (%rbx)
jmp 0xce96
leaq 0x4b7f(%rip), %rdi      # 0x11a05
jmp 0xce8f
leaq 0x4b7a(%rip), %rdi      # 0x11a09
xorl %eax, %eax
callq 0x58ab
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x190(%rsp), %r15
leaq 0x5e5e(%rip), %rsi      # 0x12d1c
leaq 0x17(%rsp), %rcx
leaq 0xa0(%rsp), %r9
movq %r15, %rdi
movq 0x20(%rsp), %rdx
movq %rbx, %r8
xorl %eax, %eax
callq 0xe2e0
cmpl $-0x1, %eax
je 0xce3b
movq 0x180(%rsp), %rbx
leaq 0xf0(%rsp), %rdi
callq 0x35e0
movq %rax, (%rbx)
testq %rax, %rax
je 0xcf35
movq 0x188(%rsp), %r14
leaq 0x30(%rsp), %rdi
callq 0x35e0
movq %rax, (%r14)
testq %rax, %rax
je 0xcf2d
movq (%rsp), %rdi
callq 0x3450
xorl %ebp, %ebp
jmp 0xce9b
movq (%rbx), %rdi
callq 0x3450
movq (%r15), %rdi
callq 0x3450
jmp 0xce3b