#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*/ *);
char *malloc(long);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int readFile(char *filename, char **buffer, long *size) {
  FILE *in;
  char *buf;
  long length;
  in = fopen(filename, "r");
  if (in == 0) {
    perror(filename);
    return -1;
  }
  fseek(in, 0, SEEK_END);
  length = ftell(in);
  rewind(in);
  buf = malloc(length);
  fread(buf, length, 1, in);
  fclose(in);
  *size = length;
  *buffer = buf;
  return length;
}


x86 assembly (clang, -O0):
<readFile>:
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    0xe61(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c8 <readFile+0x48>
mov    -0x10(%rbp),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <readFile+0xbb>
mov    -0x28(%rbp),%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x38(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFile>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xe67(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1205 <readFile+0x85>
mov    %rax,%rbx
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %r12,%rdi
call   1070 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r12,(%r14)
mov    %r13,(%r15)
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    %r12,%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r12d
jmp    11f8 <readFile+0x78>

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

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