#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 */
char EOF;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
char *ftell(int /*<<< orphan*/ *);
char *malloc(char *);
int /*<<< orphan*/ printf(char *, char *);
char *readAllText(char *path) {
  char *text;
  FILE *f;
  if ((f = fopen(path, "rb")) == NULL) {
    printf("File %s doesn't exist!\n", path);
    exit(-1);
  }
  fseek(f, 0, SEEK_END);
  text = malloc(ftell(f) + 1);
  printf("file size: %lu\n", ftell(f));
  fseek(f, 0, SEEK_SET);
  long count = 0;
  char c;
  while ((c = fgetc(f)) != EOF) {
    text[count++] = (char)c;
  }
  fclose(f);
  return text;
}


x86 assembly (clang, -O0):
<readAllText>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11c4 <readAllText+0x44>
mov    -0x8(%rbp),%rsi
lea    0xe4e(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,%rsi
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x21(%rbp)
movsbl %al,%eax
mov    0x2da5(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1264 <readAllText+0xe4>
mov    -0x21(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    1225 <readAllText+0xa5>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readAllText>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe6e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     123a <readAllText+0xba>
mov    %rax,%r12
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r12,%rdi
call   1060 <ftell@plt>
lea    0x1(%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %r12,%rdi
call   1060 <ftell@plt>
lea    0xe42(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r12,%rdi
call   1050 <fgetc@plt>
mov    0x2ddd(%rip),%r15
cmp    (%r15),%al
je     1223 <readAllText+0xa3>
mov    %r14,%rbx
nopl   0x0(%rax,%rax,1)
mov    %al,(%rbx)
mov    %r12,%rdi
call   1050 <fgetc@plt>
add    $0x1,%rbx
cmp    (%r15),%al
jne    1210 <readAllText+0x90>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdc2(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [24 x i8] c"File %s doesn't exist!\0A\00"
@SEEK_END = global i32 0
@.str.2 = [16 x i8] c"file size: %lu\0A\00"
@SEEK_SET = global i32 0
@EOF = global i8 0
define i8* @readAllText(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8
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** %4
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %1
%15 = load i32*, i32** %4
%16 = load i32, i32* @SEEK_END
%17 = call i32 @fseek(i32* %15, i32 0, i32 %16)
%18 = load i32*, i32** %4
%19 = call i8* @ftell(i32* %18)
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = call i8* @malloc(i8* %20)
store i8* %21, i8** %3
%22 = load i32*, i32** %4
%23 = call i8* @ftell(i32* %22)
%24 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = load i32*, i32** %4
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @fseek(i32* %25, i32 0, i32 %26)
store i64 0, i64* %5
br label %28
28: ; preds = %35, %14
%29 = load i32*, i32** %4
%30 = call signext i8 @fgetc(i32* %29)
store i8 %30, i8* %6
%31 = sext i8 %30 to i32
%32 = load i8, i8* @EOF
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %31, %33
br i1 %34, label %35, label %41
35: ; preds = %28
%36 = load i8, i8* %6
%37 = load i8*, i8** %3
%38 = load i64, i64* %5
%39 = add nsw i64 %38, 1
store i64 %39, i64* %5
%40 = getelementptr inbounds i8, i8* %37, i64 %38
store i8 %36, i8* %40
br label %28
41: ; preds = %28
%42 = load i32*, i32** %4
%43 = call i32 @fclose(i32* %42)
%44 = load i8*, i8** %3
ret i8* %44
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [24 x i8] c"File %s doesn't exist!\0A\00"
@SEEK_END = global i32 0
@.str.2 = [16 x i8] c"file size: %lu\0A\00"
@SEEK_SET = global i32 0
@EOF = global i8 0
define noalias i8* @readAllText(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 %7
4: ; preds = %1
%5 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %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 i8* @ftell(i32* %2)
%11 = getelementptr inbounds i8, i8* %10, i64 1
%12 = tail call i8* @malloc(i8* %11)
%13 = tail call i8* @ftell(i32* %2)
%14 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @fseek(i32* %2, i32 0, i32 %15)
%17 = tail call signext i8 @fgetc(i32* %2)
%18 = load i8, i8* @EOF
%19 = icmp eq i8 %17, %18
br i1 %19, label %28, label %20
20: ; preds = %7, %20
%21 = phi i8 [ %25, %20 ], [ %17, %7 ]
%22 = phi i64 [ %23, %20 ], [ 0, %7 ]
%23 = add nuw nsw i64 %22, 1
%24 = getelementptr inbounds i8, i8* %12, i64 %22
store i8 %21, i8* %24
%25 = tail call signext i8 @fgetc(i32* %2)
%26 = load i8, i8* @EOF
%27 = icmp eq i8 %25, %26
br i1 %27, label %28, label %20
28: ; preds = %20, %7
%29 = tail call i32 @fclose(i32* %2)
ret i8* %12
}