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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ R_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
long fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char const *);
char *file_to_buffer(const char *path) {
  FILE *fp;
  char *buffer;
  long bytes;
  if (access(path, R_OK) != 0) {
    printf("Error: could not open '%s'\n", path);
    exit(EXIT_FAILURE);
  }
  if (!(fp = fopen(path, "r"))) {
    return NULL;
  }
  buffer = malloc(1024);
  if (!buffer) {
    fclose(fp);
    printf("Error: could not allocate memory to open '%s'\n", path);
    exit(EXIT_FAILURE);
  }
  bytes = fread(buffer, 1024, 1, fp);
  if (bytes < 0) {
    free(buffer);
    fclose(fp);
    printf("Error: could not read '%s'\n", path);
    exit(EXIT_FAILURE);
  }
  fclose(fp);
  return (char *)buffer;
}


x86 assembly (clang, -O0):
<file_to_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
je     11c6 <file_to_buffer+0x46>
mov    -0x10(%rbp),%rsi
lea    0xe4d(%rip),%rdi
call   1060 <printf@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11f1 <file_to_buffer+0x71>
movq   $0x0,-0x8(%rbp)
jmp    1298 <file_to_buffer+0x118>
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1231 <file_to_buffer+0xb1>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rsi
lea    0xe00(%rip),%rdi
call   1060 <printf@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x400,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1287 <file_to_buffer+0x107>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rsi
lea    0xdd9(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_to_buffer>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
test   %rax,%rax
jne    11f3 <file_to_buffer+0x73>
lea    0xe7a(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     11e8 <file_to_buffer+0x68>
mov    %rax,%r15
mov    $0x400,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11fc <file_to_buffer+0x7c>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x400,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
js     120d <file_to_buffer+0x8d>
mov    %r15,%rdi
call   1050 <fclose@plt>
jmp    11ea <file_to_buffer+0x6a>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe06(%rip),%rdi
jmp    1224 <file_to_buffer+0xa4>
mov    %r15,%rdi
call   1050 <fclose@plt>
lea    0xe13(%rip),%rdi
jmp    1224 <file_to_buffer+0xa4>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
lea    0xe29(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@R_OK = global i32 0
@.str = [28 x i8] c"Error: could not open '%s'\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [47 x i8] c"Error: could not allocate memory to open '%s'\0A\00"
@.str.3 = [28 x i8] c"Error: could not read '%s'\0A\00"
define i8* @file_to_buffer(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @R_OK
%9 = call i64 @access(i8* %7, i32 %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %4
%19 = icmp ne i32* %18, null
br i1 %19, label %21, label %20
20: ; preds = %16
store i8* null, i8** %2
br label %51
21: ; preds = %16
%22 = call i8* @malloc(i32 1024)
store i8* %22, i8** %5
%23 = load i8*, i8** %5
%24 = icmp ne i8* %23, null
br i1 %24, label %32, label %25
25: ; preds = %21
%26 = load i32*, i32** %4
%27 = call i32 @fclose(i32* %26)
%28 = load i8*, i8** %3
%29 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %21
%33 = load i8*, i8** %5
%34 = load i32*, i32** %4
%35 = call i64 @fread(i8* %33, i32 1024, i32 1, i32* %34)
store i64 %35, i64* %6
%36 = load i64, i64* %6
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %47
38: ; preds = %32
%39 = load i8*, i8** %5
%40 = call i32 @free(i8* %39)
%41 = load i32*, i32** %4
%42 = call i32 @fclose(i32* %41)
%43 = load i8*, i8** %3
%44 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %43)
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %32
%48 = load i32*, i32** %4
%49 = call i32 @fclose(i32* %48)
%50 = load i8*, i8** %5
store i8* %50, i8** %2
br label %51
51: ; preds = %47, %20
%52 = load i8*, i8** %2
ret i8* %52
}

LLVM IR (-O2):
@R_OK = global i32 0
@.str = [28 x i8] c"Error: could not open '%s'\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [47 x i8] c"Error: could not allocate memory to open '%s'\0A\00"
@.str.3 = [28 x i8] c"Error: could not read '%s'\0A\00"
define noalias i8* @file_to_buffer(i8* %0) {
%2 = load i32, i32* @R_OK
%3 = tail call i64 @access(i8* %0, i32 %2)
%4 = icmp eq i64 %3, 0
br i1 %4, label %9, label %5
5: ; preds = %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %1
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %31, label %12
12: ; preds = %9
%13 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = tail call i32 @fclose(i32* %10)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %12
%21 = tail call i64 @fread(i8* %13, i32 1024, i32 1, i32* %10)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = tail call i32 @free(i8* %13)
%25 = tail call i32 @fclose(i32* %10)
%26 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %20
%30 = tail call i32 @fclose(i32* %10)
br label %31
31: ; preds = %9, %29
%32 = phi i8* [ %13, %29 ], [ null, %9 ]
ret i8* %32
}