#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 const *, char *);
size_t fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ mbedtls_md5(unsigned char *, size_t, unsigned char *);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ str_raw2hex(char *, unsigned char *, int);
void file_md5sum(char dst[32], const char *path) {
  FILE *file;
  size_t len;
  unsigned char *data;
  unsigned char raw_md5sum[16];
  memset(dst, '\0', 32);
  file = fopen(path, "r");
  if (file) {
    fseek(file, 0, SEEK_END);
    len = ftell(file);
    fseek(file, 0, SEEK_SET);
    data = (unsigned char *)malloc(len);
    if (fread(data, 1, len, file) == len) {
      mbedtls_md5(data, len, raw_md5sum);
      str_raw2hex(dst, raw_md5sum, 16);
    }
    free(data);
    fclose(file);
  }
}


x86 assembly (clang, -O0):
<file_md5sum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1276 <file_md5sum+0xd6>
mov    -0x18(%rbp),%rdi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x20(%rbp),%rax
jne    1264 <file_md5sum+0xc4>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1090 <mbedtls_md5@plt>
mov    -0x8(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x10,%edx
call   10c0 <str_raw2hex@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_md5sum>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
lea    0xe3a(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     125f <file_md5sum+0xbf>
mov    %rax,%rbx
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r15
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r15,%rdi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %r15,%rax
jne    124f <file_md5sum+0xaf>
mov    %rsp,%r13
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1090 <mbedtls_md5@plt>
mov    %r14,%rdi
mov    %r13,%rsi
mov    $0x10,%edx
call   10c0 <str_raw2hex@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define void @file_md5sum(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca [16 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = call i32 @memset(i8* %9, i8 signext 0, i32 32)
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %5
%13 = load i32*, i32** %5
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %46
15: ; preds = %2
%16 = load i32*, i32** %5
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i32*, i32** %5
%20 = call i64 @ftell(i32* %19)
store i64 %20, i64* %6
%21 = load i32*, i32** %5
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i64, i64* %6
%25 = call i64 @malloc(i64 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = load i64, i64* %6
%29 = load i32*, i32** %5
%30 = call i64 @fread(i8* %27, i32 1, i64 %28, i32* %29)
%31 = load i64, i64* %6
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %41
33: ; preds = %15
%34 = load i8*, i8** %7
%35 = load i64, i64* %6
%36 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%37 = call i32 @mbedtls_md5(i8* %34, i64 %35, i8* %36)
%38 = load i8*, i8** %3
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%40 = call i32 @str_raw2hex(i8* %38, i8* %39, i32 16)
br label %41
41: ; preds = %33, %15
%42 = load i8*, i8** %7
%43 = call i32 @free(i8* %42)
%44 = load i32*, i32** %5
%45 = call i32 @fclose(i32* %44)
br label %46
46: ; preds = %41, %2
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define void @file_md5sum(i8* %0, i8* nocapture %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @memset(i8* %0, i8 signext 0, i32 32)
%6 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %24, label %8
8: ; preds = %2
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %6, i32 0, i32 %9)
%11 = tail call i64 @ftell(i32* %6)
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @fseek(i32* %6, i32 0, i32 %12)
%14 = tail call i64 @malloc(i64 %11)
%15 = inttoptr i64 %14 to i8*
%16 = tail call i64 @fread(i8* %15, i32 1, i64 %11, i32* %6)
%17 = icmp eq i64 %16, %11
br i1 %17, label %18, label %21
18: ; preds = %8
%19 = call i32 @mbedtls_md5(i8* %15, i64 %11, i8* %4)
%20 = call i32 @str_raw2hex(i8* %0, i8* %4, i32 16)
br label %21
21: ; preds = %18, %8
%22 = call i32 @free(i8* %15)
%23 = call i32 @fclose(i32* %6)
br label %24
24: ; preds = %21, %2
ret void
}