#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 int64_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int fstat(int, struct stat *);
int full_read(int, char *, size_t);
char *malloc(size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int open(char const *, int /*<<< orphan*/);
int64_t copy_file_to_buffer(const char *filename, char **buffer, size_t *len) {
  size_t _len;
  if (len == NULL)
    len = &_len;
  int fd = open(filename, O_RDONLY);
  if (fd == -1)
    return -1;
  struct stat info;
  if (fstat(fd, &info) == -1) {
    close(fd);
    return -1;
  }
  *len = info.st_size;
  *buffer = malloc(*len + 1);
  if (*buffer == NULL) {
    close(fd);
    return -1;
  }
  memset(*buffer, 0, *len + 1);
  int64_t total = full_read(fd, *buffer, *len);
  if (total == -1) {
    free(*buffer);
  }
  close(fd);
  return total;
}


x86 assembly (clang, -O0):
<copy_file_to_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1197 <copy_file_to_buffer+0x27>
lea    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11c2 <copy_file_to_buffer+0x52>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <copy_file_to_buffer+0x118>
mov    -0x2c(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11eb <copy_file_to_buffer+0x7b>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <copy_file_to_buffer+0x118>
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1232 <copy_file_to_buffer+0xc2>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <copy_file_to_buffer+0x118>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
add    $0x1,%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x2c(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
call   1040 <full_read@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    127a <copy_file_to_buffer+0x10a>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file_to_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
test   %rdx,%rdx
lea    0x8(%rsp),%rbx
cmovne %rdx,%rbx
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1206 <copy_file_to_buffer+0x96>
mov    %eax,%r12d
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     11fc <copy_file_to_buffer+0x8c>
mov    (%rsp),%rbp
mov    %rbp,(%rbx)
add    $0x1,%rbp
mov    %rbp,%rdi
call   1070 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11fc <copy_file_to_buffer+0x8c>
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
call   1050 <memset@plt>
mov    (%r14),%rsi
mov    (%rbx),%rdx
mov    %r12d,%edi
call   1040 <full_read@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    11fc <copy_file_to_buffer+0x8c>
mov    (%r14),%rdi
call   1030 <free@plt>
mov    %r12d,%edi
call   1060 <close@plt>
jmp    120c <copy_file_to_buffer+0x9c>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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