#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 *);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *readFile(const char *fileName) {
  FILE *fp;
  fp = fopen(fileName, "r");
  if (fp == NULL) {
    printf("Error 1!\n");
    exit(1);
  }
  fseek(fp, 0, SEEK_END);
  long size = ftell(fp);
  rewind(fp);
  char *buffer = (char *)malloc(sizeof(char) * (size + 1));
  if (buffer == NULL) {
    printf("Error 2!\n");
    fclose(fp);
    exit(1);
  }
  size_t res = fread(buffer, 1, size, fp);
  if (res != size) {
    printf("Error 3!\n");
    fclose(fp);
    exit(1);
  }
  buffer[size] = 0;
  fclose(fp);
  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    11d1 <readFile+0x41>
lea    0xe40(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123c <readFile+0xac>
lea    0xde8(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x1,%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
je     1283 <readFile+0xf3>
lea    0xdab(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%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   %rbx
lea    0xe64(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     120e <readFile+0x7e>
mov    %rax,%r14
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r14,%rdi
call   1070 <ftell@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1060 <rewind@plt>
lea    0x1(%r15),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1224 <readFile+0x94>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    %r15,%rax
jne    122d <readFile+0x9d>
movb   $0x0,(%rbx,%r15,1)
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdff(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xde0(%rip),%rdi
jmp    1234 <readFile+0xa4>
lea    0xdce(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [10 x i8] c"Error 1!\0A\00"
@SEEK_END = global i32 0
@.str.2 = [10 x i8] c"Error 2!\0A\00"
@.str.3 = [10 x i8] c"Error 3!\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
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = load i32*, i32** %3
%16 = load i32, i32* @SEEK_END
%17 = call i32 @fseek(i32* %15, i32 0, i32 %16)
%18 = load i32*, i32** %3
%19 = call i64 @ftell(i32* %18)
store i64 %19, i64* %4
%20 = load i32*, i32** %3
%21 = call i32 @rewind(i32* %20)
%22 = load i64, i64* %4
%23 = add nsw i64 %22, 1
%24 = mul i64 1, %23
%25 = trunc i64 %24 to i32
%26 = call i64 @malloc(i32 %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 = %14
%31 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32*, i32** %3
%33 = call i32 @fclose(i32* %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %14
%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 ne i64 %40, %41
br i1 %42, label %43, label %48
43: ; preds = %35
%44 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%45 = load i32*, i32** %3
%46 = call i32 @fclose(i32* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %35
%49 = load i8*, i8** %5
%50 = load i64, i64* %4
%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 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@str = [9 x i8] c"Error 3!\00"
@str.4 = [9 x i8] c"Error 2!\00"
@str.5 = [9 x i8] c"Error 1!\00"
define i8* @readFile(i8* nocapture %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
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.5, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
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 = trunc i64 %10 to i32
%13 = add i32 %12, 1
%14 = tail call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %21
17: ; preds = %7
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.4, i64 0, i64 0))
%19 = tail call i32 @fclose(i32* %2)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %7
%22 = tail call i64 @fread(i8* %15, i32 1, i64 %10, i32* %2)
%23 = icmp eq i64 %22, %10
br i1 %23, label %28, label %24
24: ; preds = %21
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%26 = tail call i32 @fclose(i32* %2)
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %21
%29 = getelementptr inbounds i8, i8* %15, i64 %10
store i8 0, i8* %29
%30 = tail call i32 @fclose(i32* %2)
ret i8* %15
}