#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 EXIT_SUCCESS;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int fork();
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int main(int argc, char *argv[]) {
  int fd;
  char *file;
  if ((fd = open("mmap.c", O_RDWR, 0)) < 0) {
    err(1, "open");
  }
  file = (char *)mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  if (file == MAP_FAILED) {
    err(1, "map");
  }
  switch (fork()) {
  case -1:
    err(1, "fork");
  case 0:
    strcat(file, "!!!MODIFIED!!!\n");
    printf("Child says!\n%s", file);
    munmap(file, 4096);
    return (EXIT_SUCCESS);
  }
  printf("Parent says!\n%s", file);
  munmap(file, 4096);
  close(fd);
  return (EXIT_SUCCESS);
}


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)
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11ca <main+0x4a>
mov    $0x1,%edi
lea    0xe42(%rip),%rsi
call   1060 <err@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
mov    0x2df6(%rip),%rax
or     (%rax),%edx
mov    0x2ddd(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2daf(%rip),%rcx
cmp    (%rcx),%rax
jne    1223 <main+0xa3>
mov    $0x1,%edi
lea    0xdee(%rip),%rsi
call   1060 <err@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
sub    $0xffffffff,%eax
je     124d <main+0xcd>
jmp    123d <main+0xbd>
mov    -0x24(%rbp),%eax
test   %eax,%eax
je     125e <main+0xde>
jmp    129d <main+0x11d>
mov    $0x1,%edi
lea    0xdb7(%rip),%rsi
call   1060 <err@plt>
mov    -0x20(%rbp),%rdi
lea    0xdac(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rsi
lea    0xdac(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1000,%esi
call   1070 <munmap@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12cf <main+0x14f>
mov    -0x20(%rbp),%rsi
lea    0xd8c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1000,%esi
call   1070 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11b1 <main+0x31>
lea    0xe60(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
mov    0x2e40(%rip),%rax
mov    0x2e11(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2df6(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2dd0(%rip),%rax
cmp    %rbx,(%rax)
jne    11fe <main+0x7e>
lea    0xe18(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     121f <main+0x9f>
cmp    $0xffffffff,%eax
jne    124c <main+0xcc>
lea    0xdfb(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
lea    0xdef(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
lea    0xdf0(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1070 <munmap@plt>
jmp    126f <main+0xef>
lea    0xde1(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1070 <munmap@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"mmap.c\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [4 x i8] c"map\00"
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [16 x i8] c"!!!MODIFIED!!!\0A\00"
@.str.5 = [15 x i8] c"Child says!\0A%s\00"
@EXIT_SUCCESS = global i32 0
@.str.6 = [16 x i8] c"Parent says!\0A%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %8, i32 0)
store i32 %9, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %14, %15
%17 = load i32, i32* @MAP_PRIVATE
%18 = load i32, i32* %6
%19 = call i64 @mmap(i32* null, i32 4096, i32 %16, i32 %17, i32 %18, i32 0)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %7
%21 = load i8*, i8** %7
%22 = load i8*, i8** @MAP_FAILED
%23 = icmp eq i8* %21, %22
br i1 %23, label %24, label %26
24: ; preds = %13
%25 = call i32 @err(i32 1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %13
%27 = call i32 (...) @fork()
switch i32 %27, label %38 [
i32 -1, label %28
i32 0, label %30
]
28: ; preds = %26
%29 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %26, %28
%31 = load i8*, i8** %7
%32 = call i32 @strcat(i8* %31, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%33 = load i8*, i8** %7
%34 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %33)
%35 = load i8*, i8** %7
%36 = call i32 @munmap(i8* %35, i32 4096)
%37 = load i32, i32* @EXIT_SUCCESS
store i32 %37, i32* %3
br label %46
38: ; preds = %26
%39 = load i8*, i8** %7
%40 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %39)
%41 = load i8*, i8** %7
%42 = call i32 @munmap(i8* %41, i32 4096)
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* @EXIT_SUCCESS
store i32 %45, i32* %3
br label %46
46: ; preds = %38, %30
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@.str = [7 x i8] c"mmap.c\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [4 x i8] c"map\00"
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [16 x i8] c"!!!MODIFIED!!!\0A\00"
@.str.5 = [15 x i8] c"Child says!\0A%s\00"
@EXIT_SUCCESS = global i32 0
@.str.6 = [16 x i8] c"Parent says!\0A%s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @MAP_PRIVATE
%13 = tail call i64 @mmap(i32* null, i32 4096, i32 %11, i32 %12, i32 %4, i32 0)
%14 = inttoptr i64 %13 to i8*
%15 = load i8*, i8** @MAP_FAILED
%16 = icmp eq i8* %15, %14
br i1 %16, label %17, label %19
17: ; preds = %8
%18 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %8
%20 = tail call i32 (...) @fork()
switch i32 %20, label %27 [
i32 -1, label %21
i32 0, label %23
]
21: ; preds = %19
%22 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %19, %21
%24 = tail call i32 @strcat(i8* %14, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%25 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %14)
%26 = tail call i32 @munmap(i8* %14, i32 4096)
br label %31
27: ; preds = %19
%28 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %14)
%29 = tail call i32 @munmap(i8* %14, i32 4096)
%30 = tail call i32 @close(i32 %4)
br label %31
31: ; preds = %27, %23
%32 = load i32, i32* @EXIT_SUCCESS
ret i32 %32
}