#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int MAP_ANONYMOUS;
int *MAP_FAILED;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int munmap(int *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int wait(int /*<<< orphan*/ *);
int main() {
  int *addr;
  pid_t cpid;
  addr = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE,
              MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  if (addr == MAP_FAILED)
    perror("mmap");
  *addr = 1;
  cpid = fork();
  if (cpid < 0)
    perror("fork");
  if (cpid == 0) {
    printf("Child started, value = %d\n", *addr);
    (*addr)++;
    if (munmap(addr, sizeof(int)) == -1)
      perror("munmap");
    exit(EXIT_SUCCESS);
  } else {
    if (wait(NULL) == -1)
      perror("wait");
    printf("In parent, value = %d\n", *addr);
    if (munmap(addr, sizeof(int)) == -1)
      perror("munmap");
    exit(EXIT_SUCCESS);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
mov    0x2e39(%rip),%rax
or     (%rax),%edx
mov    0x2e50(%rip),%rax
mov    (%rax),%ecx
mov    0x2e37(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2def(%rip),%rcx
cmp    (%rcx),%rax
jne    11de <main+0x6e>
lea    0xe27(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    120a <main+0x9a>
lea    0xe00(%rip),%rdi
call   1060 <perror@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1263 <main+0xf3>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xde8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x4,%esi
call   1050 <munmap@plt>
cmp    $0xffffffff,%eax
jne    1255 <main+0xe5>
lea    0xdd5(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    1281 <main+0x111>
lea    0xdb0(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xda3(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x4,%esi
call   1050 <munmap@plt>
cmp    $0xffffffff,%eax
jne    12b6 <main+0x146>
lea    0xd74(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e80(%rip),%rax
mov    0x2e49(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e5e(%rip),%rax
mov    0x2e47(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e04(%rip),%rax
cmp    (%rax),%rbx
jne    11c5 <main+0x55>
lea    0xe40(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,(%rbx)
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jns    1235 <main+0xc5>
lea    0xe27(%rip),%rdi
call   1060 <perror@plt>
xor    %edi,%edi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    11fb <main+0x8b>
lea    0xe36(%rip),%rdi
call   1060 <perror@plt>
mov    (%rbx),%esi
lea    0xe2d(%rip),%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
call   1050 <munmap@plt>
cmp    $0xffffffff,%eax
jne    1227 <main+0xb7>
lea    0xe03(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jne    11e3 <main+0x73>
mov    (%rbx),%esi
lea    0xdca(%rip),%rdi
call   1040 <printf@plt>
addl   $0x1,(%rbx)
jmp    1209 <main+0x99>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i32* null
@.str = [5 x i8] c"mmap\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [27 x i8] c"Child started, value = %d\0A\00"
@.str.3 = [7 x i8] c"munmap\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [5 x i8] c"wait\00"
@.str.5 = [23 x i8] c"In parent, value = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i64
store i32 0, i32* %1
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %4, %5
%7 = load i32, i32* @MAP_SHARED
%8 = load i32, i32* @MAP_ANONYMOUS
%9 = or i32 %7, %8
%10 = call i32* @mmap(i32* null, i32 4, i32 %6, i32 %9, i32 -1, i32 0)
store i32* %10, i32** %2
%11 = load i32*, i32** %2
%12 = load i32*, i32** @MAP_FAILED
%13 = icmp eq i32* %11, %12
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = load i32*, i32** %2
store i32 1, i32* %17
%18 = call i64 (...) @fork()
store i64 %18, i64* %3
%19 = load i64, i64* %3
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = load i64, i64* %3
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %41
26: ; preds = %23
%27 = load i32*, i32** %2
%28 = load i32, i32* %27
%29 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = load i32*, i32** %2
%31 = load i32, i32* %30
%32 = add nsw i32 %31, 1
store i32 %32, i32* %30
%33 = load i32*, i32** %2
%34 = call i32 @munmap(i32* %33, i32 4)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %26
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %26
%39 = load i32, i32* @EXIT_SUCCESS
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %23
%42 = call i32 @wait(i32* null)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %44, %41
%47 = load i32*, i32** %2
%48 = load i32, i32* %47
%49 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %48)
%50 = load i32*, i32** %2
%51 = call i32 @munmap(i32* %50, i32 4)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %46
%54 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %53, %46
%56 = load i32, i32* @EXIT_SUCCESS
%57 = call i32 @exit(i32 %56)
unreachable
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i32* null
@.str = [5 x i8] c"mmap\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [27 x i8] c"Child started, value = %d\0A\00"
@.str.3 = [7 x i8] c"munmap\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [5 x i8] c"wait\00"
@.str.5 = [23 x i8] c"In parent, value = %d\0A\00"
define i32 @main() {
%1 = load i32, i32* @PROT_READ
%2 = load i32, i32* @PROT_WRITE
%3 = or i32 %2, %1
%4 = load i32, i32* @MAP_SHARED
%5 = load i32, i32* @MAP_ANONYMOUS
%6 = or i32 %5, %4
%7 = tail call i32* @mmap(i32* null, i32 4, i32 %3, i32 %6, i32 -1, i32 0)
%8 = load i32*, i32** @MAP_FAILED
%9 = icmp eq i32* %7, %8
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
store i32 1, i32* %7
%13 = tail call i64 (...) @fork()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %31
17: ; preds = %12
%18 = icmp eq i64 %13, 0
br i1 %18, label %19, label %31
19: ; preds = %17
%20 = load i32, i32* %7
%21 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %7
%23 = add nsw i32 %22, 1
store i32 %23, i32* %7
%24 = tail call i32 @munmap(i32* %7, i32 4)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %19
%29 = load i32, i32* @EXIT_SUCCESS
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %15, %17
%32 = tail call i32 @wait(i32* null)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %31
%37 = load i32, i32* %7
%38 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %37)
%39 = tail call i32 @munmap(i32* %7, i32 4)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %36
%44 = load i32, i32* @EXIT_SUCCESS
%45 = tail call i32 @exit(i32 %44)
unreachable
}