#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 */
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ line;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int MAX(int, int);
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int dup2(int, int);
int /*<<< orphan*/ err_sys(char *);
scalar_t__ fork();
scalar_t__ gets(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ strncmp(char *, char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd_in[2] = {0};
  int fd_out[2] = {0};
  if (pipe(fd_in) < 0)
    err_sys("pipe stdin error");
  if (pipe(fd_out) < 0)
    err_sys("pipe stdout error");
  char line[4096] = {0};
  int ret = 0, max = MAX(fd_out[0], STDIN_FILENO) + 1;
  printf("max fd: %d, pipe: %d, stdin: %d\n", max, fd_out[0], STDIN_FILENO);
  pid_t pid = fork();
  if (pid < 0) {
    err_sys("fork error");
  } else if (pid > 0) {
    close(fd_in[0]);
    close(fd_out[1]);
    fd_set rfds;
    FD_ZERO(&rfds);
    while (1) {
      FD_SET(fd_out[0], &rfds);
      FD_SET(STDIN_FILENO, &rfds);
      ret = select(max, &rfds, NULL, NULL, NULL);
      if (ret == -1) {
        err_sys("select error");
      } else if (ret == 0) {
        printf("select over but no event\n");
        continue;
      }
      printf("got event\n");
      if (FD_ISSET(STDIN_FILENO, &rfds)) {
        printf("select stdin\n");
        ret = read(STDIN_FILENO, line, sizeof(line));
        if (ret < 0)
          err_sys("read stdin error");
        else if (ret == 0) {
          printf("read end\n");
          break;
        } else {
          ret = write(fd_in[1], line, ret);
          if (ret < 0)
            err_sys("write pipe error");
          else if (ret == 0) {
            printf("write end\n");
            break;
          }
        }
      }
      if (FD_ISSET(fd_out[0], &rfds)) {
        printf("poll pipe in\n");
        ret = read(fd_out[0], line, sizeof(line));
        if (ret < 0)
          err_sys("read pipe error");
        else if (ret == 0) {
          printf("read end\n");
          break;
        } else {
          ret = write(STDOUT_FILENO, line, ret);
          if (ret < 0)
            err_sys("write stdout error");
          else if (ret == 0) {
            printf("write end\n");
            break;
          }
        }
      }
    }
  } else {
    close(fd_in[1]);
    close(fd_out[0]);
    printf("child begin to redirect pipe to stdin/out/err\n");
    if (fd_in[0] != STDIN_FILENO) {
      if (dup2(fd_in[0], STDIN_FILENO) != STDIN_FILENO)
        err_sys("dup2 stdin error");
      close(fd_in[0]);
    }
    if (fd_out[1] != STDOUT_FILENO) {
      if (dup2(fd_out[1], STDOUT_FILENO) != STDOUT_FILENO)
        err_sys("dup2 stdout error");
    }
    if (fd_out[1] != STDERR_FILENO) {
      if (dup2(fd_out[1], STDERR_FILENO) != STDERR_FILENO)
        err_sys("dup2 stderr error");
      close(fd_out[1]);
    }
    printf("redirect OK\n");
    while (gets(line)) {
      if (strncmp(line, "exit", 4) == 0) {
        break;
      }
      puts(line);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   10b0 <memset@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   10b0 <memset@plt>
lea    -0x18(%rbp),%rdi
call   10d0 <pipe@plt>
cmp    $0x0,%rax
jge    1268 <main+0x58>
lea    0xd9d(%rip),%rdi
call   1050 <err_sys@plt>
lea    -0x20(%rbp),%rdi
call   10d0 <pipe@plt>
cmp    $0x0,%rax
jge    1287 <main+0x77>
lea    0xd8f(%rip),%rdi
call   1050 <err_sys@plt>
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   10b0 <memset@plt>
movl   $0x0,-0x1024(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2d22(%rip),%rax
mov    (%rax),%esi
call   1080 <MAX@plt>
add    $0x1,%eax
mov    %eax,-0x1028(%rbp)
mov    -0x1028(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    0x2d02(%rip),%rax
mov    (%rax),%ecx
lea    0xd4c(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
mov    $0x0,%al
call   1130 <fork@plt>
mov    %rax,-0x1030(%rbp)
cmpq   $0x0,-0x1030(%rbp)
jge    130b <main+0xfb>
lea    0xd43(%rip),%rdi
call   1050 <err_sys@plt>
jmp    1714 <main+0x504>
cmpq   $0x0,-0x1030(%rbp)
jle    15c1 <main+0x3b1>
mov    -0x18(%rbp),%edi
call   10c0 <close@plt>
mov    -0x1c(%rbp),%edi
call   10c0 <close@plt>
lea    -0x1034(%rbp),%rdi
call   1100 <FD_ZERO@plt>
mov    -0x20(%rbp),%edi
lea    -0x1034(%rbp),%rsi
call   1120 <FD_SET@plt>
mov    0x2c85(%rip),%rax
mov    (%rax),%edi
lea    -0x1034(%rbp),%rsi
call   1120 <FD_SET@plt>
mov    -0x1028(%rbp),%edi
lea    -0x1034(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1110 <select@plt>
mov    %eax,-0x1024(%rbp)
cmpl   $0xffffffff,-0x1024(%rbp)
jne    139a <main+0x18a>
lea    0xcbf(%rip),%rdi
call   1050 <err_sys@plt>
jmp    13bf <main+0x1af>
cmpl   $0x0,-0x1024(%rbp)
jne    13ba <main+0x1aa>
lea    0xcae(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
jmp    1335 <main+0x125>
jmp    13bf <main+0x1af>
lea    0xcb0(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
mov    0x2bfc(%rip),%rax
mov    (%rax),%edi
lea    -0x1034(%rbp),%rsi
call   1070 <FD_ISSET@plt>
cmp    $0x0,%rax
je     14c5 <main+0x2b5>
lea    0xc8e(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
mov    0x2bcf(%rip),%rax
mov    (%rax),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   10e0 <read@plt>
mov    %eax,-0x1024(%rbp)
cmpl   $0x0,-0x1024(%rbp)
jge    1438 <main+0x228>
lea    0xc61(%rip),%rdi
call   1050 <err_sys@plt>
jmp    14c0 <main+0x2b0>
cmpl   $0x0,-0x1024(%rbp)
jne    1458 <main+0x248>
lea    0xc54(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
jmp    15bc <main+0x3ac>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1024(%rbp),%edx
call   1060 <write@plt>
mov    %eax,-0x1024(%rbp)
cmpl   $0x0,-0x1024(%rbp)
jge    1491 <main+0x281>
lea    0xc23(%rip),%rdi
call   1050 <err_sys@plt>
jmp    14b6 <main+0x2a6>
cmpl   $0x0,-0x1024(%rbp)
jne    14b1 <main+0x2a1>
lea    0xc16(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
jmp    15bc <main+0x3ac>
jmp    14b6 <main+0x2a6>
jmp    14bb <main+0x2ab>
jmp    14c0 <main+0x2b0>
jmp    14c5 <main+0x2b5>
mov    -0x20(%rbp),%edi
lea    -0x1034(%rbp),%rsi
call   1070 <FD_ISSET@plt>
cmp    $0x0,%rax
je     15b7 <main+0x3a7>
lea    0xbe1(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
mov    -0x20(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   10e0 <read@plt>
mov    %eax,-0x1024(%rbp)
cmpl   $0x0,-0x1024(%rbp)
jge    1524 <main+0x314>
lea    0xbba(%rip),%rdi
call   1050 <err_sys@plt>
jmp    15b2 <main+0x3a2>
cmpl   $0x0,-0x1024(%rbp)
jne    1544 <main+0x334>
lea    0xb68(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
jmp    15bc <main+0x3ac>
mov    0x2aa5(%rip),%rax
mov    (%rax),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1024(%rbp),%edx
call   1060 <write@plt>
mov    %eax,-0x1024(%rbp)
cmpl   $0x0,-0x1024(%rbp)
jge    1583 <main+0x373>
lea    0xb6b(%rip),%rdi
call   1050 <err_sys@plt>
jmp    15a8 <main+0x398>
cmpl   $0x0,-0x1024(%rbp)
jne    15a3 <main+0x393>
lea    0xb24(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
jmp    15bc <main+0x3ac>
jmp    15a8 <main+0x398>
jmp    15ad <main+0x39d>
jmp    15b2 <main+0x3a2>
jmp    15b7 <main+0x3a7>
jmp    1335 <main+0x125>
jmp    170f <main+0x4ff>
mov    -0x14(%rbp),%edi
call   10c0 <close@plt>
mov    -0x20(%rbp),%edi
call   10c0 <close@plt>
lea    0xb1f(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
mov    -0x18(%rbp),%eax
mov    0x29e7(%rip),%rcx
cmp    (%rcx),%eax
je     1625 <main+0x415>
mov    -0x18(%rbp),%edi
mov    0x29d5(%rip),%rax
mov    (%rax),%esi
call   1090 <dup2@plt>
mov    0x29c7(%rip),%rcx
cmp    (%rcx),%eax
je     161d <main+0x40d>
lea    0xb0e(%rip),%rdi
call   1050 <err_sys@plt>
mov    -0x18(%rbp),%edi
call   10c0 <close@plt>
mov    -0x1c(%rbp),%eax
mov    0x29c1(%rip),%rcx
cmp    (%rcx),%eax
je     1668 <main+0x458>
mov    -0x1c(%rbp),%edi
mov    0x29af(%rip),%rax
mov    (%rax),%esi
call   1090 <dup2@plt>
mov    0x29a1(%rip),%rcx
cmp    (%rcx),%eax
je     1663 <main+0x453>
lea    0xad9(%rip),%rdi
call   1050 <err_sys@plt>
jmp    1668 <main+0x458>
mov    -0x1c(%rbp),%eax
mov    0x2966(%rip),%rcx
cmp    (%rcx),%eax
je     16ae <main+0x49e>
mov    -0x1c(%rbp),%edi
mov    0x2954(%rip),%rax
mov    (%rax),%esi
call   1090 <dup2@plt>
mov    0x2946(%rip),%rcx
cmp    (%rcx),%eax
je     16a6 <main+0x496>
lea    0xaa8(%rip),%rdi
call   1050 <err_sys@plt>
mov    -0x1c(%rbp),%edi
call   10c0 <close@plt>
lea    0xaa6(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
lea    -0x1020(%rbp),%rdi
call   10f0 <gets@plt>
cmp    $0x0,%rax
je     170a <main+0x4fa>
lea    -0x1020(%rbp),%rdi
lea    0xa88(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    16f9 <main+0x4e9>
jmp    170a <main+0x4fa>
lea    -0x1020(%rbp),%rdi
call   1040 <puts@plt>
jmp    16bc <main+0x4ac>
jmp    170f <main+0x4ff>
jmp    1714 <main+0x504>
xor    %eax,%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rdi
call   10d0 <pipe@plt>
test   %rax,%rax
jns    124e <main+0x3e>
lea    0xdb7(%rip),%rdi
call   1050 <err_sys@plt>
lea    0x8(%rsp),%rdi
call   10d0 <pipe@plt>
test   %rax,%rax
jns    1269 <main+0x59>
lea    0xdad(%rip),%rdi
call   1050 <err_sys@plt>
lea    0x20(%rsp),%rdi
mov    $0x1000,%edx
xor    %esi,%esi
call   10b0 <memset@plt>
mov    0x8(%rsp),%edi
mov    0x2d4b(%rip),%r14
mov    (%r14),%esi
call   1080 <MAX@plt>
mov    %eax,%ebp
add    $0x1,%ebp
mov    0x8(%rsp),%edx
mov    (%r14),%ecx
lea    0xd83(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   10a0 <printf@plt>
xor    %eax,%eax
call   1130 <fork@plt>
test   %rax,%rax
js     1431 <main+0x221>
je     144e <main+0x23e>
mov    0x10(%rsp),%edi
call   10c0 <close@plt>
mov    0xc(%rsp),%edi
call   10c0 <close@plt>
lea    0x1c(%rsp),%rbx
mov    %rbx,%rdi
call   1100 <FD_ZERO@plt>
lea    0xe30(%rip),%r13
lea    0x20(%rsp),%r15
lea    0xd6b(%rip),%r12
jmp    130c <main+0xfc>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xe45(%rip),%rdi
call   1040 <puts@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
call   1120 <FD_SET@plt>
mov    (%r14),%edi
mov    %rbx,%rsi
call   1120 <FD_SET@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1110 <select@plt>
test   %eax,%eax
je     1300 <main+0xf0>
cmp    $0xffffffff,%eax
jne    1349 <main+0x139>
lea    0xd0b(%rip),%rdi
call   1050 <err_sys@plt>
mov    %r13,%rdi
call   1040 <puts@plt>
mov    (%r14),%edi
mov    %rbx,%rsi
call   1070 <FD_ISSET@plt>
test   %rax,%rax
je     13b5 <main+0x1a5>
lea    0xdc4(%rip),%rdi
call   1040 <puts@plt>
mov    (%r14),%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   10e0 <read@plt>
mov    %r12,%rdi
test   %eax,%eax
js     13b0 <main+0x1a0>
je     1442 <main+0x232>
mov    0x14(%rsp),%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1060 <write@plt>
lea    0xcce(%rip),%rdi
test   %eax,%eax
js     13b0 <main+0x1a0>
jne    13b5 <main+0x1a5>
jmp    154c <main+0x33c>
nopw   0x0(%rax,%rax,1)
call   1050 <err_sys@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
call   1070 <FD_ISSET@plt>
test   %rax,%rax
je     130c <main+0xfc>
lea    0xd4e(%rip),%rdi
call   1040 <puts@plt>
mov    0x8(%rsp),%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   10e0 <read@plt>
test   %eax,%eax
js     140f <main+0x1ff>
je     1442 <main+0x232>
mov    0x2bfc(%rip),%rcx
mov    (%rcx),%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1060 <write@plt>
test   %eax,%eax
js     1420 <main+0x210>
jne    130c <main+0xfc>
jmp    154c <main+0x33c>
lea    0xc68(%rip),%rdi
call   1050 <err_sys@plt>
jmp    130c <main+0xfc>
lea    0xc67(%rip),%rdi
call   1050 <err_sys@plt>
jmp    130c <main+0xfc>
lea    0xc0c(%rip),%rdi
call   1050 <err_sys@plt>
jmp    1558 <main+0x348>
lea    0xcfa(%rip),%rdi
jmp    1553 <main+0x343>
mov    0x14(%rsp),%edi
call   10c0 <close@plt>
mov    0x8(%rsp),%edi
call   10c0 <close@plt>
lea    0xc74(%rip),%rdi
call   1040 <puts@plt>
mov    0x10(%rsp),%edi
mov    (%r14),%esi
cmp    %esi,%edi
je     1496 <main+0x286>
call   1090 <dup2@plt>
cmp    (%r14),%eax
je     148d <main+0x27d>
lea    0xc19(%rip),%rdi
call   1050 <err_sys@plt>
mov    0x10(%rsp),%edi
call   10c0 <close@plt>
mov    0xc(%rsp),%edi
mov    0x2b4f(%rip),%rbx
mov    (%rbx),%esi
cmp    %esi,%edi
je     14bc <main+0x2ac>
call   1090 <dup2@plt>
cmp    (%rbx),%eax
je     14bc <main+0x2ac>
lea    0xbfb(%rip),%rdi
call   1050 <err_sys@plt>
mov    0xc(%rsp),%edi
mov    0x2b11(%rip),%rbx
mov    (%rbx),%esi
cmp    %esi,%edi
je     14eb <main+0x2db>
call   1090 <dup2@plt>
cmp    (%rbx),%eax
je     14e2 <main+0x2d2>
lea    0xbe7(%rip),%rdi
call   1050 <err_sys@plt>
mov    0xc(%rsp),%edi
call   10c0 <close@plt>
lea    0xc17(%rip),%rdi
call   1040 <puts@plt>
lea    0x20(%rsp),%rdi
call   10f0 <gets@plt>
test   %rax,%rax
je     1558 <main+0x348>
lea    0xbc9(%rip),%rbx
lea    0x20(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1558 <main+0x348>
mov    %rbp,%rdi
call   1040 <puts@plt>
mov    %rbp,%rdi
call   10f0 <gets@plt>
test   %rax,%rax
jne    1520 <main+0x310>
jmp    1558 <main+0x348>
lea    0xbe6(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x1028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"pipe stdin error\00"
@.str.1 = [18 x i8] c"pipe stdout error\00"
@STDIN_FILENO = global i32 0
@.str.2 = [33 x i8] c"max fd: %d, pipe: %d, stdin: %d\0A\00"
@.str.3 = [11 x i8] c"fork error\00"
@.str.4 = [13 x i8] c"select error\00"
@.str.5 = [26 x i8] c"select over but no event\0A\00"
@.str.6 = [11 x i8] c"got event\0A\00"
@.str.7 = [14 x i8] c"select stdin\0A\00"
@.str.8 = [17 x i8] c"read stdin error\00"
@.str.9 = [10 x i8] c"read end\0A\00"
@.str.10 = [17 x i8] c"write pipe error\00"
@.str.11 = [11 x i8] c"write end\0A\00"
@.str.12 = [14 x i8] c"poll pipe in\0A\00"
@.str.13 = [16 x i8] c"read pipe error\00"
@STDOUT_FILENO = global i32 0
@.str.14 = [19 x i8] c"write stdout error\00"
@.str.15 = [47 x i8] c"child begin to redirect pipe to stdin/out/err\0A\00"
@.str.16 = [17 x i8] c"dup2 stdin error\00"
@.str.17 = [18 x i8] c"dup2 stdout error\00"
@STDERR_FILENO = global i32 0
@.str.18 = [18 x i8] c"dup2 stderr error\00"
@.str.19 = [13 x i8] c"redirect OK\0A\00"
@.str.20 = [5 x i8] c"exit\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca [2 x i32]
%8 = alloca [4096 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = bitcast [2 x i32]* %6 to i8*
%14 = bitcast [2 x i32]* %7 to i8*
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%16 = call i64 @pipe(i32* %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @err_sys(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%22 = call i64 @pipe(i32* %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @err_sys(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
%27 = bitcast [4096 x i8]* %8 to i8*
store i32 0, i32* %9
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%29 = load i32, i32* %28
%30 = load i32, i32* @STDIN_FILENO
%31 = call i32 @MAX(i32 %29, i32 %30)
%32 = add nsw i32 %31, 1
store i32 %32, i32* %10
%33 = load i32, i32* %10
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%35 = load i32, i32* %34
%36 = load i32, i32* @STDIN_FILENO
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %33, i32 %35, i32 %36)
%38 = call i64 (...) @fork()
store i64 %38, i64* %11
%39 = load i64, i64* %11
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %26
%42 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %224
43: ; preds = %26
%44 = load i64, i64* %11
%45 = icmp sgt i64 %44, 0
br i1 %45, label %46, label %150
46: ; preds = %43
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%48 = load i32, i32* %47
%49 = call i32 @close(i32 %48)
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
%53 = call i32 @FD_ZERO(i32* %12)
br label %54
54: ; preds = %46, %69, %148
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%56 = load i32, i32* %55
%57 = call i32 @FD_SET(i32 %56, i32* %12)
%58 = load i32, i32* @STDIN_FILENO
%59 = call i32 @FD_SET(i32 %58, i32* %12)
%60 = load i32, i32* %10
%61 = call i32 @select(i32 %60, i32* %12, i32* null, i32* null, i32* null)
store i32 %61, i32* %9
%62 = load i32, i32* %9
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %54
%65 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %72
66: ; preds = %54
%67 = load i32, i32* %9
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %66
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %54
71: ; preds = %66
br label %72
72: ; preds = %71, %64
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%74 = load i32, i32* @STDIN_FILENO
%75 = call i64 @FD_ISSET(i32 %74, i32* %12)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %110
77: ; preds = %72
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
%79 = load i32, i32* @STDIN_FILENO
%80 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%81 = call i32 @read(i32 %79, i8* %80, i32 4096)
store i32 %81, i32* %9
%82 = load i32, i32* %9
%83 = icmp slt i32 %82, 0
br i1 %83, label %84, label %86
84: ; preds = %77
%85 = call i32 @err_sys(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
br label %109
86: ; preds = %77
%87 = load i32, i32* %9
%88 = icmp eq i32 %87, 0
br i1 %88, label %89, label %91
89: ; preds = %86
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0))
br label %149
91: ; preds = %86
%92 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%93 = load i32, i32* %92
%94 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%95 = load i32, i32* %9
%96 = call i32 @write(i32 %93, i8* %94, i32 %95)
store i32 %96, i32* %9
%97 = load i32, i32* %9
%98 = icmp slt i32 %97, 0
br i1 %98, label %99, label %101
99: ; preds = %91
%100 = call i32 @err_sys(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0))
br label %107
101: ; preds = %91
%102 = load i32, i32* %9
%103 = icmp eq i32 %102, 0
br i1 %103, label %104, label %106
104: ; preds = %101
%105 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0))
br label %149
106: ; preds = %101
br label %107
107: ; preds = %106, %99
br label %108
108: ; preds = %107
br label %109
109: ; preds = %108, %84
br label %110
110: ; preds = %109, %72
%111 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%112 = load i32, i32* %111
%113 = call i64 @FD_ISSET(i32 %112, i32* %12)
%114 = icmp ne i64 %113, 0
br i1 %114, label %115, label %148
115: ; preds = %110
%116 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.12, i64 0, i64 0))
%117 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%118 = load i32, i32* %117
%119 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%120 = call i32 @read(i32 %118, i8* %119, i32 4096)
store i32 %120, i32* %9
%121 = load i32, i32* %9
%122 = icmp slt i32 %121, 0
br i1 %122, label %123, label %125
123: ; preds = %115
%124 = call i32 @err_sys(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.13, i64 0, i64 0))
br label %147
125: ; preds = %115
%126 = load i32, i32* %9
%127 = icmp eq i32 %126, 0
br i1 %127, label %128, label %130
128: ; preds = %125
%129 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0))
br label %149
130: ; preds = %125
%131 = load i32, i32* @STDOUT_FILENO
%132 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%133 = load i32, i32* %9
%134 = call i32 @write(i32 %131, i8* %132, i32 %133)
store i32 %134, i32* %9
%135 = load i32, i32* %9
%136 = icmp slt i32 %135, 0
br i1 %136, label %137, label %139
137: ; preds = %130
%138 = call i32 @err_sys(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.14, i64 0, i64 0))
br label %145
139: ; preds = %130
%140 = load i32, i32* %9
%141 = icmp eq i32 %140, 0
br i1 %141, label %142, label %144
142: ; preds = %139
%143 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0))
br label %149
144: ; preds = %139
br label %145
145: ; preds = %144, %137
br label %146
146: ; preds = %145
br label %147
147: ; preds = %146, %123
br label %148
148: ; preds = %147, %110
br label %54
149: ; preds = %142, %128, %104, %89
br label %223
150: ; preds = %43
%151 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%152 = load i32, i32* %151
%153 = call i32 @close(i32 %152)
%154 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%155 = load i32, i32* %154
%156 = call i32 @close(i32 %155)
%157 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.15, i64 0, i64 0))
%158 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%159 = load i32, i32* %158
%160 = load i32, i32* @STDIN_FILENO
%161 = icmp ne i32 %159, %160
br i1 %161, label %162, label %175
162: ; preds = %150
%163 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%164 = load i32, i32* %163
%165 = load i32, i32* @STDIN_FILENO
%166 = call i32 @dup2(i32 %164, i32 %165)
%167 = load i32, i32* @STDIN_FILENO
%168 = icmp ne i32 %166, %167
br i1 %168, label %169, label %171
169: ; preds = %162
%170 = call i32 @err_sys(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.16, i64 0, i64 0))
br label %171
171: ; preds = %169, %162
%172 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%173 = load i32, i32* %172
%174 = call i32 @close(i32 %173)
br label %175
175: ; preds = %171, %150
%176 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%177 = load i32, i32* %176
%178 = load i32, i32* @STDOUT_FILENO
%179 = icmp ne i32 %177, %178
br i1 %179, label %180, label %190
180: ; preds = %175
%181 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%182 = load i32, i32* %181
%183 = load i32, i32* @STDOUT_FILENO
%184 = call i32 @dup2(i32 %182, i32 %183)
%185 = load i32, i32* @STDOUT_FILENO
%186 = icmp ne i32 %184, %185
br i1 %186, label %187, label %189
187: ; preds = %180
%188 = call i32 @err_sys(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.17, i64 0, i64 0))
br label %189
189: ; preds = %187, %180
br label %190
190: ; preds = %189, %175
%191 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%192 = load i32, i32* %191
%193 = load i32, i32* @STDERR_FILENO
%194 = icmp ne i32 %192, %193
br i1 %194, label %195, label %208
195: ; preds = %190
%196 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%197 = load i32, i32* %196
%198 = load i32, i32* @STDERR_FILENO
%199 = call i32 @dup2(i32 %197, i32 %198)
%200 = load i32, i32* @STDERR_FILENO
%201 = icmp ne i32 %199, %200
br i1 %201, label %202, label %204
202: ; preds = %195
%203 = call i32 @err_sys(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.18, i64 0, i64 0))
br label %204
204: ; preds = %202, %195
%205 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%206 = load i32, i32* %205
%207 = call i32 @close(i32 %206)
br label %208
208: ; preds = %204, %190
%209 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.19, i64 0, i64 0))
br label %210
210: ; preds = %219, %208
%211 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%212 = call i64 @gets(i8* %211)
%213 = icmp ne i64 %212, 0
br i1 %213, label %214, label %222
214: ; preds = %210
%215 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%216 = call i64 @strncmp(i8* %215, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.20, i64 0, i64 0), i32 4)
%217 = icmp eq i64 %216, 0
br i1 %217, label %218, label %219
218: ; preds = %214
br label %222
219: ; preds = %214
%220 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%221 = call i32 @puts(i8* %220)
br label %210
222: ; preds = %218, %210
br label %223
223: ; preds = %222, %149
br label %224
224: ; preds = %223, %41
ret i32 0
}

LLVM IR (-O2):
@.str = [17 x i8] c"pipe stdin error\00"
@.str.1 = [18 x i8] c"pipe stdout error\00"
@STDIN_FILENO = global i32 0
@.str.2 = [33 x i8] c"max fd: %d, pipe: %d, stdin: %d\0A\00"
@.str.3 = [11 x i8] c"fork error\00"
@.str.4 = [13 x i8] c"select error\00"
@.str.8 = [17 x i8] c"read stdin error\00"
@.str.10 = [17 x i8] c"write pipe error\00"
@.str.13 = [16 x i8] c"read pipe error\00"
@STDOUT_FILENO = global i32 0
@.str.14 = [19 x i8] c"write stdout error\00"
@.str.16 = [17 x i8] c"dup2 stdin error\00"
@.str.17 = [18 x i8] c"dup2 stdout error\00"
@STDERR_FILENO = global i32 0
@.str.18 = [18 x i8] c"dup2 stderr error\00"
@.str.20 = [5 x i8] c"exit\00"
@str = [46 x i8] c"child begin to redirect pipe to stdin/out/err\00"
@str.21 = [12 x i8] c"redirect OK\00"
@str.22 = [10 x i8] c"got event\00"
@str.23 = [13 x i8] c"poll pipe in\00"
@str.26 = [13 x i8] c"select stdin\00"
@str.27 = [10 x i8] c"write end\00"
@str.28 = [9 x i8] c"read end\00"
@str.29 = [25 x i8] c"select over but no event\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to [2 x i32]*
%5 = alloca i64
%6 = bitcast i64* %5 to [2 x i32]*
%7 = alloca [4096 x i8]
%8 = alloca i32
%9 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%10 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%11 = bitcast i64* %3 to i32*
%12 = call i64 @pipe(i32* %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @err_sys(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = bitcast i64* %5 to i32*
%18 = call i64 @pipe(i32* %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @err_sys(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%24 = load i32, i32* %17
%25 = load i32, i32* @STDIN_FILENO
%26 = call i32 @MAX(i32 %24, i32 %25)
%27 = add nsw i32 %26, 1
%28 = load i32, i32* %17
%29 = load i32, i32* @STDIN_FILENO
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %27, i32 %28, i32 %29)
%31 = call i64 (...) @fork()
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %22
%34 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %156
35: ; preds = %22
%36 = icmp eq i64 %31, 0
br i1 %36, label %102, label %37
37: ; preds = %35
%38 = load i32, i32* %11
%39 = call i32 @close(i32 %38)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = bitcast i32* %8 to i8*
%44 = call i32 @FD_ZERO(i32* %8)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
br label %46
46: ; preds = %81, %37
%47 = load i32, i32* %17
%48 = call i32 @FD_SET(i32 %47, i32* %8)
%49 = load i32, i32* @STDIN_FILENO
%50 = call i32 @FD_SET(i32 %49, i32* %8)
%51 = call i32 @select(i32 %27, i32* %8, i32* null, i32* null, i32* null)
switch i32 %51, label %56 [
i32 -1, label %52
i32 0, label %54
]
52: ; preds = %46
%53 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %56
54: ; preds = %46
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.29, i64 0, i64 0))
br label %81
56: ; preds = %46, %52
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.22, i64 0, i64 0))
%58 = load i32, i32* @STDIN_FILENO
%59 = call i64 @FD_ISSET(i32 %58, i32* %8)
%60 = icmp eq i64 %59, 0
br i1 %60, label %77, label %61
61: ; preds = %56
%62 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.26, i64 0, i64 0))
%63 = load i32, i32* @STDIN_FILENO
%64 = call i32 @read(i32 %63, i8* %23, i32 4096)
%65 = icmp slt i32 %64, 0
br i1 %65, label %74, label %66
66: ; preds = %61
%67 = icmp eq i32 %64, 0
br i1 %67, label %99, label %68
68: ; preds = %66
%69 = load i32, i32* %45
%70 = call i32 @write(i32 %69, i8* %23, i32 %64)
%71 = icmp slt i32 %70, 0
br i1 %71, label %74, label %72
72: ; preds = %68
%73 = icmp eq i32 %70, 0
br i1 %73, label %99, label %77
74: ; preds = %68, %61
%75 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), %61 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0), %68 ]
%76 = call i32 @err_sys(i8* %75)
br label %77
77: ; preds = %74, %72, %56
%78 = load i32, i32* %17
%79 = call i64 @FD_ISSET(i32 %78, i32* %8)
%80 = icmp eq i64 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %77, %97, %95, %87, %54
br label %46
82: ; preds = %77
%83 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.23, i64 0, i64 0))
%84 = load i32, i32* %17
%85 = call i32 @read(i32 %84, i8* %23, i32 4096)
%86 = icmp slt i32 %85, 0
br i1 %86, label %87, label %89
87: ; preds = %82
%88 = call i32 @err_sys(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.13, i64 0, i64 0))
br label %81
89: ; preds = %82
%90 = icmp eq i32 %85, 0
br i1 %90, label %99, label %91
91: ; preds = %89
%92 = load i32, i32* @STDOUT_FILENO
%93 = call i32 @write(i32 %92, i8* %23, i32 %85)
%94 = icmp slt i32 %93, 0
br i1 %94, label %95, label %97
95: ; preds = %91
%96 = call i32 @err_sys(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.14, i64 0, i64 0))
br label %81
97: ; preds = %91
%98 = icmp eq i32 %93, 0
br i1 %98, label %99, label %81
99: ; preds = %97, %89, %72, %66
%100 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @str.28, i64 0, i64 0), %66 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @str.27, i64 0, i64 0), %72 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @str.28, i64 0, i64 0), %89 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @str.27, i64 0, i64 0), %97 ]
%101 = call i32 @puts(i8* dereferenceable(1) %100)
br label %156
102: ; preds = %35
%103 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%104 = load i32, i32* %103
%105 = call i32 @close(i32 %104)
%106 = load i32, i32* %17
%107 = call i32 @close(i32 %106)
%108 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
%109 = load i32, i32* %11
%110 = load i32, i32* @STDIN_FILENO
%111 = icmp eq i32 %109, %110
br i1 %111, label %121, label %112
112: ; preds = %102
%113 = call i32 @dup2(i32 %109, i32 %110)
%114 = load i32, i32* @STDIN_FILENO
%115 = icmp eq i32 %113, %114
br i1 %115, label %118, label %116
116: ; preds = %112
%117 = call i32 @err_sys(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.16, i64 0, i64 0))
br label %118
118: ; preds = %116, %112
%119 = load i32, i32* %11
%120 = call i32 @close(i32 %119)
br label %121
121: ; preds = %118, %102
%122 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%123 = load i32, i32* %122
%124 = load i32, i32* @STDOUT_FILENO
%125 = icmp eq i32 %123, %124
br i1 %125, label %132, label %126
126: ; preds = %121
%127 = call i32 @dup2(i32 %123, i32 %124)
%128 = load i32, i32* @STDOUT_FILENO
%129 = icmp eq i32 %127, %128
br i1 %129, label %132, label %130
130: ; preds = %126
%131 = call i32 @err_sys(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.17, i64 0, i64 0))
br label %132
132: ; preds = %126, %130, %121
%133 = load i32, i32* %122
%134 = load i32, i32* @STDERR_FILENO
%135 = icmp eq i32 %133, %134
br i1 %135, label %145, label %136
136: ; preds = %132
%137 = call i32 @dup2(i32 %133, i32 %134)
%138 = load i32, i32* @STDERR_FILENO
%139 = icmp eq i32 %137, %138
br i1 %139, label %142, label %140
140: ; preds = %136
%141 = call i32 @err_sys(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.18, i64 0, i64 0))
br label %142
142: ; preds = %140, %136
%143 = load i32, i32* %122
%144 = call i32 @close(i32 %143)
br label %145
145: ; preds = %142, %132
%146 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.21, i64 0, i64 0))
%147 = call i64 @gets(i8* %23)
%148 = icmp eq i64 %147, 0
br i1 %148, label %156, label %149
149: ; preds = %145, %152
%150 = call i64 @strncmp(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.20, i64 0, i64 0), i32 4)
%151 = icmp eq i64 %150, 0
br i1 %151, label %156, label %152
152: ; preds = %149
%153 = call i32 @puts(i8* %23)
%154 = call i64 @gets(i8* %23)
%155 = icmp eq i64 %154, 0
br i1 %155, label %156, label %149
156: ; preds = %152, %149, %145, %99, %33
ret i32 0
}