#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;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long long);
size_t fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
unsigned char *malloc(size_t);
int /*<<< orphan*/ perror(char const *);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
unsigned char *load_file(const char *path, size_t *data_size) {
  FILE *fd;
  struct stat sb;
  unsigned char *buffer;
  size_t size;
  size_t n;
  fd = fopen(path, "rb");
  if (!fd) {
    perror(path);
    exit(EXIT_FAILURE);
  }
  if (stat(path, &sb)) {
    perror(path);
    exit(EXIT_FAILURE);
  }
  size = sb.st_size;
  buffer = malloc(size);
  if (!buffer) {
    fprintf(stderr, "Unable to allocate %lld bytes\n", (long long)size);
    exit(EXIT_FAILURE);
  }
  n = fread(buffer, 1, size, fd);
  if (n != size) {
    perror(path);
    exit(EXIT_FAILURE);
  }
  fclose(fd);
  *data_size = size;
  return buffer;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c6 <load_file+0x46>
mov    -0x8(%rbp),%rdi
call   1090 <perror@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     11f4 <load_file+0x74>
mov    -0x8(%rbp),%rdi
call   1090 <perror@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    123b <load_file+0xbb>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xddb(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     127a <load_file+0xfa>
mov    -0x8(%rbp),%rdi
call   1090 <perror@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe66(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1202 <load_file+0x82>
mov    %rax,%r12
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1060 <stat@plt>
test   %rax,%rax
jne    1202 <load_file+0x82>
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     120c <load_file+0x8c>
mov    %rax,%r13
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    %rbx,%rax
jne    1202 <load_file+0x82>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %rbx,(%r14)
mov    %r13,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    %r15,%rdi
call   1090 <perror@plt>
jmp    1224 <load_file+0xa4>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [3 x i8] c"rb\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.1 = [31 x i8] c"Unable to allocate %lld bytes\0A\00"
define i8* @load_file(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i32*
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %3
store i64* %1, i64** %4
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = icmp ne i32* %12, null
br i1 %13, label %19, label %14
14: ; preds = %2
%15 = load i8*, i8** %3
%16 = call i32 @perror(i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i8*, i8** %3
%21 = call i64 @stat(i8* %20, %struct.stat* %6)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i8*, i8** %3
%25 = call i32 @perror(i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %19
%29 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%30 = load i64, i64* %29
store i64 %30, i64* %8
%31 = load i64, i64* %8
%32 = call i8* @malloc(i64 %31)
store i8* %32, i8** %7
%33 = load i8*, i8** %7
%34 = icmp ne i8* %33, null
br i1 %34, label %41, label %35
35: ; preds = %28
%36 = load i32, i32* @stderr
%37 = load i64, i64* %8
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i64 %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %28
%42 = load i8*, i8** %7
%43 = load i64, i64* %8
%44 = load i32*, i32** %5
%45 = call i64 @fread(i8* %42, i32 1, i64 %43, i32* %44)
store i64 %45, i64* %9
%46 = load i64, i64* %9
%47 = load i64, i64* %8
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %54
49: ; preds = %41
%50 = load i8*, i8** %3
%51 = call i32 @perror(i8* %50)
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %41
%55 = load i32*, i32** %5
%56 = call i32 @fclose(i32* %55)
%57 = load i64, i64* %8
%58 = load i64*, i64** %4
store i64 %57, i64* %58
%59 = load i8*, i8** %7
ret i8* %59
}

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