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

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
int fstat(int, struct stat *);
int /*<<< orphan*/ log_fail(char *);
scalar_t__ malloc(int);
scalar_t__ mmap(int /*<<< orphan*/, int /*<<< orphan*/, int, int /*<<< orphan*/,
                int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
char *readFromFile(char *pathToFile) {
  char *content = (char *)malloc(sizeof(char) * 4096);
  int fd;
  struct stat fileStat;
  fd = open(pathToFile, O_RDWR);
  if (fd == -1) {
    return NULL;
  }
  if (fstat(fd, &fileStat) == -1) {
    log_fail("readFromFile: Failed to get stats on file.\n");
    exit(1);
  }
  if ((content = (char *)mmap(0, fileStat.st_size, PROT_READ | PROT_WRITE,
                              MAP_SHARED, fd, 0)) == MAP_FAILED) {
    log_fail("readFromFile: Failed to mmap file\n");
    exit(1);
  }
  return content;
}


x86 assembly (clang, -O0):
<readFromFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x1000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a6 <readFromFile+0x46>
movq   $0x0,-0x8(%rbp)
jmp    1230 <readFromFile+0xd0>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11d1 <readFromFile+0x71>
lea    0xe3e(%rip),%rdi
call   1040 <log_fail@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%esi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
mov    0x2de4(%rip),%rax
or     (%rax),%edx
mov    0x2dfb(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    0x2db7(%rip),%rcx
cmp    (%rcx),%rax
jne    1228 <readFromFile+0xc8>
lea    0xe13(%rip),%rdi
call   1040 <log_fail@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFromFile>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    $0x1000,%edi
call   1050 <malloc@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11dc <readFromFile+0x7c>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
je     11e4 <readFromFile+0x84>
mov    0x8(%rsp),%esi
mov    0x2e52(%rip),%rax
mov    0x2e1b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e30(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2df2(%rip),%rcx
cmp    %rax,(%rcx)
jne    11de <readFromFile+0x7e>
lea    0xe52(%rip),%rdi
jmp    11eb <readFromFile+0x8b>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe15(%rip),%rdi
call   1040 <log_fail@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@.str = [44 x i8] c"readFromFile: Failed to get stats on file.\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [35 x i8] c"readFromFile: Failed to mmap file\0A\00"
define i8* @readFromFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
store i8* %0, i8** %3
%7 = call i64 @malloc(i32 4096)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %4
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %1
store i8* null, i8** %2
br label %39
15: ; preds = %1
%16 = load i32, i32* %5
%17 = call i32 @fstat(i32 %16, %struct.stat* %6)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @log_fail(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %15
%23 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @PROT_WRITE
%27 = or i32 %25, %26
%28 = load i32, i32* @MAP_SHARED
%29 = load i32, i32* %5
%30 = call i64 @mmap(i32 0, i32 %24, i32 %27, i32 %28, i32 %29, i32 0)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %4
%32 = load i8*, i8** @MAP_FAILED
%33 = icmp eq i8* %31, %32
br i1 %33, label %34, label %37
34: ; preds = %22
%35 = call i32 @log_fail(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %22
%38 = load i8*, i8** %4
store i8* %38, i8** %2
br label %39
39: ; preds = %37, %14
%40 = load i8*, i8** %2
ret i8* %40
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@.str = [44 x i8] c"readFromFile: Failed to get stats on file.\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [35 x i8] c"readFromFile: Failed to mmap file\0A\00"
define i8* @readFromFile(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = tail call i64 @malloc(i32 4096)
%4 = bitcast %struct.stat* %2 to i8*
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %28, label %8
8: ; preds = %1
%9 = call i32 @fstat(i32 %6, %struct.stat* %2)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @log_fail(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %18, %17
%20 = load i32, i32* @MAP_SHARED
%21 = tail call i64 @mmap(i32 0, i32 %16, i32 %19, i32 %20, i32 %6, i32 0)
%22 = inttoptr i64 %21 to i8*
%23 = load i8*, i8** @MAP_FAILED
%24 = icmp eq i8* %23, %22
br i1 %24, label %25, label %28
25: ; preds = %14
%26 = tail call i32 @log_fail(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %14, %1
%29 = phi i8* [ null, %1 ], [ %22, %14 ]
ret i8* %29
}