#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 */
long LONG_MAX;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long);
char *readTextFile(char *path) {
  FILE *f = fopen(path, "r");
  if (!f)
    return NULL;
  if (fseek(f, 0, SEEK_END) < 0) {
    fclose(f);
    return NULL;
  }
  long len = ftell(f);
  if (len == -1L || len == LONG_MAX) {
    fclose(f);
    return NULL;
  }
  char *buffer = malloc((len + 1) * sizeof(char));
  fseek(f, 0, SEEK_SET);
  int i = 0;
  while (!feof(f))
    buffer[i++] = fgetc(f);
  buffer[i - 1] = '\0';
  fclose(f);
  return buffer;
}


x86 assembly (clang, -O0):
<readTextFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a8 <readTextFile+0x38>
movq   $0x0,-0x8(%rbp)
jmp    12b3 <readTextFile+0x143>
mov    -0x18(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
jge    11dc <readTextFile+0x6c>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b3 <readTextFile+0x143>
mov    -0x18(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
je     1208 <readTextFile+0x98>
mov    -0x20(%rbp),%rax
mov    0x2dd9(%rip),%rcx
cmp    (%rcx),%rax
jne    121e <readTextFile+0xae>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b3 <readTextFile+0x143>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d92(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    126c <readTextFile+0xfc>
jmp    1291 <readTextFile+0x121>
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,%dl
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x2c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    124e <readTextFile+0xde>
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readTextFile>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
lea    0xe80(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1260 <readTextFile+0xf0>
mov    %rax,%r15
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
js     1256 <readTextFile+0xe6>
mov    %r15,%rdi
call   1050 <ftell@plt>
xor    %r14d,%r14d
cmp    $0xffffffffffffffff,%rax
je     1256 <readTextFile+0xe6>
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%rax
je     1256 <readTextFile+0xe6>
add    $0x1,%rax
mov    %rax,%rdi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
call   1060 <feof@plt>
mov    $0xffffffffffffffff,%r13
test   %eax,%eax
jne    1251 <readTextFile+0xe1>
movabs $0xffffffff00000000,%r13
ff ff ff
movabs $0x100000000,%r12
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %r12,%r13
mov    %r15,%rdi
call   1040 <fgetc@plt>
mov    %al,(%rbx)
mov    %r15,%rdi
call   1060 <feof@plt>
add    $0x1,%rbx
test   %eax,%eax
je     1230 <readTextFile+0xc0>
sar    $0x20,%r13
movb   $0x0,(%r14,%r13,1)
mov    %r15,%rdi
call   1030 <fclose@plt>
jmp    1263 <readTextFile+0xf3>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@LONG_MAX = global i64 0
@SEEK_SET = global i32 0
define i8* @readTextFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %13, label %12
12: ; preds = %1
store i8* null, i8** %2
br label %63
13: ; preds = %1
%14 = load i32*, i32** %4
%15 = load i32, i32* @SEEK_END
%16 = call i64 @fseek(i32* %14, i32 0, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32*, i32** %4
%20 = call i32 @fclose(i32* %19)
store i8* null, i8** %2
br label %63
21: ; preds = %13
%22 = load i32*, i32** %4
%23 = call i64 @ftell(i32* %22)
store i64 %23, i64* %5
%24 = load i64, i64* %5
%25 = icmp eq i64 %24, -1
br i1 %25, label %30, label %26
26: ; preds = %21
%27 = load i64, i64* %5
%28 = load i64, i64* @LONG_MAX
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %26, %21
%31 = load i32*, i32** %4
%32 = call i32 @fclose(i32* %31)
store i8* null, i8** %2
br label %63
33: ; preds = %26
%34 = load i64, i64* %5
%35 = add nsw i64 %34, 1
%36 = mul i64 %35, 1
%37 = call i8* @malloc(i64 %36)
store i8* %37, i8** %6
%38 = load i32*, i32** %4
%39 = load i32, i32* @SEEK_SET
%40 = call i64 @fseek(i32* %38, i32 0, i32 %39)
store i32 0, i32* %7
br label %41
41: ; preds = %46, %33
%42 = load i32*, i32** %4
%43 = call i32 @feof(i32* %42)
%44 = icmp ne i32 %43, 0
%45 = xor i1 %44, true
br i1 %45, label %46, label %54
46: ; preds = %41
%47 = load i32*, i32** %4
%48 = call signext i8 @fgetc(i32* %47)
%49 = load i8*, i8** %6
%50 = load i32, i32* %7
%51 = add nsw i32 %50, 1
store i32 %51, i32* %7
%52 = sext i32 %50 to i64
%53 = getelementptr inbounds i8, i8* %49, i64 %52
store i8 %48, i8* %53
br label %41
54: ; preds = %41
%55 = load i8*, i8** %6
%56 = load i32, i32* %7
%57 = sub nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
store i8 0, i8* %59
%60 = load i32*, i32** %4
%61 = call i32 @fclose(i32* %60)
%62 = load i8*, i8** %6
store i8* %62, i8** %2
br label %63
63: ; preds = %54, %30, %18, %12
%64 = load i8*, i8** %2
ret i8* %64
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@LONG_MAX = global i64 0
@SEEK_SET = global i32 0
define noalias i8* @readTextFile(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 %37, label %4
4: ; preds = %1
%5 = load i32, i32* @SEEK_END
%6 = tail call i64 @fseek(i32* %2, i32 0, i32 %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %34, label %8
8: ; preds = %4
%9 = tail call i64 @ftell(i32* %2)
%10 = icmp eq i64 %9, -1
%11 = load i64, i64* @LONG_MAX
%12 = icmp eq i64 %9, %11
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %34, label %14
14: ; preds = %8
%15 = add nuw nsw i64 %9, 1
%16 = tail call i8* @malloc(i64 %15)
%17 = load i32, i32* @SEEK_SET
%18 = tail call i64 @fseek(i32* %2, i32 0, i32 %17)
%19 = tail call i32 @feof(i32* %2)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %31
21: ; preds = %14, %21
%22 = phi i64 [ %24, %21 ], [ 0, %14 ]
%23 = tail call signext i8 @fgetc(i32* %2)
%24 = add nuw i64 %22, 1
%25 = getelementptr inbounds i8, i8* %16, i64 %22
store i8 %23, i8* %25
%26 = tail call i32 @feof(i32* %2)
%27 = icmp eq i32 %26, 0
br i1 %27, label %21, label %28
28: ; preds = %21
%29 = shl i64 %22, 32
%30 = ashr exact i64 %29, 32
br label %31
31: ; preds = %28, %14
%32 = phi i64 [ -1, %14 ], [ %30, %28 ]
%33 = getelementptr inbounds i8, i8* %16, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %8, %4, %31
%35 = phi i8* [ %16, %31 ], [ null, %4 ], [ null, %8 ]
%36 = tail call i32 @fclose(i32* %2)
br label %37
37: ; preds = %34, %1
%38 = phi i8* [ null, %1 ], [ %35, %34 ]
ret i8* %38
}