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

/* Variables and functions */
size_t 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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
unsigned char *malloc(size_t);
int open(char const *, int /*<<< orphan*/);
int read(int, unsigned char *, int);
int mhash_with_size(const char *fpath, off_t fsize, unsigned char **mhash,
                    size_t *mhash_size) {
  MD5_CTX ctx;
  unsigned char buf[2048];
  off_t na = fsize;
  int fd;
  fd = open(fpath, O_RDONLY);
  if (fd == -1) {
    *mhash = NULL;
    *mhash_size = 0;
    return -1;
  }
  MD5_Init(&ctx);
  *mhash_size = MD5_DIGEST_LENGTH;
  *mhash = malloc(*mhash_size);
  while (na > 0) {
    int n, d;
    d = (na < sizeof(buf) ? na : sizeof(buf));
    n = read(fd, buf, d);
    if (n <= 0) {
      break;
    }
    MD5_Update(&ctx, buf, n);
    na -= n;
  }
  MD5_Final(*mhash, &ctx);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<mhash_with_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x834(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x838(%rbp)
cmpl   $0xffffffff,-0x838(%rbp)
jne    11da <mhash_with_size+0x6a>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <mhash_with_size+0x16c>
lea    -0x2c(%rbp),%rdi
call   1080 <MD5_Init@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
cmpl   $0x0,-0x834(%rbp)
jle    12ba <mhash_with_size+0x14a>
movslq -0x834(%rbp),%rax
cmp    $0x800,%rax
jae    123d <mhash_with_size+0xcd>
movslq -0x834(%rbp),%rax
mov    %rax,-0x848(%rbp)
jmp    124e <mhash_with_size+0xde>
mov    $0x800,%eax
mov    %rax,-0x848(%rbp)
jmp    124e <mhash_with_size+0xde>
mov    -0x848(%rbp),%rax
mov    %eax,-0x840(%rbp)
mov    -0x838(%rbp),%edi
lea    -0x830(%rbp),%rsi
mov    -0x840(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x83c(%rbp)
cmpl   $0x0,-0x83c(%rbp)
jg     128b <mhash_with_size+0x11b>
jmp    12ba <mhash_with_size+0x14a>
lea    -0x830(%rbp),%rsi
mov    -0x83c(%rbp),%edx
lea    -0x2c(%rbp),%rdi
call   1090 <MD5_Update@plt>
mov    -0x83c(%rbp),%ecx
mov    -0x834(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x834(%rbp)
jmp    120a <mhash_with_size+0x9a>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    -0x2c(%rbp),%rsi
call   1030 <MD5_Final@plt>
mov    -0x838(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mhash_with_size>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     121c <mhash_with_size+0xac>
mov    %eax,%r15d
lea    0xc(%rsp),%rdi
call   1080 <MD5_Init@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%rdi
mov    %rdi,(%rbx)
call   1060 <malloc@plt>
mov    %rax,(%r14)
test   %ebp,%ebp
jle    1203 <mhash_with_size+0x93>
lea    0x10(%rsp),%r13
lea    0xc(%rsp),%r12
cmp    $0x800,%ebp
mov    $0x800,%edx
cmovb  %ebp,%edx
mov    %r15d,%edi
mov    %r13,%rsi
call   1050 <read@plt>
test   %eax,%eax
jle    1200 <mhash_with_size+0x90>
mov    %eax,%ebx
mov    %r12,%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1090 <MD5_Update@plt>
sub    %ebx,%ebp
jg     11d0 <mhash_with_size+0x60>
mov    (%r14),%rax
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
call   1030 <MD5_Final@plt>
mov    %r15d,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    122f <mhash_with_size+0xbf>
movq   $0x0,(%r14)
movq   $0x0,(%rbx)
mov    $0xffffffff,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@MD5_DIGEST_LENGTH = global i64 0
define i32 @mhash_with_size(i8* %0, i32 %1, i8** %2, i64* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i64*
%10 = alloca i32
%11 = alloca [2048 x i8]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8** %2, i8*** %8
store i64* %3, i64** %9
%16 = load i32, i32* %7
store i32 %16, i32* %12
%17 = load i8*, i8** %6
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %13
%20 = load i32, i32* %13
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %4
%23 = load i8**, i8*** %8
store i8* null, i8** %23
%24 = load i64*, i64** %9
store i64 0, i64* %24
store i32 -1, i32* %5
br label %67
25: ; preds = %4
%26 = call i32 @MD5_Init(i32* %10)
%27 = load i64, i64* @MD5_DIGEST_LENGTH
%28 = load i64*, i64** %9
store i64 %27, i64* %28
%29 = load i64*, i64** %9
%30 = load i64, i64* %29
%31 = call i8* @malloc(i64 %30)
%32 = load i8**, i8*** %8
store i8* %31, i8** %32
br label %33
33: ; preds = %54, %25
%34 = load i32, i32* %12
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %61
36: ; preds = %33
%37 = load i32, i32* %12
%38 = sext i32 %37 to i64
%39 = icmp ult i64 %38, 2048
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i32, i32* %12
%42 = sext i32 %41 to i64
br label %44
43: ; preds = %36
br label %44
44: ; preds = %43, %40
%45 = phi i64 [ %42, %40 ], [ 2048, %43 ]
%46 = trunc i64 %45 to i32
store i32 %46, i32* %15
%47 = load i32, i32* %13
%48 = getelementptr inbounds [2048 x i8], [2048 x i8]* %11, i64 0, i64 0
%49 = load i32, i32* %15
%50 = call i32 @read(i32 %47, i8* %48, i32 %49)
store i32 %50, i32* %14
%51 = load i32, i32* %14
%52 = icmp sle i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %44
br label %61
54: ; preds = %44
%55 = getelementptr inbounds [2048 x i8], [2048 x i8]* %11, i64 0, i64 0
%56 = load i32, i32* %14
%57 = call i32 @MD5_Update(i32* %10, i8* %55, i32 %56)
%58 = load i32, i32* %14
%59 = load i32, i32* %12
%60 = sub nsw i32 %59, %58
store i32 %60, i32* %12
br label %33
61: ; preds = %53, %33
%62 = load i8**, i8*** %8
%63 = load i8*, i8** %62
%64 = call i32 @MD5_Final(i8* %63, i32* %10)
%65 = load i32, i32* %13
%66 = call i32 @close(i32 %65)
store i32 0, i32* %5
br label %67
67: ; preds = %61, %22
%68 = load i32, i32* %5
ret i32 %68
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@MD5_DIGEST_LENGTH = global i64 0
define i32 @mhash_with_size(i8* nocapture %0, i32 %1, i8** nocapture %2, i64* nocapture writeonly %3) {
%5 = alloca i32
%6 = alloca [2048 x i8]
%7 = bitcast i32* %5 to i8*
%8 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %4
store i8* null, i8** %2
store i64 0, i64* %3
br label %34
13: ; preds = %4
%14 = call i32 @MD5_Init(i32* %5)
%15 = load i64, i64* @MD5_DIGEST_LENGTH
store i64 %15, i64* %3
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %2
%17 = icmp sgt i32 %1, 0
br i1 %17, label %18, label %30
18: ; preds = %13, %24
%19 = phi i32 [ %26, %24 ], [ %1, %13 ]
%20 = icmp ult i32 %19, 2048
%21 = select i1 %20, i32 %19, i32 2048
%22 = call i32 @read(i32 %10, i8* %8, i32 %21)
%23 = icmp slt i32 %22, 1
br i1 %23, label %28, label %24
24: ; preds = %18
%25 = call i32 @MD5_Update(i32* %5, i8* %8, i32 %22)
%26 = sub nsw i32 %19, %22
%27 = icmp sgt i32 %26, 0
br i1 %27, label %18, label %28
28: ; preds = %18, %24
%29 = load i8*, i8** %2
br label %30
30: ; preds = %28, %13
%31 = phi i8* [ %29, %28 ], [ %16, %13 ]
%32 = call i32 @MD5_Final(i8* %31, i32* %5)
%33 = call i32 @close(i32 %10)
br label %34
34: ; preds = %30, %12
%35 = phi i32 [ -1, %12 ], [ 0, %30 ]
ret i32 %35
}