#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*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *itoa(int, int);
char *malloc(int);
int /*<<< orphan*/ stderr;
char *readFile(char *path) {
  char *source = NULL;
  FILE *fp = fopen(path, "rb");
  if (fp != NULL) {
    if (fseek(fp, 0L, SEEK_END) == 0) {
      long bufsize = ftell(fp);
      if (bufsize == -1) {
        fputs("Error reading file", stderr);
        source = itoa(-1, 10);
        return source;
      }
      source = malloc(sizeof(char) * (bufsize + 1));
      if (fseek(fp, 0L, SEEK_SET) != 0) {
        fputs("Error reading file", stderr);
        source = itoa(-1, 10);
        return source;
      }
      size_t fileLen = fread(source, sizeof(char), bufsize, fp);
      if (ferror(fp) != 0) {
        fputs("Error reading file", stderr);
        source = itoa(-1, 10);
        return source;
      } else {
        source[fileLen++] = '\0';
      }
    }
    fclose(fp);
  }
  return source;
}


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   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1323 <readFile+0x193>
mov    -0x20(%rbp),%rdi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
cmp    $0x0,%rax
jne    131a <readFile+0x18a>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    1230 <readFile+0xa0>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
lea    0xdf8(%rip),%rdi
call   1060 <fputs@plt>
mov    $0xffffffff,%edi
mov    $0xa,%esi
call   10b0 <itoa@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    132b <readFile+0x19b>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
cmp    $0x0,%rax
je     129c <readFile+0x10c>
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
lea    0xd8c(%rip),%rdi
call   1060 <fputs@plt>
mov    $0xffffffff,%edi
mov    $0xa,%esi
call   10b0 <itoa@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    132b <readFile+0x19b>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12fe <readFile+0x16e>
mov    0x2d08(%rip),%rax
mov    (%rax),%esi
lea    0xd2a(%rip),%rdi
call   1060 <fputs@plt>
mov    $0xffffffff,%edi
mov    $0xa,%esi
call   10b0 <itoa@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    132b <readFile+0x19b>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x0,(%rax,%rcx,1)
jmp    131a <readFile+0x18a>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%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   10a0 <fopen@plt>
test   %rax,%rax
je     11ce <readFile+0x3e>
mov    %rax,%rbx
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
test   %rax,%rax
je     11da <readFile+0x4a>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    11d1 <readFile+0x41>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
call   1070 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     1231 <readFile+0xa1>
mov    %rax,%r15
lea    0x1(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
test   %rax,%rax
jne    1231 <readFile+0xa1>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     125a <readFile+0xca>
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
lea    0xdc2(%rip),%rdi
call   1060 <fputs@plt>
mov    $0xffffffff,%edi
mov    $0xa,%esi
pop    %rbx
pop    %r14
pop    %r15
jmp    10b0 <itoa@plt>
movb   $0x0,(%r14,%r15,1)
jmp    11c4 <readFile+0x34>

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.1 = [19 x i8] c"Error reading file\00"
@stderr = global i32 0
@SEEK_SET = global i32 0
define 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 %39, label %4
4: ; preds = %1
%5 = load i32, i32* @SEEK_END
%6 = tail call i64 @fseek(i32* %2, i64 0, i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %36
8: ; preds = %4
%9 = tail call i64 @ftell(i32* %2)
%10 = icmp eq i64 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 @fputs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = tail call i8* @itoa(i32 -1, i32 10)
br label %39
15: ; preds = %8
%16 = trunc i64 %9 to i32
%17 = add i32 %16, 1
%18 = tail call i8* @malloc(i32 %17)
%19 = load i32, i32* @SEEK_SET
%20 = tail call i64 @fseek(i32* %2, i64 0, i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = tail call i32 @fputs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = tail call i8* @itoa(i32 -1, i32 10)
br label %39
26: ; preds = %15
%27 = tail call i64 @fread(i8* %18, i32 1, i64 %9, i32* %2)
%28 = tail call i64 @ferror(i32* %2)
%29 = icmp eq i64 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = tail call i32 @fputs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = tail call i8* @itoa(i32 -1, i32 10)
br label %39
34: ; preds = %26
%35 = getelementptr inbounds i8, i8* %18, i64 %27
store i8 0, i8* %35
br label %36
36: ; preds = %34, %4
%37 = phi i8* [ %18, %34 ], [ null, %4 ]
%38 = tail call i32 @fclose(i32* %2)
br label %39
39: ; preds = %30, %22, %11, %1, %36
%40 = phi i8* [ %37, %36 ], [ null, %1 ], [ %33, %30 ], [ %25, %22 ], [ %14, %11 ]
ret i8* %40
}