#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 close(int);
int dup2(int, int);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ stderr;
int pipeCommands(int ac, char *av[]) {
  int thepipe[2];
  pid_t pid;
  if (ac == 1) {
    fprintf(stderr, "usage: pipe3 cmd1 cmd2 ..\n");
    exit(1);
  }
  if (ac == 2) {
    execlp(av[1], av[1], NULL);
    {
      perror(av[1]);
      exit(1);
    };
  }
  if (pipe(thepipe) == -1) {
    {
      perror("Cannot get pipe");
      exit(1);
    };
  }
  pid = fork();
  if (pid == -1) {
    {
      perror("Cannot fork");
      exit(1);
    };
  }
  if (pid == 0) {
    if (dup2(thepipe[1], 1) == -1) {
      {
        perror("dup2");
        exit(1);
      };
    }
    if (close(thepipe[0]) == -1 || close(thepipe[1]) == -1) {
      {
        perror("close pipe");
        exit(1);
      };
    }
    pipeCommands(ac - 1, av);
  } else {
    if (dup2(thepipe[0], 0) == -1) {
      {
        perror("dup2");
        exit(1);
      };
    }
    if (close(thepipe[0]) == -1 || close(thepipe[1]) == -1) {
      {
        perror("close pipe");
        exit(1);
      };
    }
    execlp(av[ac - 1], av[ac - 1], NULL);
    {
      perror(av[ac - 1]);
      exit(1);
    };
  }
  return 0;
}


x86 assembly (clang, -O0):
<pipeCommands>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x4(%rbp)
jne    11c8 <pipeCommands+0x38>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x2,-0x4(%rbp)
jne    1202 <pipeCommands+0x72>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10a0 <execlp@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    122a <pipeCommands+0x9a>
lea    0xe00(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1254 <pipeCommands+0xc4>
lea    0xde6(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    12d6 <pipeCommands+0x146>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    128a <pipeCommands+0xfa>
lea    0xdbc(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     12ac <pipeCommands+0x11c>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12c2 <pipeCommands+0x132>
lea    0xd89(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
mov    -0x10(%rbp),%rsi
call   1070 <pipeCommands@plt>
jmp    1382 <pipeCommands+0x1f2>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12ff <pipeCommands+0x16f>
lea    0xd47(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1321 <pipeCommands+0x191>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1337 <pipeCommands+0x1a7>
lea    0xd14(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x10(%rbp),%rax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10a0 <execlp@plt>
mov    -0x10(%rbp),%rax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipeCommands>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
je     1224 <pipeCommands+0x94>
mov    %edi,%ebp
cmp    $0x1,%edi
je     1238 <pipeCommands+0xa8>
mov    %rsp,%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     1257 <pipeCommands+0xc7>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
jne    1216 <pipeCommands+0x86>
mov    0x4(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     126f <pipeCommands+0xdf>
mov    (%rsp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1293 <pipeCommands+0x103>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1293 <pipeCommands+0x103>
add    $0xffffffff,%ebp
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <pipeCommands@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
cmp    $0xffffffff,%eax
jne    1260 <pipeCommands+0xd0>
lea    0xe09(%rip),%rdi
jmp    129a <pipeCommands+0x10a>
mov    0x8(%rbx),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10a0 <execlp@plt>
mov    0x8(%rbx),%rdi
jmp    129a <pipeCommands+0x10a>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    0xdb8(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdbd(%rip),%rdi
jmp    129a <pipeCommands+0x10a>
mov    (%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1278 <pipeCommands+0xe8>
lea    0xdc1(%rip),%rdi
jmp    129a <pipeCommands+0x10a>
mov    (%rsp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1293 <pipeCommands+0x103>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12a9 <pipeCommands+0x119>
lea    0xda2(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movslq %ebp,%rbp
mov    -0x8(%rbx,%rbp,8),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10a0 <execlp@plt>
mov    -0x8(%rbx,%rbp,8),%rdi
jmp    129a <pipeCommands+0x10a>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"usage: pipe3 cmd1 cmd2 ..\0A\00"
@.str.1 = [16 x i8] c"Cannot get pipe\00"
@.str.2 = [12 x i8] c"Cannot fork\00"
@.str.3 = [5 x i8] c"dup2\00"
@.str.4 = [11 x i8] c"close pipe\00"
define i32 @pipeCommands(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
switch i32 %0, label %16 [
i32 1, label %5
i32 2, label %9
]
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @execlp(i8* %11, i8* %11, i32* null)
%13 = load i8*, i8** %10
%14 = tail call i32 @perror(i8* %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%18 = call i32 @pipe(i32* %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = call i32 (...) @fork()
switch i32 %24, label %50 [
i32 -1, label %25
i32 0, label %28
]
25: ; preds = %23
%26 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %23
%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)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %28
%37 = load i32, i32* %17
%38 = call i32 @close(i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = load i32, i32* %29
%42 = call i32 @close(i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %40, %36
%45 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %40
%48 = add nsw i32 %0, -1
%49 = call i32 @pipeCommands(i32 %48, i8** %1)
ret i32 0
50: ; preds = %23
%51 = load i32, i32* %17
%52 = call i32 @dup2(i32 %51, i32 0)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %50
%58 = load i32, i32* %17
%59 = call i32 @close(i32 %58)
%60 = icmp eq i32 %59, -1
br i1 %60, label %66, label %61
61: ; preds = %57
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%63 = load i32, i32* %62
%64 = call i32 @close(i32 %63)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %69
66: ; preds = %61, %57
%67 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %61
%70 = add nsw i32 %0, -1
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8*, i8** %1, i64 %71
%73 = load i8*, i8** %72
%74 = call i32 @execlp(i8* %73, i8* %73, i32* null)
%75 = load i8*, i8** %72
%76 = call i32 @perror(i8* %75)
%77 = call i32 @exit(i32 1)
unreachable
}