#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*/ free(unsigned char *);
scalar_t__ fstat(int, struct stat *);
scalar_t__ malloc(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char const *);
int read(int, unsigned char *, int);
unsigned char *load_file(const char *file, int *size) {
  struct stat statbuf;
  int len;
  unsigned char *buf;
  int fd;
  *size = 0;
  if ((fd = open(file, O_RDONLY)) < 0) {
    perror(file);
    return (NULL);
  }
  if (fstat(fd, &statbuf) != 0) {
    perror("stat");
    return (NULL);
  }
  len = statbuf.st_size;
  if ((buf = (unsigned char *)malloc(len)) == NULL) {
    perror("malloc");
    close(fd);
    return (NULL);
  }
  if (read(fd, buf, len) != len) {
    perror(file);
    free(buf);
    close(fd);
    return (NULL);
  }
  close(fd);
  *size = len;
  return (buf);
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    11be <load_file+0x4e>
mov    -0x10(%rbp),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1282 <load_file+0x112>
mov    -0x34(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
je     11ed <load_file+0x7d>
lea    0xe25(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1282 <load_file+0x112>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    122a <load_file+0xba>
lea    0xdf5(%rip),%rdi
call   1080 <perror@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1282 <load_file+0x112>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <read@plt>
cmp    -0x24(%rbp),%eax
je     1269 <load_file+0xf9>
mov    -0x10(%rbp),%rdi
call   1080 <perror@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1282 <load_file+0x112>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%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,%r15
movl   $0x0,(%rsi)
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11ea <load_file+0x7a>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %rax,%rax
jne    11ef <load_file+0x7f>
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1200 <load_file+0x90>
mov    %rax,%r12
mov    %ebx,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1050 <read@plt>
cmp    %ebp,%eax
jne    120e <load_file+0x9e>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,(%r14)
mov    %r12,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
jmp    11f6 <load_file+0x86>
lea    0xe0a(%rip),%rdi
call   1080 <perror@plt>
xor    %r12d,%r12d
jmp    11da <load_file+0x6a>
lea    0xdfe(%rip),%rdi
call   1080 <perror@plt>
jmp    121e <load_file+0xae>
mov    %r15,%rdi
call   1080 <perror@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %r12d,%r12d
jmp    11da <load_file+0x6a>

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

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