#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
char *md5(const char *filename) {
  FILE *in_file = fopen(filename, "rb");
  if (!in_file) {
    printf("%s can't be opened.\n", filename);
    return NULL;
  }
  MD5_CTX md_context;
  MD5_Init(&md_context);
  int bytes;
  unsigned char data[1024];
  while ((bytes = fread(data, 1, 1024, in_file)) != 0) {
    MD5_Update(&md_context, data, bytes);
  }
  unsigned char c[MD5_DIGEST_LENGTH];
  MD5_Final(c, &md_context);
  char tmp[2];
  char *hash = malloc((MD5_DIGEST_LENGTH * 2) + 1);
  for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
    sprintf(tmp, "%02x", c[i]);
    memcpy(hash + (i * 2), tmp, 2);
  }
  hash[MD5_DIGEST_LENGTH * 2] = '\0';
  fclose(in_file);
  return hash;
}


x86 assembly (clang, -O0):
<md5>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11eb <md5+0x4b>
mov    -0x10(%rbp),%rsi
lea    0xe2a(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1341 <md5+0x1a1>
lea    -0x1c(%rbp),%rdi
call   10a0 <MD5_Init@plt>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     1232 <md5+0x92>
lea    -0x420(%rbp),%rsi
mov    -0x20(%rbp),%edx
lea    -0x1c(%rbp),%rdi
call   10c0 <MD5_Update@plt>
jmp    11f4 <md5+0x54>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x428(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x450(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x430(%rbp)
lea    -0x1c(%rbp),%rsi
call   1030 <MD5_Final@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
shl    $0x1,%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x440(%rbp)
movl   $0x0,-0x444(%rbp)
mov    -0x444(%rbp),%eax
mov    0x2d3e(%rip),%rcx
cmp    (%rcx),%eax
jge    1309 <md5+0x169>
mov    -0x450(%rbp),%rax
lea    -0x432(%rbp),%rdi
movslq -0x444(%rbp),%rcx
lea    0xd52(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   10b0 <sprintf@plt>
mov    -0x440(%rbp),%rdi
mov    -0x444(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
lea    -0x432(%rbp),%rsi
mov    $0x2,%edx
call   1070 <memcpy@plt>
mov    -0x444(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x444(%rbp)
jmp    1295 <md5+0xf5>
mov    -0x440(%rbp),%rax
mov    0x2cc9(%rip),%rcx
mov    (%rcx),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x440(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x428(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<md5>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rdi,%rbx
lea    0xe42(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12cd <md5+0x12d>
mov    %rax,%r15
lea    -0x2c(%rbp),%rdi
call   10a0 <MD5_Init@plt>
lea    -0x450(%rbp),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %eax,%eax
je     1226 <md5+0x86>
lea    -0x2c(%rbp),%r14
lea    -0x450(%rbp),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   10c0 <MD5_Update@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jne    1200 <md5+0x60>
mov    %rsp,%rbx
mov    0x2db0(%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    (%r14),%r12d
lea    (%r12,%r12,1),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
test   %r12d,%r12d
jle    12e1 <md5+0x141>
mov    %rbx,-0x40(%rbp)
mov    %r15,-0x48(%rbp)
xor    %r15d,%r15d
lea    -0x2e(%rbp),%rbx
mov    %r14,-0x38(%rbp)
nopw   0x0(%rax,%rax,1)
movzbl 0x0(%r13,%r15,1),%edx
mov    %rbx,%rdi
lea    0xd88(%rip),%rsi
call   10b0 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1070 <memcpy@plt>
add    $0x1,%r15
mov    0x2d30(%rip),%rax
movslq (%rax),%r12
add    $0x2,%r14
cmp    %r12,%r15
jl     1280 <md5+0xe0>
add    %r12d,%r12d
mov    -0x48(%rbp),%r15
mov    -0x40(%rbp),%rbx
mov    -0x38(%rbp),%r14
jmp    12e4 <md5+0x144>
lea    0xd2f(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
xor    %r14d,%r14d
jmp    12f7 <md5+0x157>
add    %r12d,%r12d
movslq %r12d,%rax
movb   $0x0,(%r14,%rax,1)
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rsp
mov    %r14,%rax
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"
@.str.1 = [21 x i8] c"%s can't be opened.\0A\00"
@MD5_DIGEST_LENGTH = global i32 0
@.str.2 = [5 x i8] c"%02x\00"
define i8* @md5(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca i8*
%9 = alloca i64
%10 = alloca [2 x i8]
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %3
%13 = load i8*, i8** %3
%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 ne i32* %15, null
br i1 %16, label %20, label %17
17: ; preds = %1
%18 = load i8*, i8** %3
%19 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i8* null, i8** %2
br label %72
20: ; preds = %1
%21 = call i32 @MD5_Init(i32* %5)
br label %22
22: ; preds = %27, %20
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%24 = load i32*, i32** %4
%25 = call i32 @fread(i8* %23, i32 1, i32 1024, i32* %24)
store i32 %25, i32* %6
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%29 = load i32, i32* %6
%30 = call i32 @MD5_Update(i32* %5, i8* %28, i32 %29)
br label %22
31: ; preds = %22
%32 = load i32, i32* @MD5_DIGEST_LENGTH
%33 = zext i32 %32 to i64
%34 = call i8* @llvm.stacksave()
store i8* %34, i8** %8
%35 = alloca i8, i64 %33
store i64 %33, i64* %9
%36 = call i32 @MD5_Final(i8* %35, i32* %5)
%37 = load i32, i32* @MD5_DIGEST_LENGTH
%38 = mul nsw i32 %37, 2
%39 = add nsw i32 %38, 1
%40 = call i8* @malloc(i32 %39)
store i8* %40, i8** %11
store i32 0, i32* %12
br label %41
41: ; preds = %59, %31
%42 = load i32, i32* %12
%43 = load i32, i32* @MD5_DIGEST_LENGTH
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %62
45: ; preds = %41
%46 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%47 = load i32, i32* %12
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %35, i64 %48
%50 = load i8, i8* %49
%51 = call i32 @sprintf(i8* %46, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %50)
%52 = load i8*, i8** %11
%53 = load i32, i32* %12
%54 = mul nsw i32 %53, 2
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %52, i64 %55
%57 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%58 = call i32 @memcpy(i8* %56, i8* %57, i32 2)
br label %59
59: ; preds = %45
%60 = load i32, i32* %12
%61 = add nsw i32 %60, 1
store i32 %61, i32* %12
br label %41
62: ; preds = %41
%63 = load i8*, i8** %11
%64 = load i32, i32* @MD5_DIGEST_LENGTH
%65 = mul nsw i32 %64, 2
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %63, i64 %66
store i8 0, i8* %67
%68 = load i32*, i32** %4
%69 = call i32 @fclose(i32* %68)
%70 = load i8*, i8** %11
store i8* %70, i8** %2
%71 = load i8*, i8** %8
br label %72
72: ; preds = %62, %17
%73 = load i8*, i8** %2
ret i8* %73
}

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