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

/* Variables and functions */
int MD5_CBLOCK;
int MD5_DIGEST_LENGTH;
int /*<<< orphan*/ MD5_Final(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ MD5_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ MD5_Update(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
char *md5sum_file(char *file_path) {
  FILE *file = fopen(file_path, "r");
  if (file != NULL) {
    MD5_CTX md5_ctxt;
    MD5_Init(&md5_ctxt);
    char buf[MD5_CBLOCK];
    ssize_t bytes;
    while ((bytes = fread(buf, 1, MD5_CBLOCK, file)) > 0) {
      MD5_Update(&md5_ctxt, buf, bytes);
    }
    unsigned char md5sum_bytes[MD5_DIGEST_LENGTH];
    MD5_Final(md5sum_bytes, &md5_ctxt);
    char *md5sum = (char *)malloc(33);
    for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
      sprintf(md5sum + (i * 2), "%02x", md5sum_bytes[i]);
    }
    fclose(file);
    return md5sum;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<md5sum_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12c4 <md5sum_file+0x144>
lea    -0x1c(%rbp),%rdi
call   1080 <MD5_Init@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x58(%rbp),%rdi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jle    121c <md5sum_file+0x9c>
mov    -0x58(%rbp),%rsi
mov    -0x34(%rbp),%edx
lea    -0x1c(%rbp),%rdi
call   10a0 <MD5_Update@plt>
jmp    11e0 <md5sum_file+0x60>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x60(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
lea    -0x1c(%rbp),%rsi
call   1030 <MD5_Final@plt>
mov    $0x21,%edi
call   1060 <malloc@plt>
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    0x2d77(%rip),%rcx
cmp    (%rcx),%eax
jge    12a7 <md5sum_file+0x127>
mov    -0x60(%rbp),%rax
mov    -0x48(%rbp),%rdi
mov    -0x4c(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rdi
movslq -0x4c(%rbp),%rcx
lea    0xd72(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1090 <sprintf@plt>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    125f <md5sum_file+0xdf>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
jmp    12cc <md5sum_file+0x14c>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<md5sum_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe68(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     12a4 <md5sum_file+0x124>
mov    %rax,%r14
lea    -0x2c(%rbp),%rdi
call   1080 <MD5_Init@plt>
mov    %rsp,-0x48(%rbp)
mov    0x2e13(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    1215 <md5sum_file+0x95>
lea    -0x2c(%rbp),%r15
xchg   %ax,%ax
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   10a0 <MD5_Update@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jg     11f0 <md5sum_file+0x70>
mov    %r14,-0x40(%rbp)
mov    0x2dc0(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    -0x2c(%rbp),%rsi
mov    %r13,%rdi
call   1030 <MD5_Final@plt>
mov    $0x21,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpl   $0x0,(%r14)
jle    1291 <md5sum_file+0x111>
xor    %r15d,%r15d
lea    0xda4(%rip),%r12
mov    -0x38(%rbp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl 0x0(%r13,%r15,1),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <sprintf@plt>
add    $0x1,%r15
movslq (%r14),%rax
add    $0x2,%rbx
cmp    %rax,%r15
jl     1270 <md5sum_file+0xf0>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x48(%rbp),%rsp
mov    -0x38(%rbp),%rax
jmp    12a6 <md5sum_file+0x126>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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