#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 int /*<<< orphan*/ pid_t;

/* Variables and functions */
int *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ fork();
int /*<<< orphan*/ ftruncate(int, int);
int *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
          int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int shm_open(char const *, int, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  const int BIG_NUM = 100000000;
  const char *name = "untitled";
  int shm_fd;
  int *globalCounter;
  int counter;
  shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
  ftruncate(shm_fd, 1);
  globalCounter = mmap(0, 1, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
  if (globalCounter == MAP_FAILED) {
    printf("Map failed\n");
    return -1;
  }
  pid_t pid;
  pid = fork();
  if (!pid) {
    for (counter = 0; counter > -BIG_NUM; counter--) {
      globalCounter[0]--;
    }
    printf("child counter = %d\n", counter);
  } else {
    for (counter = 0; counter < BIG_NUM; counter++) {
      globalCounter[0]++;
    }
    printf("parent counter = %d\n", counter);
    wait(NULL);
    printf("global counter = %d\n", globalCounter[0]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x5f5e100,-0x8(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
mov    0x2e3b(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1060 <shm_open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1050 <ftruncate@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    0x2dff(%rip),%rax
or     (%rax),%edx
mov    0x2e16(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x1,%esi
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%rax
jne    1212 <main+0xb2>
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <main+0x16b>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1269 <main+0x109>
movl   $0x0,-0x24(%rbp)
cmpl   $0xfa0a1f00,-0x24(%rbp)
jle    1253 <main+0xf3>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    122d <main+0xcd>
mov    -0x24(%rbp),%esi
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c4 <main+0x164>
movl   $0x0,-0x24(%rbp)
cmpl   $0x5f5e100,-0x24(%rbp)
jge    1296 <main+0x136>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1270 <main+0x110>
mov    -0x24(%rbp),%esi
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <wait@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e7d(%rip),%rax
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe73(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <shm_open@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1,%esi
call   1060 <ftruncate@plt>
mov    0x2e44(%rip),%rax
mov    0x2e0d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e22(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x1,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2ddc(%rip),%rax
cmp    (%rax),%rbx
je     121e <main+0xae>
xor    %r14d,%r14d
xor    %eax,%eax
call   1090 <fork@plt>
mov    (%rbx),%ecx
test   %eax,%eax
je     1232 <main+0xc2>
add    $0x5f5e100,%ecx
mov    %ecx,(%rbx)
lea    0xe1d(%rip),%rdi
mov    $0x5f5e100,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <wait@plt>
mov    (%rbx),%esi
lea    0xe16(%rip),%rdi
jmp    1249 <main+0xd9>
lea    0xe22(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
jmp    1250 <main+0xe0>
add    $0xfa0a1f00,%ecx
mov    %ecx,(%rbx)
lea    0xdc8(%rip),%rdi
xor    %r14d,%r14d
mov    $0xfa0a1f00,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [9 x i8] c"untitled\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.1 = [12 x i8] c"Map failed\0A\00"
@.str.2 = [20 x i8] c"child counter = %d\0A\00"
@.str.3 = [21 x i8] c"parent counter = %d\0A\00"
@.str.4 = [21 x i8] c"global counter = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
store i32 100000000, i32* %2
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %3
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_RDWR
%11 = or i32 %9, %10
%12 = call i32 @shm_open(i8* %8, i32 %11, i32 438)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = call i32 @ftruncate(i32 %13, i32 1)
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %15, %16
%18 = load i32, i32* @MAP_SHARED
%19 = load i32, i32* %4
%20 = call i32* @mmap(i32 0, i32 1, i32 %17, i32 %18, i32 %19, i32 0)
store i32* %20, i32** %5
%21 = load i32*, i32** %5
%22 = load i32*, i32** @MAP_FAILED
%23 = icmp eq i32* %21, %22
br i1 %23, label %24, label %26
24: ; preds = %0
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %66
26: ; preds = %0
%27 = call i32 (...) @fork()
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp ne i32 %28, 0
br i1 %29, label %45, label %30
30: ; preds = %26
store i32 0, i32* %6
br label %31
31: ; preds = %39, %30
%32 = load i32, i32* %6
%33 = icmp sgt i32 %32, -100000000
br i1 %33, label %34, label %42
34: ; preds = %31
%35 = load i32*, i32** %5
%36 = getelementptr inbounds i32, i32* %35, i64 0
%37 = load i32, i32* %36
%38 = add nsw i32 %37, -1
store i32 %38, i32* %36
br label %39
39: ; preds = %34
%40 = load i32, i32* %6
%41 = add nsw i32 %40, -1
store i32 %41, i32* %6
br label %31
42: ; preds = %31
%43 = load i32, i32* %6
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %43)
br label %65
45: ; preds = %26
store i32 0, i32* %6
br label %46
46: ; preds = %54, %45
%47 = load i32, i32* %6
%48 = icmp slt i32 %47, 100000000
br i1 %48, label %49, label %57
49: ; preds = %46
%50 = load i32*, i32** %5
%51 = getelementptr inbounds i32, i32* %50, i64 0
%52 = load i32, i32* %51
%53 = add nsw i32 %52, 1
store i32 %53, i32* %51
br label %54
54: ; preds = %49
%55 = load i32, i32* %6
%56 = add nsw i32 %55, 1
store i32 %56, i32* %6
br label %46
57: ; preds = %46
%58 = load i32, i32* %6
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %58)
%60 = call i32 @wait(i32* null)
%61 = load i32*, i32** %5
%62 = getelementptr inbounds i32, i32* %61, i64 0
%63 = load i32, i32* %62
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %63)
br label %65
65: ; preds = %57, %42
store i32 0, i32* %1
br label %66
66: ; preds = %65, %24
%67 = load i32, i32* %1
ret i32 %67
}

LLVM IR (-O2):
@.str = [9 x i8] c"untitled\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.2 = [20 x i8] c"child counter = %d\0A\00"
@.str.3 = [21 x i8] c"parent counter = %d\0A\00"
@.str.4 = [21 x i8] c"global counter = %d\0A\00"
@str = [11 x i8] c"Map failed\00"
define i32 @main() {
%1 = load i32, i32* @O_CREAT
%2 = load i32, i32* @O_RDWR
%3 = or i32 %2, %1
%4 = tail call i32 @shm_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3, i32 438)
%5 = tail call i32 @ftruncate(i32 %4, i32 1)
%6 = load i32, i32* @PROT_READ
%7 = load i32, i32* @PROT_WRITE
%8 = or i32 %7, %6
%9 = load i32, i32* @MAP_SHARED
%10 = tail call i32* @mmap(i32 0, i32 1, i32 %8, i32 %9, i32 %4, i32 0)
%11 = load i32*, i32** @MAP_FAILED
%12 = icmp eq i32* %10, %11
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %28
15: ; preds = %0
%16 = tail call i32 (...) @fork()
%17 = icmp eq i32 %16, 0
%18 = load i32, i32* %10
br i1 %17, label %25, label %19
19: ; preds = %15
%20 = add i32 %18, 100000000
store i32 %20, i32* %10
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 100000000)
%22 = tail call i32 @wait(i32* null)
%23 = load i32, i32* %10
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %23)
br label %28
25: ; preds = %15
%26 = add i32 %18, -100000000
store i32 %26, i32* %10
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 -100000000)
br label %28
28: ; preds = %25, %19, %13
%29 = phi i32 [ -1, %13 ], [ 0, %19 ], [ 0, %25 ]
ret i32 %29
}