#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 scalar_t__ pid_t;

/* Variables and functions */
int EXIT_SUCCESS;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, char *, char *, ...);
scalar_t__ fork();
int open(char *, int, int);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  pid_t pid;
  int fd[2], fdf;
  pid = fork();
  if (pid == 0) {
    pipe(fd);
    pid = fork();
    if (pid == 0) {
      close(fd[0]);
      dup2(fd[1], STDOUT_FILENO);
      close(fd[1]);
      execlp("ls", "ls", "-l", "/etc", NULL);
    }
    close(fd[1]);
    dup2(fd[0], STDIN_FILENO);
    close(fd[0]);
    fdf = open("x", O_WRONLY | O_CREAT, 0600);
    dup2(fdf, STDERR_FILENO);
    close(fdf);
    fdf = open("y", O_WRONLY | O_CREAT, 0600);
    dup2(fdf, STDOUT_FILENO);
    close(fdf);
    execlp("wc", "wc", "-l", NULL);
  }
  wait(NULL);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12bb <main+0x14b>
lea    -0x20(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1200 <main+0x90>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
lea    0xe1d(%rip),%rsi
lea    0xe19(%rip),%rdx
lea    0xe15(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execlp@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    0x2dc7(%rip),%rax
or     (%rax),%esi
lea    0xdd1(%rip),%rdi
mov    $0x180,%edx
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
mov    0x2d88(%rip),%rax
or     (%rax),%esi
lea    0xd94(%rip),%rdi
mov    $0x180,%edx
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
lea    0xd69(%rip),%rsi
lea    0xd56(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execlp@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <wait@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    1291 <main+0x121>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    11e5 <main+0x75>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
lea    0xe36(%rip),%rdi
lea    0xe32(%rip),%rdx
lea    0xe2e(%rip),%rcx
mov    %rdi,%rsi
xor    %r8d,%r8d
xor    %eax,%eax
call   1070 <execlp@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0x2da0(%rip),%r14
mov    0x2de1(%rip),%r15
mov    (%r15),%esi
or     (%r14),%esi
lea    0xde7(%rip),%rdi
mov    $0x180,%edx
call   1060 <open@plt>
mov    %eax,%ebx
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    (%r15),%esi
or     (%r14),%esi
lea    0xdb9(%rip),%rdi
mov    $0x180,%edx
call   1060 <open@plt>
mov    %eax,%ebx
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    %ebx,%edi
call   1040 <close@plt>
lea    0xd91(%rip),%rdi
lea    0xd7e(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1070 <execlp@plt>
xor    %edi,%edi
call   1080 <wait@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@.str = [3 x i8] c"ls\00"
@.str.1 = [3 x i8] c"-l\00"
@.str.2 = [5 x i8] c"/etc\00"
@STDIN_FILENO = global i32 0
@.str.3 = [2 x i8] c"x\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@STDERR_FILENO = global i32 0
@.str.4 = [2 x i8] c"y\00"
@.str.5 = [3 x i8] c"wc\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca [2 x i32]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 (...) @fork()
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %60
12: ; preds = %2
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%14 = call i32 @pipe(i32* %13)
%15 = call i64 (...) @fork()
store i64 %15, i64* %6
%16 = load i64, i64* %6
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %12
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%23 = load i32, i32* %22
%24 = load i32, i32* @STDOUT_FILENO
%25 = call i32 @dup2(i32 %23, i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = call i32 (i8*, i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* null)
br label %30
30: ; preds = %18, %12
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%35 = load i32, i32* %34
%36 = load i32, i32* @STDIN_FILENO
%37 = call i32 @dup2(i32 %35, i32 %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* @O_WRONLY
%42 = load i32, i32* @O_CREAT
%43 = or i32 %41, %42
%44 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %43, i32 384)
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = load i32, i32* @STDERR_FILENO
%47 = call i32 @dup2(i32 %45, i32 %46)
%48 = load i32, i32* %8
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* @O_WRONLY
%51 = load i32, i32* @O_CREAT
%52 = or i32 %50, %51
%53 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %52, i32 384)
store i32 %53, i32* %8
%54 = load i32, i32* %8
%55 = load i32, i32* @STDOUT_FILENO
%56 = call i32 @dup2(i32 %54, i32 %55)
%57 = load i32, i32* %8
%58 = call i32 @close(i32 %57)
%59 = call i32 (i8*, i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* null)
br label %60
60: ; preds = %30, %2
%61 = call i32 @wait(i32* null)
%62 = load i32, i32* @EXIT_SUCCESS
ret i32 %62
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [3 x i8] c"ls\00"
@.str.1 = [3 x i8] c"-l\00"
@.str.2 = [5 x i8] c"/etc\00"
@STDIN_FILENO = global i32 0
@.str.3 = [2 x i8] c"x\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@STDERR_FILENO = global i32 0
@.str.4 = [2 x i8] c"y\00"
@.str.5 = [3 x i8] c"wc\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %46
7: ; preds = %2
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = call i64 (...) @fork()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %7
%13 = load i32, i32* %8
%14 = call i32 @close(i32 %13)
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%16 = load i32, i32* %15
%17 = load i32, i32* @STDOUT_FILENO
%18 = call i32 @dup2(i32 %16, i32 %17)
%19 = load i32, i32* %15
%20 = call i32 @close(i32 %19)
%21 = call i32 (i8*, i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* null)
br label %22
22: ; preds = %12, %7
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %8
%27 = load i32, i32* @STDIN_FILENO
%28 = call i32 @dup2(i32 %26, i32 %27)
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* @O_WRONLY
%32 = load i32, i32* @O_CREAT
%33 = or i32 %32, %31
%34 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %33, i32 384)
%35 = load i32, i32* @STDERR_FILENO
%36 = call i32 @dup2(i32 %34, i32 %35)
%37 = call i32 @close(i32 %34)
%38 = load i32, i32* @O_WRONLY
%39 = load i32, i32* @O_CREAT
%40 = or i32 %39, %38
%41 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %40, i32 384)
%42 = load i32, i32* @STDOUT_FILENO
%43 = call i32 @dup2(i32 %41, i32 %42)
%44 = call i32 @close(i32 %41)
%45 = call i32 (i8*, i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* null)
br label %46
46: ; preds = %22, %2
%47 = call i32 @wait(i32* null)
%48 = load i32, i32* @EXIT_SUCCESS
ret i32 %48
}