#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 */

/* Variables and functions */
char *malloc(int);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
char *sprint_sha1_hash(int *hashval) {
  int x = 0;
  char *out = NULL;
  if ((out = malloc(43)) == NULL)
    return NULL;
  memset(out, 0x00, 43);
  strcpy(out, "$2$");
  for (x = 0; x < 5; x++) {
    sprintf(out + (x * 8) + 3, "%08x", hashval[x]);
  }
  out[43] = 0;
  return out;
}


x86 assembly (clang, -O0):
<sprint_sha1_hash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x2b,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1180 <sprint_sha1_hash+0x40>
movq   $0x0,-0x8(%rbp)
jmp    11f9 <sprint_sha1_hash+0xb9>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x2b,%edx
call   1040 <memset@plt>
mov    -0x20(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jge    11e9 <sprint_sha1_hash+0xa9>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%eax
shl    $0x3,%eax
cltq
add    %rax,%rdi
add    $0x3,%rdi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe2e(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11a7 <sprint_sha1_hash+0x67>
mov    -0x20(%rbp),%rax
movb   $0x0,0x2b(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sprint_sha1_hash>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2b,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11d5 <sprint_sha1_hash+0x95>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x2b,%edx
call   1040 <memset@plt>
lea    0xe90(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0x3(%rbx),%rdi
mov    (%r14),%edx
lea    0xe7e(%rip),%r15
mov    %r15,%rsi
call   1060 <sprintf@plt>
lea    0xb(%rbx),%rdi
mov    0x4(%r14),%edx
mov    %r15,%rsi
call   1060 <sprintf@plt>
lea    0x13(%rbx),%rdi
mov    0x8(%r14),%edx
mov    %r15,%rsi
call   1060 <sprintf@plt>
lea    0x1b(%rbx),%rdi
mov    0xc(%r14),%edx
mov    %r15,%rsi
call   1060 <sprintf@plt>
mov    %rbx,%rdi
add    $0x23,%rdi
mov    0x10(%r14),%edx
mov    %r15,%rsi
call   1060 <sprintf@plt>
movb   $0x0,0x2b(%rbx)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"$2$\00"
@.str.1 = [5 x i8] c"%08x\00"
define i8* @sprint_sha1_hash(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
store i32* %0, i32** %3
store i32 0, i32* %4
store i8* null, i8** %5
%6 = call i8* @malloc(i32 43)
store i8* %6, i8** %5
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i8* null, i8** %2
br label %37
9: ; preds = %1
%10 = load i8*, i8** %5
%11 = call i32 @memset(i8* %10, i32 0, i32 43)
%12 = load i8*, i8** %5
%13 = call i32 @strcpy(i8* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %14
14: ; preds = %30, %9
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 5
br i1 %16, label %17, label %33
17: ; preds = %14
%18 = load i8*, i8** %5
%19 = load i32, i32* %4
%20 = mul nsw i32 %19, 8
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %18, i64 %21
%23 = getelementptr inbounds i8, i8* %22, i64 3
%24 = load i32*, i32** %3
%25 = load i32, i32* %4
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = load i32, i32* %27
%29 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %17
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
store i32 %32, i32* %4
br label %14
33: ; preds = %14
%34 = load i8*, i8** %5
%35 = getelementptr inbounds i8, i8* %34, i64 43
store i8 0, i8* %35
%36 = load i8*, i8** %5
store i8* %36, i8** %2
br label %37
37: ; preds = %33, %8
%38 = load i8*, i8** %2
ret i8* %38
}

LLVM IR (-O2):
@.str = [4 x i8] c"$2$\00"
@.str.1 = [5 x i8] c"%08x\00"
define i8* @sprint_sha1_hash(i32* nocapture %0) {
%2 = tail call dereferenceable_or_null(43) i8* @malloc(i32 43)
%3 = icmp eq i8* %2, null
br i1 %3, label %27, label %4
4: ; preds = %1
%5 = tail call i32 @memset(i8* %2, i32 0, i32 43)
%6 = tail call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds i8, i8* %2, i64 3
%8 = load i32, i32* %0
%9 = tail call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds i8, i8* %2, i64 11
%11 = getelementptr inbounds i32, i32* %0, i64 1
%12 = load i32, i32* %11
%13 = tail call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds i8, i8* %2, i64 19
%15 = getelementptr inbounds i32, i32* %0, i64 2
%16 = load i32, i32* %15
%17 = tail call i32 @sprintf(i8* %14, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds i8, i8* %2, i64 27
%19 = getelementptr inbounds i32, i32* %0, i64 3
%20 = load i32, i32* %19
%21 = tail call i32 @sprintf(i8* %18, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds i8, i8* %2, i64 35
%23 = getelementptr inbounds i32, i32* %0, i64 4
%24 = load i32, i32* %23
%25 = tail call i32 @sprintf(i8* %22, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds i8, i8* %2, i64 43
store i8 0, i8* %26
br label %27
27: ; preds = %1, %4
ret i8* %2
}