#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*/ execvp(char const *, char const **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int pipen = 0;
  int argn = 0;
  int des_p[2];
  if (pipe(des_p) == -1) {
    perror("Pipe failed");
    exit(1);
  }
  if (fork() == 0) {
    close(1);
    dup2(des_p[1], 1);
    close(des_p[0]);
    close(des_p[1]);
    const char *prog1[] = {"ls", "-l", 0};
    execvp(prog1[0], prog1);
    execvp(prog1[0], prog1);
    perror("execvp of ls failed");
    exit(1);
  }
  if (fork() == 0) {
    close(0);
    close(1);
    dup2(des_p[0], 0);
    dup2(des_p[1], 1);
    close(des_p[0]);
    close(des_p[1]);
    const char *prog2[] = {"grup", "list", 0};
    execvp(prog2[0], prog2);
    perror("execvp of wc failed");
    exit(1);
  }
  if (fork() == 0) {
    close(0);
    dup2(des_p[0], 0);
    close(des_p[0]);
    close(des_p[1]);
    const char *prog3[] = {"sort", 0};
    execvp(prog3[0], prog3);
    perror("execvp of wc failed");
    exit(1);
  }
  close(des_p[0]);
  close(des_p[1]);
  wait(0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11cc <main+0x4c>
lea    0xe43(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    1255 <main+0xd5>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    0x2bc5(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2bc2(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0x2bbf(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1070 <execvp@plt>
mov    -0x40(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1070 <execvp@plt>
lea    0xdcc(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    12e2 <main+0x162>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    0x2b4b(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    0x2b48(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    0x2b45(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    -0x60(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1070 <execvp@plt>
lea    0xd5d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    134d <main+0x1cd>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    0x2af5(%rip),%rax
mov    %rax,-0x70(%rbp)
mov    0x2af2(%rip),%rax
mov    %rax,-0x68(%rbp)
mov    -0x70(%rbp),%rdi
lea    -0x70(%rbp),%rsi
call   1070 <execvp@plt>
lea    0xcf2(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     11e1 <main+0x61>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     11ed <main+0x6d>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     125c <main+0xdc>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     12b9 <main+0x139>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
ret
lea    0xe18(%rip),%rdi
jmp    1301 <main+0x181>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x2bc2(%rip),%rax
mov    %rax,0x20(%rsp)
movaps 0x2ba6(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xdd6(%rip),%rdi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
call   1070 <execvp@plt>
mov    0x10(%rsp),%rdi
mov    %rbx,%rsi
call   1070 <execvp@plt>
lea    0xdbb(%rip),%rdi
jmp    1301 <main+0x181>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x2b61(%rip),%rax
mov    %rax,0x20(%rsp)
movaps 0x2b45(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xd6f(%rip),%rdi
jmp    12f0 <main+0x170>
xor    %edi,%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    0xc(%rsp),%edi
call   1040 <close@plt>
movaps 0x2b2c(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xd54(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1070 <execvp@plt>
lea    0xd2f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"Pipe failed\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [3 x i8] c"-l\00"
@__const.main.prog1 = [3 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* null]
@.str.3 = [20 x i8] c"execvp of ls failed\00"
@.str.4 = [5 x i8] c"grup\00"
@.str.5 = [5 x i8] c"list\00"
@__const.main.prog2 = [3 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i32 0, i32 0), i8* null]
@.str.6 = [20 x i8] c"execvp of wc failed\00"
@.str.7 = [5 x i8] c"sort\00"
@__const.main.prog3 = [2 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i32 0, i32 0), i8* null]
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [2 x i32]
%9 = alloca [3 x i8*]
%10 = alloca [3 x i8*]
%11 = alloca [2 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = call i64 (...) @fork()
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %43
21: ; preds = %18
%22 = call i32 @close(i32 1)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @dup2(i32 %24, i32 1)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = bitcast [3 x i8*]* %9 to i8*
%33 = getelementptr inbounds [3 x i8*], [3 x i8*]* %9, i64 0, i64 0
%34 = load i8*, i8** %33
%35 = getelementptr inbounds [3 x i8*], [3 x i8*]* %9, i64 0, i64 0
%36 = call i32 @execvp(i8* %34, i8** %35)
%37 = getelementptr inbounds [3 x i8*], [3 x i8*]* %9, i64 0, i64 0
%38 = load i8*, i8** %37
%39 = getelementptr inbounds [3 x i8*], [3 x i8*]* %9, i64 0, i64 0
%40 = call i32 @execvp(i8* %38, i8** %39)
%41 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %18
%44 = call i64 (...) @fork()
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %68
46: ; preds = %43
%47 = call i32 @close(i32 0)
%48 = call i32 @close(i32 1)
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%50 = load i32, i32* %49
%51 = call i32 @dup2(i32 %50, i32 0)
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%53 = load i32, i32* %52
%54 = call i32 @dup2(i32 %53, i32 1)
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
%61 = bitcast [3 x i8*]* %10 to i8*
%62 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 0
%63 = load i8*, i8** %62
%64 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 0
%65 = call i32 @execvp(i8* %63, i8** %64)
%66 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %43
%69 = call i64 (...) @fork()
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %89
71: ; preds = %68
%72 = call i32 @close(i32 0)
%73 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%74 = load i32, i32* %73
%75 = call i32 @dup2(i32 %74, i32 0)
%76 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%77 = load i32, i32* %76
%78 = call i32 @close(i32 %77)
%79 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%80 = load i32, i32* %79
%81 = call i32 @close(i32 %80)
%82 = bitcast [2 x i8*]* %11 to i8*
%83 = getelementptr inbounds [2 x i8*], [2 x i8*]* %11, i64 0, i64 0
%84 = load i8*, i8** %83
%85 = getelementptr inbounds [2 x i8*], [2 x i8*]* %11, i64 0, i64 0
%86 = call i32 @execvp(i8* %84, i8** %85)
%87 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
%88 = call i32 @exit(i32 1)
unreachable
89: ; preds = %68
%90 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%91 = load i32, i32* %90
%92 = call i32 @close(i32 %91)
%93 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%94 = load i32, i32* %93
%95 = call i32 @close(i32 %94)
%96 = call i32 @wait(i32 0)
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"Pipe failed\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [3 x i8] c"-l\00"
@__const.main.prog1 = [3 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* null]
@.str.3 = [20 x i8] c"execvp of ls failed\00"
@.str.4 = [5 x i8] c"grup\00"
@.str.5 = [5 x i8] c"list\00"
@__const.main.prog2 = [3 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i32 0, i32 0), i8* null]
@.str.6 = [20 x i8] c"execvp of wc failed\00"
@.str.7 = [5 x i8] c"sort\00"
@__const.main.prog3 = [2 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i32 0, i32 0), i8* null]
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [3 x i8*]
%5 = alloca [3 x i8*]
%6 = alloca [2 x i8*]
%7 = bitcast [2 x i32]* %3 to i8*
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %33
17: ; preds = %14
%18 = call i32 @close(i32 1)
%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* %8
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %19
%25 = call i32 @close(i32 %24)
%26 = bitcast [3 x i8*]* %4 to i8*
%27 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 0
%28 = call i32 @execvp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %27)
%29 = load i8*, i8** %27
%30 = call i32 @execvp(i8* %29, i8** %27)
%31 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %14
%34 = call i64 (...) @fork()
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %53
36: ; preds = %33
%37 = call i32 @close(i32 0)
%38 = call i32 @close(i32 1)
%39 = load i32, i32* %8
%40 = call i32 @dup2(i32 %39, i32 0)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @dup2(i32 %42, i32 1)
%44 = load i32, i32* %8
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* %41
%47 = call i32 @close(i32 %46)
%48 = bitcast [3 x i8*]* %5 to i8*
%49 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 0
%50 = call i32 @execvp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8** %49)
%51 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %33
%54 = call i64 (...) @fork()
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %70
56: ; preds = %53
%57 = call i32 @close(i32 0)
%58 = load i32, i32* %8
%59 = call i32 @dup2(i32 %58, i32 0)
%60 = load i32, i32* %8
%61 = call i32 @close(i32 %60)
%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 = bitcast [2 x i8*]* %6 to i8*
%66 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 0
%67 = call i32 @execvp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i8** %66)
%68 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %53
%71 = load i32, i32* %8
%72 = call i32 @close(i32 %71)
%73 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%74 = load i32, i32* %73
%75 = call i32 @close(i32 %74)
%76 = call i32 @wait(i32 0)
ret i32 0
}