// File: /workspace/github_c_makefile_star2_O2/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 $0x138, %rsp            # imm = 0x138
movq %r9, %rbx
movq %r8, %r14
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rdx
leaq 0x5f73(%rip), %rsi      # 0x11f16
leaq 0x18(%rsp), %rdi
xorl %eax, %eax
callq 0xd27c
pushq $-0x1
popq %rcx
cmpl $-0x1, %eax
je 0xc117
movq %r12, 0x28(%rsp)
movq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x3190
movq %rax, %rbp
movq %r13, %rdi
callq 0x3190
movq %r13, %rdx
movq %r13, 0x20(%rsp)
leaq 0xd0(%rsp), %r13
movq %r12, %rdi
movq %rbp, %rsi
movq %rax, %rcx
movq %r13, %r8
callq 0xda62
movq %r15, %rdi
callq 0x3190
pushq $0x20
popq %r12
leaq 0xb0(%rsp), %rbp
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %rax, %rcx
movq %rbp, %r8
callq 0xda62
movq %r14, %rdi
callq 0x3190
leaq 0x90(%rsp), %r13
movq %rbp, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %rax, %rcx
movq %r13, %r8
callq 0xda62
leaq 0x5ed1(%rip), %rdx      # 0x11f1d
pushq $0xc
popq %rcx
leaq 0x30(%rsp), %r8
movq %r13, %rdi
movq %r12, %rsi
callq 0xda62
movq 0x18(%rsp), %rdi
callq 0x3460
movq %rbx, %rdi
callq 0x3190
leaq 0x70(%rsp), %r13
movq %rbx, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0xd7b4
leaq 0xf0(%rsp), %rbp
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0xd370
movq %rbp, (%rsp)
leaq 0x5e85(%rip), %rsi      # 0x11f2a
leaq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq %r15, %r8
movq %r14, %r9
xorl %eax, %eax
callq 0xd27c
pushq $-0x1
popq %rcx
cmpl $-0x1, %eax
je 0xc117
movq 0x170(%rsp), %rbx
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x3190
leaq 0x30(%rsp), %rdi
pushq $0x20
popq %r15
leaq 0x50(%rsp), %r12
movq %r15, %rsi
movq %r14, %rdx
movq %rax, %rcx
movq %r12, %r8
callq 0xda62
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0xd370
movq 0x10(%rsp), %rdi
callq 0x3460
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq