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

/* Variables and functions */
int /*<<< orphan*/ EVP_DigestFinal_ex(int /*<<< orphan*/ *, unsigned char *,
                                      unsigned int *);
int /*<<< orphan*/ EVP_DigestInit_ex(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ const *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_MD_CTX_create();
int /*<<< orphan*/ EVP_MD_CTX_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_sha1();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ stderr;
scalar_t__ update_hash(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int hash_file(const char *filepath, unsigned char *digest) {
  EVP_MD_CTX *mdctx;
  const EVP_MD *md;
  FILE *file;
  int success;
  unsigned hash_len;
  file = fopen(filepath, "r");
  if (!file) {
    fprintf(stderr, "Error: couldn't open file '%s' for reading\n", filepath);
    return 0;
  }
  md = EVP_sha1();
  mdctx = EVP_MD_CTX_create();
  EVP_DigestInit_ex(mdctx, md, NULL);
  while (update_hash(file, mdctx))
    ;
  if (feof(file)) {
    success = 1;
  } else if (ferror(file)) {
    success = 0;
    fprintf(stderr, "Error in reading file '%s'\n", filepath);
  } else {
    success = 0;
    fprintf(stderr, "Error in updating the file '%s' digest\n", filepath);
  }
  fclose(file);
  if (success > 0) {
    EVP_DigestFinal_ex(mdctx, digest, &hash_len);
  }
  EVP_MD_CTX_destroy(mdctx);
  return success;
}


x86 assembly (clang, -O0):
<hash_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1204 <hash_file+0x54>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe0f(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f6 <hash_file+0x146>
mov    $0x0,%al
call   1070 <EVP_sha1@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   10d0 <EVP_MD_CTX_create@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <EVP_DigestInit_ex@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <update_hash@plt>
cmp    $0x0,%rax
je     1247 <hash_file+0x97>
jmp    122b <hash_file+0x7b>
mov    -0x30(%rbp),%rdi
call   10a0 <feof@plt>
cmp    $0x0,%rax
je     1266 <hash_file+0xb6>
movl   $0x1,-0x34(%rbp)
jmp    12c3 <hash_file+0x113>
mov    -0x30(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     129e <hash_file+0xee>
movl   $0x0,-0x34(%rbp)
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd9a(%rip),%rsi
call   1090 <fprintf@plt>
jmp    12be <hash_file+0x10e>
movl   $0x0,-0x34(%rbp)
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd91(%rip),%rsi
call   1090 <fprintf@plt>
jmp    12c3 <hash_file+0x113>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x0,-0x34(%rbp)
jle    12e7 <hash_file+0x137>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1040 <EVP_DigestFinal_ex@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <EVP_MD_CTX_destroy@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hash_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xe36(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     123c <hash_file+0x8c>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <EVP_sha1@plt>
mov    %rax,%r13
xor    %eax,%eax
call   10d0 <EVP_MD_CTX_create@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r13,%rsi
xor    %edx,%edx
call   1080 <EVP_DigestInit_ex@plt>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <update_hash@plt>
test   %rax,%rax
jne    1200 <hash_file+0x50>
mov    %rbx,%rdi
call   10a0 <feof@plt>
test   %rax,%rax
je     1258 <hash_file+0xa8>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xc(%rsp),%rdx
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <EVP_DigestFinal_ex@plt>
mov    $0x1,%ebx
jmp    1290 <hash_file+0xe0>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
mov    %r14,%rdx
call   1090 <fprintf@plt>
xor    %ebx,%ebx
jmp    1298 <hash_file+0xe8>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rax
lea    0xdb4(%rip),%rsi
cmove  %rax,%rsi
mov    %r14,%rdx
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %ebx,%ebx
mov    %r15,%rdi
call   10b0 <EVP_MD_CTX_destroy@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [44 x i8] c"Error: couldn't open file '%s' for reading\0A\00"
@.str.2 = [28 x i8] c"Error in reading file '%s'\0A\00"
@.str.3 = [40 x i8] c"Error in updating the file '%s' digest\0A\00"
define i32 @hash_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %8
%13 = load i32*, i32** %8
%14 = icmp ne i32* %13, null
br i1 %14, label %19, label %15
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %4
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %17)
store i32 0, i32* %3
br label %62
19: ; preds = %2
%20 = call i32* (...) @EVP_sha1()
store i32* %20, i32** %7
%21 = call i32* (...) @EVP_MD_CTX_create()
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = load i32*, i32** %7
%24 = call i32 @EVP_DigestInit_ex(i32* %22, i32* %23, i32* null)
br label %25
25: ; preds = %30, %19
%26 = load i32*, i32** %8
%27 = load i32*, i32** %6
%28 = call i64 @update_hash(i32* %26, i32* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %25
br label %25
31: ; preds = %25
%32 = load i32*, i32** %8
%33 = call i64 @feof(i32* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 1, i32* %9
br label %49
36: ; preds = %31
%37 = load i32*, i32** %8
%38 = call i64 @ferror(i32* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %36
store i32 0, i32* %9
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %4
%43 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %42)
br label %48
44: ; preds = %36
store i32 0, i32* %9
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %4
%47 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %44, %40
br label %49
49: ; preds = %48, %35
%50 = load i32*, i32** %8
%51 = call i32 @fclose(i32* %50)
%52 = load i32, i32* %9
%53 = icmp sgt i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %49
%55 = load i32*, i32** %6
%56 = load i8*, i8** %5
%57 = call i32 @EVP_DigestFinal_ex(i32* %55, i8* %56, i32* %10)
br label %58
58: ; preds = %54, %49
%59 = load i32*, i32** %6
%60 = call i32 @EVP_MD_CTX_destroy(i32* %59)
%61 = load i32, i32* %9
store i32 %61, i32* %3
br label %62
62: ; preds = %58, %15
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [44 x i8] c"Error: couldn't open file '%s' for reading\0A\00"
@.str.2 = [28 x i8] c"Error in reading file '%s'\0A\00"
@.str.3 = [40 x i8] c"Error in updating the file '%s' digest\0A\00"
define i32 @hash_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %33
10: ; preds = %2
%11 = tail call i32* (...) @EVP_sha1()
%12 = tail call i32* (...) @EVP_MD_CTX_create()
%13 = tail call i32 @EVP_DigestInit_ex(i32* %12, i32* %11, i32* null)
br label %14
14: ; preds = %14, %10
%15 = tail call i64 @update_hash(i32* %5, i32* %12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %14
17: ; preds = %14
%18 = tail call i64 @feof(i32* %5)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %17
%21 = tail call i64 @ferror(i32* %5)
%22 = icmp eq i64 %21, 0
%23 = load i32, i32* @stderr
%24 = select i1 %22, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0)
%25 = tail call i32 @fprintf(i32 %23, i8* %24, i8* %0)
%26 = tail call i32 @fclose(i32* %5)
br label %30
27: ; preds = %17
%28 = tail call i32 @fclose(i32* %5)
%29 = call i32 @EVP_DigestFinal_ex(i32* %12, i8* %1, i32* %3)
br label %30
30: ; preds = %20, %27
%31 = phi i32 [ 0, %20 ], [ 1, %27 ]
%32 = call i32 @EVP_MD_CTX_destroy(i32* %12)
br label %33
33: ; preds = %30, %7
%34 = phi i32 [ %31, %30 ], [ 0, %7 ]
ret i32 %34
}