#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 STDIN_FILENO;
int STDOUT_FILENO;
int close(int);
int dup2(int, int);
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
void do_command(char ***commands, int commands_nr, int in_fd) {
  pid_t pid;
  int pipes[2];
  if (commands_nr == 0) {
    exit(1);
  }
  if (commands_nr == 1) {
    if (in_fd != STDIN_FILENO) {
      if (dup2(in_fd, STDIN_FILENO) != -1)
        close(in_fd);
    }
    execv(commands[0][0], commands[0]);
    exit(1);
  } else {
    if ((pipe(pipes) == -1) || ((pid = fork()) == -1)) {
      perror("error when we initialized pipes");
    }
    if (pid == 0) {
      close(pipes[0]);
      if (dup2(in_fd, STDIN_FILENO) == -1)
        perror("Failed to redirect stdin");
      if (dup2(pipes[1], STDOUT_FILENO) == -1)
        perror("Failed to redirect stdout");
      else if ((close(pipes[1]) == -1))
        perror("Failed to close extra pipe descriptors");
      else {
        execv(commands[0][0], commands[0]);
      }
    }
    close(pipes[1]);
    close(in_fd);
    do_command(commands + 1, commands_nr - 1, pipes[0]);
  }
}


x86 assembly (clang, -O0):
<do_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11a6 <do_command+0x26>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x1,-0xc(%rbp)
jne    1209 <do_command+0x89>
mov    -0x10(%rbp),%eax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%eax
je     11e9 <do_command+0x69>
mov    -0x10(%rbp),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     11e4 <do_command+0x64>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
jmp    11e9 <do_command+0x69>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
call   1090 <execv@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x1c(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     122e <do_command+0xae>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    123a <do_command+0xba>
lea    0xdcb(%rip),%rdi
call   1070 <perror@plt>
cmpl   $0x0,-0x14(%rbp)
jne    12df <do_command+0x15f>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1272 <do_command+0xf2>
lea    0xdb3(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    129d <do_command+0x11d>
lea    0xda6(%rip),%rdi
call   1070 <perror@plt>
jmp    12da <do_command+0x15a>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    12bf <do_command+0x13f>
lea    0xd9e(%rip),%rdi
call   1070 <perror@plt>
jmp    12d5 <do_command+0x155>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
call   1090 <execv@plt>
jmp    12da <do_command+0x15a>
jmp    12df <do_command+0x15f>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rdi
add    $0x8,%rdi
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
mov    -0x1c(%rbp),%edx
call   1030 <do_command@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_command>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rdi,%rbx
cmp    $0x1,%esi
je     1276 <do_command+0xf6>
mov    %esi,%ebp
test   %esi,%esi
je     12a4 <do_command+0x124>
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     11c0 <do_command+0x40>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11ce <do_command+0x4e>
cmp    $0xffffffff,%eax
jne    121d <do_command+0x9d>
lea    0xe39(%rip),%rdi
call   1070 <perror@plt>
jmp    121d <do_command+0x9d>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     124c <do_command+0xcc>
mov    0xc(%rsp),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     125a <do_command+0xda>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     1268 <do_command+0xe8>
mov    (%rbx),%rsi
mov    (%rsi),%rdi
call   1090 <execv@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1050 <close@plt>
add    $0x8,%rbx
add    $0xffffffff,%ebp
mov    0x8(%rsp),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <do_command@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdcd(%rip),%rdi
call   1070 <perror@plt>
jmp    11ed <do_command+0x6d>
lea    0xdd8(%rip),%rdi
call   1070 <perror@plt>
jmp    121d <do_command+0x9d>
lea    0xde4(%rip),%rdi
call   1070 <perror@plt>
jmp    121d <do_command+0x9d>
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
cmp    %r14d,%esi
je     1299 <do_command+0x119>
mov    %r14d,%edi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     1299 <do_command+0x119>
mov    %r14d,%edi
call   1050 <close@plt>
mov    (%rbx),%rsi
mov    (%rsi),%rdi
call   1090 <execv@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@.str = [32 x i8] c"error when we initialized pipes\00"
@.str.1 = [25 x i8] c"Failed to redirect stdin\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [26 x i8] c"Failed to redirect stdout\00"
@.str.3 = [39 x i8] c"Failed to close extra pipe descriptors\00"
define void @do_command(i8*** %0, i32 %1, i32 %2) {
%4 = alloca i8***
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [2 x i32]
store i8*** %0, i8**** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %3
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %40
16: ; preds = %13
%17 = load i32, i32* %6
%18 = load i32, i32* @STDIN_FILENO
%19 = icmp ne i32 %17, %18
br i1 %19, label %20, label %29
20: ; preds = %16
%21 = load i32, i32* %6
%22 = load i32, i32* @STDIN_FILENO
%23 = call i32 @dup2(i32 %21, i32 %22)
%24 = icmp ne i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %25, %20
br label %29
29: ; preds = %28, %16
%30 = load i8***, i8**** %4
%31 = getelementptr inbounds i8**, i8*** %30, i64 0
%32 = load i8**, i8*** %31
%33 = getelementptr inbounds i8*, i8** %32, i64 0
%34 = load i8*, i8** %33
%35 = load i8***, i8**** %4
%36 = getelementptr inbounds i8**, i8*** %35, i64 0
%37 = load i8**, i8*** %36
%38 = call i32 @execv(i8* %34, i8** %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %13
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%42 = call i32 @pipe(i32* %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %47, label %44
44: ; preds = %40
%45 = call i32 (...) @fork()
store i32 %45, i32* %7
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %44, %40
%48 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %49
49: ; preds = %47, %44
%50 = load i32, i32* %7
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %89
52: ; preds = %49
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%54 = load i32, i32* %53
%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 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %52
%61 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %62
62: ; preds = %60, %52
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%64 = load i32, i32* %63
%65 = load i32, i32* @STDOUT_FILENO
%66 = call i32 @dup2(i32 %64, i32 %65)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %70
68: ; preds = %62
%69 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %88
70: ; preds = %62
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%72 = load i32, i32* %71
%73 = call i32 @close(i32 %72)
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %77
75: ; preds = %70
%76 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %87
77: ; preds = %70
%78 = load i8***, i8**** %4
%79 = getelementptr inbounds i8**, i8*** %78, i64 0
%80 = load i8**, i8*** %79
%81 = getelementptr inbounds i8*, i8** %80, i64 0
%82 = load i8*, i8** %81
%83 = load i8***, i8**** %4
%84 = getelementptr inbounds i8**, i8*** %83, i64 0
%85 = load i8**, i8*** %84
%86 = call i32 @execv(i8* %82, i8** %85)
br label %87
87: ; preds = %77, %75
br label %88
88: ; preds = %87, %68
br label %89
89: ; preds = %88, %49
%90 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%91 = load i32, i32* %90
%92 = call i32 @close(i32 %91)
%93 = load i32, i32* %6
%94 = call i32 @close(i32 %93)
%95 = load i8***, i8**** %4
%96 = getelementptr inbounds i8**, i8*** %95, i64 1
%97 = load i32, i32* %5
%98 = sub nsw i32 %97, 1
%99 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%100 = load i32, i32* %99
br label %101
101: ; preds = %89
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@.str = [32 x i8] c"error when we initialized pipes\00"
@.str.1 = [25 x i8] c"Failed to redirect stdin\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [26 x i8] c"Failed to redirect stdout\00"
@.str.3 = [39 x i8] c"Failed to close extra pipe descriptors\00"
define void @do_command(i8*** nocapture %0, i32 %1, i32 %2) {
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %4 to i8*
switch i32 %1, label %21 [
i32 0, label %6
i32 1, label %8
]
6: ; preds = %3
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %3
%9 = load i32, i32* @STDIN_FILENO
%10 = icmp eq i32 %9, %2
br i1 %10, label %16, label %11
11: ; preds = %8
%12 = tail call i32 @dup2(i32 %2, i32 %9)
%13 = icmp eq i32 %12, -1
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @close(i32 %2)
br label %16
16: ; preds = %11, %14, %8
%17 = load i8**, i8*** %0
%18 = load i8*, i8** %17
%19 = tail call i32 @execv(i8* %18, i8** %17)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %3
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%23 = call i32 @pipe(i32* %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %27, label %25
25: ; preds = %21
%26 = call i32 (...) @fork()
switch i32 %26, label %55 [
i32 -1, label %27
i32 0, label %29
]
27: ; preds = %25, %21
%28 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %55
29: ; preds = %25
%30 = load i32, i32* %22
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* @STDIN_FILENO
%33 = call i32 @dup2(i32 %2, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %35, %29
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%39 = load i32, i32* %38
%40 = load i32, i32* @STDOUT_FILENO
%41 = call i32 @dup2(i32 %39, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %55
45: ; preds = %37
%46 = load i32, i32* %38
%47 = call i32 @close(i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %55
51: ; preds = %45
%52 = load i8**, i8*** %0
%53 = load i8*, i8** %52
%54 = call i32 @execv(i8* %53, i8** %52)
br label %55
55: ; preds = %25, %27, %43, %51, %49
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%57 = load i32, i32* %56
%58 = call i32 @close(i32 %57)
%59 = call i32 @close(i32 %2)
%60 = getelementptr inbounds i8**, i8*** %0, i64 1
%61 = add nsw i32 %1, -1
%62 = load i32, i32* %22
ret void
}