#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ wait(int *);
void executePipe(char ***cmd) {
  int p[2];
  pid_t pid;
  int fd_in = 0;
  int status;
  while (*cmd != NULL) {
    pipe(p);
    if ((pid = fork()) == -1) {
      exit(1);
    } else if (pid == 0) {
      dup2(fd_in, 0);
      if (*(cmd + 1) != NULL) {
        dup2(p[1], 1);
      }
      close(p[0]);
      execvp((*cmd)[0], *cmd);
      exit(0);
    } else {
      wait(&status);
      close(p[1]);
      fd_in = p[0];
      cmd++;
    }
  }
}


x86 assembly (clang, -O0):
<executePipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     1239 <executePipe+0xc9>
lea    -0x10(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11b7 <executePipe+0x47>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    120c <executePipe+0x9c>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     11e7 <executePipe+0x77>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
call   1060 <execvp@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    -0x1c(%rbp),%rdi
call   1080 <wait@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x8(%rbp)
jmp    1234 <executePipe+0xc4>
jmp    1183 <executePipe+0x13>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<executePipe>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
cmpq   $0x0,(%rdi)
je     11d7 <executePipe+0x67>
mov    %rdi,%rbx
add    $0x8,%rbx
lea    0xc(%rsp),%r14
lea    0x14(%rsp),%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11e2 <executePipe+0x72>
cmp    $0xffffffff,%eax
je     121b <executePipe+0xab>
mov    %r15,%rdi
call   1080 <wait@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%ebp
cmpq   $0x0,(%rbx)
lea    0x8(%rbx),%rbx
jne    11a0 <executePipe+0x30>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
cmpq   $0x0,(%rbx)
je     11ff <executePipe+0x8f>
mov    0x10(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    -0x8(%rbx),%rsi
mov    (%rsi),%rdi
call   1060 <execvp@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
define void @executePipe(i8*** %0) {
%2 = alloca i8***
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8*** %0, i8**** %2
store i32 0, i32* %5
br label %7
7: ; preds = %54, %1
%8 = load i8***, i8**** %2
%9 = load i8**, i8*** %8
%10 = icmp ne i8** %9, null
br i1 %10, label %11, label %55
11: ; preds = %7
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = call i32 (...) @fork()
store i32 %14, i32* %4
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %44
21: ; preds = %18
%22 = load i32, i32* %5
%23 = call i32 @dup2(i32 %22, i32 0)
%24 = load i8***, i8**** %2
%25 = getelementptr inbounds i8**, i8*** %24, i64 1
%26 = load i8**, i8*** %25
%27 = icmp ne i8** %26, null
br i1 %27, label %28, label %32
28: ; preds = %21
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @dup2(i32 %30, i32 1)
br label %32
32: ; preds = %28, %21
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = load i8***, i8**** %2
%37 = load i8**, i8*** %36
%38 = getelementptr inbounds i8*, i8** %37, i64 0
%39 = load i8*, i8** %38
%40 = load i8***, i8**** %2
%41 = load i8**, i8*** %40
%42 = call i32 @execvp(i8* %39, i8** %41)
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %18
%45 = call i32 @wait(i32* %6)
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%50 = load i32, i32* %49
store i32 %50, i32* %5
%51 = load i8***, i8**** %2
%52 = getelementptr inbounds i8**, i8*** %51, i32 1
store i8*** %52, i8**** %2
br label %53
53: ; preds = %44
br label %54
54: ; preds = %53
br label %7
55: ; preds = %7
ret void
}

LLVM IR (-O2):
define void @executePipe(i8*** nocapture %0) {
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = bitcast [2 x i32]* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = load i8**, i8*** %0
%7 = icmp eq i8** %6, null
br i1 %7, label %41, label %8
8: ; preds = %1
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
br label %11
11: ; preds = %8, %33
%12 = phi i32 [ 0, %8 ], [ %37, %33 ]
%13 = phi i8*** [ %0, %8 ], [ %38, %33 ]
%14 = call i32 @pipe(i32* %9)
%15 = call i32 (...) @fork()
switch i32 %15, label %33 [
i32 -1, label %16
i32 0, label %18
]
16: ; preds = %11
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = call i32 @dup2(i32 %12, i32 0)
%20 = getelementptr inbounds i8**, i8*** %13, i64 1
%21 = load i8**, i8*** %20
%22 = icmp eq i8** %21, null
br i1 %22, label %26, label %23
23: ; preds = %18
%24 = load i32, i32* %10
%25 = call i32 @dup2(i32 %24, i32 1)
br label %26
26: ; preds = %23, %18
%27 = load i32, i32* %9
%28 = call i32 @close(i32 %27)
%29 = load i8**, i8*** %13
%30 = load i8*, i8** %29
%31 = call i32 @execvp(i8* %30, i8** %29)
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %11
%34 = call i32 @wait(i32* %3)
%35 = load i32, i32* %10
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* %9
%38 = getelementptr inbounds i8**, i8*** %13, i64 1
%39 = load i8**, i8*** %38
%40 = icmp eq i8** %39, null
br i1 %40, label %41, label %11
41: ; preds = %33, %1
ret void
}