#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 *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
size_t fsize(char const *);
scalar_t__ malloc(size_t);
int farray_data_file_to_memory(const char *filename, char **mem_space,
                               size_t *mem_size) {
  FILE *f;
  *mem_size = fsize(filename);
  if (*mem_size == 0) {
    *mem_space = NULL;
    return 1;
  }
  *mem_space = (char *)malloc(*mem_size);
  if (*mem_space == NULL) {
    *mem_size = 0;
    return 2;
  }
  f = fopen(filename, "r");
  *mem_size = fread(*mem_space, 1, *mem_size, f);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<farray_data_file_to_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <fsize@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    119c <farray_data_file_to_memory+0x4c>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1221 <farray_data_file_to_memory+0xd1>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11d7 <farray_data_file_to_memory+0x87>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x2,-0x4(%rbp)
jmp    1221 <farray_data_file_to_memory+0xd1>
mov    -0x10(%rbp),%rdi
lea    0xe1e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<farray_data_file_to_memory>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
call   1060 <fsize@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     11af <farray_data_file_to_memory+0x5f>
mov    %rax,%rdi
call   1050 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11bd <farray_data_file_to_memory+0x6d>
lea    0xe7e(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    (%r14),%rdi
mov    (%r15),%rdx
mov    $0x1,%esi
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rax,(%r15)
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    11c9 <farray_data_file_to_memory+0x79>
movq   $0x0,(%r14)
mov    $0x1,%eax
jmp    11c9 <farray_data_file_to_memory+0x79>
movq   $0x0,(%r15)
mov    $0x2,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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