#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 scalar_t__ ssize_t;
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*/ *, char *, scalar_t__);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int /*<<< orphan*/ stdout;
int calculate_md5sum(const char *filename, char *digest) {
  FILE *file_ptr;
  file_ptr = fopen(filename, "r");
  if (file_ptr == NULL) {
    perror("Error opening file");
    fflush(stdout);
    return 1;
  }
  int n;
  MD5_CTX c;
  char buf[512];
  ssize_t bytes;
  unsigned char out[MD5_DIGEST_LENGTH];
  MD5_Init(&c);
  do {
    bytes = fread(buf, 1, 512, file_ptr);
    MD5_Update(&c, buf, bytes);
  } while (bytes > 0);
  MD5_Final(out, &c);
  for (n = 0; n < MD5_DIGEST_LENGTH; n++) {
    char hex[3] = {0};
    sprintf(hex, "%02x", out[n]);
    digest[n * 2] = hex[0];
    digest[n * 2 + 1] = hex[1];
  }
  fclose(file_ptr);
  return 0;
}


x86 assembly (clang, -O0):
<calculate_md5sum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe42(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f8 <calculate_md5sum+0x58>
lea    0xe29(%rip),%rdi
call   1090 <perror@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
movl   $0x1,-0x4(%rbp)
jmp    1331 <calculate_md5sum+0x191>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x240(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x258(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x248(%rbp)
lea    -0x28(%rbp),%rdi
call   10a0 <MD5_Init@plt>
lea    -0x230(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1040 <fread@plt>
mov    %rax,-0x238(%rbp)
lea    -0x230(%rbp),%rsi
mov    -0x238(%rbp),%rdx
lea    -0x28(%rbp),%rdi
call   10c0 <MD5_Update@plt>
cmpq   $0x0,-0x238(%rbp)
jg     1236 <calculate_md5sum+0x96>
mov    -0x258(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <MD5_Final@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2d43(%rip),%rcx
cmp    (%rcx),%eax
jge    1317 <calculate_md5sum+0x177>
lea    -0x24b(%rbp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1060 <memset@plt>
mov    -0x258(%rbp),%rax
lea    -0x24b(%rbp),%rdi
movslq -0x24(%rbp),%rcx
lea    0xd44(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   10b0 <sprintf@plt>
mov    -0x24b(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24a(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1293 <calculate_md5sum+0xf3>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x240(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_md5sum>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%r13
lea    0xe52(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     12b0 <calculate_md5sum+0x120>
mov    %rax,%r14
mov    %rsp,-0x40(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
lea    -0x30(%rbp),%r12
mov    %r12,%rdi
call   1090 <MD5_Init@plt>
lea    -0x240(%rbp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
mov    %rax,%r15
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   10b0 <MD5_Update@plt>
test   %r15,%r15
jg     1200 <calculate_md5sum+0x70>
lea    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%r12
mov    %r12,%rdi
call   1030 <MD5_Final@plt>
mov    0x2d9e(%rip),%rax
cmpl   $0x0,(%rax)
jle    1293 <calculate_md5sum+0x103>
xor    %ebx,%ebx
lea    -0x2c(%rbp),%r15
nopl   (%rax)
movb   $0x0,-0x2a(%rbp)
movw   $0x0,-0x2c(%rbp)
movzbl (%r12,%rbx,1),%edx
mov    %r15,%rdi
lea    0xdac(%rip),%rsi
call   10a0 <sprintf@plt>
movzbl -0x2c(%rbp),%eax
mov    %al,0x0(%r13,%rbx,2)
movzbl -0x2b(%rbp),%eax
mov    %al,0x1(%r13,%rbx,2)
add    $0x1,%rbx
mov    0x2d55(%rip),%rax
movslq (%rax),%rax
cmp    %rax,%rbx
jl     1250 <calculate_md5sum+0xc0>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd4b(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0x1,%eax
jmp    12a1 <calculate_md5sum+0x111>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"Error opening file\00"
@stdout = global i32 0
@MD5_DIGEST_LENGTH = global i32 0
@.str.2 = [5 x i8] c"%02x\00"
define i32 @calculate_md5sum(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [512 x i8]
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i64
%13 = alloca [3 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%14 = load i8*, i8** %4
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %6
%16 = load i32*, i32** %6
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @stdout
%21 = call i32 @fflush(i32 %20)
store i32 1, i32* %3
br label %74
22: ; preds = %2
%23 = load i32, i32* @MD5_DIGEST_LENGTH
%24 = zext i32 %23 to i64
%25 = call i8* @llvm.stacksave()
store i8* %25, i8** %11
%26 = alloca i8, i64 %24
store i64 %24, i64* %12
%27 = call i32 @MD5_Init(i32* %8)
br label %28
28: ; preds = %35, %22
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%30 = load i32*, i32** %6
%31 = call i64 @fread(i8* %29, i32 1, i32 512, i32* %30)
store i64 %31, i64* %10
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%33 = load i64, i64* %10
%34 = call i32 @MD5_Update(i32* %8, i8* %32, i64 %33)
br label %35
35: ; preds = %28
%36 = load i64, i64* %10
%37 = icmp sgt i64 %36, 0
br i1 %37, label %28, label %38
38: ; preds = %35
%39 = call i32 @MD5_Final(i8* %26, i32* %8)
store i32 0, i32* %7
br label %40
40: ; preds = %67, %38
%41 = load i32, i32* %7
%42 = load i32, i32* @MD5_DIGEST_LENGTH
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %70
44: ; preds = %40
%45 = bitcast [3 x i8]* %13 to i8*
%46 = getelementptr inbounds [3 x i8], [3 x i8]* %13, i64 0, i64 0
%47 = load i32, i32* %7
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %26, 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 = getelementptr inbounds [3 x i8], [3 x i8]* %13, i64 0, i64 0
%53 = load i8, i8* %52
%54 = load i8*, i8** %5
%55 = load i32, i32* %7
%56 = mul nsw i32 %55, 2
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %54, i64 %57
store i8 %53, i8* %58
%59 = getelementptr inbounds [3 x i8], [3 x i8]* %13, i64 0, i64 1
%60 = load i8, i8* %59
%61 = load i8*, i8** %5
%62 = load i32, i32* %7
%63 = mul nsw i32 %62, 2
%64 = add nsw i32 %63, 1
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %61, i64 %65
store i8 %60, i8* %66
br label %67
67: ; preds = %44
%68 = load i32, i32* %7
%69 = add nsw i32 %68, 1
store i32 %69, i32* %7
br label %40
70: ; preds = %40
%71 = load i32*, i32** %6
%72 = call i32 @fclose(i32* %71)
store i32 0, i32* %3
%73 = load i8*, i8** %11
br label %74
74: ; preds = %70, %18
%75 = load i32, i32* %3
ret i32 %75
}

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