#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 scalar_t__ uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fread(char *, int, scalar_t__, int /*<<< orphan*/ *);
scalar_t__ getFileSize(int /*<<< orphan*/ *);
char *malloc(scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
char *openFile(char *filename) {
  FILE *fd = fopen(filename, "rb");
  if (fd == NULL) {
    printf("%s. ", filename);
    perror("Opening file to read into memory failed.");
    exit(1);
  }
  uint32_t fileSize = getFileSize(fd);
  char *fileContents = malloc(fileSize);
  if (fread(fileContents, 1, fileSize, fd) < fileSize) {
    printf("Reading file to memory didnt work.\n");
    exit(-1);
  }
  fclose(fd);
  return fileContents;
}


x86 assembly (clang, -O0):
<openFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d3 <openFile+0x53>
mov    -0x8(%rbp),%rsi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe44(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1060 <getFileSize@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x18(%rbp),%rax
jge    1225 <openFile+0xa5>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openFile>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe61(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11eb <openFile+0x5b>
mov    %rax,%r15
mov    %rax,%rdi
call   1070 <getFileSize@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    %rbx,%rax
jl     1212 <openFile+0x82>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe11(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe05(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe18(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [5 x i8] c"%s. \00"
@.str.2 = [41 x i8] c"Opening file to read into memory failed.\00"
@.str.3 = [36 x i8] c"Reading file to memory didnt work.\0A\00"
define i8* @openFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = load i32*, i32** %3
%17 = call i64 @getFileSize(i32* %16)
store i64 %17, i64* %4
%18 = load i64, i64* %4
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %5
%20 = load i8*, i8** %5
%21 = load i64, i64* %4
%22 = load i32*, i32** %3
%23 = call i64 @fread(i8* %20, i32 1, i64 %21, i32* %22)
%24 = load i64, i64* %4
%25 = icmp slt i64 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %15
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %15
%30 = load i32*, i32** %3
%31 = call i32 @fclose(i32* %30)
%32 = load i8*, i8** %5
ret i8* %32
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [5 x i8] c"%s. \00"
@.str.2 = [41 x i8] c"Opening file to read into memory failed.\00"
@str = [35 x i8] c"Reading file to memory didnt work.\00"
define noalias i8* @openFile(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%6 = tail call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %1
%9 = tail call i64 @getFileSize(i32* %2)
%10 = tail call i8* @malloc(i64 %9)
%11 = tail call i64 @fread(i8* %10, i32 1, i64 %9, i32* %2)
%12 = icmp slt i64 %11, %9
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %8
%17 = tail call i32 @fclose(i32* %2)
ret i8* %10
}