#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 */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ close(int const);
int fstat(int const, struct stat *const);
char *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int const, int /*<<< orphan*/);
int open(char const *const, int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *, char const *const, ...);
char *map_file(const char *const fn, struct stat *const st) {
  assert(fn != NULL);
  const int fd = open(fn, O_RDONLY);
  if (fd == -1) {
    warn("error: cannot open %s", fn);
    return NULL;
  }
  if (fstat(fd, st) == -1) {
    warn("error: cannot stat %s (fd %d)", fn, fd);
    (void)close(fd);
    return NULL;
  }
  char *const map = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (map == NULL) {
    warn("error: cannot map %s (fd %d)", fn, fd);
    (void)close(fd);
    return NULL;
  }
  (void)close(fd);
  return map;
}


x86 assembly (clang, -O0):
<map_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11c0 <map_file+0x60>
mov    -0x10(%rbp),%rsi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
movq   $0x0,-0x8(%rbp)
jmp    1270 <map_file+0x110>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11ff <map_file+0x9f>
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1270 <map_file+0x110>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
mov    0x2dbb(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1260 <map_file+0x100>
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1270 <map_file+0x110>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_file>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1060 <assert@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11cb <map_file+0x6b>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
je     11e1 <map_file+0x81>
mov    (%r14),%esi
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
mov    0x2e1e(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
test   %rax,%rax
je     11ea <map_file+0x8a>
mov    %rax,%r14
jmp    1200 <map_file+0xa0>
lea    0xe2e(%rip),%rdi
xor    %r14d,%r14d
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <warn@plt>
jmp    1207 <map_file+0xa7>
lea    0xe2e(%rip),%rdi
jmp    11f1 <map_file+0x91>
lea    0xe43(%rip),%rdi
xor    %r14d,%r14d
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <warn@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [22 x i8] c"error: cannot open %s\00"
@.str.1 = [30 x i8] c"error: cannot stat %s (fd %d)\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.2 = [29 x i8] c"error: cannot map %s (fd %d)\00"
define i8* @map_file(i8* %0, %struct.stat* nocapture %1) {
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 (i8*, i8*, ...) @warn(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %29
11: ; preds = %2
%12 = tail call i32 @fstat(i32 %7, %struct.stat* %1)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = tail call i32 (i8*, i8*, ...) @warn(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %7)
%16 = tail call i32 @close(i32 %7)
br label %29
17: ; preds = %11
%18 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%19 = load i32, i32* %18
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @MAP_PRIVATE
%22 = tail call i8* @mmap(i32* null, i32 %19, i32 %20, i32 %21, i32 %7, i32 0)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = tail call i32 (i8*, i8*, ...) @warn(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %7)
%26 = tail call i32 @close(i32 %7)
br label %29
27: ; preds = %17
%28 = tail call i32 @close(i32 %7)
br label %29
29: ; preds = %24, %27, %14, %9
%30 = phi i8* [ null, %9 ], [ null, %14 ], [ null, %24 ], [ %22, %27 ]
ret i8* %30
}