#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 */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ read(int, int *, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, int *, int);
int main() {
  int ball = 1;
  int a2b[2], b2a[2];
  pipe(a2b);
  pipe(b2a);
  if (fork() == 0) {
    while (ball < 10) {
      read(b2a[0], &ball, sizeof(int));
      printf("A: %d\n", ball);
      ball++;
      write(a2b[1], &ball, sizeof(int));
    }
    close(a2b[0]);
    close(a2b[1]);
    close(b2a[0]);
    close(b2a[1]);
    exit(0);
  }
  if (fork() == 0) {
    while (ball < 10) {
      read(a2b[0], &ball, sizeof(int));
      printf("B: %d\n", ball);
      ball++;
      write(b2a[1], &ball, sizeof(int));
    }
    close(a2b[0]);
    close(a2b[1]);
    close(b2a[0]);
    close(b2a[1]);
    exit(0);
  }
  write(b2a[1], &ball, sizeof(int));
  wait(0);
  wait(0);
  close(a2b[0]);
  close(a2b[1]);
  close(b2a[0]);
  close(b2a[1]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
call   1060 <pipe@plt>
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    122e <main+0xae>
jmp    11be <main+0x3e>
cmpl   $0xa,-0x8(%rbp)
jge    1207 <main+0x87>
mov    -0x18(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    -0x8(%rbp),%esi
lea    0xe1d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    11be <main+0x3e>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    12b4 <main+0x134>
jmp    1244 <main+0xc4>
cmpl   $0xa,-0x8(%rbp)
jge    128d <main+0x10d>
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    -0x8(%rbp),%esi
lea    0xd9e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    1244 <main+0xc4>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %edi,%edi
call   1090 <wait@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
movl   $0x1,0xc(%rsp)
lea    0x18(%rsp),%rdi
call   1060 <pipe@plt>
lea    0x10(%rsp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1212 <main+0x92>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     125e <main+0xde>
mov    0x14(%rsp),%edi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %edi,%edi
call   1090 <wait@plt>
mov    0x18(%rsp),%edi
call   1050 <close@plt>
mov    0x1c(%rsp),%edi
call   1050 <close@plt>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
mov    0x14(%rsp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xc(%rsp),%r15
lea    0xde2(%rip),%r14
xchg   %ax,%ax
mov    0x10(%rsp),%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    0xc(%rsp),%ebx
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
lea    0x1(%rbx),%eax
mov    %eax,0xc(%rsp)
mov    0x1c(%rsp),%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x9,%ebx
jl     1220 <main+0xa0>
jmp    12ac <main+0x12c>
lea    0xc(%rsp),%r15
lea    0xd9d(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    0x18(%rsp),%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    0xc(%rsp),%ebx
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
lea    0x1(%rbx),%eax
mov    %eax,0xc(%rsp)
mov    0x14(%rsp),%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x9,%ebx
jl     1270 <main+0xf0>
mov    0x18(%rsp),%edi
call   1050 <close@plt>
mov    0x1c(%rsp),%edi
call   1050 <close@plt>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
mov    0x14(%rsp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"A: %d\0A\00"
@.str.1 = [7 x i8] c"B: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i32]
%4 = alloca [2 x i32]
store i32 0, i32* %1
store i32 1, i32* %2
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = call i64 (...) @fork()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %40
11: ; preds = %0
br label %12
12: ; preds = %15, %11
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 10
br i1 %14, label %15, label %26
15: ; preds = %12
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%17 = load i32, i32* %16
%18 = call i32 @read(i32 %17, i32* %2, i32 4)
%19 = load i32, i32* %2
%20 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %2
%22 = add nsw i32 %21, 1
store i32 %22, i32* %2
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @write(i32 %24, i32* %2, i32 4)
br label %12
26: ; preds = %12
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %0
%41 = call i64 (...) @fork()
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %72
43: ; preds = %40
br label %44
44: ; preds = %47, %43
%45 = load i32, i32* %2
%46 = icmp slt i32 %45, 10
br i1 %46, label %47, label %58
47: ; preds = %44
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%49 = load i32, i32* %48
%50 = call i32 @read(i32 %49, i32* %2, i32 4)
%51 = load i32, i32* %2
%52 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %51)
%53 = load i32, i32* %2
%54 = add nsw i32 %53, 1
store i32 %54, i32* %2
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%56 = load i32, i32* %55
%57 = call i32 @write(i32 %56, i32* %2, i32 4)
br label %44
58: ; preds = %44
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%60 = load i32, i32* %59
%61 = call i32 @close(i32 %60)
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%63 = load i32, i32* %62
%64 = call i32 @close(i32 %63)
%65 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%66 = load i32, i32* %65
%67 = call i32 @close(i32 %66)
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%69 = load i32, i32* %68
%70 = call i32 @close(i32 %69)
%71 = call i32 @exit(i32 0)
unreachable
72: ; preds = %40
%73 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%74 = load i32, i32* %73
%75 = call i32 @write(i32 %74, i32* %2, i32 4)
%76 = call i32 @wait(i32 0)
%77 = call i32 @wait(i32 0)
%78 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%79 = load i32, i32* %78
%80 = call i32 @close(i32 %79)
%81 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%82 = load i32, i32* %81
%83 = call i32 @close(i32 %82)
%84 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%85 = load i32, i32* %84
%86 = call i32 @close(i32 %85)
%87 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%88 = load i32, i32* %87
%89 = call i32 @close(i32 %88)
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"A: %d\0A\00"
@.str.1 = [7 x i8] c"B: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [2 x i32]
%4 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%5 = bitcast [2 x i32]* %2 to i8*
%6 = bitcast [2 x i32]* %3 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = call i64 (...) @fork()
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %36
13: ; preds = %0
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
br label %15
15: ; preds = %13, %15
%16 = load i32, i32* %9
%17 = call i32 @read(i32 %16, i32* %1, i32 4)
%18 = load i32, i32* %1
%19 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = add nsw i32 %18, 1
store i32 %20, i32* %1
%21 = load i32, i32* %14
%22 = call i32 @write(i32 %21, i32* %1, i32 4)
%23 = icmp slt i32 %18, 9
br i1 %23, label %15, label %24
24: ; preds = %15
%25 = load i32, i32* %7
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %9
%31 = call i32 @close(i32 %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %0
%37 = call i64 (...) @fork()
%38 = icmp eq i64 %37, 0
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
br i1 %38, label %40, label %61
40: ; preds = %36, %40
%41 = load i32, i32* %7
%42 = call i32 @read(i32 %41, i32* %1, i32 4)
%43 = load i32, i32* %1
%44 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %43)
%45 = add nsw i32 %43, 1
store i32 %45, i32* %1
%46 = load i32, i32* %39
%47 = call i32 @write(i32 %46, i32* %1, i32 4)
%48 = icmp slt i32 %43, 9
br i1 %48, label %40, label %49
49: ; preds = %40
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%53 = load i32, i32* %52
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* %9
%56 = call i32 @close(i32 %55)
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%58 = load i32, i32* %57
%59 = call i32 @close(i32 %58)
%60 = call i32 @exit(i32 0)
unreachable
61: ; preds = %36
%62 = load i32, i32* %39
%63 = call i32 @write(i32 %62, i32* %1, i32 4)
%64 = call i32 @wait(i32 0)
%65 = call i32 @wait(i32 0)
%66 = load i32, i32* %7
%67 = call i32 @close(i32 %66)
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%69 = load i32, i32* %68
%70 = call i32 @close(i32 %69)
%71 = load i32, i32* %9
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* %39
%74 = call i32 @close(i32 %73)
ret i32 0
}