#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*/ client(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ server(int, int);
int wait(int *);
int main() {
  int childpid, pipe1[2], pipe2[2];
  if (pipe(pipe1) < 0 || pipe(pipe2) < 0)
    printf("pipe error\n");
  if ((childpid = fork()) < 0)
    printf("fork error\n");
  else if (childpid > 0) {
    close(pipe1[0]);
    close(pipe2[1]);
    client(pipe2[0], pipe1[1]);
    while (wait((int *)0) != childpid)
      ;
    close(pipe1[1]);
    close(pipe2[0]);
    exit(0);
  } else {
    close(pipe1[1]);
    close(pipe2[0]);
    server(pipe1[0], pipe2[1]);
    close(pipe1[0]);
    close(pipe2[1]);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jl     11b5 <main+0x35>
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jge    11c1 <main+0x41>
lea    0xe44(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11e5 <main+0x65>
lea    0xe31(%rip),%rdi
call   1030 <printf@plt>
jmp    126a <main+0xea>
cmpl   $0x0,-0x8(%rbp)
jle    1238 <main+0xb8>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1070 <client@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
cmp    -0x8(%rbp),%eax
je     1221 <main+0xa1>
jmp    120a <main+0x8a>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1060 <server@plt>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
js     11a1 <main+0x21>
mov    %rsp,%rdi
call   1050 <pipe@plt>
test   %rax,%rax
jns    11ad <main+0x2d>
lea    0xe63(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jns    11cc <main+0x4c>
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
jne    11fb <main+0x7b>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
mov    0x4(%rsp),%esi
call   1060 <server@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
jmp    1237 <main+0xb7>
mov    %eax,%ebx
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
mov    0xc(%rsp),%esi
call   1070 <client@plt>
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
call   1090 <wait@plt>
cmp    %ebx,%eax
jne    1220 <main+0xa0>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"pipe error\0A\00"
@.str.1 = [12 x i8] c"fork error\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
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %0
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%10 = call i64 @pipe(i32* %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8, %0
%13 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = call i32 (...) @fork()
store i32 %15, i32* %2
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %66
19: ; preds = %14
%20 = load i32, i32* %2
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %47
22: ; preds = %19
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%30 = load i32, i32* %29
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @client(i32 %30, i32 %32)
br label %34
34: ; preds = %38, %22
%35 = call i32 @wait(i32* null)
%36 = load i32, i32* %2
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %34
br label %34
39: ; preds = %34
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
%46 = call i32 @exit(i32 0)
unreachable
47: ; preds = %19
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%52 = load i32, i32* %51
%53 = call i32 @close(i32 %52)
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%55 = load i32, i32* %54
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%57 = load i32, i32* %56
%58 = call i32 @server(i32 %55, i32 %57)
%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]* %4, i64 0, i64 1
%63 = load i32, i32* %62
%64 = call i32 @close(i32 %63)
%65 = call i32 @exit(i32 0)
unreachable
66: ; preds = %17
%67 = load i32, i32* %1
ret i32 %67
}

LLVM IR (-O2):
@str = [11 x i8] c"fork error\00"
@str.2 = [11 x i8] c"pipe error\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 i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %0
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%10 = call i64 @pipe(i32* %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8, %0
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = call i32 (...) @fork()
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
ret i32 0
19: ; preds = %14
%20 = icmp eq i32 %15, 0
br i1 %20, label %41, label %21
21: ; preds = %19
%22 = load i32, i32* %5
%23 = call i32 @close(i32 %22)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%28 = load i32, i32* %27
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @client(i32 %28, i32 %30)
br label %32
32: ; preds = %32, %21
%33 = call i32 @wait(i32* null)
%34 = icmp eq i32 %33, %15
br i1 %34, label %35, label %32
35: ; preds = %32
%36 = load i32, i32* %29
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %27
%39 = call i32 @close(i32 %38)
%40 = call i32 @exit(i32 0)
unreachable
41: ; preds = %19
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %5
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @server(i32 %48, i32 %50)
%52 = load i32, i32* %5
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %49
%55 = call i32 @close(i32 %54)
%56 = call i32 @exit(i32 0)
unreachable
}