#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 const *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
long fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(long);
char *realloc(char *, long);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
const char *readFile(const char *filename) {
  long int size = 0;
  FILE *file = fopen(filename, "rb");
  if (!file) {
    fputs("File error.\n", stderr);
    return NULL;
  }
  fseek(file, 0, SEEK_END);
  size = ftell(file);
  rewind(file);
  char *result = (char *)malloc(size);
  if (!result) {
    fputs("Memory error.\n", stderr);
    return NULL;
  }
  if (fread(result, 1, size, file) != size) {
    fputs("Read error.\n", stderr);
    return NULL;
  }
  fclose(file);
  result = realloc(result, size + sizeof(char));
  result[size] = '\0';
  return result;
}


x86 assembly (clang, -O0):
<readFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e5 <readFile+0x55>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    0xe30(%rip),%rdi
call   1060 <fputs@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bd <readFile+0x12d>
mov    -0x20(%rbp),%rdi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x18(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1249 <readFile+0xb9>
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
lea    0xdd9(%rip),%rdi
call   1060 <fputs@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bd <readFile+0x12d>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x18(%rbp),%rax
je     128b <readFile+0xfb>
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
lea    0xda6(%rip),%rdi
call   1060 <fputs@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bd <readFile+0x12d>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   10a0 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFile>:
push   %r15
push   %r14
push   %rbx
lea    0xe64(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1215 <readFile+0x85>
mov    %rax,%rbx
mov    0x2e38(%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
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %r14,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1227 <readFile+0x97>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %r14,%rax
jne    1239 <readFile+0xa9>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rsi
add    $0x1,%rsi
mov    %r15,%rdi
call   10a0 <realloc@plt>
movb   $0x0,(%rax,%r14,1)
jmp    1250 <readFile+0xc0>
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
lea    0xdde(%rip),%rdi
jmp    1249 <readFile+0xb9>
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
lea    0xdd9(%rip),%rdi
jmp    1249 <readFile+0xb9>
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
lea    0xdd6(%rip),%rdi
call   1060 <fputs@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [13 x i8] c"File error.\0A\00"
@stderr = global i32 0
@SEEK_END = global i32 0
@.str.2 = [15 x i8] c"Memory error.\0A\00"
@.str.3 = [13 x i8] c"Read error.\0A\00"
define noalias i8* @readFile(i8* nocapture %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 %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fputs(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %5)
br label %29
7: ; preds = %1
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %2, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %2)
%11 = tail call i32 @rewind(i32* %2)
%12 = tail call i64 @malloc(i64 %10)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = load i32, i32* @stderr
%17 = tail call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %16)
br label %29
18: ; preds = %7
%19 = tail call i64 @fread(i8* %13, i32 1, i64 %10, i32* %2)
%20 = icmp eq i64 %19, %10
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = tail call i32 @fputs(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %22)
br label %29
24: ; preds = %18
%25 = tail call i32 @fclose(i32* %2)
%26 = add i64 %10, 1
%27 = tail call i8* @realloc(i8* %13, i64 %26)
%28 = getelementptr inbounds i8, i8* %27, i64 %10
store i8 0, i8* %28
br label %29
29: ; preds = %15, %21, %24, %4
%30 = phi i8* [ null, %4 ], [ null, %21 ], [ %27, %24 ], [ null, %15 ]
ret i8* %30
}