#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(long);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int read_file_to_array(char *filename, char **buffer, long *filelen) {
  FILE *fileptr;
  if ((fileptr = fopen(filename, "rb"))) {
    fseek(fileptr, 0, SEEK_END);
    *filelen = ftell(fileptr);
    rewind(fileptr);
    *buffer = (char *)malloc(*filelen + 1);
    fread(*buffer, *filelen, 1, fileptr);
    fclose(fileptr);
    return *filelen;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_file_to_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1220 <read_file_to_array+0xb0>
mov    -0x28(%rbp),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    1227 <read_file_to_array+0xb7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_to_array>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xe7e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11e4 <read_file_to_array+0x74>
mov    %rax,%rbx
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,(%r14)
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    (%r14),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,(%r15)
mov    (%r14),%rsi
mov    %rax,%rdi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    (%r14),%eax
jmp    11e6 <read_file_to_array+0x76>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
define i32 @read_file_to_array(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 i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %38
12: ; preds = %3
%13 = load i32*, i32** %8
%14 = load i32, i32* @SEEK_END
%15 = call i32 @fseek(i32* %13, i32 0, i32 %14)
%16 = load i32*, i32** %8
%17 = call i64 @ftell(i32* %16)
%18 = load i64*, i64** %7
store i64 %17, i64* %18
%19 = load i32*, i32** %8
%20 = call i32 @rewind(i32* %19)
%21 = load i64*, i64** %7
%22 = load i64, i64* %21
%23 = add nsw i64 %22, 1
%24 = call i64 @malloc(i64 %23)
%25 = inttoptr i64 %24 to i8*
%26 = load i8**, i8*** %6
store i8* %25, i8** %26
%27 = load i8**, i8*** %6
%28 = load i8*, i8** %27
%29 = load i64*, i64** %7
%30 = load i64, i64* %29
%31 = load i32*, i32** %8
%32 = call i32 @fread(i8* %28, i64 %30, i32 1, i32* %31)
%33 = load i32*, i32** %8
%34 = call i32 @fclose(i32* %33)
%35 = load i64*, i64** %7
%36 = load i64, i64* %35
%37 = trunc i64 %36 to i32
store i32 %37, i32* %4
br label %39
38: ; preds = %3
store i32 0, i32* %4
br label %39
39: ; preds = %38, %12
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
define i32 @read_file_to_array(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 %20, label %6
6: ; preds = %3
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %4, i32 0, i32 %7)
%9 = tail call i64 @ftell(i32* %4)
store i64 %9, i64* %2
%10 = tail call i32 @rewind(i32* %4)
%11 = load i64, i64* %2
%12 = add nsw i64 %11, 1
%13 = tail call i64 @malloc(i64 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %1
%15 = load i64, i64* %2
%16 = tail call i32 @fread(i8* %14, i64 %15, i32 1, i32* %4)
%17 = tail call i32 @fclose(i32* %4)
%18 = load i64, i64* %2
%19 = trunc i64 %18 to i32
br label %20
20: ; preds = %3, %6
%21 = phi i32 [ %19, %6 ], [ 0, %3 ]
ret i32 %21
}