#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*/ failIf(int, int, char *, char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *readFile(const char *path) {
  FILE *file = fopen(path, "r");
  failIf(file == NULL, 66, "Could not open file \"%s\".\n", path);
  fseek(file, 0L, SEEK_END);
  size_t fileSize = ftell(file);
  rewind(file);
  char *buffer = malloc(fileSize + 1);
  failIf(buffer == NULL, 74, "Could not read file \"%s\".\n", path);
  size_t bytesRead = fread(buffer, sizeof(char), fileSize, file);
  failIf(bytesRead < fileSize, 74, "Could not read file \"%s\".\n", path);
  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    0xe69(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
mov    -0x8(%rbp),%rcx
mov    $0x42,%esi
lea    0xe45(%rip),%rdx
call   1080 <failIf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
mov    -0x8(%rbp),%rcx
mov    $0x4a,%esi
lea    0xe01(%rip),%rdx
call   1080 <failIf@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
setb   %al
and    $0x1,%al
movzbl %al,%edi
mov    -0x8(%rbp),%rcx
mov    $0x4a,%esi
lea    0xdc2(%rip),%rdx
call   1080 <failIf@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   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe6b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r15
xor    %edi,%edi
test   %rax,%rax
sete   %dil
lea    0xe55(%rip),%rdx
mov    $0x42,%esi
mov    %r14,%rcx
call   1080 <failIf@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r15,%rdi
call   1060 <ftell@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1050 <rewind@plt>
lea    0x1(%r12),%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
sete   %dil
lea    0xe20(%rip),%r13
mov    $0x4a,%esi
mov    %r13,%rdx
mov    %r14,%rcx
call   1080 <failIf@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %rax,%rbp
xor    %edi,%edi
cmp    %r12,%rax
setb   %dil
mov    $0x4a,%esi
mov    %r13,%rdx
mov    %r14,%rcx
call   1080 <failIf@plt>
movb   $0x0,(%rbx,%rbp,1)
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [27 x i8] c"Could not open file \22%s\22.\0A\00"
@SEEK_END = global i32 0
@.str.2 = [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 ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = icmp eq i32* %9, null
%11 = zext i1 %10 to i32
%12 = load i8*, i8** %2
%13 = call i32 @failIf(i32 %11, i32 66, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = load i32*, i32** %3
%15 = load i32, i32* @SEEK_END
%16 = call i32 @fseek(i32* %14, i64 0, i32 %15)
%17 = load i32*, i32** %3
%18 = call i64 @ftell(i32* %17)
store i64 %18, i64* %4
%19 = load i32*, i32** %3
%20 = call i32 @rewind(i32* %19)
%21 = load i64, i64* %4
%22 = add i64 %21, 1
%23 = call i8* @malloc(i64 %22)
store i8* %23, i8** %5
%24 = load i8*, i8** %5
%25 = icmp eq i8* %24, null
%26 = zext i1 %25 to i32
%27 = load i8*, i8** %2
%28 = call i32 @failIf(i32 %26, i32 74, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = load i8*, i8** %5
%30 = load i64, i64* %4
%31 = load i32*, i32** %3
%32 = call i64 @fread(i8* %29, i32 1, i64 %30, i32* %31)
store i64 %32, i64* %6
%33 = load i64, i64* %6
%34 = load i64, i64* %4
%35 = icmp ult i64 %33, %34
%36 = zext i1 %35 to i32
%37 = load i8*, i8** %2
%38 = call i32 @failIf(i32 %36, i32 74, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = load i8*, i8** %5
%40 = load i64, i64* %6
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8 0, i8* %41
%42 = load i32*, i32** %3
%43 = call i32 @fclose(i32* %42)
%44 = load i8*, i8** %5
ret i8* %44
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [27 x i8] c"Could not open file \22%s\22.\0A\00"
@SEEK_END = global i32 0
@.str.2 = [27 x i8] c"Could not read file \22%s\22.\0A\00"
define noalias i8* @readFile(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @failIf(i32 %4, i32 66, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %2, i64 0, i32 %6)
%8 = tail call i64 @ftell(i32* %2)
%9 = tail call i32 @rewind(i32* %2)
%10 = add i64 %8, 1
%11 = tail call i8* @malloc(i64 %10)
%12 = icmp eq i8* %11, null
%13 = zext i1 %12 to i32
%14 = tail call i32 @failIf(i32 %13, i32 74, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%15 = tail call i64 @fread(i8* %11, i32 1, i64 %8, i32* %2)
%16 = icmp ult i64 %15, %8
%17 = zext i1 %16 to i32
%18 = tail call i32 @failIf(i32 %17, i32 74, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%19 = getelementptr inbounds i8, i8* %11, i64 %15
store i8 0, i8* %19
%20 = tail call i32 @fclose(i32* %2)
ret i8* %11
}