#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 */
struct stat {
  size_t st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ fstat(int, struct stat *);
char *malloc(size_t);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
size_t read(int, char *, size_t);
int /*<<< orphan*/ stderr;
void load(const char *input_file, char **input, size_t *len) {
  int input_fd;
  struct stat stat_buf;
  input_fd = open(input_file, O_RDONLY);
  if (input_fd < 0) {
    perror("open(input)");
    exit(1);
  }
  if (fstat(input_fd, &stat_buf) < 0) {
    perror("fstat");
    exit(1);
  }
  *len = stat_buf.st_size;
  *input = malloc(*len);
  if (!*input) {
    fprintf(stderr, "memory exhausted\n");
    exit(1);
  }
  if (read(input_fd, *input, *len) != *len) {
    perror("read");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11b9 <load+0x49>
lea    0xe56(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jge    11e5 <load+0x75>
lea    0xe36(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1233 <load+0xc3>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
call   1030 <read@plt>
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
je     126c <load+0xfc>
lea    0xdc7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11d4 <load+0x64>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %rax,%rax
js     11dd <load+0x6d>
mov    (%rsp),%rbx
mov    %rbx,(%r15)
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11e6 <load+0x76>
mov    %ebp,%edi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
cmp    (%r15),%rax
jne    1205 <load+0x95>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe25(%rip),%rdi
jmp    120c <load+0x9c>
lea    0xe28(%rip),%rdi
jmp    120c <load+0x9c>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe18(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [12 x i8] c"open(input)\00"
@.str.1 = [6 x i8] c"fstat\00"
@stderr = global i32 0
@.str.2 = [18 x i8] c"memory exhausted\0A\00"
@.str.3 = [5 x i8] c"read\00"
define void @load(i8* %0, i8** %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i32
%8 = alloca %struct.stat
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i64* %2, i64** %6
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %3
%18 = load i32, i32* %7
%19 = call i64 @fstat(i32 %18, %struct.stat* %8)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %17
%25 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%26 = load i64, i64* %25
%27 = load i64*, i64** %6
store i64 %26, i64* %27
%28 = load i64*, i64** %6
%29 = load i64, i64* %28
%30 = call i8* @malloc(i64 %29)
%31 = load i8**, i8*** %5
store i8* %30, i8** %31
%32 = load i8**, i8*** %5
%33 = load i8*, i8** %32
%34 = icmp ne i8* %33, null
br i1 %34, label %39, label %35
35: ; preds = %24
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %24
%40 = load i32, i32* %7
%41 = load i8**, i8*** %5
%42 = load i8*, i8** %41
%43 = load i64*, i64** %6
%44 = load i64, i64* %43
%45 = call i64 @read(i32 %40, i8* %42, i64 %44)
%46 = load i64*, i64** %6
%47 = load i64, i64* %46
%48 = icmp ne i64 %45, %47
br i1 %48, label %49, label %52
49: ; preds = %39
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %39
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [12 x i8] c"open(input)\00"
@.str.1 = [6 x i8] c"fstat\00"
@stderr = global i32 0
@.str.2 = [18 x i8] c"memory exhausted\0A\00"
@.str.3 = [5 x i8] c"read\00"
define void @load(i8* nocapture %0, i8** nocapture writeonly %1, i64* nocapture %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %3
%13 = call i64 @fstat(i32 %7, %struct.stat* %4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %12
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%20 = load i64, i64* %19
store i64 %20, i64* %2
%21 = tail call i8* @malloc(i64 %20)
store i8* %21, i8** %1
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %18
%28 = tail call i64 @read(i32 %7, i8* %21, i64 %20)
%29 = load i64, i64* %2
%30 = icmp eq i64 %28, %29
br i1 %30, label %34, label %31
31: ; preds = %27
%32 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %27
ret void
}