#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*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *file_read(const char *filename) {
  FILE *file = fopen(filename, "r");
  if (!file) {
    fprintf(stderr, "Error opening %s: ", filename);
    perror("");
    return NULL;
  }
  fseek(file, 0, SEEK_END);
  size_t size = ftell(file);
  rewind(file);
  char *buffer = (char *)malloc(size + 1);
  if (!buffer) {
    fprintf(stderr, "Could not load file (%s): Memory error: ", filename);
    perror("");
    fclose(file);
    return NULL;
  }
  buffer[size] = '\0';
  size_t size_read = fread(buffer, 1, size, file);
  if (size_read != size) {
    fprintf(stderr, "Could not load file (%s): Read error: ", filename);
    perror("");
    fclose(file);
    free(buffer);
    return NULL;
  }
  fclose(file);
  return buffer;
}


x86 assembly (clang, -O0):
<file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11fd <file_read+0x5d>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe23(%rip),%rsi
call   1070 <fprintf@plt>
lea    0xe79(%rip),%rdi
call   10c0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1307 <file_read+0x167>
mov    -0x18(%rbp),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    127e <file_read+0xde>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdbe(%rip),%rsi
call   1070 <fprintf@plt>
lea    0xe01(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1307 <file_read+0x167>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     12f6 <file_read+0x156>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd78(%rip),%rsi
call   1070 <fprintf@plt>
lea    0xd92(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1307 <file_read+0x167>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe4e(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1229 <file_read+0x89>
mov    %rax,%r12
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r12,%rdi
call   1080 <ftell@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1060 <rewind@plt>
lea    0x1(%rbx),%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     1252 <file_read+0xb2>
mov    %rax,%r15
movb   $0x0,(%rax,%rbx,1)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r12,%rcx
call   1040 <fread@plt>
cmp    %rbx,%rax
jne    1283 <file_read+0xe3>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
mov    %r14,%rdx
call   1070 <fprintf@plt>
lea    0xe1c(%rip),%rdi
call   10c0 <perror@plt>
xor    %r15d,%r15d
jmp    121a <file_read+0x7a>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
mov    %r14,%rdx
call   1070 <fprintf@plt>
lea    0xdf3(%rip),%rdi
call   10c0 <perror@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
xor    %r15d,%r15d
jmp    121a <file_read+0x7a>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    0xdab(%rip),%rsi
mov    %r14,%rdx
call   1070 <fprintf@plt>
lea    0xdc2(%rip),%rdi
call   10c0 <perror@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
jmp    121a <file_read+0x7a>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Error opening %s: \00"
@.str.2 = [1 x i8] zeroinitializer
@SEEK_END = global i32 0
@.str.3 = [41 x i8] c"Could not load file (%s): Memory error: \00"
@.str.4 = [39 x i8] c"Could not load file (%s): Read error: \00"
define i8* @file_read(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %17, label %12
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %3
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %62
17: ; preds = %1
%18 = load i32*, i32** %4
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %4
%22 = call i64 @ftell(i32* %21)
store i64 %22, i64* %5
%23 = load i32*, i32** %4
%24 = call i32 @rewind(i32* %23)
%25 = load i64, i64* %5
%26 = add i64 %25, 1
%27 = call i64 @malloc(i64 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = icmp ne i8* %29, null
br i1 %30, label %38, label %31
31: ; preds = %17
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %3
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %33)
%35 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32*, i32** %4
%37 = call i32 @fclose(i32* %36)
store i8* null, i8** %2
br label %62
38: ; preds = %17
%39 = load i8*, i8** %6
%40 = load i64, i64* %5
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %6
%43 = load i64, i64* %5
%44 = load i32*, i32** %4
%45 = call i64 @fread(i8* %42, i32 1, i64 %43, i32* %44)
store i64 %45, i64* %7
%46 = load i64, i64* %7
%47 = load i64, i64* %5
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %58
49: ; preds = %38
%50 = load i32, i32* @stderr
%51 = load i8*, i8** %3
%52 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i8* %51)
%53 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%54 = load i32*, i32** %4
%55 = call i32 @fclose(i32* %54)
%56 = load i8*, i8** %6
%57 = call i32 @free(i8* %56)
store i8* null, i8** %2
br label %62
58: ; preds = %38
%59 = load i32*, i32** %4
%60 = call i32 @fclose(i32* %59)
%61 = load i8*, i8** %6
store i8* %61, i8** %2
br label %62
62: ; preds = %58, %49, %31, %12
%63 = load i8*, i8** %2
ret i8* %63
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Error opening %s: \00"
@.str.2 = [1 x i8] zeroinitializer
@SEEK_END = global i32 0
@.str.3 = [41 x i8] c"Could not load file (%s): Memory error: \00"
@.str.4 = [39 x i8] c"Could not load file (%s): Read error: \00"
define i8* @file_read(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%7 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %34
8: ; preds = %1
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %2, i32 0, i32 %9)
%11 = tail call i64 @ftell(i32* %2)
%12 = tail call i32 @rewind(i32* %2)
%13 = add i64 %11, 1
%14 = tail call i64 @malloc(i64 %13)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %22
17: ; preds = %8
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%20 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 @fclose(i32* %2)
br label %34
22: ; preds = %8
%23 = getelementptr inbounds i8, i8* %15, i64 %11
store i8 0, i8* %23
%24 = tail call i64 @fread(i8* %15, i32 1, i64 %11, i32* %2)
%25 = icmp eq i64 %24, %11
br i1 %25, label %32, label %26
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%29 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @fclose(i32* %2)
%31 = tail call i32 @free(i8* %15)
br label %34
32: ; preds = %22
%33 = tail call i32 @fclose(i32* %2)
br label %34
34: ; preds = %17, %32, %26, %4
%35 = phi i8* [ null, %4 ], [ null, %17 ], [ null, %26 ], [ %15, %32 ]
ret i8* %35
}