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


x86 assembly (clang, -O0):
<getMD5>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x450(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1208 <getMD5+0x88>
mov    -0x10(%rbp),%rsi
lea    0xe17(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x444(%rbp)
jmp    12cc <getMD5+0x14c>
lea    -0x3c(%rbp),%rdi
call   1080 <MD5_Init@plt>
lea    -0x440(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
mov    %eax,-0x40(%rbp)
cmp    $0x0,%eax
je     124f <getMD5+0xcf>
lea    -0x440(%rbp),%rsi
mov    -0x40(%rbp),%edx
lea    -0x3c(%rbp),%rdi
call   10a0 <MD5_Update@plt>
jmp    1211 <getMD5+0x91>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x450(%rbp),%rdi
lea    -0x3c(%rbp),%rsi
call   1030 <MD5_Final@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2d67(%rip),%rcx
cmp    (%rcx),%eax
jge    12ba <getMD5+0x13a>
mov    -0x450(%rbp),%rax
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rdi
movslq -0x2c(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xd71(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    126f <getMD5+0xef>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x444(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getMD5>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%rbx
mov    %rdi,%r12
mov    0x2e3f(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    0xe44(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1286 <getMD5+0x106>
mov    %rax,%r13
mov    %rbx,-0x38(%rbp)
lea    -0x2c(%rbp),%rdi
call   1080 <MD5_Init@plt>
lea    -0x440(%rbp),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
test   %eax,%eax
je     1236 <getMD5+0xb6>
lea    -0x2c(%rbp),%r12
lea    -0x440(%rbp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   10a0 <MD5_Update@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jne    1210 <getMD5+0x90>
mov    %r13,%rdi
call   1050 <fclose@plt>
lea    -0x2c(%rbp),%rsi
mov    %r15,%rdi
call   1030 <MD5_Final@plt>
cmpl   $0x0,(%r14)
jle    1280 <getMD5+0x100>
xor    %ebx,%ebx
lea    0xdbf(%rip),%r12
mov    -0x38(%rbp),%r13
nopl   (%rax)
movzbl (%r15,%rbx,1),%edx
mov    %r13,%rdi
mov    %r12,%rsi
call   1090 <sprintf@plt>
add    $0x1,%rbx
movslq (%r14),%rax
add    $0x2,%r13
cmp    %rax,%rbx
jl     1260 <getMD5+0xe0>
mov    -0x38(%rbp),%rax
jmp    1297 <getMD5+0x117>
lea    0xd76(%rip),%rdi
mov    %r12,%rsi
call   1060 <printf@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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