#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 {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
unsigned char *malloc(int);
int open(char const *, int /*<<< orphan*/);
int read(int, unsigned char *, int);
int stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
unsigned char *load_file(const char *filepath, unsigned char **end) {
  struct stat st;
  if (stat(filepath, &st) == -1) {
    fprintf(stderr,
            "ERROR: "
            "can't find file"
            " %s\n",
            (filepath));
    ;
    return 0;
  }
  int fd = open(filepath, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr,
            "ERROR: "
            "can't open file"
            " %s\n",
            (filepath));
    ;
    return 0;
  }
  unsigned char *text = malloc(st.st_size + 1);
  if (st.st_size != read(fd, text, st.st_size)) {
    fprintf(stderr,
            "ERROR: "
            "can't read file"
            " %s\n",
            (filepath));
    ;
    close(fd);
    return 0;
  }
  close(fd);
  text[st.st_size] = '\0';
  *end = text + st.st_size;
  return text;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
jne    11ac <load_file+0x4c>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe66(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    127e <load_file+0x11e>
mov    -0x10(%rbp),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11f1 <load_file+0x91>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe3c(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    127e <load_file+0x11e>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1040 <read@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
je     1250 <load_file+0xf0>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe00(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    127e <load_file+0x11e>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x30(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rcx
movslq -0x20(%rbp),%rax
add    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     11d5 <load_file+0x75>
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11e7 <load_file+0x87>
mov    %eax,%r15d
mov    0x8(%rsp),%ebx
lea    0x1(%rbx),%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1040 <read@plt>
cmp    %eax,%ebx
jne    1201 <load_file+0xa1>
movslq %ebx,%rbx
mov    %r15d,%edi
call   1030 <close@plt>
mov    %rbp,%rax
add    %rbx,%rax
movb   $0x0,0x0(%rbp,%rbx,1)
mov    %rax,(%r14)
jmp    1223 <load_file+0xc3>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
jmp    11f7 <load_file+0x97>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
mov    %r12,%rdx
call   1050 <fprintf@plt>
jmp    1221 <load_file+0xc1>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
mov    %r12,%rdx
call   1050 <fprintf@plt>
mov    %r15d,%edi
call   1030 <close@plt>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [27 x i8] c"ERROR: can't find file %s\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [27 x i8] c"ERROR: can't open file %s\0A\00"
@.str.2 = [27 x i8] c"ERROR: can't read file %s\0A\00"
define i8* @load_file(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
%9 = load i8*, i8** %4
%10 = call i32 @stat(i8* %9, %struct.stat* %6)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %4
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %14)
store i8* null, i8** %3
br label %60
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %4
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %24)
store i8* null, i8** %3
br label %60
26: ; preds = %16
%27 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%28 = load i32, i32* %27
%29 = add nsw i32 %28, 1
%30 = call i8* @malloc(i32 %29)
store i8* %30, i8** %8
%31 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%32 = load i32, i32* %31
%33 = load i32, i32* %7
%34 = load i8*, i8** %8
%35 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @read(i32 %33, i8* %34, i32 %36)
%38 = icmp ne i32 %32, %37
br i1 %38, label %39, label %45
39: ; preds = %26
%40 = load i32, i32* @stderr
%41 = load i8*, i8** %4
%42 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %41)
%43 = load i32, i32* %7
%44 = call i32 @close(i32 %43)
store i8* null, i8** %3
br label %60
45: ; preds = %26
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
%48 = load i8*, i8** %8
%49 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%50 = load i32, i32* %49
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
store i8 0, i8* %52
%53 = load i8*, i8** %8
%54 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%55 = load i32, i32* %54
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %53, i64 %56
%58 = load i8**, i8*** %5
store i8* %57, i8** %58
%59 = load i8*, i8** %8
store i8* %59, i8** %3
br label %60
60: ; preds = %45, %39, %22, %12
%61 = load i8*, i8** %3
ret i8* %61
}

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