#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*/ cmd_binary(char **, int, char **);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ exit(int);
int fork();
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *, ...);
int /*<<< orphan*/ pipe(int *);
char **strtoken(char *, char *, int *);
int /*<<< orphan*/ trim(char *);
int waitpid(int, int *, int /*<<< orphan*/);
int runPipe(char *tokens[], char *envp[], int pipes) {
  int fd[2], pid, status;
  int param_len;
  char **params = (char **)malloc(200 * sizeof(char *));
  trim(tokens[pipes]);
  params = strtoken(tokens[pipes], " ", &param_len);
  pipe(fd);
  if (pipes == 0) {
    close(fd[0]);
    cmd_binary(params, param_len, envp);
    exit(0);
  } else {
    pid = fork();
    if (pid == -1)
      perror("unable to fork child\n");
    else if (pid == 0) {
      close(fd[0]);
      dup2(fd[1], 1);
      runPipe(tokens, envp, --pipes);
    } else {
      close(fd[1]);
      dup2(fd[0], 0);
      if (pid == waitpid(pid, &status, 0)) {
        ;
      } else {
        perror("error in waitpid. Error no is %d\n", status);
        exit(1);
      }
      cmd_binary(params, param_len, envp);
      exit(0);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<runPipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x640,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1060 <trim@plt>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xdfb(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   10a0 <strtoken@plt>
mov    %rax,-0x30(%rbp)
lea    -0x1c(%rbp),%rdi
call   1080 <pipe@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1244 <runPipe+0x84>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1030 <cmd_binary@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    126b <runPipe+0xab>
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   10c0 <perror@plt>
jmp    1314 <runPipe+0x154>
cmpl   $0x0,-0x20(%rbp)
jne    12a5 <runPipe+0xe5>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
add    $0xffffffff,%edx
mov    %edx,-0x14(%rbp)
call   1040 <runPipe@plt>
jmp    130f <runPipe+0x14f>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   10b0 <waitpid@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
jne    12dd <runPipe+0x11d>
jmp    12f8 <runPipe+0x138>
mov    -0x24(%rbp),%esi
lea    0xd31(%rip),%rdi
mov    $0x0,%al
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1030 <cmd_binary@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
jmp    1314 <runPipe+0x154>
jmp    1319 <runPipe+0x159>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runPipe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r13
mov    $0x640,%edi
call   1090 <malloc@plt>
movslq %ebp,%rbx
mov    0x0(%r13,%rbx,8),%rdi
call   1060 <trim@plt>
mov    0x0(%r13,%rbx,8),%rdi
lea    0xe07(%rip),%rsi
lea    0x8(%rsp),%rdx
call   10a0 <strtoken@plt>
mov    %rax,%r14
lea    0xc(%rsp),%rdi
call   1080 <pipe@plt>
test   %ebx,%ebx
je     126f <runPipe+0xaf>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
jne    1257 <runPipe+0x97>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x10(%rsp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
add    $0xffffffff,%ebp
mov    %r13,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1040 <runPipe@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    127a <runPipe+0xba>
lea    0xd9c(%rip),%rdi
xor    %eax,%eax
call   10c0 <perror@plt>
jmp    1246 <runPipe+0x86>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
jmp    12a2 <runPipe+0xe2>
mov    0x10(%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
lea    0x14(%rsp),%rsi
mov    %r12d,%edi
xor    %edx,%edx
call   10b0 <waitpid@plt>
cmp    %eax,%r12d
jne    12b8 <runPipe+0xf8>
mov    0x8(%rsp),%esi
mov    %r14,%rdi
mov    %r15,%rdx
call   1030 <cmd_binary@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    0x14(%rsp),%esi
lea    0xd55(%rip),%rdi
xor    %eax,%eax
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
@.str.1 = [22 x i8] c"unable to fork child\0A\00"
@.str.2 = [34 x i8] c"error in waitpid. Error no is %d\0A\00"
define i32 @runPipe(i8** %0, i8** %1, i32 %2) {
%4 = alloca i8**
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [2 x i32]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8**
store i8** %0, i8*** %4
store i8** %1, i8*** %5
store i32 %2, i32* %6
%12 = call i64 @malloc(i32 1600)
%13 = inttoptr i64 %12 to i8**
store i8** %13, i8*** %11
%14 = load i8**, i8*** %4
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8*, i8** %14, i64 %16
%18 = load i8*, i8** %17
%19 = call i32 @trim(i8* %18)
%20 = load i8**, i8*** %4
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8*, i8** %20, i64 %22
%24 = load i8*, i8** %23
%25 = call i8** @strtoken(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32* %10)
store i8** %25, i8*** %11
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%27 = call i32 @pipe(i32* %26)
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %3
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = load i8**, i8*** %11
%35 = load i32, i32* %10
%36 = load i8**, i8*** %5
%37 = call i32 @cmd_binary(i8** %34, i32 %35, i8** %36)
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %3
%40 = call i32 (...) @fork()
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 (i8*, ...) @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %83
45: ; preds = %39
%46 = load i32, i32* %8
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %60
48: ; preds = %45
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%53 = load i32, i32* %52
%54 = call i32 @dup2(i32 %53, i32 1)
%55 = load i8**, i8*** %4
%56 = load i8**, i8*** %5
%57 = load i32, i32* %6
%58 = add nsw i32 %57, -1
store i32 %58, i32* %6
%59 = call i32 @runPipe(i8** %55, i8** %56, i32 %58)
br label %82
60: ; preds = %45
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%62 = load i32, i32* %61
%63 = call i32 @close(i32 %62)
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%65 = load i32, i32* %64
%66 = call i32 @dup2(i32 %65, i32 0)
%67 = load i32, i32* %8
%68 = load i32, i32* %8
%69 = call i32 @waitpid(i32 %68, i32* %9, i32 0)
%70 = icmp eq i32 %67, %69
br i1 %70, label %71, label %72
71: ; preds = %60
br label %76
72: ; preds = %60
%73 = load i32, i32* %9
%74 = call i32 (i8*, ...) @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %73)
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %71
%77 = load i8**, i8*** %11
%78 = load i32, i32* %10
%79 = load i8**, i8*** %5
%80 = call i32 @cmd_binary(i8** %77, i32 %78, i8** %79)
%81 = call i32 @exit(i32 0)
unreachable
82: ; preds = %48
br label %83
83: ; preds = %82, %43
br label %84
84: ; preds = %83
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
@.str.1 = [22 x i8] c"unable to fork child\0A\00"
@.str.2 = [34 x i8] c"error in waitpid. Error no is %d\0A\00"
define i32 @runPipe(i8** nocapture %0, i8** %1, i32 %2) {
%4 = alloca [2 x i32]
%5 = alloca i32
%6 = alloca i32
%7 = bitcast [2 x i32]* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = tail call i64 @malloc(i32 1600)
%11 = sext i32 %2 to i64
%12 = getelementptr inbounds i8*, i8** %0, i64 %11
%13 = load i8*, i8** %12
%14 = tail call i32 @trim(i8* %13)
%15 = load i8*, i8** %12
%16 = call i8** @strtoken(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32* %6)
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%18 = call i32 @pipe(i32* %17)
%19 = icmp eq i32 %2, 0
br i1 %19, label %20, label %26
20: ; preds = %3
%21 = load i32, i32* %17
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %6
%24 = call i32 @cmd_binary(i8** %16, i32 %23, i8** %1)
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %3
%27 = call i32 (...) @fork()
switch i32 %27, label %38 [
i32 -1, label %28
i32 0, label %30
]
28: ; preds = %26
%29 = call i32 (i8*, ...) @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %54
30: ; preds = %26
%31 = load i32, i32* %17
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @dup2(i32 %34, i32 1)
%36 = add nsw i32 %2, -1
%37 = call i32 @runPipe(i8** %0, i8** %1, i32 %36)
br label %54
38: ; preds = %26
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %17
%43 = call i32 @dup2(i32 %42, i32 0)
%44 = call i32 @waitpid(i32 %27, i32* %5, i32 0)
%45 = icmp eq i32 %27, %44
br i1 %45, label %50, label %46
46: ; preds = %38
%47 = load i32, i32* %5
%48 = call i32 (i8*, ...) @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %47)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %38
%51 = load i32, i32* %6
%52 = call i32 @cmd_binary(i8** %16, i32 %51, i8** %1)
%53 = call i32 @exit(i32 0)
unreachable
54: ; preds = %28, %30
ret i32 0
}