#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 */

/* Variables and functions */
int O_RDONLY;
int /*<<< orphan*/ close(int);
int filesize(int);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned char *, int);
unsigned char *file_read(char *filename, int *size) {
  unsigned char *buf = NULL;
  int n, fd = -1;
  fd = open(filename, O_RDONLY | 0);
  if (fd < 0) {
    printf("[ERR] Could open file for reading, aborting\n");
    perror(filename);
    goto error;
  }
  *size = filesize(fd);
  buf = malloc(*size);
  if (buf == NULL) {
    printf("[ERR] Could not allocate memory, aborting\n");
    goto error;
  }
  n = read(fd, buf, *size);
  if (n != *size) {
    printf("[ERR] Could not read from %s\n", filename);
    goto error;
  }
  return buf;
error:
  if (fd >= 0)
    close(fd);
  if (buf)
    free(buf);
  return NULL;
}


x86 assembly (clang, -O0):
<file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0xffffffff,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11dd <file_read+0x5d>
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <perror@plt>
jmp    1262 <file_read+0xe2>
mov    -0x28(%rbp),%edi
call   1090 <filesize@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121a <file_read+0x9a>
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1262 <file_read+0xe2>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
je     1255 <file_read+0xd5>
mov    -0x10(%rbp),%rsi
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1262 <file_read+0xe2>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1290 <file_read+0x110>
cmpl   $0x0,-0x28(%rbp)
jl     1274 <file_read+0xf4>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
cmpq   $0x0,-0x20(%rbp)
je     1288 <file_read+0x108>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     1225 <file_read+0x95>
mov    %eax,%r12d
mov    %eax,%edi
call   10a0 <filesize@plt>
mov    %eax,%ebp
mov    %eax,(%r15)
mov    %eax,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1203 <file_read+0x73>
mov    %rax,%rbx
mov    %r12d,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1070 <read@plt>
cmp    (%r15),%eax
je     1219 <file_read+0x89>
lea    0xe19(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1217 <file_read+0x87>
lea    0xe14(%rip),%rdi
call   1040 <puts@plt>
mov    %r12d,%edi
call   1060 <close@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1c(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   10b0 <perror@plt>
jmp    1217 <file_read+0x87>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [45 x i8] c"[ERR] Could open file for reading, aborting\0A\00"
@.str.1 = [43 x i8] c"[ERR] Could not allocate memory, aborting\0A\00"
@.str.2 = [30 x i8] c"[ERR] Could not read from %s\0A\00"
define i8* @file_read(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i8* null, i8** %6
store i32 -1, i32* %8
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = or i32 %10, 0
%12 = call i32 @open(i8* %9, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%17 = load i8*, i8** %4
%18 = call i32 @perror(i8* %17)
br label %45
19: ; preds = %2
%20 = load i32, i32* %8
%21 = call i32 @filesize(i32 %20)
%22 = load i32*, i32** %5
store i32 %21, i32* %22
%23 = load i32*, i32** %5
%24 = load i32, i32* %23
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %19
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %45
30: ; preds = %19
%31 = load i32, i32* %8
%32 = load i8*, i8** %6
%33 = load i32*, i32** %5
%34 = load i32, i32* %33
%35 = call i32 @read(i32 %31, i8* %32, i32 %34)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = load i32*, i32** %5
%38 = load i32, i32* %37
%39 = icmp ne i32 %36, %38
br i1 %39, label %40, label %43
40: ; preds = %30
%41 = load i8*, i8** %4
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %41)
br label %45
43: ; preds = %30
%44 = load i8*, i8** %6
store i8* %44, i8** %3
br label %58
45: ; preds = %40, %28, %15
%46 = load i32, i32* %8
%47 = icmp sge i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %48, %45
%52 = load i8*, i8** %6
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i8*, i8** %6
%56 = call i32 @free(i8* %55)
br label %57
57: ; preds = %54, %51
store i8* null, i8** %3
br label %58
58: ; preds = %57, %43
%59 = load i8*, i8** %3
ret i8* %59
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.2 = [30 x i8] c"[ERR] Could not read from %s\0A\00"
@str = [42 x i8] c"[ERR] Could not allocate memory, aborting\00"
@str.3 = [44 x i8] c"[ERR] Could open file for reading, aborting\00"
define i8* @file_read(i8* %0, i32* nocapture %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %17, label %6
6: ; preds = %2
%7 = tail call i32 @filesize(i32 %4)
store i32 %7, i32* %1
%8 = tail call i8* @malloc(i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @close(i32 %4)
br label %24
13: ; preds = %6
%14 = tail call i32 @read(i32 %4, i8* %8, i32 %7)
%15 = load i32, i32* %1
%16 = icmp eq i32 %14, %15
br i1 %16, label %24, label %20
17: ; preds = %2
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.3, i64 0, i64 0))
%19 = tail call i32 @perror(i8* %0)
br label %24
20: ; preds = %13
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%22 = tail call i32 @close(i32 %4)
%23 = tail call i32 @free(i8* %8)
br label %24
24: ; preds = %10, %17, %20, %13
%25 = phi i8* [ %8, %13 ], [ null, %20 ], [ null, %17 ], [ null, %10 ]
ret i8* %25
}