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

/* Variables and functions */
int MD5_DIGEST_LENGTH;
int /*<<< orphan*/ MD5_Final(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ MD5_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ MD5_Update(int /*<<< orphan*/ *, unsigned char *, int);
int /*<<< orphan*/ errx(int, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ save_file(char *, char *, int);
char *create_hash(char *path) {
  char *md5 = malloc(sizeof(char) * MD5_DIGEST_LENGTH);
  FILE *file = fopen(path, "rb");
  MD5_CTX mdContext;
  int bytes;
  unsigned char data[4096];
  if (file == NULL) {
    errx(-1, "The file can't be opened");
  }
  MD5_Init(&mdContext);
  while ((bytes = fread(data, 1, 4096, file)) != 0) {
    MD5_Update(&mdContext, data, bytes);
  }
  MD5_Final((unsigned char *)md5, &mdContext);
  save_file(md5, "res/hash_orig", MD5_DIGEST_LENGTH);
  fclose(file);
  return md5;
}


x86 assembly (clang, -O0):
<create_hash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e3a(%rip),%rax
movslq (%rax),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e8 <create_hash+0x58>
mov    $0xffffffff,%edi
lea    0xe20(%rip),%rsi
call   1060 <errx@plt>
lea    -0x1c(%rbp),%rdi
call   10a0 <MD5_Init@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     122f <create_hash+0x9f>
lea    -0x1020(%rbp),%rsi
mov    -0x20(%rbp),%edx
lea    -0x1c(%rbp),%rdi
call   10b0 <MD5_Update@plt>
jmp    11f1 <create_hash+0x61>
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <MD5_Final@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
lea    0xdcc(%rip),%rsi
call   1090 <save_file@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_hash>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1010,%rsp
mov    %rdi,%rbx
mov    0x2e36(%rip),%r13
mov    0x0(%r13),%edi
call   1070 <malloc@plt>
mov    %rax,%r14
lea    0xe43(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11de <create_hash+0x4e>
lea    0xe2f(%rip),%rsi
mov    $0xffffffff,%edi
call   1060 <errx@plt>
lea    0xc(%rsp),%rdi
call   10a0 <MD5_Init@plt>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
test   %eax,%eax
je     1236 <create_hash+0xa6>
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%rbx
nopl   (%rax)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   10b0 <MD5_Update@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jne    1210 <create_hash+0x80>
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
call   1030 <MD5_Final@plt>
mov    0x0(%r13),%edx
lea    0xdce(%rip),%rsi
mov    %r14,%rdi
call   1090 <save_file@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r14,%rax
add    $0x1010,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@MD5_DIGEST_LENGTH = global i32 0
@.str = [3 x i8] c"rb\00"
@.str.1 = [25 x i8] c"The file can't be opened\00"
@.str.2 = [14 x i8] c"res/hash_orig\00"
define i8* @create_hash(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [4096 x i8]
store i8* %0, i8** %2
%8 = load i32, i32* @MD5_DIGEST_LENGTH
%9 = sext i32 %8 to i64
%10 = mul i64 1, %9
%11 = trunc i64 %10 to i32
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %3
%13 = load i8*, i8** %2
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %4
%15 = load i32*, i32** %4
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = call i32 @errx(i32 -1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %1
%20 = call i32 @MD5_Init(i32* %5)
br label %21
21: ; preds = %26, %19
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%23 = load i32*, i32** %4
%24 = call i32 @fread(i8* %22, i32 1, i32 4096, i32* %23)
store i32 %24, i32* %6
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%28 = load i32, i32* %6
%29 = call i32 @MD5_Update(i32* %5, i8* %27, i32 %28)
br label %21
30: ; preds = %21
%31 = load i8*, i8** %3
%32 = call i32 @MD5_Final(i8* %31, i32* %5)
%33 = load i8*, i8** %3
%34 = load i32, i32* @MD5_DIGEST_LENGTH
%35 = call i32 @save_file(i8* %33, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = load i32*, i32** %4
%37 = call i32 @fclose(i32* %36)
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
@MD5_DIGEST_LENGTH = global i32 0
@.str = [3 x i8] c"rb\00"
@.str.1 = [25 x i8] c"The file can't be opened\00"
@.str.2 = [14 x i8] c"res/hash_orig\00"
define i8* @create_hash(i8* nocapture %0) {
%2 = alloca i32
%3 = alloca [4096 x i8]
%4 = load i32, i32* @MD5_DIGEST_LENGTH
%5 = tail call i8* @malloc(i32 %4)
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = bitcast i32* %2 to i8*
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%9 = icmp eq i32* %6, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 @errx(i32 -1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
%13 = call i32 @MD5_Init(i32* %2)
%14 = call i32 @fread(i8* %8, i32 1, i32 4096, i32* %6)
%15 = icmp eq i32 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %12, %16
%17 = phi i32 [ %19, %16 ], [ %14, %12 ]
%18 = call i32 @MD5_Update(i32* %2, i8* %8, i32 %17)
%19 = call i32 @fread(i8* %8, i32 1, i32 4096, i32* %6)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %16
21: ; preds = %16, %12
%22 = call i32 @MD5_Final(i8* %5, i32* %2)
%23 = load i32, i32* @MD5_DIGEST_LENGTH
%24 = call i32 @save_file(i8* %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = call i32 @fclose(i32* %6)
ret i8* %5
}