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

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int fstat(int, struct stat *);
int /*<<< orphan*/ log_internal(char, char *, char const *, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void *map_file(const char *path, size_t *size) {
  int fd = open(path, O_RDONLY);
  if (fd < 0) {
    log_internal('E', "Could not open file \"%s\": %s", path, strerror(errno));
    return NULL;
  }
  struct stat st;
  int err = fstat(fd, &st);
  if (err != 0) {
    log_internal('E', "Could not stat file \"%s\": %s", path, strerror(errno));
    close(fd);
    return NULL;
  }
  size_t file_size = st.st_size;
  void *data =
      mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  close(fd);
  if (data == MAP_FAILED) {
    log_internal('E', "Could not map file \"%s\": %s", path, strerror(errno));
    return NULL;
  }
  *size = file_size;
  return data;
}


x86 assembly (clang, -O0):
<map_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11c9 <map_file+0x69>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x48(%rbp),%rdx
mov    %eax,%ecx
mov    $0x45,%edi
lea    0xe49(%rip),%rsi
call   1030 <log_internal@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c8 <map_file+0x168>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1080 <fstat@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1224 <map_file+0xc4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x50(%rbp),%rdx
mov    %eax,%ecx
mov    $0x45,%edi
lea    0xe13(%rip),%rsi
call   1030 <log_internal@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c8 <map_file+0x168>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
mov    0x2dc1(%rip),%rax
mov    (%rax),%edx
mov    0x2d88(%rip),%rax
or     (%rax),%edx
mov    0x2d77(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x40(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x40(%rbp),%rax
mov    0x2d46(%rip),%rcx
cmp    (%rcx),%rax
jne    12b5 <map_file+0x155>
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x58(%rbp),%rdx
mov    %eax,%ecx
mov    $0x45,%edi
lea    0xd97(%rip),%rsi
call   1030 <log_internal@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c8 <map_file+0x168>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11c6 <map_file+0x66>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %eax,%eax
je     11dd <map_file+0x7d>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe72(%rip),%rsi
mov    $0x45,%edi
mov    %r12,%rdx
mov    %eax,%ecx
call   1030 <log_internal@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    124c <map_file+0xec>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe25(%rip),%rsi
jmp    123d <map_file+0xdd>
mov    0x8(%rsp),%r15
mov    0x2e0f(%rip),%rax
mov    0x2dd8(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dc5(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2d9a(%rip),%rax
cmp    (%rax),%rbx
je     1228 <map_file+0xc8>
mov    %r15,(%r14)
jmp    124e <map_file+0xee>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdfd(%rip),%rsi
mov    $0x45,%edi
mov    %r12,%rdx
mov    %eax,%ecx
call   1030 <log_internal@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [29 x i8] c"Could not open file \22%s\22: %s\00"
@errno = global i32 0
@.str.1 = [29 x i8] c"Could not stat file \22%s\22: %s\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [28 x i8] c"Could not map file \22%s\22: %s\00"
define i8* @map_file(i8* %0, i64* nocapture 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 %11
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = tail call i32 @log_internal(i8 signext 69, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %9)
br label %38
11: ; preds = %2
%12 = bitcast %struct.stat* %3 to i8*
%13 = call i32 @fstat(i32 %5, %struct.stat* %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %11
%16 = load i32, i32* @errno
%17 = tail call i32 @strerror(i32 %16)
%18 = tail call i32 @log_internal(i8 signext 69, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %17)
%19 = tail call i32 @close(i32 %5)
br label %36
20: ; preds = %11
%21 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%22 = load i64, i64* %21
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @PROT_WRITE
%25 = or i32 %24, %23
%26 = load i32, i32* @MAP_PRIVATE
%27 = tail call i8* @mmap(i32* null, i64 %22, i32 %25, i32 %26, i32 %5, i32 0)
%28 = tail call i32 @close(i32 %5)
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp eq i8* %27, %29
br i1 %30, label %31, label %35
31: ; preds = %20
%32 = load i32, i32* @errno
%33 = tail call i32 @strerror(i32 %32)
%34 = tail call i32 @log_internal(i8 signext 69, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %33)
br label %36
35: ; preds = %20
store i64 %22, i64* %1
br label %36
36: ; preds = %31, %35, %15
%37 = phi i8* [ null, %15 ], [ null, %31 ], [ %27, %35 ]
br label %38
38: ; preds = %36, %7
%39 = phi i8* [ null, %7 ], [ %37, %36 ]
ret i8* %39
}