#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*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
char **parse_line(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *);
char *strchr(char *, char);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void fork_exec(int fdi, int fdo, char line[]) {
  if (strchr(line, '|') == NULL) {
    char **args;
    args = parse_line(line);
    int id = fork();
    if (id == 0) {
      dup2(fdi, 0);
      dup2(fdo, 1);
      execvp(args[0], args);
      printf("child couldn't be executed\n");
      exit(0);
    } else if (id > 0) {
      int status;
      waitpid(id, &status, 0);
    } else {
      printf("id<0\n");
    }
  } else {
    char *pipe_place = strchr(line, '|');
    char *first_proc_end = pipe_place - 1;
    *pipe_place = '\0';
    while (*first_proc_end == ' ') {
      *first_proc_end = '\0';
      first_proc_end--;
    }
    char *first_proc_line = line;
    line = pipe_place + 1;
    while (*line == ' ') {
      line++;
    }
    int fd[2];
    pipe(fd);
    char **args;
    args = parse_line(first_proc_line);
    int id = fork();
    if (id == 0) {
      dup2(fdi, 0);
      dup2(fd[1], 1);
      close(fd[0]);
      execvp(args[0], args);
      exit(0);
    } else if (id > 0) {
      close(fd[1]);
      fork_exec(fd[0], 1, line);
      int status;
      waitpid(id, &status, 0);
    } else {
      printf("id<0\n");
    }
  }
}


