#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *readFile(const char *path) {
  FILE *file = fopen(path, "rb");
  if (file == NULL) {
    fprintf(stderr, "Could not open file \"%s\".\n", path);
    exit(74);
  }
  fseek(file, 0L, SEEK_END);
  size_t fileSize = ftell(file);
  rewind(file);
  char *buffer = (char *)malloc(fileSize + 1);
  if (buffer == NULL) {
    fprintf(stderr, "Not enough memory to read \"%s\".\n", path);
    exit(74);
  }
  size_t bytesRead = fread(buffer, sizeof(char), fileSize, file);
  if (bytesRead < fileSize) {
    fprintf(stderr, "Could not read file \"%s\".\n", path);
    exit(74);
  }
  buffer[bytesRead] = '\0';
  fclose(file);
  return buffer;
}


x86 assembly (clang, -O0):
<readFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11de <readFile+0x4e>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe34(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x4a,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1249 <readFile+0xb9>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xde4(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x4a,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1294 <readFile+0x104>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdba(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x4a,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe5e(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     121a <readFile+0x8a>
mov    %rax,%r15
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r15,%rdi
call   1070 <ftell@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1050 <rewind@plt>
lea    0x1(%r12),%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     122c <readFile+0x9c>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    %r12,%rax
jb     123e <readFile+0xae>
movb   $0x0,(%rbx,%rax,1)
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
jmp    124e <readFile+0xbe>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
jmp    124e <readFile+0xbe>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0x4a,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Could not open file \22%s\22.\0A\00"
@SEEK_END = global i32 0
@.str.2 = [33 x i8] c"Not enough memory to read \22%s\22.\0A\00"
@.str.3 = [27 x i8] c"Could not read file \22%s\22.\0A\00"
define i8* @readFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %2
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 74)
unreachable
16: ; preds = %1
%17 = load i32*, i32** %3
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i64 0, i32 %18)
%20 = load i32*, i32** %3
%21 = call i64 @ftell(i32* %20)
store i64 %21, i64* %4
%22 = load i32*, i32** %3
%23 = call i32 @rewind(i32* %22)
%24 = load i64, i64* %4
%25 = add i64 %24, 1
%26 = call i64 @malloc(i64 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %5
%28 = load i8*, i8** %5
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %35
30: ; preds = %16
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %2
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = call i32 @exit(i32 74)
unreachable
35: ; preds = %16
%36 = load i8*, i8** %5
%37 = load i64, i64* %4
%38 = load i32*, i32** %3
%39 = call i64 @fread(i8* %36, i32 1, i64 %37, i32* %38)
store i64 %39, i64* %6
%40 = load i64, i64* %6
%41 = load i64, i64* %4
%42 = icmp ult i64 %40, %41
br i1 %42, label %43, label %48
43: ; preds = %35
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %2
%46 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %45)
%47 = call i32 @exit(i32 74)
unreachable
48: ; preds = %35
%49 = load i8*, i8** %5
%50 = load i64, i64* %6
%51 = getelementptr inbounds i8, i8* %49, i64 %50
store i8 0, i8* %51
%52 = load i32*, i32** %3
%53 = call i32 @fclose(i32* %52)
%54 = load i8*, i8** %5
ret i8* %54
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Could not open file \22%s\22.\0A\00"
@SEEK_END = global i32 0
@.str.2 = [33 x i8] c"Not enough memory to read \22%s\22.\0A\00"
@.str.3 = [27 x i8] c"Could not read file \22%s\22.\0A\00"
define i8* @readFile(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%7 = tail call i32 @exit(i32 74)
unreachable
8: ; preds = %1
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %2, i64 0, i32 %9)
%11 = tail call i64 @ftell(i32* %2)
%12 = tail call i32 @rewind(i32* %2)
%13 = add i64 %11, 1
%14 = tail call i64 @malloc(i64 %13)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %21
17: ; preds = %8
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%20 = tail call i32 @exit(i32 74)
unreachable
21: ; preds = %8
%22 = tail call i64 @fread(i8* %15, i32 1, i64 %11, i32* %2)
%23 = icmp ult i64 %22, %11
br i1 %23, label %24, label %28
24: ; preds = %21
%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), i8* %0)
%27 = tail call i32 @exit(i32 74)
unreachable
28: ; preds = %21
%29 = getelementptr inbounds i8, i8* %15, i64 %22
store i8 0, i8* %29
%30 = tail call i32 @fclose(i32* %2)
ret i8* %15
}