#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*/ dup(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 des_p[2];
  if (pipe(des_p) == -1) {
    perror("Pipe failed");
    exit(1);
  }
  if (fork() == 0) {
    close(1);
    dup(des_p[1]);
    close(des_p[0]);
    close(des_p[1]);
    const char *prog1[] = {"ls", "-l", 0};
    execvp(prog1[0], prog1);
    perror("execvp of ls failed");
    exit(1);
  }
  if (fork() == 0) {
    close(0);
    dup(des_p[0]);
    close(des_p[1]);
    close(des_p[0]);
    const char *prog2[] = {"wc", "-l", 0};
    execvp(prog2[0], prog2);
    perror("execvp of wc failed");
    exit(1);
  }
  close(des_p[0]);
  close(des_p[1]);
  wait(0);
  wait(0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11be <main+0x3e>
lea    0xe51(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    1235 <main+0xb5>
mov    $0x1,%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    0x2be8(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2be5(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2be2(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <execvp@plt>
lea    0xdec(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    12a9 <main+0x129>
xor    %edi,%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    0x2b94(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0x2b91(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0x2b8e(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x50(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1070 <execvp@plt>
lea    0xd8f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x28,%rsp
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     11d6 <main+0x56>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     11e2 <main+0x62>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1239 <main+0xb9>
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>
xor    %eax,%eax
add    $0x28,%rsp
ret
lea    0xe23(%rip),%rdi
jmp    128b <main+0x10b>
mov    $0x1,%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>
mov    0x2be2(%rip),%rax
mov    %rax,0x20(%rsp)
movaps 0x2bc6(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xde6(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1070 <execvp@plt>
lea    0xddb(%rip),%rdi
jmp    128b <main+0x10b>
xor    %edi,%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>
mov    0x2bae(%rip),%rax
mov    %rax,0x20(%rsp)
movaps 0x2b92(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xdac(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1070 <execvp@plt>
lea    0xd9e(%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 = [3 x i8] c"wc\00"
@__const.main.prog2 = [3 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* null]
@.str.5 = [20 x i8] c"execvp of wc failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca [3 x i8*]
%8 = alloca [3 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = call i64 (...) @fork()
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %36
18: ; preds = %15
%19 = call i32 @close(i32 1)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @dup(i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = bitcast [3 x i8*]* %7 to i8*
%30 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 0
%31 = load i8*, i8** %30
%32 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 0
%33 = call i32 @execvp(i8* %31, i8** %32)
%34 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %15
%37 = call i64 (...) @fork()
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %57
39: ; preds = %36
%40 = call i32 @close(i32 0)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%42 = load i32, i32* %41
%43 = call i32 @dup(i32 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%48 = load i32, i32* %47
%49 = call i32 @close(i32 %48)
%50 = bitcast [3 x i8*]* %8 to i8*
%51 = getelementptr inbounds [3 x i8*], [3 x i8*]* %8, i64 0, i64 0
%52 = load i8*, i8** %51
%53 = getelementptr inbounds [3 x i8*], [3 x i8*]* %8, i64 0, i64 0
%54 = call i32 @execvp(i8* %52, i8** %53)
%55 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %36
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%62 = load i32, i32* %61
%63 = call i32 @close(i32 %62)
%64 = call i32 @wait(i32 0)
%65 = 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 = [3 x i8] c"wc\00"
@__const.main.prog2 = [3 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* null]
@.str.5 = [20 x i8] c"execvp of wc failed\00"
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 = bitcast [2 x i32]* %3 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %13
%17 = call i32 @close(i32 1)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @dup(i32 %19)
%21 = load i32, i32* %7
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %18
%24 = call i32 @close(i32 %23)
%25 = bitcast [3 x i8*]* %4 to i8*
%26 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 0
%27 = call i32 @execvp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %26)
%28 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %13
%31 = call i64 (...) @fork()
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %47
33: ; preds = %30
%34 = call i32 @close(i32 0)
%35 = load i32, i32* %7
%36 = call i32 @dup(i32 %35)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %7
%41 = call i32 @close(i32 %40)
%42 = bitcast [3 x i8*]* %5 to i8*
%43 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 0
%44 = call i32 @execvp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %43)
%45 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %30
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
%53 = call i32 @wait(i32 0)
%54 = call i32 @wait(i32 0)
ret i32 0
}