// File: /workspace/github_c_makefile_star2_O1/Tarsnap[P]kivaloo/liball/../libcperciva/aws/aws_sign.c
// Function: aws_sign
static int
aws_sign(const char * key_secret, const char * date, const char * datetime,
    const char * region, const char * service, const char * creq,
    char sigbuf[65])
{
	char * AWS4_key;
	uint8_t kDate[32];
	uint8_t kRegion[32];
	uint8_t kService[32];
	uint8_t kSigning[32];
	uint8_t h_creq[32];
	char hhex_creq[65];
	char * STS;
	uint8_t hmac[32];

	/* Construct "AWS4" + key_secret. */
	if (asprintf(&AWS4_key, "AWS4%s", key_secret) == -1)
		goto err0;

	/* kDate = HMAC("AWS4" + kSecret, Date). */
	HMAC_SHA256_Buf(AWS4_key, strlen(AWS4_key), date, strlen(date), kDate);

	/* kRegion = HMAC(kDate, Region). */
	HMAC_SHA256_Buf(kDate, 32, region, strlen(region), kRegion);

	/* kService = HMAC(kRegion, Service). */
	HMAC_SHA256_Buf(kRegion, 32, service, strlen(service), kService);

	/* kSigning = HMAC(kService, "aws4_request"). */
	HMAC_SHA256_Buf(kService, 32, "aws4_request", strlen("aws4_request"),
	    kSigning);

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

	/* Generate the hexified hash of the Canonical Request string. */
	SHA256_Buf(creq, strlen(creq), h_creq);
	hexify(h_creq, hhex_creq, 32);

	/* Construct the String to Sign. */
	if (asprintf(&STS,
	    "AWS4-HMAC-SHA256\n"
	    "%s\n"
	    "%s/%s/%s/aws4_request\n"
	    "%s",
	    datetime, date, region, service, hhex_creq) == -1)
		goto err0;

	/* Sign and hexify the String to Sign. */
	HMAC_SHA256_Buf(kSigning, 32, STS, strlen(STS), hmac);
	hexify(hmac, sigbuf, 32);

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

	/* Success! */
	return (0);

err0:
	/* Failure! */
	return (-1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %r9, %rbx
movq %r8, %r14
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rdx
leaq 0x61a7(%rip), %rsi      # 0x13113
leaq 0x10(%rsp), %rdi
xorl %eax, %eax
callq 0xe2e0
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
je 0xd0e6
movq %r12, 0x18(%rsp)
movq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x3190
movq %rax, %rbp
movq %r13, %rdi
callq 0x3190
movq %r13, %rdx
leaq 0xc0(%rsp), %r13
movq %r12, %rdi
movq %rbp, %rsi
movq %rdx, %rbp
movq %rax, %rcx
movq %r13, %r8
callq 0xec35
movq %r15, %rdi
callq 0x3190
leaq 0xa0(%rsp), %r12
movl $0x20, %esi
movq %r13, %rdi
movq %r15, %rdx
movq %rax, %rcx
movq %r12, %r8
callq 0xec35
movq %r14, %rdi
callq 0x3190
leaq 0x80(%rsp), %r13
movl $0x20, %esi
movq %r12, %rdi
movq %r14, %rdx
movq %rax, %rcx
movq %r13, %r8
callq 0xec35
leaq 0x6105(%rip), %rdx      # 0x1311a
leaq 0x20(%rsp), %r8
movl $0x20, %esi
movl $0xc, %ecx
movq %r13, %rdi
callq 0xec35
movq 0x10(%rsp), %rdi
callq 0x3450
movq %rbx, %rdi
callq 0x3190
leaq 0x60(%rsp), %r12
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0xe971
leaq 0xe0(%rsp), %r13
movl $0x20, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0xe3e4
movq %r13, (%rsp)
leaq 0x60b3(%rip), %rsi      # 0x13127
leaq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq %rbp, %rcx
movq %r15, %r8
movq %r14, %r9
xorl %eax, %eax
callq 0xe2e0
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
je 0xd0e6
movq 0x160(%rsp), %rbx
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x3190
leaq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %r15
movl $0x20, %esi
movq %r14, %rdx
movq %rax, %rcx
movq %r15, %r8
callq 0xec35
movl $0x20, %edx
movq %r15, %rdi
movq %rbx, %rsi
callq 0xe3e4
movq 0x8(%rsp), %rdi
callq 0x3450
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq