#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 off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(void *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
void *malloc(int);
int open(char const *, int /*<<< orphan*/);
int read(int, void *, int);
void *load_file(const char *fn, size_t *_sz) {
  int fd;
  off_t sz;
  void *data = NULL;
  fd = open(fn, O_RDONLY);
  if (fd < 0)
    goto fail;
  sz = lseek(fd, 0, SEEK_END);
  if (sz < 0)
    goto fail;
  if (lseek(fd, 0, SEEK_SET))
    goto fail;
  if ((data = malloc(sz + 4)) == NULL)
    goto fail;
  if (read(fd, data, sz) != sz)
    goto fail;
  *_sz = sz;
  return data;
fail:
  if (data)
    free(data);
  if (fd >= 0)
    close(fd);
  return NULL;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    119c <load_file+0x3c>
jmp    1235 <load_file+0xd5>
mov    -0x1c(%rbp),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11c1 <load_file+0x61>
jmp    1235 <load_file+0xd5>
mov    -0x1c(%rbp),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x0,%eax
je     11e2 <load_file+0x82>
jmp    1235 <load_file+0xd5>
mov    -0x20(%rbp),%edi
add    $0x4,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1200 <load_file+0xa0>
jmp    1235 <load_file+0xd5>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1060 <read@plt>
cmp    -0x20(%rbp),%eax
je     121d <load_file+0xbd>
jmp    1235 <load_file+0xd5>
movslq -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1263 <load_file+0x103>
cmpq   $0x0,-0x28(%rbp)
je     1249 <load_file+0xe9>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x1c(%rbp)
jl     125b <load_file+0xfb>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     11b4 <load_file+0x54>
mov    %eax,%ebp
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %eax,%eax
js     11ad <load_file+0x4d>
mov    %eax,%r15d
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %eax,%eax
je     11c4 <load_file+0x64>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x4(%r15),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11ad <load_file+0x4d>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r15d,%edx
call   1060 <read@plt>
cmp    %r15d,%eax
jne    11ef <load_file+0x8f>
mov    %r15d,%eax
mov    %rax,(%r14)
jmp    11b6 <load_file+0x56>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11ad <load_file+0x4d>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @load_file(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %5
store i8* null, i8** %8
%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 slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
br label %47
15: ; preds = %2
%16 = load i32, i32* %6
%17 = load i32, i32* @SEEK_END
%18 = call i32 @lseek(i32 %16, i32 0, i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
br label %47
22: ; preds = %15
%23 = load i32, i32* %6
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @lseek(i32 %23, i32 0, i32 %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %22
br label %47
28: ; preds = %22
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 4
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %8
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %34
33: ; preds = %28
br label %47
34: ; preds = %28
%35 = load i32, i32* %6
%36 = load i8*, i8** %8
%37 = load i32, i32* %7
%38 = call i32 @read(i32 %35, i8* %36, i32 %37)
%39 = load i32, i32* %7
%40 = icmp ne i32 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %34
br label %47
42: ; preds = %34
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = load i64*, i64** %5
store i64 %44, i64* %45
%46 = load i8*, i8** %8
store i8* %46, i8** %3
br label %60
47: ; preds = %41, %33, %27, %21, %14
%48 = load i8*, i8** %8
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i8*, i8** %8
%52 = call i32 @free(i8* %51)
br label %53
53: ; preds = %50, %47
%54 = load i32, i32* %6
%55 = icmp sge i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
br label %59
59: ; preds = %56, %53
store i8* null, i8** %3
br label %60
60: ; preds = %59, %42
%61 = load i8*, i8** %3
ret i8* %61
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @load_file(i8* nocapture %0, i64* nocapture writeonly %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %27, label %6
6: ; preds = %2
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @lseek(i32 %4, i32 0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %25, label %10
10: ; preds = %6
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @lseek(i32 %4, i32 0, i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %25
14: ; preds = %10
%15 = add nuw nsw i32 %8, 4
%16 = tail call i8* @malloc(i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %25, label %18
18: ; preds = %14
%19 = tail call i32 @read(i32 %4, i8* %16, i32 %8)
%20 = icmp eq i32 %19, %8
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = zext i32 %8 to i64
store i64 %22, i64* %1
br label %27
23: ; preds = %18
%24 = tail call i32 @free(i8* %16)
br label %25
25: ; preds = %14, %10, %6, %23
%26 = tail call i32 @close(i32 %4)
br label %27
27: ; preds = %2, %25, %21
%28 = phi i8* [ %16, %21 ], [ null, %25 ], [ null, %2 ]
ret i8* %28
}