#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ TELL_CHILD(scalar_t__);
int /*<<< orphan*/ TELL_PARENT(int /*<<< orphan*/);
int /*<<< orphan*/ TELL_WAIT();
int /*<<< orphan*/ WAIT_CHILD();
int /*<<< orphan*/ WAIT_PARENT();
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_quit(char *, int, int);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ getppid();
void *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int update(long *);
int main(void) {
  int fd, i, counter;
  pid_t pid;
  void *area;
  if ((fd = open("/dev/zero", O_RDWR)) < 0)
    err_sys("open error");
  if ((area = mmap(0, sizeof(long), PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                   0)) == MAP_FAILED)
    err_sys("mmap error");
  close(fd);
  TELL_WAIT();
  if ((pid = fork()) < 0) {
    err_sys("fork error");
  } else if (pid > 0) {
    for (i = 0; i < 1000; i += 2) {
      if ((counter = update((long *)area)) != i)
        err_quit("parent: expected %d, got %d", i, counter);
      TELL_CHILD(pid);
      WAIT_CHILD();
    }
  } else {
    for (i = 1; i < 1000 + 1; i += 2) {
      WAIT_PARENT();
      if ((counter = update((long *)area)) != i)
        err_quit("child: expected %d, got %d", i, counter);
      TELL_PARENT(getppid());
    }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    0xe01(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    121c <main+0x3c>
lea    0xdf3(%rip),%rdi
call   1050 <err_sys@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
mov    0x2d9c(%rip),%rax
or     (%rax),%edx
mov    0x2db3(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x8,%esi
mov    %r9d,%edi
call   1080 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d6a(%rip),%rcx
cmp    (%rcx),%rax
jne    126b <main+0x8b>
lea    0xdaf(%rip),%rdi
call   1050 <err_sys@plt>
mov    -0x8(%rbp),%edi
call   10a0 <close@plt>
mov    $0x0,%al
call   1090 <TELL_WAIT@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    12a0 <main+0xc0>
lea    0xd8a(%rip),%rdi
call   1050 <err_sys@plt>
jmp    1371 <main+0x191>
cmpq   $0x0,-0x18(%rbp)
jle    1309 <main+0x129>
movl   $0x0,-0xc(%rbp)
cmpl   $0x3e8,-0xc(%rbp)
jge    1304 <main+0x124>
mov    -0x20(%rbp),%rdi
call   1030 <update@plt>
mov    %eax,-0x10(%rbp)
cmp    -0xc(%rbp),%eax
je     12e6 <main+0x106>
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xd4a(%rip),%rdi
call   10b0 <err_quit@plt>
mov    -0x18(%rbp),%rdi
call   1060 <TELL_CHILD@plt>
mov    $0x0,%al
call   1040 <WAIT_CHILD@plt>
mov    -0xc(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0xc(%rbp)
jmp    12b2 <main+0xd2>
jmp    136c <main+0x18c>
movl   $0x1,-0xc(%rbp)
cmpl   $0x3e9,-0xc(%rbp)
jge    1367 <main+0x187>
mov    $0x0,%al
call   10f0 <WAIT_PARENT@plt>
mov    -0x20(%rbp),%rdi
call   1030 <update@plt>
mov    %eax,-0x10(%rbp)
cmp    -0xc(%rbp),%eax
je     134b <main+0x16b>
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xd01(%rip),%rdi
call   10b0 <err_quit@plt>
mov    $0x0,%al
call   10d0 <getppid@plt>
mov    %eax,%edi
call   1070 <TELL_PARENT@plt>
mov    -0xc(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0xc(%rbp)
jmp    1310 <main+0x130>
jmp    136c <main+0x18c>
jmp    1371 <main+0x191>
xor    %edi,%edi
call   10e0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    0xe09(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    120e <main+0x2e>
lea    0xe01(%rip),%rdi
call   1050 <err_sys@plt>
mov    0x2de3(%rip),%rax
mov    0x2dac(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dc1(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x8,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1080 <mmap@plt>
mov    %rax,%rbx
mov    0x2d7b(%rip),%rax
cmp    (%rax),%rbx
jne    1256 <main+0x76>
lea    0xdc4(%rip),%rdi
call   1050 <err_sys@plt>
mov    %ebp,%edi
call   10a0 <close@plt>
xor    %eax,%eax
call   1090 <TELL_WAIT@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %rax,%rax
jns    1283 <main+0xa3>
lea    0xda9(%rip),%rdi
call   1050 <err_sys@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
jne    12da <main+0xfa>
mov    $0xffffffff,%ebp
lea    0xdb6(%rip),%r14
jmp    12b6 <main+0xd6>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   10d0 <getppid@plt>
mov    %eax,%edi
call   1070 <TELL_PARENT@plt>
cmp    $0x3e7,%ebp
jae    1324 <main+0x144>
add    $0x2,%ebp
xor    %eax,%eax
call   10f0 <WAIT_PARENT@plt>
mov    %rbx,%rdi
call   1030 <update@plt>
cmp    %eax,%ebp
je     12a0 <main+0xc0>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   10b0 <err_quit@plt>
jmp    12a0 <main+0xc0>
mov    %rax,%r14
mov    $0xfffffffe,%ebp
lea    0xd42(%rip),%r15
jmp    1307 <main+0x127>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1060 <TELL_CHILD@plt>
xor    %eax,%eax
call   1040 <WAIT_CHILD@plt>
cmp    $0x3e6,%ebp
jae    1324 <main+0x144>
add    $0x2,%ebp
mov    %rbx,%rdi
call   1030 <update@plt>
cmp    %eax,%ebp
je     12f0 <main+0x110>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   10b0 <err_quit@plt>
jmp    12f0 <main+0x110>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDWR = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [11 x i8] c"mmap error\00"
@.str.3 = [11 x i8] c"fork error\00"
@.str.4 = [28 x i8] c"parent: expected %d, got %d\00"
@.str.5 = [27 x i8] c"child: expected %d, got %d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
store i32 0, i32* %1
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %13, %14
%16 = load i32, i32* @MAP_SHARED
%17 = load i32, i32* %2
%18 = call i8* @mmap(i32 0, i32 8, i32 %15, i32 %16, i32 %17, i32 0)
store i8* %18, i8** %6
%19 = load i8*, i8** @MAP_FAILED
%20 = icmp eq i8* %18, %19
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %12
%24 = load i32, i32* %2
%25 = call i32 @close(i32 %24)
%26 = call i32 (...) @TELL_WAIT()
%27 = call i64 (...) @fork()
store i64 %27, i64* %5
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %79
31: ; preds = %23
%32 = load i64, i64* %5
%33 = icmp sgt i64 %32, 0
br i1 %33, label %34, label %56
34: ; preds = %31
store i32 0, i32* %3
br label %35
35: ; preds = %52, %34
%36 = load i32, i32* %3
%37 = icmp slt i32 %36, 1000
br i1 %37, label %38, label %55
38: ; preds = %35
%39 = load i8*, i8** %6
%40 = bitcast i8* %39 to i64*
%41 = call i32 @update(i64* %40)
store i32 %41, i32* %4
%42 = load i32, i32* %3
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %48
44: ; preds = %38
%45 = load i32, i32* %3
%46 = load i32, i32* %4
%47 = call i32 @err_quit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %45, i32 %46)
br label %48
48: ; preds = %44, %38
%49 = load i64, i64* %5
%50 = call i32 @TELL_CHILD(i64 %49)
%51 = call i32 (...) @WAIT_CHILD()
br label %52
52: ; preds = %48
%53 = load i32, i32* %3
%54 = add nsw i32 %53, 2
store i32 %54, i32* %3
br label %35
55: ; preds = %35
br label %78
56: ; preds = %31
store i32 1, i32* %3
br label %57
57: ; preds = %74, %56
%58 = load i32, i32* %3
%59 = icmp slt i32 %58, 1001
br i1 %59, label %60, label %77
60: ; preds = %57
%61 = call i32 (...) @WAIT_PARENT()
%62 = load i8*, i8** %6
%63 = bitcast i8* %62 to i64*
%64 = call i32 @update(i64* %63)
store i32 %64, i32* %4
%65 = load i32, i32* %3
%66 = icmp ne i32 %64, %65
br i1 %66, label %67, label %71
67: ; preds = %60
%68 = load i32, i32* %3
%69 = load i32, i32* %4
%70 = call i32 @err_quit(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %68, i32 %69)
br label %71
71: ; preds = %67, %60
%72 = call i32 (...) @getppid()
%73 = call i32 @TELL_PARENT(i32 %72)
br label %74
74: ; preds = %71
%75 = load i32, i32* %3
%76 = add nsw i32 %75, 2
store i32 %76, i32* %3
br label %57
77: ; preds = %57
br label %78
78: ; preds = %77, %55
br label %79
79: ; preds = %78, %29
%80 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDWR = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [11 x i8] c"mmap error\00"
@.str.3 = [11 x i8] c"fork error\00"
@.str.4 = [28 x i8] c"parent: expected %d, got %d\00"
@.str.5 = [27 x i8] c"child: expected %d, got %d\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = load i32, i32* @PROT_READ
%8 = load i32, i32* @PROT_WRITE
%9 = or i32 %8, %7
%10 = load i32, i32* @MAP_SHARED
%11 = tail call i8* @mmap(i32 0, i32 8, i32 %9, i32 %10, i32 %2, i32 0)
%12 = load i8*, i8** @MAP_FAILED
%13 = icmp eq i8* %11, %12
br i1 %13, label %14, label %16
14: ; preds = %6
%15 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %6
%17 = tail call i32 @close(i32 %2)
%18 = tail call i32 (...) @TELL_WAIT()
%19 = tail call i64 (...) @fork()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %49
23: ; preds = %16
%24 = icmp eq i64 %19, 0
%25 = bitcast i8* %11 to i64*
br i1 %24, label %37, label %26
26: ; preds = %23, %32
%27 = phi i32 [ %35, %32 ], [ 0, %23 ]
%28 = tail call i32 @update(i64* %25)
%29 = icmp eq i32 %28, %27
br i1 %29, label %32, label %30
30: ; preds = %26
%31 = tail call i32 @err_quit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %27, i32 %28)
br label %32
32: ; preds = %30, %26
%33 = tail call i32 @TELL_CHILD(i64 %19)
%34 = tail call i32 (...) @WAIT_CHILD()
%35 = add nuw nsw i32 %27, 2
%36 = icmp ult i32 %27, 998
br i1 %36, label %26, label %49
37: ; preds = %23, %44
%38 = phi i32 [ %47, %44 ], [ 1, %23 ]
%39 = tail call i32 (...) @WAIT_PARENT()
%40 = tail call i32 @update(i64* %25)
%41 = icmp eq i32 %40, %38
br i1 %41, label %44, label %42
42: ; preds = %37
%43 = tail call i32 @err_quit(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %38, i32 %40)
br label %44
44: ; preds = %42, %37
%45 = tail call i32 (...) @getppid()
%46 = tail call i32 @TELL_PARENT(i32 %45)
%47 = add nuw nsw i32 %38, 2
%48 = icmp ult i32 %38, 999
br i1 %48, label %37, label %49
49: ; preds = %32, %44, %21
%50 = tail call i32 @exit(i32 0)
unreachable
}