#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*/ fail(char *);
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*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *safe_malloc(long);
char *readfile(const char *in_pathname) {
  FILE *fh;
  long size;
  char *buffer;
  size_t bytes;
  fh = fopen(in_pathname, "rb");
  if (!fh)
    fail("Can't open file for reading");
  fseek(fh, 0, SEEK_END);
  size = ftell(fh);
  rewind(fh);
  if (size > 250 * 1024 * 1024)
    fail("Out of memory reading file");
  buffer = safe_malloc(size + 1);
  bytes = fread(buffer, 1, size, fh);
  if (size != bytes)
    fail("Couldn't read whole file");
  fclose(fh);
  buffer[size] = 0;
  return buffer;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b7 <readfile+0x37>
lea    0xe51(%rip),%rdi
call   1050 <fail@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <rewind@plt>
cmpq   $0xfa00000,-0x18(%rbp)
0f
jle    11fb <readfile+0x7b>
lea    0xe29(%rip),%rdi
call   1050 <fail@plt>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1030 <safe_malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     1240 <readfile+0xc0>
lea    0xdff(%rip),%rdi
call   1050 <fail@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %r15
push   %r14
push   %rbx
lea    0xe74(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11a5 <readfile+0x25>
lea    0xe63(%rip),%rdi
call   1050 <fail@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r14,%rdi
call   1080 <ftell@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1070 <rewind@plt>
cmp    $0xfa00001,%rbx
jl     11e0 <readfile+0x60>
lea    0xe44(%rip),%rdi
call   1050 <fail@plt>
lea    0x1(%rbx),%rdi
call   1030 <safe_malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    %rax,%rbx
je     1210 <readfile+0x90>
lea    0xe2f(%rip),%rdi
call   1050 <fail@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [28 x i8] c"Can't open file for reading\00"
@SEEK_END = global i32 0
@.str.2 = [27 x i8] c"Out of memory reading file\00"
@.str.3 = [25 x i8] c"Couldn't read whole file\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 ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = icmp ne i32* %9, null
br i1 %10, label %13, label %11
11: ; preds = %1
%12 = call i32 @fail(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
%14 = load i32*, i32** %3
%15 = load i32, i32* @SEEK_END
%16 = call i32 @fseek(i32* %14, i32 0, i32 %15)
%17 = load i32*, i32** %3
%18 = call i64 @ftell(i32* %17)
store i64 %18, i64* %4
%19 = load i32*, i32** %3
%20 = call i32 @rewind(i32* %19)
%21 = load i64, i64* %4
%22 = icmp sgt i64 %21, 262144000
br i1 %22, label %23, label %25
23: ; preds = %13
%24 = call i32 @fail(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %13
%26 = load i64, i64* %4
%27 = add nsw i64 %26, 1
%28 = call i8* @safe_malloc(i64 %27)
store i8* %28, i8** %5
%29 = load i8*, i8** %5
%30 = load i64, i64* %4
%31 = load i32*, i32** %3
%32 = call i64 @fread(i8* %29, i32 1, i64 %30, i32* %31)
store i64 %32, i64* %6
%33 = load i64, i64* %4
%34 = load i64, i64* %6
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %25
%37 = call i32 @fail(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %25
%39 = load i32*, i32** %3
%40 = call i32 @fclose(i32* %39)
%41 = load i8*, i8** %5
%42 = load i64, i64* %4
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8 0, i8* %43
%44 = load i8*, i8** %5
ret i8* %44
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [28 x i8] c"Can't open file for reading\00"
@SEEK_END = global i32 0
@.str.2 = [27 x i8] c"Out of memory reading file\00"
@.str.3 = [25 x i8] c"Couldn't read whole file\00"
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 %4, label %6
4: ; preds = %1
%5 = tail call i32 @fail(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %2, i32 0, i32 %7)
%9 = tail call i64 @ftell(i32* %2)
%10 = tail call i32 @rewind(i32* %2)
%11 = icmp sgt i64 %9, 262144000
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 @fail(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %6
%15 = add nsw i64 %9, 1
%16 = tail call i8* @safe_malloc(i64 %15)
%17 = tail call i64 @fread(i8* %16, i32 1, i64 %9, i32* %2)
%18 = icmp eq i64 %9, %17
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = tail call i32 @fail(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %14
%22 = tail call i32 @fclose(i32* %2)
%23 = getelementptr inbounds i8, i8* %16, i64 %9
store i8 0, i8* %23
ret i8* %16
}