#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*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc != 4) {
    fprintf(stderr, "usage: pipe cmd1 cmd2 cmd3\n");
    exit(EXIT_FAILURE);
  }
  int pipefd[2];
  pipe(pipefd);
  int pid = fork();
  if (pid == 0) {
    close(pipefd[0]);
    dup2(pipefd[1], 1);
    close(pipefd[1]);
    execlp(argv[1], argv[1], NULL);
  } else {
    close(pipefd[1]);
    dup2(pipefd[0], 0);
    close(pipefd[0]);
    pipe(pipefd);
    pid = fork();
    if (pid == 0) {
      close(pipefd[0]);
      dup2(pipefd[1], 1);
      close(pipefd[1]);
      execlp(argv[2], argv[2], NULL);
    } else {
      close(pipefd[1]);
      dup2(pipefd[0], 0);
      close(pipefd[0]);
      execlp(argv[3], argv[3], NULL);
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11b3 <main+0x43>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    120b <main+0x9b>
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    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <execlp@plt>
jmp    12b5 <main+0x145>
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>
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    127d <main+0x10d>
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    0x10(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <execlp@plt>
jmp    12b0 <main+0x140>
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    0x18(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <execlp@plt>
jmp    12b5 <main+0x145>
mov    0x2d14(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x4,%edi
jne    124e <main+0xde>
mov    %rsi,%rbx
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11eb <main+0x7b>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     1211 <main+0xa1>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0x18(%rbx),%rdi
jmp    1235 <main+0xc5>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rbx),%rdi
jmp    1235 <main+0xc5>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x10(%rbx),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1080 <execlp@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xda2(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"usage: pipe cmd1 cmd2 cmd3\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 4
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%17 = call i32 @pipe(i32* %16)
%18 = call i32 (...) @fork()
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %38
21: ; preds = %15
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @dup2(i32 %26, i32 1)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 @execlp(i8* %33, i8* %36, i32* null)
br label %88
38: ; preds = %15
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%43 = load i32, i32* %42
%44 = call i32 @dup2(i32 %43, i32 0)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%49 = call i32 @pipe(i32* %48)
%50 = call i32 (...) @fork()
store i32 %50, i32* %7
%51 = load i32, i32* %7
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %70
53: ; preds = %38
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%55 = load i32, i32* %54
%56 = call i32 @close(i32 %55)
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%58 = load i32, i32* %57
%59 = call i32 @dup2(i32 %58, i32 1)
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%61 = load i32, i32* %60
%62 = call i32 @close(i32 %61)
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i64 2
%65 = load i8*, i8** %64
%66 = load i8**, i8*** %5
%67 = getelementptr inbounds i8*, i8** %66, i64 2
%68 = load i8*, i8** %67
%69 = call i32 @execlp(i8* %65, i8* %68, i32* null)
br label %87
70: ; preds = %38
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%72 = load i32, i32* %71
%73 = call i32 @close(i32 %72)
%74 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%75 = load i32, i32* %74
%76 = call i32 @dup2(i32 %75, i32 0)
%77 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%78 = load i32, i32* %77
%79 = call i32 @close(i32 %78)
%80 = load i8**, i8*** %5
%81 = getelementptr inbounds i8*, i8** %80, i64 3
%82 = load i8*, i8** %81
%83 = load i8**, i8*** %5
%84 = getelementptr inbounds i8*, i8** %83, i64 3
%85 = load i8*, i8** %84
%86 = call i32 @execlp(i8* %82, i8* %85, i32* null)
br label %87
87: ; preds = %70, %53
br label %88
88: ; preds = %87, %21
%89 = load i32, i32* @EXIT_SUCCESS
ret i32 %89
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"usage: pipe cmd1 cmd2 cmd3\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = icmp eq i32 %0, 4
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = bitcast [2 x i32]* %3 to i8*
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = call i32 (...) @fork()
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %10
%17 = load i32, i32* %12
%18 = call i32 @close(i32 %17)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @dup2(i32 %20, i32 1)
%22 = load i32, i32* %19
%23 = call i32 @close(i32 %22)
%24 = getelementptr inbounds i8*, i8** %1, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @execlp(i8* %25, i8* %25, i32* null)
br label %58
27: ; preds = %10
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %12
%32 = call i32 @dup2(i32 %31, i32 0)
%33 = load i32, i32* %12
%34 = call i32 @close(i32 %33)
%35 = call i32 @pipe(i32* %12)
%36 = call i32 (...) @fork()
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %48
38: ; preds = %27
%39 = load i32, i32* %12
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %28
%42 = call i32 @dup2(i32 %41, i32 1)
%43 = load i32, i32* %28
%44 = call i32 @close(i32 %43)
%45 = getelementptr inbounds i8*, i8** %1, i64 2
%46 = load i8*, i8** %45
%47 = call i32 @execlp(i8* %46, i8* %46, i32* null)
br label %58
48: ; preds = %27
%49 = load i32, i32* %28
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %12
%52 = call i32 @dup2(i32 %51, i32 0)
%53 = load i32, i32* %12
%54 = call i32 @close(i32 %53)
%55 = getelementptr inbounds i8*, i8** %1, i64 3
%56 = load i8*, i8** %55
%57 = call i32 @execlp(i8* %56, i8* %56, i32* null)
br label %58
58: ; preds = %38, %48, %16
%59 = load i32, i32* @EXIT_SUCCESS
ret i32 %59
}