#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ SHA512_CTX;

/* Variables and functions */
int SHA512_DIGEST_LENGTH;
int /*<<< orphan*/ SHA512_Final(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA512_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA512_Update(int /*<<< orphan*/ *, unsigned char *, long);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
void sha512_hash_string(unsigned char *string, long len,
                        char outputBuffer[129]) {
  int i = 0;
  unsigned char hash[SHA512_DIGEST_LENGTH];
  SHA512_CTX sha512;
  SHA512_Init(&sha512);
  SHA512_Update(&sha512, string, len);
  SHA512_Final(hash, &sha512);
  for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
    sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
  }
  outputBuffer[128] = 0;
}


x86 assembly (clang, -O0):
<sha512_hash_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
lea    -0x34(%rbp),%rdi
call   1060 <SHA512_Init@plt>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    -0x34(%rbp),%rdi
call   1030 <SHA512_Update@plt>
mov    -0x40(%rbp),%rdi
lea    -0x34(%rbp),%rsi
call   1040 <SHA512_Final@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e29(%rip),%rcx
cmp    (%rcx),%eax
jge    11fd <sha512_hash_string+0xbd>
mov    -0x40(%rbp),%rax
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rdi
movslq -0x1c(%rbp),%rcx
lea    0xe1a(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1050 <sprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b5 <sha512_hash_string+0x75>
mov    -0x18(%rbp),%rax
movb   $0x0,0x80(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha512_hash_string>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,-0x30(%rbp)
mov    %rsi,%r15
mov    %rdi,%r13
mov    0x2e86(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    -0x34(%rbp),%rbx
mov    %rbx,%rdi
call   1060 <SHA512_Init@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
call   1030 <SHA512_Update@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1040 <SHA512_Final@plt>
cmpl   $0x0,(%r14)
jle    11d0 <sha512_hash_string+0x90>
xor    %ebx,%ebx
lea    0xe56(%rip),%r15
mov    -0x30(%rbp),%r13
xchg   %ax,%ax
movzbl (%r12,%rbx,1),%edx
mov    %r13,%rdi
mov    %r15,%rsi
call   1050 <sprintf@plt>
add    $0x1,%rbx
movslq (%r14),%rax
add    $0x2,%r13
cmp    %rax,%rbx
jl     11b0 <sha512_hash_string+0x70>
mov    -0x30(%rbp),%rax
movb   $0x0,0x80(%rax)
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SHA512_DIGEST_LENGTH = global i32 0
@.str = [5 x i8] c"%02x\00"
define void @sha512_hash_string(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
store i32 0, i32* %7
%11 = load i32, i32* @SHA512_DIGEST_LENGTH
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %8
%14 = alloca i8, i64 %12
store i64 %12, i64* %9
%15 = call i32 @SHA512_Init(i32* %10)
%16 = load i8*, i8** %4
%17 = load i64, i64* %5
%18 = call i32 @SHA512_Update(i32* %10, i8* %16, i64 %17)
%19 = call i32 @SHA512_Final(i8* %14, i32* %10)
store i32 0, i32* %7
br label %20
20: ; preds = %35, %3
%21 = load i32, i32* %7
%22 = load i32, i32* @SHA512_DIGEST_LENGTH
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %38
24: ; preds = %20
%25 = load i8*, i8** %6
%26 = load i32, i32* %7
%27 = mul nsw i32 %26, 2
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %14, i64 %31
%33 = load i8, i8* %32
%34 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %33)
br label %35
35: ; preds = %24
%36 = load i32, i32* %7
%37 = add nsw i32 %36, 1
store i32 %37, i32* %7
br label %20
38: ; preds = %20
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i64 128
store i8 0, i8* %40
%41 = load i8*, i8** %8
ret void
}

LLVM IR (-O2):
@SHA512_DIGEST_LENGTH = global i32 0
@.str = [5 x i8] c"%02x\00"
define void @sha512_hash_string(i8* %0, i64 %1, i8* nocapture writeonly %2) {
%4 = alloca i32
%5 = load i32, i32* @SHA512_DIGEST_LENGTH
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast i32* %4 to i8*
%9 = call i32 @SHA512_Init(i32* %4)
%10 = call i32 @SHA512_Update(i32* %4, i8* %0, i64 %1)
%11 = call i32 @SHA512_Final(i8* %7, i32* %4)
%12 = load i32, i32* @SHA512_DIGEST_LENGTH
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %25
14: ; preds = %3, %14
%15 = phi i64 [ %21, %14 ], [ 0, %3 ]
%16 = shl nuw nsw i64 %15, 1
%17 = getelementptr inbounds i8, i8* %2, i64 %16
%18 = getelementptr inbounds i8, i8* %7, i64 %15
%19 = load i8, i8* %18
%20 = call i32 @sprintf(i8* dereferenceable(1) %17, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %19)
%21 = add nuw nsw i64 %15, 1
%22 = load i32, i32* @SHA512_DIGEST_LENGTH
%23 = sext i32 %22 to i64
%24 = icmp slt i64 %21, %23
br i1 %24, label %14, label %25
25: ; preds = %14, %3
%26 = getelementptr inbounds i8, i8* %2, i64 128
store i8 0, i8* %26
ret void
}