#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*/ SHA_CTX;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SHA1_Final(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Update(int /*<<< orphan*/ *, char *, size_t);
int SHA_DIGEST_LENGTH;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *sha1_on_file(char *filename) {
  FILE *fp = NULL;
  unsigned char temp[SHA_DIGEST_LENGTH];
  char buff[32768] = {0};
  char *checksum = NULL;
  SHA_CTX context;
  size_t resultRead = 0;
  int i = 0;
  fp = fopen(filename, "rb");
  if (!fp) {
    perror("File not found\n");
    return NULL;
  }
  checksum = (char *)malloc(sizeof(char) * 41);
  if (!checksum) {
    perror("Error while allocating the checksum in memory\n");
    return NULL;
  }
  SHA1_Init(&context);
  do {
    resultRead = fread(buff, sizeof(char), 32768, fp);
    SHA1_Update(&context, buff, resultRead);
  } while (resultRead != 0);
  fclose(fp);
  SHA1_Final(temp, &context);
  for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
    sprintf(checksum + (2 * i), "%02X", 0xff & temp[i]);
  }
  checksum[40] = '\0';
  return checksum;
}


x86 assembly (clang, -O0):
<sha1_on_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8060,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e2a(%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,-0x8058(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    -0x8030(%rbp),%rdi
xor    %esi,%esi
mov    $0x8000,%edx
call   1050 <memset@plt>
movq   $0x0,-0x8038(%rbp)
movq   $0x0,-0x8048(%rbp)
movl   $0x0,-0x804c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xddc(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    125b <sha1_on_file+0xbb>
lea    0xdc4(%rip),%rdi
call   10a0 <perror@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x8050(%rbp)
jmp    139b <sha1_on_file+0x1fb>
mov    $0x29,%edi
call   1060 <malloc@plt>
mov    %rax,-0x8038(%rbp)
cmpq   $0x0,-0x8038(%rbp)
jne    129d <sha1_on_file+0xfd>
lea    0xd92(%rip),%rdi
call   10a0 <perror@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x8050(%rbp)
jmp    139b <sha1_on_file+0x1fb>
lea    -0x803c(%rbp),%rdi
call   1080 <SHA1_Init@plt>
lea    -0x8030(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8000,%edx
call   1030 <fread@plt>
mov    %rax,-0x8048(%rbp)
lea    -0x8030(%rbp),%rsi
mov    -0x8048(%rbp),%rdx
lea    -0x803c(%rbp),%rdi
call   1070 <SHA1_Update@plt>
cmpq   $0x0,-0x8048(%rbp)
jne    12a9 <sha1_on_file+0x109>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x8058(%rbp),%rdi
lea    -0x803c(%rbp),%rsi
call   10c0 <SHA1_Final@plt>
movl   $0x0,-0x804c(%rbp)
mov    -0x804c(%rbp),%eax
mov    0x2cc3(%rip),%rcx
cmp    (%rcx),%eax
jge    137b <sha1_on_file+0x1db>
mov    -0x8058(%rbp),%rax
mov    -0x8038(%rbp),%rdi
mov    -0x804c(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rdi
movslq -0x804c(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
and    $0xff,%edx
lea    0xce0(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x804c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x804c(%rbp)
jmp    1318 <sha1_on_file+0x178>
mov    -0x8038(%rbp),%rax
movb   $0x0,0x28(%rax)
mov    -0x8038(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x8050(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha1_on_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x8018,%rsp
mov    %rdi,%rbx
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    -0x8040(%rbp),%rdi
mov    $0x8000,%edx
xor    %esi,%esi
call   1050 <memset@plt>
lea    0xe15(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     12c8 <sha1_on_file+0x128>
mov    %rax,%r13
mov    $0x29,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     12d1 <sha1_on_file+0x131>
mov    %rax,-0x38(%rbp)
lea    -0x2c(%rbp),%r12
mov    %r12,%rdi
call   1080 <SHA1_Init@plt>
lea    -0x8040(%rbp),%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x8000,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
mov    %rax,%r14
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1070 <SHA1_Update@plt>
test   %r14,%r14
jne    1230 <sha1_on_file+0x90>
mov    %r13,%rdi
call   1040 <fclose@plt>
lea    -0x2c(%rbp),%rsi
mov    %r15,%rdi
call   10c0 <SHA1_Final@plt>
mov    0x2d72(%rip),%r13
cmpl   $0x0,0x0(%r13)
jle    12b1 <sha1_on_file+0x111>
xor    %ebx,%ebx
lea    0xdbc(%rip),%r12
mov    -0x38(%rbp),%r14
nopw   0x0(%rax,%rax,1)
movzbl (%r15,%rbx,1),%edx
mov    %r14,%rdi
mov    %r12,%rsi
call   10b0 <sprintf@plt>
add    $0x1,%rbx
movslq 0x0(%r13),%rax
add    $0x2,%r14
cmp    %rax,%rbx
jl     1290 <sha1_on_file+0xf0>
mov    -0x38(%rbp),%rax
movb   $0x0,0x28(%rax)
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd34(%rip),%rdi
jmp    12d8 <sha1_on_file+0x138>
lea    0xd3b(%rip),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
jmp    12b9 <sha1_on_file+0x119>

LLVM IR (-O0):
@SHA_DIGEST_LENGTH = global i32 0
@.str = [3 x i8] c"rb\00"
@.str.1 = [16 x i8] c"File not found\0A\00"
@.str.2 = [47 x i8] c"Error while allocating the checksum in memory\0A\00"
@.str.3 = [5 x i8] c"%02X\00"
define i8* @sha1_on_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [32768 x i8]
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %3
store i32* null, i32** %4
%13 = load i32, i32* @SHA_DIGEST_LENGTH
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %5
%16 = alloca i8, i64 %14
store i64 %14, i64* %6
%17 = bitcast [32768 x i8]* %7 to i8*
store i8* null, i8** %8
store i64 0, i64* %10
store i32 0, i32* %11
%18 = load i8*, i8** %3
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %4
%20 = load i32*, i32** %4
%21 = icmp ne i32* %20, null
br i1 %21, label %24, label %22
22: ; preds = %1
%23 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
store i32 1, i32* %12
br label %71
24: ; preds = %1
%25 = call i64 @malloc(i32 41)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = icmp ne i8* %27, null
br i1 %28, label %31, label %29
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
store i32 1, i32* %12
br label %71
31: ; preds = %24
%32 = call i32 @SHA1_Init(i32* %9)
br label %33
33: ; preds = %40, %31
%34 = getelementptr inbounds [32768 x i8], [32768 x i8]* %7, i64 0, i64 0
%35 = load i32*, i32** %4
%36 = call i64 @fread(i8* %34, i32 1, i32 32768, i32* %35)
store i64 %36, i64* %10
%37 = getelementptr inbounds [32768 x i8], [32768 x i8]* %7, i64 0, i64 0
%38 = load i64, i64* %10
%39 = call i32 @SHA1_Update(i32* %9, i8* %37, i64 %38)
br label %40
40: ; preds = %33
%41 = load i64, i64* %10
%42 = icmp ne i64 %41, 0
br i1 %42, label %33, label %43
43: ; preds = %40
%44 = load i32*, i32** %4
%45 = call i32 @fclose(i32* %44)
%46 = call i32 @SHA1_Final(i8* %16, i32* %9)
store i32 0, i32* %11
br label %47
47: ; preds = %64, %43
%48 = load i32, i32* %11
%49 = load i32, i32* @SHA_DIGEST_LENGTH
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %67
51: ; preds = %47
%52 = load i8*, i8** %8
%53 = load i32, i32* %11
%54 = mul nsw i32 2, %53
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %52, i64 %55
%57 = load i32, i32* %11
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %16, i64 %58
%60 = load i8, i8* %59
%61 = zext i8 %60 to i32
%62 = and i32 255, %61
%63 = call i32 @sprintf(i8* %56, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %62)
br label %64
64: ; preds = %51
%65 = load i32, i32* %11
%66 = add nsw i32 %65, 1
store i32 %66, i32* %11
br label %47
67: ; preds = %47
%68 = load i8*, i8** %8
%69 = getelementptr inbounds i8, i8* %68, i64 40
store i8 0, i8* %69
%70 = load i8*, i8** %8
store i8* %70, i8** %2
store i32 1, i32* %12
br label %71
71: ; preds = %67, %29, %22
%72 = load i8*, i8** %5
%73 = load i8*, i8** %2
ret i8* %73
}

LLVM IR (-O2):
@SHA_DIGEST_LENGTH = global i32 0
@.str = [3 x i8] c"rb\00"
@.str.1 = [16 x i8] c"File not found\0A\00"
@.str.2 = [47 x i8] c"Error while allocating the checksum in memory\0A\00"
@.str.3 = [5 x i8] c"%02X\00"
define i8* @sha1_on_file(i8* nocapture %0) {
%2 = alloca [32768 x i8]
%3 = alloca i32
%4 = load i32, i32* @SHA_DIGEST_LENGTH
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = getelementptr inbounds [32768 x i8], [32768 x i8]* %2, i64 0, i64 0
%8 = bitcast i32* %3 to i8*
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %44
13: ; preds = %1
%14 = tail call i64 @malloc(i32 41)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
br label %44
19: ; preds = %13
%20 = call i32 @SHA1_Init(i32* %3)
br label %21
21: ; preds = %21, %19
%22 = call i64 @fread(i8* %7, i32 1, i32 32768, i32* %9)
%23 = call i32 @SHA1_Update(i32* %3, i8* %7, i64 %22)
%24 = icmp eq i64 %22, 0
br i1 %24, label %25, label %21
25: ; preds = %21
%26 = call i32 @fclose(i32* %9)
%27 = call i32 @SHA1_Final(i8* %6, i32* %3)
%28 = load i32, i32* @SHA_DIGEST_LENGTH
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %42
30: ; preds = %25, %30
%31 = phi i64 [ %38, %30 ], [ 0, %25 ]
%32 = shl nuw nsw i64 %31, 1
%33 = getelementptr inbounds i8, i8* %15, i64 %32
%34 = getelementptr inbounds i8, i8* %6, i64 %31
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = call i32 @sprintf(i8* %33, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = add nuw nsw i64 %31, 1
%39 = load i32, i32* @SHA_DIGEST_LENGTH
%40 = sext i32 %39 to i64
%41 = icmp slt i64 %38, %40
br i1 %41, label %30, label %42
42: ; preds = %30, %25
%43 = getelementptr inbounds i8, i8* %15, i64 40
store i8 0, i8* %43
br label %44
44: ; preds = %42, %17, %11
%45 = phi i8* [ %15, %42 ], [ null, %17 ], [ null, %11 ]
ret i8* %45
}