#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;
};
typedef int ssize_t;

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
ssize_t load_file(const char *path, char **dst) {
  int fd = open(path, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr, "couldn't open file '%s': ", path);
    perror(NULL);
    return -1;
  }
  struct stat st;
  fstat(fd, &st);
  char *src = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  close(fd);
  if (src == MAP_FAILED) {
    fprintf(stderr, "couldn't load file '%s': ", path);
    perror(NULL);
    return -2;
  }
  *dst = src;
  return st.st_size;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11bd <load_file+0x5d>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5d(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124d <load_file+0xed>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
mov    -0x20(%rbp),%esi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
mov    0x2dec(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%rax
jne    123c <load_file+0xdc>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdf8(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    124d <load_file+0xed>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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,%r15
mov    %rdi,%r14
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11e3 <load_file+0x83>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
mov    0x8(%rsp),%r12d
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
mov    0x2e20(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r12d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2df5(%rip),%rax
cmp    (%rax),%rbp
je     120a <load_file+0xaa>
mov    %rbp,(%r15)
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1070 <perror@plt>
mov    $0xffffffff,%r12d
jmp    11d3 <load_file+0x73>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1070 <perror@plt>
mov    $0xfffffffe,%r12d
jmp    11d3 <load_file+0x73>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"couldn't open file '%s': \00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [26 x i8] c"couldn't load file '%s': \00"
define i32 @load_file(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %4
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @perror(i32* null)
store i32 -1, i32* %3
br label %43
19: ; preds = %2
%20 = load i32, i32* %6
%21 = call i32 @fstat(i32 %20, %struct.stat* %7)
%22 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @MAP_PRIVATE
%26 = load i32, i32* %6
%27 = call i8* @mmap(i32* null, i32 %23, i32 %24, i32 %25, i32 %26, i32 0)
store i8* %27, i8** %8
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
%30 = load i8*, i8** %8
%31 = load i8*, i8** @MAP_FAILED
%32 = icmp eq i8* %30, %31
br i1 %32, label %33, label %38
33: ; preds = %19
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %4
%36 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = call i32 @perror(i32* null)
store i32 -2, i32* %3
br label %43
38: ; preds = %19
%39 = load i8*, i8** %8
%40 = load i8**, i8*** %5
store i8* %39, i8** %40
%41 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%42 = load i32, i32* %41
store i32 %42, i32* %3
br label %43
43: ; preds = %38, %33, %14
%44 = load i32, i32* %3
ret i32 %44
}

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