#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*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
scalar_t__ fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *);
int main(int argc, char **argv) {
  int fd[2];
  if (pipe(fd) < 0) {
    printf("pipe failed\n");
    return -1;
  }
  if (fork() == 0) {
    dup2(fd[1], STDOUT_FILENO);
    close(fd[0]);
    close(fd[1]);
    execlp("ls", "ls", NULL);
  }
  dup2(fd[0], STDIN_FILENO);
  close(fd[0]);
  close(fd[1]);
  execlp("wc", "wc", NULL);
  return 0;
}


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)
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    11a1 <main+0x41>
lea    0xe70(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1221 <main+0xc1>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jne    11e6 <main+0x86>
mov    -0x14(%rbp),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
lea    0xe33(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1070 <execlp@plt>
mov    -0x18(%rbp),%edi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
lea    0xe02(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1070 <execlp@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     11ee <main+0x8e>
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    11b7 <main+0x57>
mov    0xc(%rsp),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0xe53(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1070 <execlp@plt>
mov    0x8(%rsp),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0xe21(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1070 <execlp@plt>
jmp    11ff <main+0x9f>
lea    0xe11(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"pipe failed\0A\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [3 x i8] c"ls\00"
@STDIN_FILENO = global i32 0
@.str.2 = [3 x i8] c"wc\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %39
12: ; preds = %2
%13 = call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %27
15: ; preds = %12
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%17 = load i32, i32* %16
%18 = load i32, i32* @STDOUT_FILENO
%19 = call i32 @dup2(i32 %17, i32 %18)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* null)
br label %27
27: ; preds = %15, %12
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%29 = load i32, i32* %28
%30 = load i32, i32* @STDIN_FILENO
%31 = call i32 @dup2(i32 %29, i32 %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* null)
store i32 0, i32* %3
br label %39
39: ; preds = %27, %10
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str.1 = [3 x i8] c"ls\00"
@STDIN_FILENO = global i32 0
@.str.2 = [3 x i8] c"wc\00"
@str = [12 x i8] c"pipe failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %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 i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %33
10: ; preds = %2
%11 = call i64 (...) @fork()
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %10
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%15 = load i32, i32* %14
%16 = load i32, i32* @STDOUT_FILENO
%17 = call i32 @dup2(i32 %15, i32 %16)
%18 = load i32, i32* %5
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* %14
%21 = call i32 @close(i32 %20)
%22 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* null)
br label %23
23: ; preds = %13, %10
%24 = load i32, i32* %5
%25 = load i32, i32* @STDIN_FILENO
%26 = call i32 @dup2(i32 %24, i32 %25)
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* null)
br label %33
33: ; preds = %23, %8
%34 = phi i32 [ -1, %8 ], [ 0, %23 ]
ret i32 %34
}