#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 */
int /*<<< orphan*/ SYNC_INIT();
int /*<<< orphan*/ SYNC_TELL(scalar_t__, int);
int /*<<< orphan*/ SYNC_WAIT(int);
int WEXITSTATUS(int);
int /*<<< orphan*/ err_sys(char *);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int read_increase_write();
scalar_t__ wait(int *);
int main(void) {
  int ret = 0;
  pid_t cid = 0;
  SYNC_INIT();
  cid = fork();
  if (cid < 0) {
    err_sys("fork error");
    return -1;
  } else if (cid == 0) {
    printf("child %u running\n", getpid());
    while (ret < 10) {
      ret = read_increase_write();
      SYNC_TELL(getppid(), 0);
      printf("notify parent\n");
      if (ret < 0 || ret >= 10)
        break;
      SYNC_WAIT(1);
      printf("wait parent %u\n", getppid());
    }
    return 2;
  }
  while (ret < 10 && ret >= 0) {
    SYNC_WAIT(0);
    printf("wait child\n");
    ret = read_increase_write();
    SYNC_TELL(cid, 1);
    printf("notify child\n");
  }
  pid_t wid = 0;
  int status = 0;
  if ((wid = wait(&status)) < 0)
    err_sys("wait error");
  printf("wait child %u exit %d\n", wid, WEXITSTATUS(status));
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1080 <SYNC_INIT@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1203 <main+0x53>
lea    0xe0e(%rip),%rdi
call   1030 <err_sys@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1373 <main+0x1c3>
cmpq   $0x0,-0x10(%rbp)
jne    12a3 <main+0xf3>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0xa,-0x8(%rbp)
jge    1297 <main+0xe7>
mov    $0x0,%al
call   1070 <read_increase_write@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   10b0 <getppid@plt>
movslq %eax,%rdi
xor    %esi,%esi
call   1050 <SYNC_TELL@plt>
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jl     126c <main+0xbc>
cmpl   $0xa,-0x8(%rbp)
jl     1271 <main+0xc1>
jmp    1297 <main+0xe7>
mov    $0x1,%edi
call   1090 <SYNC_WAIT@plt>
mov    $0x0,%al
call   10b0 <getppid@plt>
mov    %eax,%esi
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1225 <main+0x75>
movl   $0x2,-0x4(%rbp)
jmp    1373 <main+0x1c3>
jmp    12a8 <main+0xf8>
jmp    12ad <main+0xfd>
xor    %eax,%eax
cmpl   $0xa,-0x8(%rbp)
mov    %al,-0x1d(%rbp)
jge    12c6 <main+0x116>
cmpl   $0x0,-0x8(%rbp)
setge  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    12d6 <main+0x126>
jmp    1316 <main+0x166>
xor    %edi,%edi
call   1090 <SYNC_WAIT@plt>
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <read_increase_write@plt>
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1050 <SYNC_TELL@plt>
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12ad <main+0xfd>
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x1c(%rbp),%rdi
call   10c0 <wait@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1348 <main+0x198>
lea    0xd13(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xcfc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1090 <SYNC_INIT@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
js     127f <main+0xbf>
je     1296 <main+0xd6>
mov    %rax,%rbx
lea    0xe61(%rip),%r14
lea    0xe65(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
xor    %edi,%edi
call   10a0 <SYNC_WAIT@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <read_increase_write@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    $0x1,%esi
call   1060 <SYNC_TELL@plt>
mov    %r15,%rdi
call   1030 <puts@plt>
cmp    $0xa,%ebp
jb     1200 <main+0x40>
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rdi
call   10d0 <wait@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    1258 <main+0x98>
lea    0xdda(%rip),%rdi
call   1040 <err_sys@plt>
mov    0x4(%rsp),%edi
call   10b0 <WEXITSTATUS@plt>
lea    0xdd0(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%r14d
jmp    1334 <main+0x174>
lea    0xd7a(%rip),%rdi
call   1040 <err_sys@plt>
mov    $0xffffffff,%r14d
jmp    1334 <main+0x174>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd67(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1080 <read_increase_write@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10c0 <getppid@plt>
movslq %eax,%rdi
xor    %esi,%esi
call   1060 <SYNC_TELL@plt>
lea    0xd99(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%r14d
cmp    $0x9,%ebx
ja     1334 <main+0x174>
lea    0xd38(%rip),%r15
lea    0xd7b(%rip),%rbp
nopl   0x0(%rax)
mov    $0x1,%edi
call   10a0 <SYNC_WAIT@plt>
xor    %eax,%eax
call   10c0 <getppid@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1080 <read_increase_write@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10c0 <getppid@plt>
movslq %eax,%rdi
xor    %esi,%esi
call   1060 <SYNC_TELL@plt>
mov    %rbp,%rdi
call   1030 <puts@plt>
cmp    $0x9,%ebx
jbe    12f0 <main+0x130>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [18 x i8] c"child %u running\0A\00"
@.str.2 = [15 x i8] c"notify parent\0A\00"
@.str.3 = [16 x i8] c"wait parent %u\0A\00"
@.str.4 = [12 x i8] c"wait child\0A\00"
@.str.5 = [14 x i8] c"notify child\0A\00"
@.str.6 = [11 x i8] c"wait error\00"
@.str.7 = [23 x i8] c"wait child %u exit %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i64 0, i64* %3
%6 = call i32 (...) @SYNC_INIT()
%7 = call i64 (...) @fork()
store i64 %7, i64* %3
%8 = load i64, i64* %3
%9 = icmp slt i64 %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, i64 0, i64 0))
store i32 -1, i32* %1
br label %65
12: ; preds = %0
%13 = load i64, i64* %3
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %38
15: ; preds = %12
%16 = call i32 (...) @getpid()
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %33, %15
%19 = load i32, i32* %2
%20 = icmp slt i32 %19, 10
br i1 %20, label %21, label %37
21: ; preds = %18
%22 = call i32 (...) @read_increase_write()
store i32 %22, i32* %2
%23 = call i32 (...) @getppid()
%24 = sext i32 %23 to i64
%25 = call i32 @SYNC_TELL(i64 %24, i32 0)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* %2
%28 = icmp slt i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %21
%30 = load i32, i32* %2
%31 = icmp sge i32 %30, 10
br i1 %31, label %32, label %33
32: ; preds = %29, %21
br label %37
33: ; preds = %29
%34 = call i32 @SYNC_WAIT(i32 1)
%35 = call i32 (...) @getppid()
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %35)
br label %18
37: ; preds = %32, %18
store i32 2, i32* %1
br label %65
38: ; preds = %12
br label %39
39: ; preds = %38
br label %40
40: ; preds = %48, %39
%41 = load i32, i32* %2
%42 = icmp slt i32 %41, 10
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i32, i32* %2
%45 = icmp sge i32 %44, 0
br label %46
46: ; preds = %43, %40
%47 = phi i1 [ false, %40 ], [ %45, %43 ]
br i1 %47, label %48, label %55
48: ; preds = %46
%49 = call i32 @SYNC_WAIT(i32 0)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 (...) @read_increase_write()
store i32 %51, i32* %2
%52 = load i64, i64* %3
%53 = call i32 @SYNC_TELL(i64 %52, i32 1)
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %40
55: ; preds = %46
store i64 0, i64* %4
store i32 0, i32* %5
%56 = call i64 @wait(i32* %5)
store i64 %56, i64* %4
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
br label %60
60: ; preds = %58, %55
%61 = load i64, i64* %4
%62 = load i32, i32* %5
%63 = call i32 @WEXITSTATUS(i32 %62)
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i64 %61, i32 %63)
store i32 1, i32* %1
br label %65
65: ; preds = %60, %37, %10
%66 = load i32, i32* %1
ret i32 %66
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [18 x i8] c"child %u running\0A\00"
@.str.3 = [16 x i8] c"wait parent %u\0A\00"
@.str.6 = [11 x i8] c"wait error\00"
@.str.7 = [23 x i8] c"wait child %u exit %d\0A\00"
@str = [11 x i8] c"wait child\00"
@str.8 = [13 x i8] c"notify child\00"
@str.9 = [14 x i8] c"notify parent\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 (...) @SYNC_INIT()
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %45
7: ; preds = %0
%8 = icmp eq i64 %3, 0
br i1 %8, label %9, label %28
9: ; preds = %7
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = tail call i32 (...) @read_increase_write()
%13 = tail call i32 (...) @getppid()
%14 = sext i32 %13 to i64
%15 = tail call i32 @SYNC_TELL(i64 %14, i32 0)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
%17 = icmp ugt i32 %12, 9
br i1 %17, label %45, label %18
18: ; preds = %9, %18
%19 = tail call i32 @SYNC_WAIT(i32 1)
%20 = tail call i32 (...) @getppid()
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = tail call i32 (...) @read_increase_write()
%23 = tail call i32 (...) @getppid()
%24 = sext i32 %23 to i64
%25 = tail call i32 @SYNC_TELL(i64 %24, i32 0)
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
%27 = icmp ugt i32 %22, 9
br i1 %27, label %45, label %18
28: ; preds = %7, %28
%29 = tail call i32 @SYNC_WAIT(i32 0)
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%31 = tail call i32 (...) @read_increase_write()
%32 = tail call i32 @SYNC_TELL(i64 %3, i32 1)
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
%34 = icmp ult i32 %31, 10
br i1 %34, label %28, label %35
35: ; preds = %28
%36 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%37 = call i64 @wait(i32* %1)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
%42 = load i32, i32* %1
%43 = call i32 @WEXITSTATUS(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i64 %37, i32 %43)
br label %45
45: ; preds = %18, %9, %41, %5
%46 = phi i32 [ -1, %5 ], [ 1, %41 ], [ 2, %9 ], [ 2, %18 ]
ret i32 %46
}