#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
long fread(unsigned char *, int, long, int /*<<< orphan*/ *);
scalar_t__ malloc(long);
long milk_file_get_size_f(int /*<<< orphan*/ *);
int milk_file_get_contents(const char *filename, unsigned char **data_out,
                           long *size_out) {
  FILE *f;
  long size, num_read;
  unsigned char *data;
  f = fopen(filename, "rb");
  if (!f)
    return -1;
  size = milk_file_get_size_f(f);
  data = (unsigned char *)malloc(*size_out);
  num_read = fread(data, 1, size, f);
  if (num_read != size)
    goto err;
  fclose(f);
  *size_out = size;
  *data_out = data;
  return 0;
err:
  fclose(f);
  return -1;
}


x86 assembly (clang, -O0):
<milk_file_get_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118f <milk_file_get_contents+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <milk_file_get_contents+0xc4>
mov    -0x28(%rbp),%rdi
call   1070 <milk_file_get_size_f@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     11d9 <milk_file_get_contents+0x89>
jmp    1204 <milk_file_get_contents+0xb4>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1214 <milk_file_get_contents+0xc4>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<milk_file_get_contents>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbx
lea    0xe98(%rip),%rsi
call   1060 <fopen@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
je     11c5 <milk_file_get_contents+0x75>
mov    %rax,%rbp
mov    %rbx,(%rsp)
mov    %rax,%rdi
call   1070 <milk_file_get_size_f@plt>
mov    %rax,%rbx
mov    (%r15),%rdi
call   1050 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %rbp,%rcx
call   1030 <fread@plt>
mov    %rax,%r14
mov    %rbp,%rdi
call   1040 <fclose@plt>
cmp    %rbx,%r14
jne    11c5 <milk_file_get_contents+0x75>
mov    %rbx,(%r15)
mov    (%rsp),%rax
mov    %r13,(%rax)
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
define i32 @milk_file_get_contents(i8* nocapture %0, i8** nocapture writeonly %1, i64* nocapture %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %15, label %6
6: ; preds = %3
%7 = tail call i64 @milk_file_get_size_f(i32* %4)
%8 = load i64, i64* %2
%9 = tail call i64 @malloc(i64 %8)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i64 @fread(i8* %10, i32 1, i64 %7, i32* %4)
%12 = icmp eq i64 %11, %7
%13 = tail call i32 @fclose(i32* %4)
br i1 %12, label %14, label %15
14: ; preds = %6
store i64 %7, i64* %2
store i8* %10, i8** %1
br label %15
15: ; preds = %6, %3, %14
%16 = phi i32 [ 0, %14 ], [ -1, %3 ], [ -1, %6 ]
ret i32 %16
}