#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*/ execve(char *, char **, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/, int /*<<< orphan*/);
void divide_and_conquer(char *cmd, char **envp, int no_of_commands,
                        char ***main_arguments) {
  int fd[2];
  int i;
  for (i = 0; i < no_of_commands; i++) {
    pipe(fd);
    int pid = fork();
    if (pid != 0) {
      close(fd[1]);
      dup2(fd[0], 0);
      close(fd[0]);
      waitpid(pid, 0, 0);
      execve(main_arguments[no_of_commands - i - 1][0],
             main_arguments[no_of_commands - i - 1], envp);
      printf("No such file or directory\n");
      exit(0);
    } else {
      if (i == (no_of_commands - 1))
        exit(0);
      close(fd[0]);
      dup2(fd[1], 1);
      close(fd[1]);
    }
  }
}


x86 assembly (clang, -O0):
<divide_and_conquer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    127c <divide_and_conquer+0xfc>
lea    -0x28(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     1234 <divide_and_conquer+0xb4>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1080 <waitpid@plt>
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x10(%rbp),%rdx
call   1070 <execve@plt>
lea    0xdd8(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x2c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    124c <divide_and_conquer+0xcc>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
jmp    126e <divide_and_conquer+0xee>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    119e <divide_and_conquer+0x1e>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<divide_and_conquer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jg     119e <divide_and_conquer+0x1e>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rcx,%r15
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rsp,%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    1214 <divide_and_conquer+0x94>
mov    $0x1,%ebx
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    %ebx,%ebp
je     1263 <divide_and_conquer+0xe3>
mov    (%rsp),%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    %r12,%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
add    $0x1,%ebx
test   %eax,%eax
je     11d0 <divide_and_conquer+0x50>
mov    %eax,%r13d
neg    %ebx
jmp    121c <divide_and_conquer+0x9c>
mov    %eax,%r13d
mov    $0xffffffff,%ebx
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    (%rsp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    (%rsp),%edi
call   1050 <close@plt>
mov    %r13d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
add    %ebp,%ebx
movslq %ebx,%rax
mov    (%r15,%rax,8),%rsi
mov    (%rsi),%rdi
mov    %r14,%rdx
call   1070 <execve@plt>
lea    0xda2(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"No such file or directory\0A\00"
define void @divide_and_conquer(i8* %0, i8** %1, i32 %2, i8*** %3) {
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i8***
%9 = alloca [2 x i32]
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
store i8*** %3, i8**** %8
store i32 0, i32* %10
br label %12
12: ; preds = %74, %4
%13 = load i32, i32* %10
%14 = load i32, i32* %7
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %77
16: ; preds = %12
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%18 = call i32 @pipe(i32* %17)
%19 = call i32 (...) @fork()
store i32 %19, i32* %11
%20 = load i32, i32* %11
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %56
22: ; preds = %16
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @dup2(i32 %27, i32 0)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %11
%33 = call i32 @waitpid(i32 %32, i32 0, i32 0)
%34 = load i8***, i8**** %8
%35 = load i32, i32* %7
%36 = load i32, i32* %10
%37 = sub nsw i32 %35, %36
%38 = sub nsw i32 %37, 1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8**, i8*** %34, i64 %39
%41 = load i8**, i8*** %40
%42 = getelementptr inbounds i8*, i8** %41, i64 0
%43 = load i8*, i8** %42
%44 = load i8***, i8**** %8
%45 = load i32, i32* %7
%46 = load i32, i32* %10
%47 = sub nsw i32 %45, %46
%48 = sub nsw i32 %47, 1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8**, i8*** %44, i64 %49
%51 = load i8**, i8*** %50
%52 = load i8**, i8*** %6
%53 = call i32 @execve(i8* %43, i8** %51, i8** %52)
%54 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%55 = call i32 @exit(i32 0)
unreachable
56: ; preds = %16
%57 = load i32, i32* %10
%58 = load i32, i32* %7
%59 = sub nsw i32 %58, 1
%60 = icmp eq i32 %57, %59
br i1 %60, label %61, label %63
61: ; preds = %56
%62 = call i32 @exit(i32 0)
unreachable
63: ; preds = %56
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%65 = load i32, i32* %64
%66 = call i32 @close(i32 %65)
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%68 = load i32, i32* %67
%69 = call i32 @dup2(i32 %68, i32 1)
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%71 = load i32, i32* %70
%72 = call i32 @close(i32 %71)
br label %73
73: ; preds = %63
br label %74
74: ; preds = %73
%75 = load i32, i32* %10
%76 = add nsw i32 %75, 1
store i32 %76, i32* %10
br label %12
77: ; preds = %12
ret void
}

LLVM IR (-O2):
@str = [26 x i8] c"No such file or directory\00"
define void @divide_and_conquer(i8* nocapture readnone %0, i8** %1, i32 %2, i8*** nocapture %3) {
%5 = alloca [2 x i32]
%6 = bitcast [2 x i32]* %5 to i8*
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %51
8: ; preds = %4
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%10 = add nsw i32 %2, -1
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%12 = call i32 @pipe(i32* %9)
%13 = call i32 (...) @fork()
%14 = icmp eq i32 %13, 0
br i1 %14, label %35, label %17
15: ; preds = %40
%16 = sub nuw i32 -2, %36
br label %17
17: ; preds = %15, %8
%18 = phi i32 [ %16, %15 ], [ -1, %8 ]
%19 = phi i32 [ %49, %15 ], [ %13, %8 ]
%20 = load i32, i32* %11
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %9
%23 = call i32 @dup2(i32 %22, i32 0)
%24 = load i32, i32* %9
%25 = call i32 @close(i32 %24)
%26 = call i32 @waitpid(i32 %19, i32 0, i32 0)
%27 = add i32 %18, %2
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8**, i8*** %3, i64 %28
%30 = load i8**, i8*** %29
%31 = load i8*, i8** %30
%32 = call i32 @execve(i8* %31, i8** %30, i8** %1)
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %8, %40
%36 = phi i32 [ %47, %40 ], [ 0, %8 ]
%37 = icmp eq i32 %36, %10
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %35
%41 = load i32, i32* %9
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %11
%44 = call i32 @dup2(i32 %43, i32 1)
%45 = load i32, i32* %11
%46 = call i32 @close(i32 %45)
%47 = add nuw nsw i32 %36, 1
%48 = call i32 @pipe(i32* %9)
%49 = call i32 (...) @fork()
%50 = icmp eq i32 %49, 0
br i1 %50, label %35, label %15
51: ; preds = %4
ret void
}