#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 */
void *__mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  printf("Calling mmap...\n");
  void *page = __mmap(NULL, 0x1000, 7, 1, -1, 0);
  int *num = page;
  if (!page)
    printf("mmap failed!\n");
  printf("mmap success!\n");
  *num = 0;
  int f = fork();
  if (f == 0) {
    printf("In child.\n");
    (*num)++;
    int last = 1;
    while (last < 50) {
      while (*num == last)
        ;
      last = *num + 1;
      (*num)++;
      printf("Child at %d\n", last);
    }
    printf("Child done\n");
  } else if (f > 0) {
    printf("In parent.\n");
    int last = 0;
    ;
    while (last < 50) {
      while (*num == last)
        ;
      last = *num + 1;
      (*num)++;
      printf("Parent at %d\n", last);
    }
    printf("Parent done!\n");
    waitpid(f, NULL, 0);
  } else {
    printf("Fork failed!\n");
  }
  return 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)
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
mov    $0x7,%edx
mov    $0x1,%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <__mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11aa <main+0x6a>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1259 <main+0x119>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
movl   $0x1,-0x28(%rbp)
cmpl   $0x32,-0x28(%rbp)
jge    1246 <main+0x106>
jmp    1205 <main+0xc5>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
cmp    -0x28(%rbp),%eax
jne    1219 <main+0xd9>
jmp    1205 <main+0xc5>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x28(%rbp),%esi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f6 <main+0xb6>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12fc <main+0x1bc>
cmpl   $0x0,-0x24(%rbp)
jle    12e9 <main+0x1a9>
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x32,-0x2c(%rbp)
jge    12c8 <main+0x188>
jmp    1287 <main+0x147>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
cmp    -0x2c(%rbp),%eax
jne    129b <main+0x15b>
jmp    1287 <main+0x147>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%esi
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1278 <main+0x138>
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
jmp    12f7 <main+0x1b7>
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12fc <main+0x1bc>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
lea    0xebc(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
mov    $0x1000,%esi
mov    $0x7,%edx
mov    $0x1,%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <__mmap@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1197 <main+0x47>
lea    0xe99(%rip),%rdi
call   1030 <puts@plt>
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,(%r12)
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     1222 <main+0xd2>
jle    1272 <main+0x122>
mov    %eax,%r14d
lea    0xe8d(%rip),%rdi
call   1030 <puts@plt>
lea    0xe3b(%rip),%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r12),%ebx
cmp    %ebp,%ebx
je     1220 <main+0xd0>
lea    0x1(%rbx),%ebp
mov    %ebp,(%r12)
mov    %r15,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x31,%ebx
jl     11e0 <main+0x90>
lea    0xe57(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
jmp    1287 <main+0x137>
nopw   0x0(%rax,%rax,1)
jmp    1220 <main+0xd0>
lea    0xe42(%rip),%rdi
call   1030 <puts@plt>
mov    (%r12),%eax
lea    0x1(%rax),%ebx
mov    %ebx,(%r12)
test   %eax,%eax
je     1270 <main+0x120>
lea    0xdbc(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0x1(%rbx),%ebp
mov    %ebp,(%r12)
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x30,%ebx
jg     127b <main+0x12b>
mov    (%r12),%ebx
cmp    %ebp,%ebx
jne    1250 <main+0x100>
jmp    1270 <main+0x120>
lea    0xdcd(%rip),%rdi
jmp    1282 <main+0x132>
lea    0xdf3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"Calling mmap...\0A\00"
@.str.1 = [14 x i8] c"mmap failed!\0A\00"
@.str.2 = [15 x i8] c"mmap success!\0A\00"
@.str.3 = [11 x i8] c"In child.\0A\00"
@.str.4 = [13 x i8] c"Child at %d\0A\00"
@.str.5 = [12 x i8] c"Child done\0A\00"
@.str.6 = [12 x i8] c"In parent.\0A\00"
@.str.7 = [14 x i8] c"Parent at %d\0A\00"
@.str.8 = [14 x i8] c"Parent done!\0A\00"
@.str.9 = [14 x i8] c"Fork failed!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%12 = call i8* @__mmap(i32* null, i32 4096, i32 7, i32 1, i32 -1, i32 0)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = bitcast i8* %13 to i32*
store i32* %14, i32** %7
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32*, i32** %7
store i32 0, i32* %21
%22 = call i32 (...) @fork()
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %51
25: ; preds = %19
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32*, i32** %7
%28 = load i32, i32* %27
%29 = add nsw i32 %28, 1
store i32 %29, i32* %27
store i32 1, i32* %9
br label %30
30: ; preds = %40, %25
%31 = load i32, i32* %9
%32 = icmp slt i32 %31, 50
br i1 %32, label %33, label %49
33: ; preds = %30
br label %34
34: ; preds = %39, %33
%35 = load i32*, i32** %7
%36 = load i32, i32* %35
%37 = load i32, i32* %9
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %34
br label %34
40: ; preds = %34
%41 = load i32*, i32** %7
%42 = load i32, i32* %41
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
%44 = load i32*, i32** %7
%45 = load i32, i32* %44
%46 = add nsw i32 %45, 1
store i32 %46, i32* %44
%47 = load i32, i32* %9
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %47)
br label %30
49: ; preds = %30
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %82
51: ; preds = %19
%52 = load i32, i32* %8
%53 = icmp sgt i32 %52, 0
br i1 %53, label %54, label %79
54: ; preds = %51
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %10
br label %56
56: ; preds = %66, %54
%57 = load i32, i32* %10
%58 = icmp slt i32 %57, 50
br i1 %58, label %59, label %75
59: ; preds = %56
br label %60
60: ; preds = %65, %59
%61 = load i32*, i32** %7
%62 = load i32, i32* %61
%63 = load i32, i32* %10
%64 = icmp eq i32 %62, %63
br i1 %64, label %65, label %66
65: ; preds = %60
br label %60
66: ; preds = %60
%67 = load i32*, i32** %7
%68 = load i32, i32* %67
%69 = add nsw i32 %68, 1
store i32 %69, i32* %10
%70 = load i32*, i32** %7
%71 = load i32, i32* %70
%72 = add nsw i32 %71, 1
store i32 %72, i32* %70
%73 = load i32, i32* %10
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 %73)
br label %56
75: ; preds = %56
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0))
%77 = load i32, i32* %8
%78 = call i32 @waitpid(i32 %77, i32* null, i32 0)
br label %81
79: ; preds = %51
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0))
br label %81
81: ; preds = %79, %75
br label %82
82: ; preds = %81, %49
ret i32 0
}

