#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;
char *calloc(long, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fread(char *, long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *read_whole_file(const char *filename, int *filesize) {
  *filesize = 0;
  FILE *new_file = fopen(filename, "rb");
  if (new_file == NULL) {
    fprintf(stderr, "ERROR OF OPENING FILE\n");
    exit(1);
  }
  fseek(new_file, 0, SEEK_END);
  long file_size = ftell(new_file);
  *filesize = file_size;
  fseek(new_file, 0, SEEK_SET);
  char *output_string = calloc(file_size, sizeof(char));
  fread(output_string, file_size, 1, new_file);
  fclose(new_file);
  return output_string;
}


x86 assembly (clang, -O0):
<read_whole_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    0xe5b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d8 <read_whole_file+0x58>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rdi
mov    0x2dc0(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_whole_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
movl   $0x0,(%rsi)
lea    0xe6b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     120a <read_whole_file+0x8a>
mov    %rax,%rbx
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r15
mov    %r15d,(%r14)
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"ERROR OF OPENING FILE\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @read_whole_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %3
store i32* %1, i32** %4
%8 = load i32*, i32** %4
store i32 0, i32* %8
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32*, i32** %5
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %5
%22 = call i64 @ftell(i32* %21)
store i64 %22, i64* %6
%23 = load i64, i64* %6
%24 = trunc i64 %23 to i32
%25 = load i32*, i32** %4
store i32 %24, i32* %25
%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* %6
%30 = call i8* @calloc(i64 %29, i32 1)
store i8* %30, i8** %7
%31 = load i8*, i8** %7
%32 = load i64, i64* %6
%33 = load i32*, i32** %5
%34 = call i32 @fread(i8* %31, i64 %32, i32 1, i32* %33)
%35 = load i32*, i32** %5
%36 = call i32 @fclose(i32* %35)
%37 = load i8*, i8** %7
ret i8* %37
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"ERROR OF OPENING FILE\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @read_whole_file(i8* nocapture %0, i32* nocapture writeonly %1) {
store i32 0, i32* %1
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @fseek(i32* %3, i32 0, i32 %10)
%12 = tail call i64 @ftell(i32* %3)
%13 = trunc i64 %12 to i32
store i32 %13, i32* %1
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @fseek(i32* %3, i32 0, i32 %14)
%16 = tail call i8* @calloc(i64 %12, i32 1)
%17 = tail call i32 @fread(i8* %16, i64 %12, i32 1, i32* %3)
%18 = tail call i32 @fclose(i32* %3)
ret i8* %16
}