#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*/ EXIT_SUCCESS;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ childLoop(int, int);
int close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
long fork();
int /*<<< orphan*/ parentLoop(int, int);
int /*<<< orphan*/ pexit(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  int writePfd[2], readPfd[2];
  long childPid;
  if (pipe(writePfd) == -1)
    pexit("pipe");
  if (pipe(readPfd) == -1)
    pexit("pipe");
  childPid = fork();
  switch (childPid) {
  case -1:
    pexit("fork");
  case 0:
    if (close(writePfd[1]) == -1)
      pexit("close write end of write pipe - child");
    if (close(readPfd[0]) == -1)
      pexit("close read end of read pipe - child");
    childLoop(writePfd[0], readPfd[1]);
    _exit(EXIT_SUCCESS);
  default:
    if (close(writePfd[0]) == -1)
      pexit("close read end of write pipe - parent");
    if (close(readPfd[1]) == -1)
      pexit("close write end of read pipe - parent");
    parentLoop(readPfd[0], writePfd[1]);
    if (close(writePfd[1]) == -1)
      pexit("close write end of write pipe - parent");
    printf("\n");
  }
  wait(NULL);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11cd <main+0x2d>
lea    0xe38(%rip),%rdi
call   1090 <pexit@plt>
lea    -0x14(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11eb <main+0x4b>
lea    0xe1a(%rip),%rdi
call   1090 <pexit@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
sub    $0xffffffffffffffff,%rax
je     121f <main+0x7f>
jmp    120d <main+0x6d>
mov    -0x28(%rbp),%rax
test   %rax,%rax
je     122b <main+0x8b>
jmp    127e <main+0xde>
lea    0xddf(%rip),%rdi
call   1090 <pexit@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    1248 <main+0xa8>
lea    0xdc7(%rip),%rdi
call   1090 <pexit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    1265 <main+0xc5>
lea    0xdd0(%rip),%rdi
call   1090 <pexit@plt>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1070 <childLoop@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    129b <main+0xfb>
lea    0xdbe(%rip),%rdi
call   1090 <pexit@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    12b8 <main+0x118>
lea    0xdc7(%rip),%rdi
call   1090 <pexit@plt>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%esi
call   10c0 <parentLoop@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    12e0 <main+0x140>
lea    0xdc5(%rip),%rdi
call   1090 <pexit@plt>
lea    0xde0(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11bf <main+0x1f>
lea    0xe46(%rip),%rdi
call   1090 <pexit@plt>
lea    0x10(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11da <main+0x3a>
lea    0xe2b(%rip),%rdi
call   1090 <pexit@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     11f8 <main+0x58>
cmp    $0xffffffffffffffff,%rax
jne    1247 <main+0xa7>
lea    0xe12(%rip),%rdi
call   1090 <pexit@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    1212 <main+0x72>
lea    0xdfd(%rip),%rdi
call   1090 <pexit@plt>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    122c <main+0x8c>
lea    0xe09(%rip),%rdi
call   1090 <pexit@plt>
mov    0x8(%rsp),%edi
mov    0x14(%rsp),%esi
call   1070 <childLoop@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1040 <_exit@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    1261 <main+0xc1>
lea    0xdf8(%rip),%rdi
call   1090 <pexit@plt>
mov    0x14(%rsp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    127b <main+0xdb>
lea    0xe04(%rip),%rdi
call   1090 <pexit@plt>
mov    0x10(%rsp),%edi
mov    0xc(%rsp),%esi
call   10c0 <parentLoop@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    12a2 <main+0x102>
lea    0xe03(%rip),%rdi
call   1090 <pexit@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

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

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