LLVM IR (-O2):
@.str.4 = [13 x i8] c"Child at %d\0A\00"
@.str.7 = [14 x i8] c"Parent at %d\0A\00"
@str = [16 x i8] c"Calling mmap...\00"
@str.10 = [13 x i8] c"mmap failed!\00"
@str.11 = [14 x i8] c"mmap success!\00"
@str.12 = [13 x i8] c"Fork failed!\00"
@str.13 = [11 x i8] c"In parent.\00"
@str.14 = [13 x i8] c"Parent done!\00"
@str.15 = [10 x i8] c"In child.\00"
@str.16 = [11 x i8] c"Child done\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%4 = tail call i8* @__mmap(i32* null, i32 4096, i32 7, i32 1, i32 -1, i32 0)
%5 = bitcast i8* %4 to i32*
%6 = icmp eq i8* %4, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.10, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.11, i64 0, i64 0))
store i32 0, i32* %5
%11 = tail call i32 (...) @fork()
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %30
13: ; preds = %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.15, i64 0, i64 0))
%15 = load i32, i32* %5
%16 = add nsw i32 %15, 1
store i32 %16, i32* %5
%17 = icmp eq i32 %15, 0
br i1 %17, label %18, label %20
18: ; preds = %25, %13
br label %19
19: ; preds = %18, %19
br label %19
20: ; preds = %13, %25
%21 = phi i32 [ %26, %25 ], [ %16, %13 ]
%22 = add nsw i32 %21, 1
store i32 %22, i32* %5
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%24 = icmp slt i32 %21, 49
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, %22
br i1 %27, label %18, label %20
28: ; preds = %20
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.16, i64 0, i64 0))
br label %48
30: ; preds = %9
%31 = icmp sgt i32 %11, 0
br i1 %31, label %32, label %46
32: ; preds = %30
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.13, i64 0, i64 0))
br label %34
34: ; preds = %32, %39
%35 = phi i32 [ 0, %32 ], [ %40, %39 ]
%36 = load i32, i32* %5
%37 = icmp eq i32 %36, %35
br i1 %37, label %38, label %39
38: ; preds = %34, %38
br label %38
39: ; preds = %34
%40 = add nsw i32 %36, 1
store i32 %40, i32* %5
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 %40)
%42 = icmp slt i32 %36, 49
br i1 %42, label %34, label %43
43: ; preds = %39
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.14, i64 0, i64 0))
%45 = tail call i32 @waitpid(i32 %11, i32* null, i32 0)
br label %48
46: ; preds = %30
%47 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.12, i64 0, i64 0))
br label %48
48: ; preds = %43, %46, %28
ret i32 0
}