#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 MAP_ANON;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int main() {
  int x;
  int *ptr;
  ptr = (int *)mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE,
                    MAP_SHARED | MAP_ANON, -1, 0);
  if (fork() == 0) {
    for (x = 1; x < 5; x++) {
      printf("child: %d\n", (*ptr)++);
      sleep(1);
    }
    exit(0);
  }
  for (x = 1; x < 5; x++) {
    printf("parent: %d\n", (*ptr)++);
    sleep(1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%edx
mov    0x2e61(%rip),%rax
or     (%rax),%edx
mov    0x2e70(%rip),%rax
mov    (%rax),%ecx
mov    0x2e47(%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    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
jne    11f8 <main+0xa8>
movl   $0x1,-0x8(%rbp)
cmpl   $0x5,-0x8(%rbp)
jge    11f1 <main+0xa1>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    %esi,%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
lea    0xe2c(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11b6 <main+0x66>
xor    %edi,%edi
call   1050 <exit@plt>
movl   $0x1,-0x8(%rbp)
cmpl   $0x5,-0x8(%rbp)
jge    123a <main+0xea>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    %esi,%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
lea    0xdee(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11ff <main+0xaf>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e9d(%rip),%rax
mov    0x2e6e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e7b(%rip),%rax
mov    0x2e54(%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
xor    %eax,%eax
call   1070 <fork@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%ecx
mov    %ecx,(%rbx)
test   %rax,%rax
je     1211 <main+0xc1>
lea    0xe61(%rip),%r14
mov    %r14,%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xde8(%rip),%r14
mov    %r14,%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANON = global i32 0
@.str = [11 x i8] c"child: %d\0A\00"
@.str.1 = [12 x i8] c"parent: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
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_ANON
%9 = or i32 %7, %8
%10 = call i64 @mmap(i32* null, i32 4, i32 %6, i32 %9, i32 -1, i32 0)
%11 = inttoptr i64 %10 to i32*
store i32* %11, i32** %3
%12 = call i64 (...) @fork()
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %29
14: ; preds = %0
store i32 1, i32* %2
br label %15
15: ; preds = %24, %14
%16 = load i32, i32* %2
%17 = icmp slt i32 %16, 5
br i1 %17, label %18, label %27
18: ; preds = %15
%19 = load i32*, i32** %3
%20 = load i32, i32* %19
%21 = add nsw i32 %20, 1
store i32 %21, i32* %19
%22 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %20)
%23 = call i32 @sleep(i32 1)
br label %24
24: ; preds = %18
%25 = load i32, i32* %2
%26 = add nsw i32 %25, 1
store i32 %26, i32* %2
br label %15
27: ; preds = %15
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %0
store i32 1, i32* %2
br label %30
30: ; preds = %39, %29
%31 = load i32, i32* %2
%32 = icmp slt i32 %31, 5
br i1 %32, label %33, label %42
33: ; preds = %30
%34 = load i32*, i32** %3
%35 = load i32, i32* %34
%36 = add nsw i32 %35, 1
store i32 %36, i32* %34
%37 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%38 = call i32 @sleep(i32 1)
br label %39
39: ; preds = %33
%40 = load i32, i32* %2
%41 = add nsw i32 %40, 1
store i32 %41, i32* %2
br label %30
42: ; preds = %30
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANON = global i32 0
@.str = [11 x i8] c"child: %d\0A\00"
@.str.1 = [12 x i8] c"parent: %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_ANON
%6 = or i32 %5, %4
%7 = tail call i64 @mmap(i32* null, i32 4, i32 %3, i32 %6, i32 -1, i32 0)
%8 = inttoptr i64 %7 to i32*
%9 = tail call i64 (...) @fork()
%10 = icmp eq i64 %9, 0
%11 = load i32, i32* %8
%12 = add nsw i32 %11, 1
store i32 %12, i32* %8
br i1 %10, label %28, label %13
13: ; preds = %0
%14 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%15 = tail call i32 @sleep(i32 1)
%16 = load i32, i32* %8
%17 = add nsw i32 %16, 1
store i32 %17, i32* %8
%18 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%19 = tail call i32 @sleep(i32 1)
%20 = load i32, i32* %8
%21 = add nsw i32 %20, 1
store i32 %21, i32* %8
%22 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%23 = tail call i32 @sleep(i32 1)
%24 = load i32, i32* %8
%25 = add nsw i32 %24, 1
store i32 %25, i32* %8
%26 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%27 = tail call i32 @sleep(i32 1)
ret i32 0
28: ; preds = %0
%29 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %11)
%30 = tail call i32 @sleep(i32 1)
%31 = load i32, i32* %8
%32 = add nsw i32 %31, 1
store i32 %32, i32* %8
%33 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %31)
%34 = tail call i32 @sleep(i32 1)
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
%37 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %35)
%38 = tail call i32 @sleep(i32 1)
%39 = load i32, i32* %8
%40 = add nsw i32 %39, 1
store i32 %40, i32* %8
%41 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %39)
%42 = tail call i32 @sleep(i32 1)
%43 = tail call i32 @exit(i32 0)
unreachable
}