#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 /*<<< orphan*/ printf(char *, int, int);
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);
      printf("parent: expected %d, got %d\n", 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);
      printf("child: expected %d, got %d\n", 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    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    0xdf1(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    122c <main+0x3c>
lea    0xde3(%rip),%rdi
call   1050 <err_sys@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
mov    0x2d8c(%rip),%rax
or     (%rax),%edx
mov    0x2da3(%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    0x2d5a(%rip),%rcx
cmp    (%rcx),%rax
jne    127b <main+0x8b>
lea    0xd9f(%rip),%rdi
call   1050 <err_sys@plt>
mov    -0x8(%rbp),%edi
call   10b0 <close@plt>
mov    $0x0,%al
call   10a0 <TELL_WAIT@plt>
mov    $0x0,%al
call   1110 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    12b0 <main+0xc0>
lea    0xd7a(%rip),%rdi
call   1050 <err_sys@plt>
jmp    13a5 <main+0x1b5>
cmpq   $0x0,-0x18(%rbp)
jle    132b <main+0x13b>
movl   $0x0,-0xc(%rbp)
cmpl   $0x3e8,-0xc(%rbp)
jge    1326 <main+0x136>
mov    -0x20(%rbp),%rdi
call   1030 <update@plt>
mov    %eax,-0x10(%rbp)
cmp    -0xc(%rbp),%eax
je     12f6 <main+0x106>
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xd3a(%rip),%rdi
call   10c0 <err_quit@plt>
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xd44(%rip),%rdi
call   1090 <printf@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    12c2 <main+0xd2>
jmp    13a0 <main+0x1b0>
movl   $0x1,-0xc(%rbp)
cmpl   $0x3e9,-0xc(%rbp)
jge    139b <main+0x1ab>
mov    $0x0,%al
call   1100 <WAIT_PARENT@plt>
mov    -0x20(%rbp),%rdi
call   1030 <update@plt>
mov    %eax,-0x10(%rbp)
cmp    -0xc(%rbp),%eax
je     136d <main+0x17d>
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xcfc(%rip),%rdi
call   10c0 <err_quit@plt>
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xd05(%rip),%rdi
call   1090 <printf@plt>
mov    $0x0,%al
call   10e0 <getppid@plt>
mov    %eax,%edi
call   1070 <TELL_PARENT@plt>
mov    -0xc(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0xc(%rbp)
jmp    1332 <main+0x142>
jmp    13a0 <main+0x1b0>
jmp    13a5 <main+0x1b5>
xor    %edi,%edi
call   10f0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
lea    0xdf5(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1222 <main+0x32>
lea    0xded(%rip),%rdi
call   1050 <err_sys@plt>
mov    0x2dcf(%rip),%rax
mov    0x2d98(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dad(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x8,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1080 <mmap@plt>
mov    %rax,%r13
mov    0x2d67(%rip),%rax
cmp    (%rax),%r13
jne    126a <main+0x7a>
lea    0xdb0(%rip),%rdi
call   1050 <err_sys@plt>
mov    %ebp,%edi
call   10b0 <close@plt>
xor    %eax,%eax
call   10a0 <TELL_WAIT@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
jns    1297 <main+0xa7>
lea    0xd95(%rip),%rdi
call   1050 <err_sys@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
jne    12fc <main+0x10c>
mov    $0xffffffff,%ebp
lea    0xdda(%rip),%r14
lea    0xdb8(%rip),%r15
jmp    12d6 <main+0xe6>
xchg   %ax,%ax
mov    %r14,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1090 <printf@plt>
xor    %eax,%eax
call   10e0 <getppid@plt>
mov    %eax,%edi
call   1070 <TELL_PARENT@plt>
cmp    $0x3e7,%ebp
jae    1362 <main+0x172>
add    $0x2,%ebp
xor    %eax,%eax
call   1100 <WAIT_PARENT@plt>
mov    %r13,%rdi
call   1030 <update@plt>
mov    %eax,%ebx
cmp    %eax,%ebp
je     12b0 <main+0xc0>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   10c0 <err_quit@plt>
jmp    12b0 <main+0xc0>
mov    %rax,%r14
mov    $0xfffffffe,%ebp
lea    0xd3c(%rip),%r15
lea    0xd19(%rip),%r12
jmp    1343 <main+0x153>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1090 <printf@plt>
mov    %r14,%rdi
call   1060 <TELL_CHILD@plt>
xor    %eax,%eax
call   1040 <WAIT_CHILD@plt>
cmp    $0x3e6,%ebp
jae    1362 <main+0x172>
add    $0x2,%ebp
mov    %r13,%rdi
call   1030 <update@plt>
mov    %eax,%ebx
cmp    %eax,%ebp
je     1320 <main+0x130>
mov    %r12,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   10c0 <err_quit@plt>
jmp    1320 <main+0x130>
xor    %edi,%edi
call   10f0 <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 = [29 x i8] c"parent: expected %d, got %d\0A\00"
@.str.6 = [27 x i8] c"child: expected %d, got %d\00"
@.str.7 = [28 x i8] c"child: expected %d, got %d\0A\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 %85
31: ; preds = %23
%32 = load i64, i64* %5
%33 = icmp sgt i64 %32, 0
br i1 %33, label %34, label %59
34: ; preds = %31
store i32 0, i32* %3
br label %35
35: ; preds = %55, %34
%36 = load i32, i32* %3
%37 = icmp slt i32 %36, 1000
br i1 %37, label %38, label %58
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 i32, i32* %3
%50 = load i32, i32* %4
%51 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %49, i32 %50)
%52 = load i64, i64* %5
%53 = call i32 @TELL_CHILD(i64 %52)
%54 = call i32 (...) @WAIT_CHILD()
br label %55
55: ; preds = %48
%56 = load i32, i32* %3
%57 = add nsw i32 %56, 2
store i32 %57, i32* %3
br label %35
58: ; preds = %35
br label %84
59: ; preds = %31
store i32 1, i32* %3
br label %60
60: ; preds = %80, %59
%61 = load i32, i32* %3
%62 = icmp slt i32 %61, 1001
br i1 %62, label %63, label %83
63: ; preds = %60
%64 = call i32 (...) @WAIT_PARENT()
%65 = load i8*, i8** %6
%66 = bitcast i8* %65 to i64*
%67 = call i32 @update(i64* %66)
store i32 %67, i32* %4
%68 = load i32, i32* %3
%69 = icmp ne i32 %67, %68
br i1 %69, label %70, label %74
70: ; preds = %63
%71 = load i32, i32* %3
%72 = load i32, i32* %4
%73 = call i32 @err_quit(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %71, i32 %72)
br label %74
74: ; preds = %70, %63
%75 = load i32, i32* %3
%76 = load i32, i32* %4
%77 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i32 %75, i32 %76)
%78 = call i32 (...) @getppid()
%79 = call i32 @TELL_PARENT(i32 %78)
br label %80
80: ; preds = %74
%81 = load i32, i32* %3
%82 = add nsw i32 %81, 2
store i32 %82, i32* %3
br label %60
83: ; preds = %60
br label %84
84: ; preds = %83, %58
br label %85
85: ; preds = %84, %29
%86 = 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 = [29 x i8] c"parent: expected %d, got %d\0A\00"
@.str.6 = [27 x i8] c"child: expected %d, got %d\00"
@.str.7 = [28 x i8] c"child: expected %d, got %d\0A\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 %51
23: ; preds = %16
%24 = icmp eq i64 %19, 0
%25 = bitcast i8* %11 to i64*
br i1 %24, label %38, label %26
26: ; preds = %23, %32
%27 = phi i32 [ %36, %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 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %27, i32 %28)
%34 = tail call i32 @TELL_CHILD(i64 %19)
%35 = tail call i32 (...) @WAIT_CHILD()
%36 = add nuw nsw i32 %27, 2
%37 = icmp ult i32 %27, 998
br i1 %37, label %26, label %51
38: ; preds = %23, %45
%39 = phi i32 [ %49, %45 ], [ 1, %23 ]
%40 = tail call i32 (...) @WAIT_PARENT()
%41 = tail call i32 @update(i64* %25)
%42 = icmp eq i32 %41, %39
br i1 %42, label %45, label %43
43: ; preds = %38
%44 = tail call i32 @err_quit(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %39, i32 %41)
br label %45
45: ; preds = %43, %38
%46 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i32 %39, i32 %41)
%47 = tail call i32 (...) @getppid()
%48 = tail call i32 @TELL_PARENT(i32 %47)
%49 = add nuw nsw i32 %39, 2
%50 = icmp ult i32 %39, 999
br i1 %50, label %38, label %51
51: ; preds = %32, %45, %21
%52 = tail call i32 @exit(i32 0)
unreachable
}