#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 {
  unsigned long st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ fstat(int, struct stat *);
scalar_t__ malloc(unsigned long);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ read(int, char *, unsigned long);
char *load(char *name) {
  struct stat fbuf;
  int fd;
  unsigned long size;
  char *buf = NULL;
  if ((fd = open(name, O_RDONLY)) == -1) {
    perror("open");
    return NULL;
  }
  fstat(fd, &fbuf);
  size = fbuf.st_size;
  buf = (char *)malloc(size);
  read(fd, buf, size);
  return buf;
}


x86 assembly (clang, -O0):
<load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    119b <load+0x4b>
lea    0xe77(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d4 <load+0x84>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <fstat@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <read@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11a4 <load+0x54>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
mov    0x8(%rsp),%r15
mov    %r15,%rdi
call   1040 <malloc@plt>
mov    %rax,%r14
mov    %ebx,%edi
mov    %rax,%rsi
mov    %r15,%rdx
call   1030 <read@plt>
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe55(%rip),%rdi
call   1060 <perror@plt>
xor    %r14d,%r14d
jmp    1197 <load+0x47>

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

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
define i8* @load(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%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 %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %16
9: ; preds = %1
%10 = call i32 @fstat(i32 %5, %struct.stat* %2)
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i64, i64* %11
%13 = tail call i64 @malloc(i64 %12)
%14 = inttoptr i64 %13 to i8*
%15 = tail call i32 @read(i32 %5, i8* %14, i64 %12)
br label %16
16: ; preds = %9, %7
%17 = phi i8* [ null, %7 ], [ %14, %9 ]
ret i8* %17
}