#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 {
  scalar_t__ st_size;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
char const *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int);
const char *read_file_to_mem(const char *filename, size_t *filesize) {
  int fd = open(filename, O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "open for %s failed: %s\n", filename, strerror(fd));
    exit(EXIT_FAILURE);
  }
  struct stat filestat;
  int ret = fstat(fd, &filestat);
  if (ret < 0) {
    fprintf(stderr, "fstat for %s failed: %s\n", filename, strerror(fd));
    exit(EXIT_FAILURE);
  }
  const char *file =
      mmap(NULL, filestat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (file == MAP_FAILED) {
    fprintf(stderr, "mmap for %s failed: %s\n", filename, strerror(fd));
    exit(EXIT_FAILURE);
  }
  if (filesize != NULL) {
    *filesize = (size_t)filestat.st_size;
  }
  return file;
}


x86 assembly (clang, -O0):
<read_file_to_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11cf <read_file_to_mem+0x6f>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe44(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1228 <read_file_to_mem+0xc8>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xe03(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rsi
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
mov    0x2d84(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2d5b(%rip),%rcx
cmp    (%rcx),%rax
jne    12a6 <read_file_to_mem+0x146>
mov    0x2d63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xd9e(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
je     12bc <read_file_to_mem+0x15c>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_to_mem>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11d4 <read_file_to_mem+0x74>
mov    %rsp,%rsi
mov    %ebp,%edi
call   1080 <fstat@plt>
test   %eax,%eax
js     11ed <read_file_to_mem+0x8d>
mov    (%rsp),%rbx
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
mov    0x2e1d(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2dfc(%rip),%rcx
cmp    (%rcx),%rax
je     1206 <read_file_to_mem+0xa6>
test   %r15,%r15
je     11c9 <read_file_to_mem+0x69>
mov    %rbx,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2df5(%rip),%rax
mov    (%rax),%ebx
mov    %ebp,%edi
call   1070 <strerror@plt>
lea    0xe15(%rip),%rsi
jmp    121d <read_file_to_mem+0xbd>
mov    0x2ddc(%rip),%rax
mov    (%rax),%ebx
mov    %ebp,%edi
call   1070 <strerror@plt>
lea    0xe14(%rip),%rsi
jmp    121d <read_file_to_mem+0xbd>
mov    0x2dc3(%rip),%rax
mov    (%rax),%ebx
mov    %ebp,%edi
call   1070 <strerror@plt>
lea    0xe14(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"open for %s failed: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [25 x i8] c"fstat for %s failed: %s\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [24 x i8] c"mmap for %s failed: %s\0A\00"
define i8* @read_file_to_mem(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %3
store i64* %1, i64** %4
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %3
%17 = load i32, i32* %5
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
%23 = load i32, i32* %5
%24 = call i32 @fstat(i32 %23, %struct.stat* %6)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %3
%30 = load i32, i32* %5
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %29, i8* %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %22
%36 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%37 = load i64, i64* %36
%38 = load i32, i32* @PROT_READ
%39 = load i32, i32* @MAP_PRIVATE
%40 = load i32, i32* %5
%41 = call i8* @mmap(i32* null, i64 %37, i32 %38, i32 %39, i32 %40, i32 0)
store i8* %41, i8** %8
%42 = load i8*, i8** %8
%43 = load i8*, i8** @MAP_FAILED
%44 = icmp eq i8* %42, %43
br i1 %44, label %45, label %53
45: ; preds = %35
%46 = load i32, i32* @stderr
%47 = load i8*, i8** %3
%48 = load i32, i32* %5
%49 = call i8* @strerror(i32 %48)
%50 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %47, i8* %49)
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %35
%54 = load i64*, i64** %4
%55 = icmp ne i64* %54, null
br i1 %55, label %56, label %60
56: ; preds = %53
%57 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%58 = load i64, i64* %57
%59 = load i64*, i64** %4
store i64 %58, i64* %59
br label %60
60: ; preds = %56, %53
%61 = load i8*, i8** %8
ret i8* %61
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"open for %s failed: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [25 x i8] c"fstat for %s failed: %s\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [24 x i8] c"mmap for %s failed: %s\0A\00"
define i8* @read_file_to_mem(i8* %0, i64* writeonly %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i8* @strerror(i32 %5)
%10 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = bitcast %struct.stat* %3 to i8*
%15 = call i32 @fstat(i32 %5, %struct.stat* %3)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = tail call i8* @strerror(i32 %5)
%20 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %13
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%25 = load i64, i64* %24
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @MAP_PRIVATE
%28 = tail call i8* @mmap(i32* null, i64 %25, i32 %26, i32 %27, i32 %5, i32 0)
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp eq i8* %28, %29
br i1 %30, label %31, label %37
31: ; preds = %23
%32 = load i32, i32* @stderr
%33 = tail call i8* @strerror(i32 %5)
%34 = tail call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %33)
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %23
%38 = icmp eq i64* %1, null
br i1 %38, label %40, label %39
39: ; preds = %37
store i64 %25, i64* %1
br label %40
40: ; preds = %39, %37
ret i8* %28
}