#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*/ uint8_t;
typedef scalar_t__ uint32_t;
typedef scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ EX_NOINPUT;
int /*<<< orphan*/ EX_SOFTWARE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *malloc(scalar_t__);
int open(char const *, int /*<<< orphan*/);
scalar_t__ read(int, int /*<<< orphan*/ *, scalar_t__);
void load_file(const char *fname, uint8_t **pptr, uint32_t *plen) {
  uint8_t *ptr;
  uint32_t len;
  off_t off;
  int f;
  f = open(fname, O_RDONLY);
  if (f < 0)
    err(EX_NOINPUT, "Cannot open file '%s'", fname);
  off = lseek(f, 0, SEEK_END);
  if (off < 0) {
    err(EX_NOINPUT,
        "Cannot seek to "
        "end of file '%s'",
        fname);
  }
  if (lseek(f, 0, SEEK_SET) < 0) {
    err(EX_NOINPUT,
        "Cannot seek to "
        "beginning of file '%s'",
        fname);
  }
  len = off;
  if (len != off)
    err(EX_NOINPUT, "File '%s' is too big", fname);
  ptr = malloc(len);
  if (ptr == NULL)
    errx(EX_SOFTWARE, "Out of memory");
  if (read(f, ptr, len) != len)
    err(EX_NOINPUT, "Cannot read all data");
  close(f);
  *pptr = ptr;
  *plen = len;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11be <load_file+0x4e>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
mov    -0x34(%rbp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11fb <load_file+0x8b>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
mov    -0x34(%rbp),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    1233 <load_file+0xc3>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1264 <load_file+0xf4>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdff(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
mov    -0x28(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1291 <load_file+0x121>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
call   1030 <errx@plt>
mov    -0x34(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <read@plt>
cmp    -0x28(%rbp),%rax
je     12c2 <load_file+0x152>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%r13d
test   %eax,%eax
jns    11b3 <load_file+0x43>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <err@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    11e8 <load_file+0x78>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <err@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
jns    121a <load_file+0xaa>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <err@plt>
mov    %rbx,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    123f <load_file+0xcf>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
call   1030 <errx@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1060 <read@plt>
cmp    %rbx,%rax
je     1269 <load_file+0xf9>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
xor    %eax,%eax
call   1070 <err@plt>
mov    %r13d,%edi
call   1050 <close@plt>
mov    %rbp,(%r15)
mov    %rbx,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@EX_NOINPUT = global i32 0
@.str = [22 x i8] c"Cannot open file '%s'\00"
@SEEK_END = global i32 0
@.str.1 = [32 x i8] c"Cannot seek to end of file '%s'\00"
@SEEK_SET = global i32 0
@.str.2 = [38 x i8] c"Cannot seek to beginning of file '%s'\00"
@.str.3 = [21 x i8] c"File '%s' is too big\00"
@EX_SOFTWARE = global i32 0
@.str.4 = [14 x i8] c"Out of memory\00"
@.str.5 = [21 x i8] c"Cannot read all data\00"
define void @load_file(i8* %0, i32** %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i32**
%6 = alloca i64*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %4
store i32** %1, i32*** %5
store i64* %2, i64** %6
%11 = load i8*, i8** %4
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %3
%17 = load i32, i32* @EX_NOINPUT
%18 = load i8*, i8** %4
%19 = call i32 (i32, i8*, ...) @err(i32 %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %16, %3
%21 = load i32, i32* %10
%22 = load i32, i32* @SEEK_END
%23 = call i64 @lseek(i32 %21, i32 0, i32 %22)
store i64 %23, i64* %9
%24 = load i64, i64* %9
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = load i32, i32* @EX_NOINPUT
%28 = load i8*, i8** %4
%29 = call i32 (i32, i8*, ...) @err(i32 %27, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %26, %20
%31 = load i32, i32* %10
%32 = load i32, i32* @SEEK_SET
%33 = call i64 @lseek(i32 %31, i32 0, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = load i32, i32* @EX_NOINPUT
%37 = load i8*, i8** %4
%38 = call i32 (i32, i8*, ...) @err(i32 %36, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %35, %30
%40 = load i64, i64* %9
store i64 %40, i64* %8
%41 = load i64, i64* %8
%42 = load i64, i64* %9
%43 = icmp ne i64 %41, %42
br i1 %43, label %44, label %48
44: ; preds = %39
%45 = load i32, i32* @EX_NOINPUT
%46 = load i8*, i8** %4
%47 = call i32 (i32, i8*, ...) @err(i32 %45, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %44, %39
%49 = load i64, i64* %8
%50 = call i32* @malloc(i64 %49)
store i32* %50, i32** %7
%51 = load i32*, i32** %7
%52 = icmp eq i32* %51, null
br i1 %52, label %53, label %56
53: ; preds = %48
%54 = load i32, i32* @EX_SOFTWARE
%55 = call i32 @errx(i32 %54, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %56
56: ; preds = %53, %48
%57 = load i32, i32* %10
%58 = load i32*, i32** %7
%59 = load i64, i64* %8
%60 = call i64 @read(i32 %57, i32* %58, i64 %59)
%61 = load i64, i64* %8
%62 = icmp ne i64 %60, %61
br i1 %62, label %63, label %66
63: ; preds = %56
%64 = load i32, i32* @EX_NOINPUT
%65 = call i32 (i32, i8*, ...) @err(i32 %64, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %66
66: ; preds = %63, %56
%67 = load i32, i32* %10
%68 = call i32 @close(i32 %67)
%69 = load i32*, i32** %7
%70 = load i32**, i32*** %5
store i32* %69, i32** %70
%71 = load i64, i64* %8
%72 = load i64*, i64** %6
store i64 %71, i64* %72
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@EX_NOINPUT = global i32 0
@.str = [22 x i8] c"Cannot open file '%s'\00"
@SEEK_END = global i32 0
@.str.1 = [32 x i8] c"Cannot seek to end of file '%s'\00"
@SEEK_SET = global i32 0
@.str.2 = [38 x i8] c"Cannot seek to beginning of file '%s'\00"
@EX_SOFTWARE = global i32 0
@.str.4 = [14 x i8] c"Out of memory\00"
@.str.5 = [21 x i8] c"Cannot read all data\00"
define void @load_file(i8* %0, i32** nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @EX_NOINPUT
%9 = tail call i32 (i32, i8*, ...) @err(i32 %8, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %10
10: ; preds = %7, %3
%11 = load i32, i32* @SEEK_END
%12 = tail call i64 @lseek(i32 %5, i32 0, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* @EX_NOINPUT
%16 = tail call i32 (i32, i8*, ...) @err(i32 %15, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %17
17: ; preds = %14, %10
%18 = load i32, i32* @SEEK_SET
%19 = tail call i64 @lseek(i32 %5, i32 0, i32 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* @EX_NOINPUT
%23 = tail call i32 (i32, i8*, ...) @err(i32 %22, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %24
24: ; preds = %17, %21
%25 = tail call i32* @malloc(i64 %12)
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @EX_SOFTWARE
%29 = tail call i32 @errx(i32 %28, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %30
30: ; preds = %27, %24
%31 = tail call i64 @read(i32 %5, i32* %25, i64 %12)
%32 = icmp eq i64 %31, %12
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = load i32, i32* @EX_NOINPUT
%35 = tail call i32 (i32, i8*, ...) @err(i32 %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %36
36: ; preds = %33, %30
%37 = tail call i32 @close(i32 %5)
store i32* %25, i32** %1
store i64 %12, i64* %2
ret void
}