#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 *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *readFile(const char *zFilename) {
  FILE *in;
  char *z;
  long n;
  size_t got;
  in = fopen(zFilename, "rb");
  if (in == 0) {
    fprintf(stderr, "unable to open '%s' for reading\n", zFilename);
    exit(1);
  }
  fseek(in, 0, SEEK_END);
  n = ftell(in);
  rewind(in);
  z = malloc(n + 1);
  if (z == 0) {
    fprintf(stderr, "cannot allocate %d bytes to store '%s'\n", (int)(n + 1),
            zFilename);
    exit(1);
  }
  got = fread(z, 1, n, in);
  fclose(in);
  if (got != (size_t)n) {
    fprintf(stderr, "only read %d of %d bytes from '%s'\n", (int)got, (int)n,
            zFilename);
    exit(1);
  }
  z[n] = 0;
  return z;
}


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    11e0 <readFile+0x50>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1255 <readFile+0xc5>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %eax,%edx
mov    -0x8(%rbp),%rcx
lea    0xde0(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     12b7 <readFile+0x127>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
mov    -0x20(%rbp),%rax
mov    %eax,%ecx
mov    -0x8(%rbp),%r8
lea    0xda6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFile>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
lea    0xe5d(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     121f <readFile+0x8f>
mov    %rax,%r12
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r12,%rdi
call   1070 <ftell@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1050 <rewind@plt>
lea    0x1(%r15),%r13
mov    %r13,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1243 <readFile+0xb3>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r12,%rcx
call   1030 <fread@plt>
mov    %rax,%r13
mov    %r12,%rdi
call   1040 <fclose@plt>
cmp    %r15,%r13
jne    126a <readFile+0xda>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
mov    %r13d,%edx
mov    %r14,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
lea    0xdd2(%rip),%rsi
mov    %r13d,%edx
mov    %r15d,%ecx
mov    %r14,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"unable to open '%s' for reading\0A\00"
@SEEK_END = global i32 0
@.str.2 = [40 x i8] c"cannot allocate %d bytes to store '%s'\0A\00"
@.str.3 = [36 x i8] c"only read %d of %d bytes from '%s'\0A\00"
define i8* @readFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
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** %3
%9 = load i32*, i32** %3
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %2
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %1
%17 = load i32*, i32** %3
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = load i32*, i32** %3
%21 = call i64 @ftell(i32* %20)
store i64 %21, i64* %5
%22 = load i32*, i32** %3
%23 = call i32 @rewind(i32* %22)
%24 = load i64, i64* %5
%25 = add nsw i64 %24, 1
%26 = call i8* @malloc(i64 %25)
store i8* %26, i8** %4
%27 = load i8*, i8** %4
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %37
29: ; preds = %16
%30 = load i32, i32* @stderr
%31 = load i64, i64* %5
%32 = add nsw i64 %31, 1
%33 = trunc i64 %32 to i32
%34 = load i8*, i8** %2
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %33, i8* %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %16
%38 = load i8*, i8** %4
%39 = load i64, i64* %5
%40 = load i32*, i32** %3
%41 = call i64 @fread(i8* %38, i32 1, i64 %39, i32* %40)
store i64 %41, i64* %6
%42 = load i32*, i32** %3
%43 = call i32 @fclose(i32* %42)
%44 = load i64, i64* %6
%45 = load i64, i64* %5
%46 = icmp ne i64 %44, %45
br i1 %46, label %47, label %56
47: ; preds = %37
%48 = load i32, i32* @stderr
%49 = load i64, i64* %6
%50 = trunc i64 %49 to i32
%51 = load i64, i64* %5
%52 = trunc i64 %51 to i32
%53 = load i8*, i8** %2
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %50, i32 %52, i8* %53)
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %37
%57 = load i8*, i8** %4
%58 = load i64, i64* %5
%59 = getelementptr inbounds i8, i8* %57, i64 %58
store i8 0, i8* %59
%60 = load i8*, i8** %4
ret i8* %60
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"unable to open '%s' for reading\0A\00"
@SEEK_END = global i32 0
@.str.2 = [40 x i8] c"cannot allocate %d bytes to store '%s'\0A\00"
@.str.3 = [36 x i8] c"only read %d of %d bytes from '%s'\0A\00"
define noalias i8* @readFile(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 %8
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %1
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %2, i32 0, i32 %9)
%11 = tail call i64 @ftell(i32* %2)
%12 = tail call i32 @rewind(i32* %2)
%13 = add nsw i64 %11, 1
%14 = tail call i8* @malloc(i64 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %21
16: ; preds = %8
%17 = load i32, i32* @stderr
%18 = trunc i64 %13 to i32
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %18, i8* %0)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %8
%22 = tail call i64 @fread(i8* %14, i32 1, i64 %11, i32* %2)
%23 = tail call i32 @fclose(i32* %2)
%24 = icmp eq i64 %22, %11
br i1 %24, label %31, label %25
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = trunc i64 %22 to i32
%28 = trunc i64 %11 to i32
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %27, i32 %28, i8* %0)
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %21
%32 = getelementptr inbounds i8, i8* %14, i64 %11
store i8 0, i8* %32
ret i8* %14
}