#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 MAP_PRIVATE;
int MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, size_t, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
int open(char const *, int /*<<< orphan*/);
void *kwboot_mmap_image(const char *path, size_t *size, int prot) {
  int rc, fd, flags;
  struct stat st;
  void *img;
  rc = -1;
  fd = -1;
  img = NULL;
  fd = open(path, O_RDONLY);
  if (fd < 0)
    goto out;
  rc = fstat(fd, &st);
  if (rc)
    goto out;
  flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED;
  img = mmap(NULL, st.st_size, prot, flags, fd, 0);
  if (img == MAP_FAILED) {
    img = NULL;
    goto out;
  }
  rc = 0;
  *size = st.st_size;
out:
  if (rc && img) {
    munmap(img, st.st_size);
    img = NULL;
  }
  if (fd >= 0)
    close(fd);
  return img;
}


x86 assembly (clang, -O0):
<kwboot_mmap_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    119d <kwboot_mmap_image+0x4d>
jmp    1244 <kwboot_mmap_image+0xf4>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <fstat@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11bb <kwboot_mmap_image+0x6b>
jmp    1244 <kwboot_mmap_image+0xf4>
mov    -0x14(%rbp),%eax
mov    0x2e0b(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11e1 <kwboot_mmap_image+0x91>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    11ed <kwboot_mmap_image+0x9d>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2da4(%rip),%rcx
cmp    (%rcx),%rax
jne    1232 <kwboot_mmap_image+0xe2>
movq   $0x0,-0x30(%rbp)
jmp    1244 <kwboot_mmap_image+0xf4>
movl   $0x0,-0x18(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
cmpl   $0x0,-0x18(%rbp)
je     126e <kwboot_mmap_image+0x11e>
cmpq   $0x0,-0x30(%rbp)
je     126e <kwboot_mmap_image+0x11e>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <munmap@plt>
movq   $0x0,-0x30(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1280 <kwboot_mmap_image+0x130>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kwboot_mmap_image>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     117a <kwboot_mmap_image+0x3a>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
test   %eax,%eax
je     117e <kwboot_mmap_image+0x3e>
xor    %ebp,%ebp
jmp    11ca <kwboot_mmap_image+0x8a>
xor    %ebp,%ebp
jmp    11d1 <kwboot_mmap_image+0x91>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
and    %r15d,%eax
jne    1195 <kwboot_mmap_image+0x55>
mov    0x2e5d(%rip),%rax
jmp    119c <kwboot_mmap_image+0x5c>
mov    0x2e2c(%rip),%rax
mov    (%rax),%ecx
mov    0x8(%rsp),%r12
xor    %ebp,%ebp
xor    %edi,%edi
mov    %r12,%rsi
mov    %r15d,%edx
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e01(%rip),%rcx
cmp    (%rcx),%rax
je     11ca <kwboot_mmap_image+0x8a>
mov    %r12,(%r14)
mov    %rax,%rbp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %rbp,%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
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i8* @kwboot_mmap_image(i8* %0, i64* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %5
store i32 %2, i32* %6
store i32 -1, i32* %7
store i32 -1, i32* %8
store i8* null, i8** %11
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
br label %49
18: ; preds = %3
%19 = load i32, i32* %8
%20 = call i32 @fstat(i32 %19, %struct.stat* %10)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
br label %49
24: ; preds = %18
%25 = load i32, i32* %6
%26 = load i32, i32* @PROT_WRITE
%27 = and i32 %25, %26
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = load i32, i32* @MAP_PRIVATE
br label %33
31: ; preds = %24
%32 = load i32, i32* @MAP_SHARED
br label %33
33: ; preds = %31, %29
%34 = phi i32 [ %30, %29 ], [ %32, %31 ]
store i32 %34, i32* %9
%35 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%36 = load i64, i64* %35
%37 = load i32, i32* %6
%38 = load i32, i32* %9
%39 = load i32, i32* %8
%40 = call i8* @mmap(i32* null, i64 %36, i32 %37, i32 %38, i32 %39, i32 0)
store i8* %40, i8** %11
%41 = load i8*, i8** %11
%42 = load i8*, i8** @MAP_FAILED
%43 = icmp eq i8* %41, %42
br i1 %43, label %44, label %45
44: ; preds = %33
store i8* null, i8** %11
br label %49
45: ; preds = %33
store i32 0, i32* %7
%46 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%47 = load i64, i64* %46
%48 = load i64*, i64** %5
store i64 %47, i64* %48
br label %49
49: ; preds = %45, %44, %23, %17
%50 = load i32, i32* %7
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %60
52: ; preds = %49
%53 = load i8*, i8** %11
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %60
55: ; preds = %52
%56 = load i8*, i8** %11
%57 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%58 = load i64, i64* %57
%59 = call i32 @munmap(i8* %56, i64 %58)
store i8* null, i8** %11
br label %60
60: ; preds = %55, %52, %49
%61 = load i32, i32* %8
%62 = icmp sge i32 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = load i32, i32* %8
%65 = call i32 @close(i32 %64)
br label %66
66: ; preds = %63, %60
%67 = load i8*, i8** %11
ret i8* %67
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i8* @kwboot_mmap_image(i8* nocapture %0, i64* nocapture writeonly %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %28, label %9
9: ; preds = %3
%10 = call i32 @fstat(i32 %7, %struct.stat* %4)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %25
12: ; preds = %9
%13 = load i32, i32* @PROT_WRITE
%14 = and i32 %13, %2
%15 = icmp eq i32 %14, 0
%16 = load i32, i32* @MAP_PRIVATE
%17 = load i32, i32* @MAP_SHARED
%18 = select i1 %15, i32 %17, i32 %16
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%20 = load i64, i64* %19
%21 = tail call i8* @mmap(i32* null, i64 %20, i32 %2, i32 %18, i32 %7, i32 0)
%22 = load i8*, i8** @MAP_FAILED
%23 = icmp eq i8* %21, %22
br i1 %23, label %25, label %24
24: ; preds = %12
store i64 %20, i64* %1
br label %25
25: ; preds = %24, %9, %12
%26 = phi i8* [ null, %12 ], [ %21, %24 ], [ null, %9 ]
%27 = tail call i32 @close(i32 %7)
br label %28
28: ; preds = %3, %25
%29 = phi i8* [ %26, %25 ], [ null, %3 ]
ret i8* %29
}