#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 /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           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 rc, fd;
  struct stat st;
  void *img;
  rc = -1;
  img = NULL;
  fd = open(path, O_RDONLY);
  if (fd < 0)
    goto out;
  rc = fstat(fd, &st);
  if (rc)
    goto out;
  img = mmap(NULL, st.st_size, PROT_WRITE, MAP_PRIVATE, 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    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1193 <kwboot_mmap_image+0x43>
jmp    120e <kwboot_mmap_image+0xbe>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <fstat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11b1 <kwboot_mmap_image+0x61>
jmp    120e <kwboot_mmap_image+0xbe>
mov    -0x20(%rbp),%rsi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
mov    0x2e0b(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2de2(%rip),%rcx
cmp    (%rcx),%rax
jne    11fc <kwboot_mmap_image+0xac>
movq   $0x0,-0x28(%rbp)
jmp    120e <kwboot_mmap_image+0xbe>
movl   $0x0,-0x14(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
cmpl   $0x0,-0x14(%rbp)
je     1238 <kwboot_mmap_image+0xe8>
cmpq   $0x0,-0x28(%rbp)
je     1238 <kwboot_mmap_image+0xe8>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <munmap@plt>
movq   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     124a <kwboot_mmap_image+0xfa>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kwboot_mmap_image>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1172 <kwboot_mmap_image+0x32>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
test   %eax,%eax
je     1177 <kwboot_mmap_image+0x37>
xor    %r15d,%r15d
jmp    11b2 <kwboot_mmap_image+0x72>
xor    %r15d,%r15d
jmp    11b9 <kwboot_mmap_image+0x79>
mov    (%rsp),%r12
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
mov    0x2e45(%rip),%rax
mov    (%rax),%ecx
xor    %r15d,%r15d
xor    %edi,%edi
mov    %r12,%rsi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%rax
je     11b2 <kwboot_mmap_image+0x72>
mov    %r12,(%r14)
mov    %rax,%r15
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
define i8* @kwboot_mmap_image(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i8*
store i8* %0, i8** %3
store i64* %1, i64** %4
store i32 -1, i32* %5
store i8* null, i8** %8
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
br label %36
15: ; preds = %2
%16 = load i32, i32* %6
%17 = call i32 @fstat(i32 %16, %struct.stat* %7)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %15
br label %36
21: ; preds = %15
%22 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%23 = load i64, i64* %22
%24 = load i32, i32* @PROT_WRITE
%25 = load i32, i32* @MAP_PRIVATE
%26 = load i32, i32* %6
%27 = call i8* @mmap(i32* null, i64 %23, i32 %24, i32 %25, i32 %26, i32 0)
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp eq i8* %28, %29
br i1 %30, label %31, label %32
31: ; preds = %21
store i8* null, i8** %8
br label %36
32: ; preds = %21
store i32 0, i32* %5
%33 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%34 = load i64, i64* %33
%35 = load i64*, i64** %4
store i64 %34, i64* %35
br label %36
36: ; preds = %32, %31, %20, %14
%37 = load i32, i32* %5
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %36
%40 = load i8*, i8** %8
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = load i8*, i8** %8
%44 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%45 = load i64, i64* %44
%46 = call i32 @munmap(i8* %43, i64 %45)
store i8* null, i8** %8
br label %47
47: ; preds = %42, %39, %36
%48 = load i32, i32* %6
%49 = icmp sge i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
br label %53
53: ; preds = %50, %47
%54 = load i8*, i8** %8
ret i8* %54
}

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