#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 *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(long);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *fileRead(char *filename, long *file_length) {
  FILE *file = fopen(filename, "r");
  if (file == NULL) {
    perror("fopen");
  }
  fseek(file, 0, SEEK_END);
  *file_length = ftell(file);
  if (*file_length == -1) {
    perror("ftell");
  }
  fseek(file, 0, SEEK_SET);
  char *data = (char *)malloc(*file_length);
  if (fread(data, 1, *file_length, file) != (size_t)*file_length) {
    fprintf(stderr, "couldn't read entire file\n");
    free(data);
  }
  fclose(file);
  return data;
}


x86 assembly (clang, -O0):
<fileRead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cb <fileRead+0x3b>
lea    0xe3c(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0xffffffffffffffff,(%rax)
jne    120c <fileRead+0x7c>
lea    0xe01(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x18(%rbp),%rdi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
je     1274 <fileRead+0xe4>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileRead>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xe61(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1238 <fileRead+0xa8>
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,(%r14)
cmp    $0xffffffffffffffff,%rax
je     1249 <fileRead+0xb9>
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    (%r14),%rdi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    (%r14),%rdx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    (%r14),%rax
je     1227 <fileRead+0x97>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
call   1060 <fprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdc3(%rip),%rdi
call   10b0 <perror@plt>
jmp    11b0 <fileRead+0x20>
lea    0xdb8(%rip),%rdi
call   10b0 <perror@plt>
jmp    11d4 <fileRead+0x44>

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@SEEK_END = global i32 0
@.str.2 = [6 x i8] c"ftell\00"
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str.3 = [27 x i8] c"couldn't read entire file\0A\00"
define i8* @fileRead(i8* nocapture %0, i64* nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %3)
store i64 %10, i64* %1
%11 = icmp eq i64 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %7
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @fseek(i32* %3, i32 0, i32 %15)
%17 = load i64, i64* %1
%18 = tail call i64 @malloc(i64 %17)
%19 = inttoptr i64 %18 to i8*
%20 = load i64, i64* %1
%21 = tail call i64 @fread(i8* %19, i32 1, i64 %20, i32* %3)
%22 = load i64, i64* %1
%23 = icmp eq i64 %21, %22
br i1 %23, label %28, label %24
24: ; preds = %14
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%27 = tail call i32 @free(i8* %19)
br label %28
28: ; preds = %24, %14
%29 = tail call i32 @fclose(i32* %3)
ret i8* %19
}