#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*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ err_sys(char *);
scalar_t__ execl(char *, int /*<<< orphan*/ *);
int fork();
scalar_t__ pipe(int *);
int read(int, char *, int);
scalar_t__ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ write(int, char *, int);
int main() {
  int fd[2];
  int fd1[2];
  int pid;
  if (pipe(fd) < 0)
    err_sys("pipe error");
  if (pipe(fd1) < 0)
    err_sys("pipe1 error");
  if ((pid = fork()) < 0)
    err_sys("fork error");
  else if (pid > 0) {
    close(fd[0]);
    close(fd1[1]);
    if (write(fd[1], "2\n", 2) < 0)
      err_sys("write error");
    char tmp[255];
    int n;
    if ((n = read(fd1[0], tmp, 255)) < 0)
      err_sys("read error");
    write(1, "parent:", 7);
    if (write(1, tmp, n) < 0)
      err_sys("write error");
  } else {
    close(fd[1]);
    close(fd1[0]);
    dup2(fd[0], STDIN_FILENO);
    dup2(fd1[1], STDOUT_FILENO);
    close(fd[0]);
    close(fd1[1]);
    if (execl("./child", NULL) < 0)
      err_sys("exec error");
  }
  if ((waitpid(pid, NULL, 0) < 0))
    err_sys("wait_pid error");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11c1 <main+0x31>
lea    0xe44(%rip),%rdi
call   1030 <err_sys@plt>
lea    -0x14(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11e0 <main+0x50>
lea    0xe30(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1204 <main+0x74>
lea    0xe1d(%rip),%rdi
call   1030 <err_sys@plt>
jmp    1331 <main+0x1a1>
cmpl   $0x0,-0x18(%rbp)
jle    12bf <main+0x12f>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
lea    0xdfa(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    1248 <main+0xb8>
lea    0xde2(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1080 <read@plt>
mov    %eax,-0x124(%rbp)
cmp    $0x0,%eax
jge    1277 <main+0xe7>
lea    0xdbf(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0x1,%edi
lea    0xdb9(%rip),%rsi
mov    $0x7,%edx
call   1040 <write@plt>
lea    -0x120(%rbp),%rsi
mov    -0x124(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
cmp    $0x0,%rax
jge    12ba <main+0x12a>
lea    0xd70(%rip),%rdi
call   1030 <err_sys@plt>
jmp    132c <main+0x19c>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
mov    0x2cff(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x10(%rbp),%edi
mov    0x2d06(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
lea    0xd3c(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <execl@plt>
cmp    $0x0,%rax
jge    1327 <main+0x197>
lea    0xd2a(%rip),%rdi
call   1030 <err_sys@plt>
jmp    132c <main+0x19c>
jmp    1331 <main+0x1a1>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
cmp    $0x0,%rax
jge    1355 <main+0x1c5>
lea    0xd07(%rip),%rdi
call   1030 <err_sys@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x118,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
jns    11b4 <main+0x24>
lea    0xe51(%rip),%rdi
call   1030 <err_sys@plt>
mov    %rsp,%rdi
call   1070 <pipe@plt>
test   %rax,%rax
jns    11cd <main+0x3d>
lea    0xe43(%rip),%rdi
call   1030 <err_sys@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1275 <main+0xe5>
je     127e <main+0xee>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
lea    0xe21(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
test   %rax,%rax
jns    121c <main+0x8c>
lea    0xe0e(%rip),%rdi
call   1030 <err_sys@plt>
mov    (%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0xff,%edx
call   1080 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1240 <main+0xb0>
lea    0xdf6(%rip),%rdi
call   1030 <err_sys@plt>
lea    0xdf5(%rip),%rsi
mov    $0x1,%edi
mov    $0x7,%edx
call   1040 <write@plt>
lea    0x10(%rsp),%rsi
mov    $0x1,%edi
mov    %ebp,%edx
call   1040 <write@plt>
test   %rax,%rax
jns    12e4 <main+0x154>
lea    0xdb2(%rip),%rdi
jmp    12df <main+0x14f>
lea    0xd9b(%rip),%rdi
jmp    12df <main+0x14f>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    (%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    0x4(%rsp),%edi
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
lea    0xd78(%rip),%rdi
xor    %esi,%esi
call   10a0 <execl@plt>
test   %rax,%rax
jns    12e4 <main+0x154>
lea    0xd6d(%rip),%rdi
call   1030 <err_sys@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
test   %rax,%rax
jns    1300 <main+0x170>
lea    0xd5c(%rip),%rdi
call   1030 <err_sys@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"pipe error\00"
@.str.1 = [12 x i8] c"pipe1 error\00"
@.str.2 = [11 x i8] c"fork error\00"
@.str.3 = [3 x i8] c"2\0A\00"
@.str.4 = [12 x i8] c"write error\00"
@.str.5 = [11 x i8] c"read error\00"
@.str.6 = [8 x i8] c"parent:\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.7 = [8 x i8] c"./child\00"
@.str.8 = [11 x i8] c"exec error\00"
@.str.9 = [15 x i8] c"wait_pid error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca [255 x i8]
%6 = alloca i32
store i32 0, i32* %1
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%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))
br label %12
12: ; preds = %10, %0
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%14 = call i64 @pipe(i32* %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = call i32 (...) @fork()
store i32 %19, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %83
23: ; preds = %18
%24 = load i32, i32* %4
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %56
26: ; preds = %23
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %2, 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]* %2, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i64 @write(i32 %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %26
%38 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %37, %26
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%41 = load i32, i32* %40
%42 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 255)
store i32 %43, i32* %6
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %47
47: ; preds = %45, %39
%48 = call i64 @write(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 7)
%49 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%50 = load i32, i32* %6
%51 = call i64 @write(i32 1, i8* %49, i32 %50)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %55
55: ; preds = %53, %47
br label %82
56: ; preds = %23
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%58 = load i32, i32* %57
%59 = call i32 @close(i32 %58)
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%61 = load i32, i32* %60
%62 = call i32 @close(i32 %61)
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%64 = load i32, i32* %63
%65 = load i32, i32* @STDIN_FILENO
%66 = call i32 @dup2(i32 %64, i32 %65)
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%68 = load i32, i32* %67
%69 = load i32, i32* @STDOUT_FILENO
%70 = call i32 @dup2(i32 %68, i32 %69)
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%72 = load i32, i32* %71
%73 = call i32 @close(i32 %72)
%74 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%75 = load i32, i32* %74
%76 = call i32 @close(i32 %75)
%77 = call i64 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32* null)
%78 = icmp slt i64 %77, 0
br i1 %78, label %79, label %81
79: ; preds = %56
%80 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
br label %81
81: ; preds = %79, %56
br label %82
82: ; preds = %81, %55
br label %83
83: ; preds = %82, %21
%84 = load i32, i32* %4
%85 = call i64 @waitpid(i32 %84, i32* null, i32 0)
%86 = icmp slt i64 %85, 0
br i1 %86, label %87, label %89
87: ; preds = %83
%88 = call i32 @err_sys(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
br label %89
89: ; preds = %87, %83
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"pipe error\00"
@.str.1 = [12 x i8] c"pipe1 error\00"
@.str.2 = [11 x i8] c"fork error\00"
@.str.3 = [3 x i8] c"2\0A\00"
@.str.4 = [12 x i8] c"write error\00"
@.str.5 = [11 x i8] c"read error\00"
@.str.6 = [8 x i8] c"parent:\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.7 = [8 x i8] c"./child\00"
@.str.8 = [11 x i8] c"exec error\00"
@.str.9 = [15 x i8] c"wait_pid error\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [2 x i32]
%3 = alloca [255 x i8]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = bitcast [2 x i32]* %2 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%13 = call i64 @pipe(i32* %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = call i32 (...) @fork()
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %71
22: ; preds = %17
%23 = icmp eq i32 %18, 0
br i1 %23, label %50, label %24
24: ; preds = %22
%25 = load i32, i32* %6
%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 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i64 @write(i32 %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %24
%35 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %24
%37 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%38 = load i32, i32* %12
%39 = call i32 @read(i32 %38, i8* %37, i32 255)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %43
43: ; preds = %41, %36
%44 = call i64 @write(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 7)
%45 = call i64 @write(i32 1, i8* %37, i32 %39)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %49
49: ; preds = %47, %43
br label %71
50: ; preds = %22
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%52 = load i32, i32* %51
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %12
%55 = call i32 @close(i32 %54)
%56 = load i32, i32* %6
%57 = load i32, i32* @STDIN_FILENO
%58 = call i32 @dup2(i32 %56, i32 %57)
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%60 = load i32, i32* %59
%61 = load i32, i32* @STDOUT_FILENO
%62 = call i32 @dup2(i32 %60, i32 %61)
%63 = load i32, i32* %6
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* %59
%66 = call i32 @close(i32 %65)
%67 = call i64 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32* null)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %50
%70 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
br label %71
71: ; preds = %49, %69, %50, %20
%72 = call i64 @waitpid(i32 %18, i32* null, i32 0)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %71
%75 = call i32 @err_sys(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
br label %76
76: ; preds = %74, %71
ret i32 0
}