x86 assembly (clang, -O0):
<fork_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x7c,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
jne    1268 <fork_exec+0xb8>
mov    -0x10(%rbp),%rdi
call   1080 <parse_line@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1235 <fork_exec+0x85>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   10b0 <execvp@plt>
lea    0xdd7(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    1252 <fork_exec+0xa2>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
jmp    125e <fork_exec+0xae>
lea    0xdc3(%rip),%rdi
call   1050 <printf@plt>
jmp    1263 <fork_exec+0xb3>
jmp    1396 <fork_exec+0x1e6>
mov    -0x10(%rbp),%rdi
mov    $0x7c,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    12b5 <fork_exec+0x105>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
jmp    128d <fork_exec+0xdd>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    12ea <fork_exec+0x13a>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    12c9 <fork_exec+0x119>
lea    -0x40(%rbp),%rdi
call   1070 <pipe@plt>
mov    -0x38(%rbp),%rdi
call   1080 <parse_line@plt>
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jne    134a <fork_exec+0x19a>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x3c(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x40(%rbp),%edi
call   1060 <close@plt>
mov    -0x48(%rbp),%rax
mov    (%rax),%rdi
mov    -0x48(%rbp),%rsi
call   10b0 <execvp@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x4c(%rbp)
jle    1380 <fork_exec+0x1d0>
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
mov    -0x40(%rbp),%edi
mov    -0x10(%rbp),%rdx
mov    $0x1,%esi
call   1090 <fork_exec@plt>
mov    -0x4c(%rbp),%edi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
jmp    138c <fork_exec+0x1dc>
lea    0xc95(%rip),%rdi
call   1050 <printf@plt>
jmp    1391 <fork_exec+0x1e1>
jmp    1396 <fork_exec+0x1e6>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_exec>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %esi,%r15d
mov    %edi,%r14d
mov    %rdx,%rdi
mov    $0x7c,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1251 <fork_exec+0xa1>
mov    %rax,%rbx
movb   $0x0,(%rax)
cmpb   $0x20,-0x1(%rax)
jne    1200 <fork_exec+0x50>
mov    %rbx,%rax
add    $0xffffffffffffffff,%rax
nopl   0x0(%rax,%rax,1)
movb   $0x0,(%rax)
cmpb   $0x20,-0x1(%rax)
lea    -0x1(%rax),%rax
je     11f0 <fork_exec+0x40>
nopl   (%rax)
cmpb   $0x20,0x1(%rbx)
lea    0x1(%rbx),%rbx
je     1200 <fork_exec+0x50>
lea    0xc(%rsp),%rdi
call   1070 <pipe@plt>
mov    %rbp,%rdi
call   1080 <parse_line@plt>
mov    %rax,%r15
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     12a6 <fork_exec+0xf6>
jle    1279 <fork_exec+0xc9>
mov    %eax,%ebp
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
mov    %rbx,%rdx
call   1090 <fork_exec@plt>
lea    0x14(%rsp),%rsi
mov    %ebp,%edi
jmp    1270 <fork_exec+0xc0>
mov    %rbp,%rdi
call   1080 <parse_line@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     12d9 <fork_exec+0x129>
jle    1290 <fork_exec+0xe0>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   10a0 <waitpid@plt>
jmp    1285 <fork_exec+0xd5>
lea    0xd80(%rip),%rdi
call   1030 <puts@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd69(%rip),%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>
mov    %r14d,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    0x10(%rsp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    (%r15),%rdi
mov    %r15,%rsi
call   10b0 <execvp@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    %r14d,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %r15d,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   10b0 <execvp@plt>
lea    0xd03(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"child couldn't be executed\0A\00"
@.str.1 = [6 x i8] c"id<0\0A\00"
define void @fork_exec(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca [2 x i32]
%14 = alloca i8**
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%17 = load i8*, i8** %6
%18 = call i8* @strchr(i8* %17, i8 signext 124)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %48
20: ; preds = %3
%21 = load i8*, i8** %6
%22 = call i8** @parse_line(i8* %21)
store i8** %22, i8*** %7
%23 = call i32 (...) @fork()
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %38
26: ; preds = %20
%27 = load i32, i32* %4
%28 = call i32 @dup2(i32 %27, i32 0)
%29 = load i32, i32* %5
%30 = call i32 @dup2(i32 %29, i32 1)
%31 = load i8**, i8*** %7
%32 = getelementptr inbounds i8*, i8** %31, i64 0
%33 = load i8*, i8** %32
%34 = load i8**, i8*** %7
%35 = call i32 @execvp(i8* %33, i8** %34)
%36 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @exit(i32 0)
unreachable
38: ; preds = %20
%39 = load i32, i32* %8
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i32, i32* %8
%43 = call i32 @waitpid(i32 %42, i32* %9, i32 0)
br label %46
44: ; preds = %38
%45 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %46
46: ; preds = %44, %41
br label %47
47: ; preds = %46
br label %114
48: ; preds = %3
%49 = load i8*, i8** %6
%50 = call i8* @strchr(i8* %49, i8 signext 124)
store i8* %50, i8** %10
%51 = load i8*, i8** %10
%52 = getelementptr inbounds i8, i8* %51, i64 -1
store i8* %52, i8** %11
%53 = load i8*, i8** %10
store i8 0, i8* %53
br label %54
54: ; preds = %59, %48
%55 = load i8*, i8** %11
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp eq i32 %57, 32
br i1 %58, label %59, label %63
59: ; preds = %54
%60 = load i8*, i8** %11
store i8 0, i8* %60
%61 = load i8*, i8** %11
%62 = getelementptr inbounds i8, i8* %61, i32 -1
store i8* %62, i8** %11
br label %54
63: ; preds = %54
%64 = load i8*, i8** %6
store i8* %64, i8** %12
%65 = load i8*, i8** %10
%66 = getelementptr inbounds i8, i8* %65, i64 1
store i8* %66, i8** %6
br label %67
67: ; preds = %72, %63
%68 = load i8*, i8** %6
%69 = load i8, i8* %68
%70 = sext i8 %69 to i32
%71 = icmp eq i32 %70, 32
br i1 %71, label %72, label %75
72: ; preds = %67
%73 = load i8*, i8** %6
%74 = getelementptr inbounds i8, i8* %73, i32 1
store i8* %74, i8** %6
br label %67
75: ; preds = %67
%76 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 0
%77 = call i32 @pipe(i32* %76)
%78 = load i8*, i8** %12
%79 = call i8** @parse_line(i8* %78)
store i8** %79, i8*** %14
%80 = call i32 (...) @fork()
store i32 %80, i32* %15
%81 = load i32, i32* %15
%82 = icmp eq i32 %81, 0
br i1 %82, label %83, label %98
83: ; preds = %75
%84 = load i32, i32* %4
%85 = call i32 @dup2(i32 %84, i32 0)
%86 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 1
%87 = load i32, i32* %86
%88 = call i32 @dup2(i32 %87, i32 1)
%89 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 0
%90 = load i32, i32* %89
%91 = call i32 @close(i32 %90)
%92 = load i8**, i8*** %14
%93 = getelementptr inbounds i8*, i8** %92, i64 0
%94 = load i8*, i8** %93
%95 = load i8**, i8*** %14
%96 = call i32 @execvp(i8* %94, i8** %95)
%97 = call i32 @exit(i32 0)
unreachable
98: ; preds = %75
%99 = load i32, i32* %15
%100 = icmp sgt i32 %99, 0
br i1 %100, label %101, label %110
101: ; preds = %98
%102 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 1
%103 = load i32, i32* %102
%104 = call i32 @close(i32 %103)
%105 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 0
%106 = load i32, i32* %105
%107 = load i8*, i8** %6
%108 = load i32, i32* %15
%109 = call i32 @waitpid(i32 %108, i32* %16, i32 0)
br label %112
110: ; preds = %98
%111 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %112
112: ; preds = %110, %101
br label %113
113: ; preds = %112
br label %114
114: ; preds = %113, %47
ret void
}

LLVM IR (-O2):
@str.2 = [5 x i8] c"id<0\00"
@str.3 = [27 x i8] c"child couldn't be executed\00"
define void @fork_exec(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca [2 x i32]
%6 = alloca i32
%7 = tail call i8* @strchr(i8* dereferenceable(1) %2, i8 signext 124)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %27
9: ; preds = %3
%10 = tail call i8** @parse_line(i8* %2)
%11 = tail call i32 (...) @fork()
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %9
%14 = tail call i32 @dup2(i32 %0, i32 0)
%15 = tail call i32 @dup2(i32 %1, i32 1)
%16 = load i8*, i8** %10
%17 = tail call i32 @execvp(i8* %16, i8** %10)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.3, i64 0, i64 0))
%19 = tail call i32 @exit(i32 0)
unreachable
20: ; preds = %9
%21 = icmp sgt i32 %11, 0
br i1 %21, label %22, label %25
22: ; preds = %20
%23 = bitcast i32* %4 to i8*
%24 = call i32 @waitpid(i32 %11, i32* %4, i32 0)
br label %71
25: ; preds = %20
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.2, i64 0, i64 0))
br label %71
27: ; preds = %3
store i8 0, i8* %7
%28 = getelementptr inbounds i8, i8* %7, i64 -1
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 32
br i1 %30, label %31, label %36
31: ; preds = %27, %31
%32 = phi i8* [ %33, %31 ], [ %28, %27 ]
store i8 0, i8* %32
%33 = getelementptr inbounds i8, i8* %32, i64 -1
%34 = load i8, i8* %33
%35 = icmp eq i8 %34, 32
br i1 %35, label %31, label %36
36: ; preds = %31, %27
br label %37
37: ; preds = %36, %37
%38 = phi i8* [ %39, %37 ], [ %7, %36 ]
%39 = getelementptr inbounds i8, i8* %38, i64 1
%40 = load i8, i8* %39
%41 = icmp eq i8 %40, 32
br i1 %41, label %37, label %42
42: ; preds = %37
%43 = bitcast [2 x i32]* %5 to i8*
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%45 = call i32 @pipe(i32* %44)
%46 = call i8** @parse_line(i8* %2)
%47 = call i32 (...) @fork()
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %59
49: ; preds = %42
%50 = call i32 @dup2(i32 %0, i32 0)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%52 = load i32, i32* %51
%53 = call i32 @dup2(i32 %52, i32 1)
%54 = load i32, i32* %44
%55 = call i32 @close(i32 %54)
%56 = load i8*, i8** %46
%57 = call i32 @execvp(i8* %56, i8** %46)
%58 = call i32 @exit(i32 0)
unreachable
59: ; preds = %42
%60 = icmp sgt i32 %47, 0
br i1 %60, label %61, label %68
61: ; preds = %59
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%63 = load i32, i32* %62
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* %44
%66 = bitcast i32* %6 to i8*
%67 = call i32 @waitpid(i32 %47, i32* %6, i32 0)
br label %70
68: ; preds = %59
%69 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.2, i64 0, i64 0))
br label %70
70: ; preds = %61, %68
br label %71
71: ; preds = %25, %22, %70
ret void
}