#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*/ *, char *,
                                 int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int /*<<< orphan*/ strlen(char *);
void sha512(char *string, char *salt, char outputBuffer[129]) {
  unsigned char hash[SHA512_DIGEST_LENGTH];
  SHA512_CTX sha512;
  SHA512_Init(&sha512);
  SHA512_Update(&sha512, string, strlen(string));
  SHA512_Update(&sha512, salt, strlen(salt));
  SHA512_Final(hash, &sha512);
  int i = 0;
  for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
    sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
  }
  outputBuffer[129 - 1] = 0;
}


x86 assembly (clang, -O0):
<sha512>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    -0x2c(%rbp),%rdi
call   1070 <SHA512_Init@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
lea    -0x2c(%rbp),%rdi
call   1040 <SHA512_Update@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    -0x2c(%rbp),%rdi
call   1040 <SHA512_Update@plt>
mov    -0x38(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   1050 <SHA512_Final@plt>
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    0x2dea(%rip),%rcx
cmp    (%rcx),%eax
jge    123c <sha512+0xec>
mov    -0x38(%rbp),%rax
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rdi
movslq -0x30(%rbp),%rcx
lea    0xddb(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1060 <sprintf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11f4 <sha512+0xa4>
mov    -0x18(%rbp),%rax
movb   $0x0,0x80(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha512>:
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,%rbx
mov    0x2e76(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    -0x34(%rbp),%r13
mov    %r13,%rdi
call   1070 <SHA512_Init@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <SHA512_Update@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <SHA512_Update@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   1050 <SHA512_Final@plt>
cmpl   $0x0,(%r14)
jle    1200 <sha512+0xb0>
xor    %ebx,%ebx
lea    0xe2a(%rip),%r15
mov    -0x30(%rbp),%r13
nopw   0x0(%rax,%rax,1)
movzbl (%r12,%rbx,1),%edx
mov    %r13,%rdi
mov    %r15,%rsi
call   1060 <sprintf@plt>
add    $0x1,%rbx
movslq (%r14),%rax
add    $0x2,%r13
cmp    %rax,%rbx
jl     11e0 <sha512+0x90>
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(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%11 = load i32, i32* @SHA512_DIGEST_LENGTH
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
%15 = call i32 @SHA512_Init(i32* %9)
%16 = load i8*, i8** %4
%17 = load i8*, i8** %4
%18 = call i32 @strlen(i8* %17)
%19 = call i32 @SHA512_Update(i32* %9, i8* %16, i32 %18)
%20 = load i8*, i8** %5
%21 = load i8*, i8** %5
%22 = call i32 @strlen(i8* %21)
%23 = call i32 @SHA512_Update(i32* %9, i8* %20, i32 %22)
%24 = call i32 @SHA512_Final(i8* %14, i32* %9)
store i32 0, i32* %10
store i32 0, i32* %10
br label %25
25: ; preds = %40, %3
%26 = load i32, i32* %10
%27 = load i32, i32* @SHA512_DIGEST_LENGTH
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %43
29: ; preds = %25
%30 = load i8*, i8** %6
%31 = load i32, i32* %10
%32 = mul nsw i32 %31, 2
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
%35 = load i32, i32* %10
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %14, i64 %36
%38 = load i8, i8* %37
%39 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %38)
br label %40
40: ; preds = %29
%41 = load i32, i32* %10
%42 = add nsw i32 %41, 1
store i32 %42, i32* %10
br label %25
43: ; preds = %25
%44 = load i8*, i8** %6
%45 = getelementptr inbounds i8, i8* %44, i64 128
store i8 0, i8* %45
%46 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
@SHA512_DIGEST_LENGTH = global i32 0
@.str = [5 x i8] c"%02x\00"
define void @sha512(i8* %0, i8* %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 @strlen(i8* %0)
%11 = call i32 @SHA512_Update(i32* %4, i8* %0, i32 %10)
%12 = call i32 @strlen(i8* %1)
%13 = call i32 @SHA512_Update(i32* %4, i8* %1, i32 %12)
%14 = call i32 @SHA512_Final(i8* %7, i32* %4)
%15 = load i32, i32* @SHA512_DIGEST_LENGTH
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %3, %17
%18 = phi i64 [ %24, %17 ], [ 0, %3 ]
%19 = shl nuw nsw i64 %18, 1
%20 = getelementptr inbounds i8, i8* %2, i64 %19
%21 = getelementptr inbounds i8, i8* %7, i64 %18
%22 = load i8, i8* %21
%23 = call i32 @sprintf(i8* dereferenceable(1) %20, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %22)
%24 = add nuw nsw i64 %18, 1
%25 = load i32, i32* @SHA512_DIGEST_LENGTH
%26 = sext i32 %25 to i64
%27 = icmp slt i64 %24, %26
br i1 %27, label %17, label %28
28: ; preds = %17, %3
%29 = getelementptr inbounds i8, i8* %2, i64 128
store i8 0, i8* %29
ret void
}