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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    1237 <main+0xb7>
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
lea    0xe49(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
lea    0xe35(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    1220 <main+0xa0>
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    -0x8(%rbp),%esi
lea    0xe0c(%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    11d7 <main+0x57>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    12d8 <main+0x158>
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
lea    0xda8(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
lea    0xd94(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    12c1 <main+0x141>
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    -0x8(%rbp),%esi
lea    0xd72(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    1278 <main+0xf8>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
lea    0xd1c(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%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>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movl   $0x1,0xc(%rsp)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     11fc <main+0x7c>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     127c <main+0xfc>
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
lea    0xe44(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
lea    0xc(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %edi,%edi
call   1090 <wait@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
lea    0xdf4(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r14d
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
lea    0xde0(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r12d
lea    0xc(%rsp),%rbx
lea    0xdd0(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    0xc(%rsp),%ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
lea    0x1(%rbp),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x9,%ebp
jl     1240 <main+0xc0>
jmp    12fa <main+0x17a>
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
lea    0xd74(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r14d
mov    0x2d35(%rip),%rax
mov    (%rax),%esi
lea    0xd60(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r12d
lea    0xc(%rsp),%rbx
lea    0xd57(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    0xc(%rsp),%ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
lea    0x1(%rbp),%eax
mov    %eax,0xc(%rsp)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x9,%ebp
jl     12c0 <main+0x140>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %r12d,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str = [4 x i8] c"a2b\00"
@O_WRONLY = global i32 0
@.str.1 = [4 x i8] c"b2a\00"
@O_RDONLY = global i32 0
@.str.2 = [7 x i8] c"A: %d\0A\00"
@.str.3 = [7 x i8] c"B: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %21
5: ; preds = %0
%6 = load i32, i32* @O_WRONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %5, %10
%11 = call i32 @read(i32 %9, i32* %1, i32 4)
%12 = load i32, i32* %1
%13 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = add nsw i32 %12, 1
store i32 %14, i32* %1
%15 = call i32 @write(i32 %7, i32* %1, i32 4)
%16 = icmp slt i32 %12, 9
br i1 %16, label %10, label %17
17: ; preds = %10
%18 = tail call i32 @close(i32 %7)
%19 = tail call i32 @close(i32 %9)
%20 = tail call i32 @exit(i32 0)
unreachable
21: ; preds = %0
%22 = tail call i64 (...) @fork()
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %40
24: ; preds = %21
%25 = load i32, i32* @O_RDONLY
%26 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = load i32, i32* @O_WRONLY
%28 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %24, %29
%30 = call i32 @read(i32 %26, i32* %1, i32 4)
%31 = load i32, i32* %1
%32 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = add nsw i32 %31, 1
store i32 %33, i32* %1
%34 = call i32 @write(i32 %28, i32* %1, i32 4)
%35 = icmp slt i32 %31, 9
br i1 %35, label %29, label %36
36: ; preds = %29
%37 = tail call i32 @close(i32 %26)
%38 = tail call i32 @close(i32 %28)
%39 = tail call i32 @exit(i32 0)
unreachable
40: ; preds = %21
%41 = load i32, i32* @O_WRONLY
%42 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %41)
%43 = call i32 @write(i32 %42, i32* %1, i32 4)
%44 = tail call i32 @wait(i32 0)
%45 = tail call i32 @wait(i32 0)
%46 = tail call i32 @close(i32 %42)
ret i32 0
}