#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(void *);
int fstat(int, struct stat *);
void *malloc(int);
int open(char *, int /*<<< orphan*/);
int read(int, void *, int);
void *getfile(char *path, int *n_bytes) {
  int fd;
  struct stat st;
  void *ptr = NULL;
  int ret;
  fd = open(path, O_RDONLY);
  if (!fd)
    return NULL;
  ret = fstat(fd, &st);
  if (ret < 0) {
    close(fd);
    return NULL;
  }
  ptr = malloc(st.st_size);
  if (!ptr) {
    close(fd);
    return NULL;
  }
  ret = read(fd, ptr, st.st_size);
  if (ret != st.st_size) {
    free(ptr);
    close(fd);
    return NULL;
  }
  if (n_bytes)
    *n_bytes = st.st_size;
  close(fd);
  return ptr;
}


x86 assembly (clang, -O0):
<getfile>:
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    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11a4 <getfile+0x44>
movq   $0x0,-0x8(%rbp)
jmp    125e <getfile+0xfe>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11d2 <getfile+0x72>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    125e <getfile+0xfe>
mov    -0x20(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11fe <getfile+0x9e>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    125e <getfile+0xfe>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x20(%rbp),%eax
je     123a <getfile+0xda>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    125e <getfile+0xfe>
cmpq   $0x0,-0x18(%rbp)
je     124e <getfile+0xee>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
je     11bc <getfile+0x5c>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %eax,%eax
js     11c8 <getfile+0x68>
mov    (%rsp),%r15d
mov    %r15d,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11c8 <getfile+0x68>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r15d,%edx
call   1050 <read@plt>
cmp    %r15d,%eax
jne    11c0 <getfile+0x60>
test   %r14,%r14
je     11ca <getfile+0x6a>
mov    %r15d,(%r14)
jmp    11ca <getfile+0x6a>
xor    %ebx,%ebx
jmp    11d1 <getfile+0x71>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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