#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 */

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int /*<<< orphan*/ O_RDWR;
int O_WRONLY;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/,
                int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  char *ptr;
  int len = 3;
  unlink("mapwrite.data");
  fd = open("mapwrite.data", O_WRONLY | O_CREAT, 0666);
  if (fd < 0) {
    perror("mapwrite.data");
    exit(1);
  }
  write(fd, "NO\n", 3);
  close(fd);
  fd = open("mapwrite.data", O_RDWR);
  if (fd < 0) {
    perror("mapwrite.data");
    exit(1);
  }
  ptr = (char *)mmap(0, len, PROT_WRITE, MAP_SHARED, fd, 0);
  if (!ptr) {
    perror("mmap(2)");
    exit(1);
  }
  close(fd);
  ptr[0] = 'O';
  ptr[1] = 'K';
  ptr[2] = '\n';
  munmap(ptr, len);
  system("cat "
         "mapwrite.data");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x3,-0x24(%rbp)
lea    0xe5c(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    0x2e2f(%rip),%rax
or     (%rax),%esi
lea    0xe3e(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1201 <main+0x71>
lea    0xe1e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    0xdf5(%rip),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1257 <main+0xc7>
lea    0xdc8(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x24(%rbp),%esi
mov    0x2d67(%rip),%rax
mov    (%rax),%edx
mov    0x2d7e(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1050 <mmap@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12a0 <main+0x110>
lea    0xd73(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%rax
movb   $0x4f,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x4b,0x1(%rax)
mov    -0x20(%rbp),%rax
movb   $0xa,0x2(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1080 <munmap@plt>
lea    0xd3a(%rip),%rdi
call   1060 <system@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe75(%rip),%rbx
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2e0e(%rip),%rax
mov    0x2e47(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
js     11ff <main+0x6f>
mov    %eax,%ebx
lea    0xe2f(%rip),%rsi
mov    %eax,%edi
mov    $0x3,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
lea    0xe1c(%rip),%rdi
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
jns    1215 <main+0x85>
lea    0xe0a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %eax,%ebx
mov    0x2daa(%rip),%rax
mov    (%rax),%edx
mov    0x2dc1(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x3,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
test   %rax,%rax
jne    1249 <main+0xb9>
lea    0xdbd(%rip),%rdi
jmp    1206 <main+0x76>
mov    %rax,%r14
mov    %ebx,%edi
call   1070 <close@plt>
movw   $0x4b4f,(%r14)
movb   $0xa,0x2(%r14)
mov    %r14,%rdi
mov    $0x3,%esi
call   1080 <munmap@plt>
lea    0xd9a(%rip),%rdi
call   1060 <system@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"mapwrite.data\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [4 x i8] c"NO\0A\00"
@O_RDWR = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [8 x i8] c"mmap(2)\00"
@.str.3 = [18 x i8] c"cat mapwrite.data\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 3, i32* %8
%9 = call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %12, i32 438)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i32, i32* %6
%21 = call i32 @write(i32 %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%22 = load i32, i32* %6
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* @O_RDWR
%25 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %19
%32 = load i32, i32* %8
%33 = load i32, i32* @PROT_WRITE
%34 = load i32, i32* @MAP_SHARED
%35 = load i32, i32* %6
%36 = call i64 @mmap(i32 0, i32 %32, i32 %33, i32 %34, i32 %35, i32 0)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %7
%38 = load i8*, i8** %7
%39 = icmp ne i8* %38, null
br i1 %39, label %43, label %40
40: ; preds = %31
%41 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %31
%44 = load i32, i32* %6
%45 = call i32 @close(i32 %44)
%46 = load i8*, i8** %7
%47 = getelementptr inbounds i8, i8* %46, i64 0
store i8 79, i8* %47
%48 = load i8*, i8** %7
%49 = getelementptr inbounds i8, i8* %48, i64 1
store i8 75, i8* %49
%50 = load i8*, i8** %7
%51 = getelementptr inbounds i8, i8* %50, i64 2
store i8 10, i8* %51
%52 = load i8*, i8** %7
%53 = load i32, i32* %8
%54 = call i32 @munmap(i8* %52, i32 %53)
%55 = call i32 @system(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%56 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [14 x i8] c"mapwrite.data\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [4 x i8] c"NO\0A\00"
@O_RDWR = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [8 x i8] c"mmap(2)\00"
@.str.3 = [18 x i8] c"cat mapwrite.data\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %6, i32 438)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = tail call i32 @write(i32 %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%14 = tail call i32 @close(i32 %7)
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = load i32, i32* @PROT_WRITE
%23 = load i32, i32* @MAP_SHARED
%24 = tail call i64 @mmap(i32 0, i32 3, i32 %22, i32 %23, i32 %16, i32 0)
%25 = inttoptr i64 %24 to i8*
%26 = icmp eq i64 %24, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %21
%31 = tail call i32 @close(i32 %16)
store i8 79, i8* %25
%32 = getelementptr inbounds i8, i8* %25, i64 1
store i8 75, i8* %32
%33 = getelementptr inbounds i8, i8* %25, i64 2
store i8 10, i8* %33
%34 = tail call i32 @munmap(i8* %25, i32 3)
%35 = tail call i32 @system(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%36 = tail call i32 @exit(i32 0)
unreachable
}