#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int fd[2];
  pid_t child_1, child_2;
  char *ls_args[] = {"ls", "-t", 0};
  char *grep_args[] = {"grep", argv[1], 0};
  if (pipe(fd) == -1) {
    perror("Pipe failed");
    exit(EXIT_FAILURE);
  }
  if ((child_1 = fork()) == 0) {
    close(STDOUT_FILENO);
    dup(fd[1]);
    close(fd[0]);
    close(fd[1]);
    execvp(ls_args[0], ls_args);
    perror("execvp of ls failed");
    exit(EXIT_FAILURE);
  }
  if ((child_2 = fork()) == 0) {
    close(STDIN_FILENO);
    dup(fd[0]);
    close(fd[1]);
    close(fd[0]);
    execvp(grep_args[0], grep_args);
    perror("execvp of wc failed");
    exit(EXIT_FAILURE);
  }
  close(fd[0]);
  close(fd[1]);
  wait(NULL);
  wait(NULL);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2c43(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2c40(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0x2c3d(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe48(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x58(%rbp)
movq   $0x0,-0x50(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1202 <main+0x82>
lea    0xe1c(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    1262 <main+0xe2>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1030 <dup@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x40(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1070 <execvp@plt>
lea    0xdc8(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jne    12c2 <main+0x142>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1030 <dup@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x60(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1070 <execvp@plt>
lea    0xd7c(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
mov    0x2cdd(%rip),%rax
mov    (%rax),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x48,%rsp
mov    0x2c65(%rip),%rax
mov    %rax,0x40(%rsp)
movaps 0x2c49(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
lea    0xe63(%rip),%rax
mov    %rax,0x10(%rsp)
mov    0x8(%rsi),%rax
mov    %rax,0x18(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     120d <main+0x8d>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     1219 <main+0x99>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     125c <main+0xdc>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %edi,%edi
call   1090 <wait@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
add    $0x48,%rsp
ret
lea    0xdf7(%rip),%rdi
jmp    129d <main+0x11d>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1030 <dup@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
lea    0xdb7(%rip),%rdi
lea    0x30(%rsp),%rsi
call   1070 <execvp@plt>
lea    0xdbd(%rip),%rdi
jmp    129d <main+0x11d>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1030 <dup@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
lea    0xd7a(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1070 <execvp@plt>
lea    0xd8e(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str = [3 x i8] c"ls\00"
@.str.1 = [3 x i8] c"-t\00"
@__const.main.ls_args = [3 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.2 = [5 x i8] c"grep\00"
@.str.3 = [12 x i8] c"Pipe failed\00"
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
@.str.4 = [20 x i8] c"execvp of ls failed\00"
@STDIN_FILENO = global i32 0
@.str.5 = [20 x i8] c"execvp of wc failed\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = alloca [3 x i8*]
%5 = alloca [3 x i8*]
%6 = bitcast [2 x i32]* %3 to i8*
%7 = bitcast [3 x i8*]* %4 to i8*
%8 = bitcast [3 x i8*]* %5 to i8*
%9 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 1
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
store i8* %12, i8** %10
%13 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 2
store i8* null, i8** %13
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%15 = call i32 @pipe(i32* %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = call i32 (...) @fork()
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %39
24: ; preds = %21
%25 = load i32, i32* @STDOUT_FILENO
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @dup(i32 %28)
%30 = load i32, i32* %14
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %27
%33 = call i32 @close(i32 %32)
%34 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 0
%35 = call i32 @execvp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %34)
%36 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %21
%40 = call i32 (...) @fork()
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %56
42: ; preds = %39
%43 = load i32, i32* @STDIN_FILENO
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %14
%46 = call i32 @dup(i32 %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%48 = load i32, i32* %47
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* %14
%51 = call i32 @close(i32 %50)
%52 = call i32 @execvp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %9)
%53 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %39
%57 = load i32, i32* %14
%58 = call i32 @close(i32 %57)
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%60 = load i32, i32* %59
%61 = call i32 @close(i32 %60)
%62 = call i32 @wait(i32* null)
%63 = call i32 @wait(i32* null)
%64 = load i32, i32* @EXIT_SUCCESS
ret i32 %64
}