#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 */
typedef int /*<<< orphan*/ uint8_t;
struct stat {
  int st_size;
};
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, int /*<<< orphan*/ *, int);
int32_t get_buffer_from_file(char *filename, void **buffer) {
  uint8_t *file_buffer;
  struct stat file_stat;
  int fd;
  int32_t bytes_read = 0;
  fd = open(filename, O_RDONLY);
  if (fd == -1) {
    printf("Error opening input file '%s'\n", filename);
    return -1;
  }
  if (fstat(fd, &file_stat) != 0) {
    printf("Error stat'ing file\n");
    close(fd);
    return -1;
  }
  file_buffer = (uint8_t *)malloc(file_stat.st_size);
  while (bytes_read < file_stat.st_size)
    bytes_read +=
        read(fd, (file_buffer + bytes_read), (file_stat.st_size - bytes_read));
  *buffer = file_buffer;
  close(fd);
  return bytes_read;
}


x86 assembly (clang, -O0):
<get_buffer_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11b4 <get_buffer_from_file+0x54>
mov    -0x10(%rbp),%rsi
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <get_buffer_from_file+0xe5>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     11ec <get_buffer_from_file+0x8c>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <get_buffer_from_file+0xe5>
mov    -0x28(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    122c <get_buffer_from_file+0xcc>
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x30(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x28(%rbp),%edx
sub    -0x30(%rbp),%edx
call   1050 <read@plt>
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    11f8 <get_buffer_from_file+0x98>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_buffer_from_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11c1 <get_buffer_from_file+0x51>
mov    %eax,%r12d
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %rax,%rax
je     11d9 <get_buffer_from_file+0x69>
lea    0xe72(%rip),%rdi
call   1030 <puts@plt>
mov    %r12d,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
jmp    1218 <get_buffer_from_file+0xa8>
lea    0xe38(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
jmp    1218 <get_buffer_from_file+0xa8>
mov    (%rsp),%r13d
mov    %r13d,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
xor    %ebx,%ebx
test   %r13d,%r13d
jle    120d <get_buffer_from_file+0x9d>
nop
movslq %ebx,%rbp
lea    (%r15,%rbp,4),%rsi
mov    %r13d,%edx
sub    %ebp,%edx
mov    %r12d,%edi
call   1060 <read@plt>
mov    %eax,%ebx
add    %ebp,%ebx
cmp    %ebx,%r13d
jg     11f0 <get_buffer_from_file+0x80>
mov    %r15,(%r14)
mov    %r12d,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [31 x i8] c"Error opening input file '%s'\0A\00"
@str = [20 x i8] c"Error stat'ing file\00"
define i32 @get_buffer_from_file(i8* %0, i8** nocapture 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, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %34
10: ; preds = %2
%11 = call i64 @fstat(i32 %6, %struct.stat* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @close(i32 %6)
br label %34
16: ; preds = %10
%17 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%18 = load i32, i32* %17
%19 = tail call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i32*
%21 = icmp sgt i32 %18, 0
br i1 %21, label %22, label %30
22: ; preds = %16, %22
%23 = phi i32 [ %28, %22 ], [ 0, %16 ]
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %20, i64 %24
%26 = sub nsw i32 %18, %23
%27 = tail call i32 @read(i32 %6, i32* %25, i32 %26)
%28 = add nsw i32 %27, %23
%29 = icmp sgt i32 %18, %28
br i1 %29, label %22, label %30
30: ; preds = %22, %16
%31 = phi i32 [ 0, %16 ], [ %28, %22 ]
%32 = inttoptr i64 %19 to i8*
store i8* %32, i8** %1
%33 = tail call i32 @close(i32 %6)
br label %34
34: ; preds = %30, %13, %8
%35 = phi i32 [ -1, %8 ], [ -1, %13 ], [ %31, %30 ]
ret i32 %35
}