#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*/ execlp(char *, char *, int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ pipe(int *);
extern void repipe(int n, char *argv[]) {
  int pipefd[2];
  pipe(pipefd);
  int pid = fork();
  if (pid == 0) {
    close(pipefd[0]);
    dup2(pipefd[1], 1);
    close(pipefd[1]);
    execlp(argv[n - 1], argv[n - 1], NULL);
  } else {
    close(pipefd[1]);
    dup2(pipefd[0], 0);
    close(pipefd[0]);
    if (argv[n + 1] == NULL)
      execlp(argv[n], argv[n], NULL);
    else
      repipe(n + 1, argv);
  }
}


x86 assembly (clang, -O0):
<repipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11d9 <repipe+0x79>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@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   1070 <execlp@plt>
jmp    1245 <repipe+0xe5>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    -0x4(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    1231 <repipe+0xd1>
mov    -0x10(%rbp),%rax
movslq -0x4(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x10(%rbp),%rax
movslq -0x4(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <execlp@plt>
jmp    1240 <repipe+0xe0>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
mov    -0x10(%rbp),%rsi
call   1060 <repipe@plt>
jmp    1245 <repipe+0xe5>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<repipe>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11b0 <repipe+0x50>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
movslq %ebp,%rax
cmpq   $0x0,0x8(%rbx,%rax,8)
je     11d9 <repipe+0x79>
add    $0x1,%ebp
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <repipe@plt>
jmp    11e7 <repipe+0x87>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
movslq %ebp,%rax
mov    -0x8(%rbx,%rax,8),%rdi
jmp    11dd <repipe+0x7d>
mov    (%rbx,%rax,8),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1070 <execlp@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define void @repipe(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 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %35
12: ; preds = %2
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%14 = load i32, i32* %13
%15 = call i32 @close(i32 %14)
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @dup2(i32 %17, i32 1)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
%22 = load i8**, i8*** %4
%23 = load i32, i32* %3
%24 = sub nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8*, i8** %22, i64 %25
%27 = load i8*, i8** %26
%28 = load i8**, i8*** %4
%29 = load i32, i32* %3
%30 = sub nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8*, i8** %28, i64 %31
%33 = load i8*, i8** %32
%34 = call i32 @execlp(i8* %27, i8* %33, i32* null)
br label %69
35: ; preds = %2
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%40 = load i32, i32* %39
%41 = call i32 @dup2(i32 %40, i32 0)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = load i8**, i8*** %4
%46 = load i32, i32* %3
%47 = add nsw i32 %46, 1
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8*, i8** %45, i64 %48
%50 = load i8*, i8** %49
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %64
52: ; preds = %35
%53 = load i8**, i8*** %4
%54 = load i32, i32* %3
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8*, i8** %53, i64 %55
%57 = load i8*, i8** %56
%58 = load i8**, i8*** %4
%59 = load i32, i32* %3
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8*, i8** %58, i64 %60
%62 = load i8*, i8** %61
%63 = call i32 @execlp(i8* %57, i8* %62, i32* null)
br label %68
64: ; preds = %35
%65 = load i32, i32* %3
%66 = add nsw i32 %65, 1
%67 = load i8**, i8*** %4
br label %68
68: ; preds = %64, %52
br label %69
69: ; preds = %68, %12
ret void
}

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