#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;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
scalar_t__ md5(char *, size_t);
int /*<<< orphan*/ memcpy(char *, char *, int);
int md5_from_file(char *filepath, char hash[33]) {
  char *output = NULL;
  FILE *fd = fopen(filepath, "r");
  if (fd != NULL) {
    fseek(fd, 0L, SEEK_END);
    size_t size = ftell(fd);
    fseek(fd, 0L, SEEK_SET);
    char *file_content = (char *)malloc(size);
    int count = 0;
    char last_read;
    size_t last_read_size = fread(&last_read, 1, 1, fd);
    while (last_read_size > 0) {
      file_content[count++] = last_read;
      last_read_size = fread(&last_read, 1, 1, fd);
    }
    output = (char *)md5(file_content, size);
    memcpy(hash, output, 33);
    free(output);
    free(file_content);
    fclose(fd);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<md5_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe4d(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12b6 <md5_from_file+0x126>
mov    -0x28(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x28(%rbp),%rcx
lea    -0x3d(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jbe    126c <md5_from_file+0xdc>
mov    -0x3d(%rbp),%dl
mov    -0x38(%rbp),%rax
mov    -0x3c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x3c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rcx
lea    -0x3d(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
mov    %rax,-0x48(%rbp)
jmp    122c <md5_from_file+0x9c>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10b0 <md5@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x21,%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12bd <md5_from_file+0x12d>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<md5_from_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
lea    0xe58(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1291 <md5_from_file+0x101>
mov    %rax,%rbx
mov    %rbp,0x10(%rsp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r13
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r13,%rdi
call   1080 <malloc@plt>
mov    %rax,%r15
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     1257 <md5_from_file+0xc7>
lea    0xf(%rsp),%rbp
mov    %r15,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl 0xf(%rsp),%eax
mov    %al,(%r12)
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
add    $0x1,%r12
test   %rax,%rax
jne    1230 <md5_from_file+0xa0>
mov    %r15,%rdi
mov    %r13,%rsi
call   10b0 <md5@plt>
mov    %rax,%rbp
mov    0x10(%rsp),%rdi
mov    %rax,%rsi
mov    $0x21,%edx
call   1070 <memcpy@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1297 <md5_from_file+0x107>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @md5_from_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8
%12 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %6
%13 = load i8*, i8** %4
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %57
17: ; preds = %2
%18 = load i32*, i32** %7
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i64 0, i32 %19)
%21 = load i32*, i32** %7
%22 = call i64 @ftell(i32* %21)
store i64 %22, i64* %8
%23 = load i32*, i32** %7
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @fseek(i32* %23, i64 0, i32 %24)
%26 = load i64, i64* %8
%27 = call i64 @malloc(i64 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %9
store i32 0, i32* %10
%29 = load i32*, i32** %7
%30 = call i64 @fread(i8* %11, i32 1, i32 1, i32* %29)
store i64 %30, i64* %12
br label %31
31: ; preds = %34, %17
%32 = load i64, i64* %12
%33 = icmp ugt i64 %32, 0
br i1 %33, label %34, label %43
34: ; preds = %31
%35 = load i8, i8* %11
%36 = load i8*, i8** %9
%37 = load i32, i32* %10
%38 = add nsw i32 %37, 1
store i32 %38, i32* %10
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 %35, i8* %40
%41 = load i32*, i32** %7
%42 = call i64 @fread(i8* %11, i32 1, i32 1, i32* %41)
store i64 %42, i64* %12
br label %31
43: ; preds = %31
%44 = load i8*, i8** %9
%45 = load i64, i64* %8
%46 = call i64 @md5(i8* %44, i64 %45)
%47 = inttoptr i64 %46 to i8*
store i8* %47, i8** %6
%48 = load i8*, i8** %5
%49 = load i8*, i8** %6
%50 = call i32 @memcpy(i8* %48, i8* %49, i32 33)
%51 = load i8*, i8** %6
%52 = call i32 @free(i8* %51)
%53 = load i8*, i8** %9
%54 = call i32 @free(i8* %53)
%55 = load i32*, i32** %7
%56 = call i32 @fclose(i32* %55)
store i32 0, i32* %3
br label %58
57: ; preds = %2
store i32 -1, i32* %3
br label %58
58: ; preds = %57, %43
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @md5_from_file(i8* nocapture %0, i8* %1) {
%3 = alloca i8
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %30, label %6
6: ; preds = %2
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %4, i64 0, i32 %7)
%9 = tail call i64 @ftell(i32* %4)
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @fseek(i32* %4, i64 0, i32 %10)
%12 = tail call i64 @malloc(i64 %9)
%13 = inttoptr i64 %12 to i8*
%14 = call i64 @fread(i8* %3, i32 1, i32 1, i32* %4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %6, %16
%17 = phi i64 [ %19, %16 ], [ 0, %6 ]
%18 = load i8, i8* %3
%19 = add nuw i64 %17, 1
%20 = getelementptr inbounds i8, i8* %13, i64 %17
store i8 %18, i8* %20
%21 = call i64 @fread(i8* %3, i32 1, i32 1, i32* %4)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %16
23: ; preds = %16, %6
%24 = tail call i64 @md5(i8* %13, i64 %9)
%25 = inttoptr i64 %24 to i8*
%26 = tail call i32 @memcpy(i8* %1, i8* %25, i32 33)
%27 = tail call i32 @free(i8* %25)
%28 = tail call i32 @free(i8* %13)
%29 = tail call i32 @fclose(i32* %4)
br label %30
30: ; preds = %2, %23
%31 = phi i32 [ 0, %23 ], [ -1, %2 ]
ret i32 %31
}