#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 *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
char *malloc(int);
int m_file_to_buffer(const char *filename, int *size, char **buffer) {
  if (buffer == NULL)
    return 0;
  FILE *file = fopen(filename, "rb");
  if (file == NULL)
    return 0;
  fseek(file, 0L, SEEK_END);
  *size = ftell(file);
  fseek(file, 0L, SEEK_SET);
  *buffer = malloc(*size);
  if (*buffer != NULL)
    fread(*buffer, *size, 1, file);
  fclose(file);
  return 1;
}


x86 assembly (clang, -O0):
<m_file_to_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118b <m_file_to_buffer+0x2b>
movl   $0x0,-0x4(%rbp)
jmp    1241 <m_file_to_buffer+0xe1>
mov    -0x10(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b6 <m_file_to_buffer+0x56>
movl   $0x0,-0x4(%rbp)
jmp    1241 <m_file_to_buffer+0xe1>
mov    -0x28(%rbp),%rdi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     1231 <m_file_to_buffer+0xd1>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<m_file_to_buffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %ebp,%ebp
test   %rdx,%rdx
je     11ea <m_file_to_buffer+0x8a>
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xe85(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11ea <m_file_to_buffer+0x8a>
mov    %rax,%rbx
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %eax,(%r15)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    (%r15),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11dd <m_file_to_buffer+0x7d>
mov    %rax,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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