#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*/ buf;
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 const *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
void hashFile(const char *path, char *shaHash) {
  char buf[4096];
  FILE *fd = fopen(path, "rb");
  size_t read;
  SHA_CTX shaCtx;
  SHA1_Init(&shaCtx);
  while ((read = fread(buf, 1, sizeof(buf), fd))) {
    SHA1_Update(&shaCtx, buf, read);
  }
  fclose(fd);
  unsigned char hash[SHA_DIGEST_LENGTH];
  SHA1_Final(hash, &shaCtx);
  for (int offset = 0; offset < SHA_DIGEST_LENGTH; ++offset) {
    sprintf(shaHash + offset * 2, "%02x", hash[offset]);
  }
  shaHash[SHA_DIGEST_LENGTH * 2] = 0;
}


x86 assembly (clang, -O0):
<hashFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x1018(%rbp)
lea    -0x1024(%rbp),%rdi
call   1060 <SHA1_Init@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x1018(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %rax,-0x1020(%rbp)
cmp    $0x0,%rax
je     11f3 <hashFile+0x83>
lea    -0x1010(%rbp),%rsi
mov    -0x1020(%rbp),%rdx
lea    -0x1024(%rbp),%rdi
call   1050 <SHA1_Update@plt>
jmp    11a6 <hashFile+0x36>
mov    -0x1018(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x1030(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x1048(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x1038(%rbp)
lea    -0x1024(%rbp),%rsi
call   1090 <SHA1_Final@plt>
movl   $0x0,-0x103c(%rbp)
mov    -0x103c(%rbp),%eax
mov    0x2d91(%rip),%rcx
cmp    (%rcx),%eax
jge    12a4 <hashFile+0x134>
mov    -0x1048(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    -0x103c(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rdi
movslq -0x103c(%rbp),%rcx
lea    0xd7c(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1080 <sprintf@plt>
mov    -0x103c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x103c(%rbp)
jmp    124a <hashFile+0xda>
mov    -0x10(%rbp),%rax
mov    0x2d39(%rip),%rcx
mov    (%rcx),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1030(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hashFile>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,-0x38(%rbp)
lea    0xe71(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r12
lea    -0x2c(%rbp),%rdi
call   1060 <SHA1_Init@plt>
lea    -0x1040(%rbp),%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11f8 <hashFile+0x88>
lea    -0x2c(%rbp),%r15
lea    -0x1040(%rbp),%rbx
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <SHA1_Update@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jne    11d0 <hashFile+0x60>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %rsp,%rbx
mov    0x2dde(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    -0x2c(%rbp),%rsi
mov    %r12,%rdi
call   1090 <SHA1_Final@plt>
mov    (%r14),%eax
test   %eax,%eax
jle    1274 <hashFile+0x104>
mov    %rbx,-0x40(%rbp)
xor    %r13d,%r13d
lea    0xdc4(%rip),%r15
mov    -0x38(%rbp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl (%r12,%r13,1),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <sprintf@plt>
add    $0x1,%r13
movslq (%r14),%rax
add    $0x2,%rbx
cmp    %rax,%r13
jl     1250 <hashFile+0xe0>
mov    -0x40(%rbp),%rbx
cltq
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rcx,%rax,2)
mov    %rbx,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SHA_DIGEST_LENGTH = global i32 0
@.str.1 = [5 x i8] c"%02x\00"
define void @hashFile(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [4096 x i8]
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%12 = load i8*, i8** %3
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %6
%14 = call i32 @SHA1_Init(i32* %8)
br label %15
15: ; preds = %20, %2
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%17 = load i32*, i32** %6
%18 = call i64 @fread(i8* %16, i32 1, i32 4096, i32* %17)
store i64 %18, i64* %7
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%22 = load i64, i64* %7
%23 = call i32 @SHA1_Update(i32* %8, i8* %21, i64 %22)
br label %15
24: ; preds = %15
%25 = load i32*, i32** %6
%26 = call i32 @fclose(i32* %25)
%27 = load i32, i32* @SHA_DIGEST_LENGTH
%28 = zext i32 %27 to i64
%29 = call i8* @llvm.stacksave()
store i8* %29, i8** %9
%30 = alloca i8, i64 %28
store i64 %28, i64* %10
%31 = call i32 @SHA1_Final(i8* %30, i32* %8)
store i32 0, i32* %11
br label %32
32: ; preds = %47, %24
%33 = load i32, i32* %11
%34 = load i32, i32* @SHA_DIGEST_LENGTH
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %50
36: ; preds = %32
%37 = load i8*, i8** %4
%38 = load i32, i32* %11
%39 = mul nsw i32 %38, 2
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
%42 = load i32, i32* %11
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %30, i64 %43
%45 = load i8, i8* %44
%46 = call i32 @sprintf(i8* %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %45)
br label %47
47: ; preds = %36
%48 = load i32, i32* %11
%49 = add nsw i32 %48, 1
store i32 %49, i32* %11
br label %32
50: ; preds = %32
%51 = load i8*, i8** %4
%52 = load i32, i32* @SHA_DIGEST_LENGTH
%53 = mul nsw i32 %52, 2
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %51, i64 %54
store i8 0, i8* %55
%56 = load i8*, i8** %9
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SHA_DIGEST_LENGTH = global i32 0
@.str.1 = [5 x i8] c"%02x\00"
define void @hashFile(i8* nocapture %0, i8* nocapture writeonly %1) {
%3 = alloca [4096 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = bitcast i32* %4 to i8*
%8 = call i32 @SHA1_Init(i32* %4)
%9 = call i64 @fread(i8* %5, i32 1, i32 4096, i32* %6)
%10 = icmp eq i64 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %2, %11
%12 = phi i64 [ %14, %11 ], [ %9, %2 ]
%13 = call i32 @SHA1_Update(i32* %4, i8* %5, i64 %12)
%14 = call i64 @fread(i8* %5, i32 1, i32 4096, i32* %6)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %11
16: ; preds = %11, %2
%17 = call i32 @fclose(i32* %6)
%18 = load i32, i32* @SHA_DIGEST_LENGTH
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
%21 = alloca i8, i64 %19
%22 = call i32 @SHA1_Final(i8* %21, i32* %4)
%23 = load i32, i32* @SHA_DIGEST_LENGTH
%24 = icmp sgt i32 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %30, %16
%26 = phi i32 [ %23, %16 ], [ %38, %30 ]
%27 = shl nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %1, i64 %28
store i8 0, i8* %29
ret void
30: ; preds = %16, %30
%31 = phi i64 [ %37, %30 ], [ 0, %16 ]
%32 = shl nuw nsw i64 %31, 1
%33 = getelementptr inbounds i8, i8* %1, i64 %32
%34 = getelementptr inbounds i8, i8* %21, i64 %31
%35 = load i8, i8* %34
%36 = call i32 @sprintf(i8* dereferenceable(1) %33, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %35)
%37 = add nuw nsw i64 %31, 1
%38 = load i32, i32* @SHA_DIGEST_LENGTH
%39 = sext i32 %38 to i64
%40 = icmp slt i64 %37, %39
br i1 %40, label %30, label %25
}