#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(void *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
void *malloc(size_t);
scalar_t__ stat(char const *, struct stat *);
void *get_file_contents(const char *path, void **buf, size_t *len) {
  struct stat st;
  FILE *f;
  if (stat(path, &st)) {
    return (NULL);
  }
  if (!(f = fopen(path, "rb"))) {
    return (NULL);
  }
  if (!(*buf = malloc(st.st_size))) {
    fclose(f);
    return (NULL);
  }
  if (fread(*buf, 1, st.st_size, f) != st.st_size) {
    free(*buf);
    fclose(f);
    return (*buf = NULL);
  }
  fclose(f);
  *len = st.st_size;
  return (*buf);
}


x86 assembly (clang, -O0):
<get_file_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     1198 <get_file_contents+0x38>
movq   $0x0,-0x8(%rbp)
jmp    1262 <get_file_contents+0x102>
mov    -0x10(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11c3 <get_file_contents+0x63>
movq   $0x0,-0x8(%rbp)
jmp    1262 <get_file_contents+0x102>
mov    -0x28(%rbp),%rdi
call   1070 <malloc@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    11f3 <get_file_contents+0x93>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1262 <get_file_contents+0x102>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x28(%rbp),%rax
je     1243 <get_file_contents+0xe3>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1262 <get_file_contents+0x102>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_contents>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %rax,%rax
je     118c <get_file_contents+0x2c>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe6d(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     117e <get_file_contents+0x1e>
mov    %rax,%rbx
mov    (%rsp),%r12
mov    %r12,%rdi
call   1070 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     11e4 <get_file_contents+0x84>
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    (%rsp),%rax
jne    11ee <get_file_contents+0x8e>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    (%rsp),%rax
mov    %rax,(%r14)
mov    (%r15),%rax
jmp    1180 <get_file_contents+0x20>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    117e <get_file_contents+0x1e>
mov    (%r15),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
movq   $0x0,(%r15)
jmp    117e <get_file_contents+0x1e>

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

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