#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 dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, const char *argv[]) {
  int fd[2];
  pipe(fd);
  int pid = fork();
  if (pid == 0) {
    close(fd[1]);
    int ret = dup2(fd[0], 0);
    if (ret < 0)
      perror("dup2");
    char *argv[10];
    argv[0] = "more";
    argv[1] = NULL;
    execvp("more", argv);
  }
  int pid2 = fork();
  if (pid2 == 0) {
    int ret = dup2(fd[1], 1);
    if (ret < 0)
      perror("dup2");
    char *argv[10];
    argv[0] = "ls";
    argv[1] = "-l";
    argv[2] = NULL;
    execvp("ls", argv);
  }
  close(fd[0]);
  close(fd[1]);
  int status;
  waitpid(pid, &status, 0);
  printf("Done!\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1204 <main+0x84>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11e1 <main+0x61>
lea    0xe24(%rip),%rdi
call   1080 <perror@plt>
lea    0xe1d(%rip),%rax
mov    %rax,-0x70(%rbp)
movq   $0x0,-0x68(%rbp)
lea    -0x70(%rbp),%rsi
lea    0xe06(%rip),%rdi
call   1090 <execvp@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jne    1278 <main+0xf8>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %eax,-0x78(%rbp)
cmpl   $0x0,-0x78(%rbp)
jge    123e <main+0xbe>
lea    0xdc7(%rip),%rdi
call   1080 <perror@plt>
lea    0xdc5(%rip),%rax
mov    %rax,-0xd0(%rbp)
lea    0xdba(%rip),%rax
mov    %rax,-0xc8(%rbp)
movq   $0x0,-0xc0(%rbp)
lea    -0xd0(%rbp),%rsi
lea    0xd97(%rip),%rdi
call   1090 <execvp@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
lea    -0xd4(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
lea    0xd70(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x60,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    11d7 <main+0x57>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
test   %eax,%eax
js     1253 <main+0xd3>
lea    0xe46(%rip),%rdi
mov    %rdi,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x10(%rsp),%rsi
call   1090 <execvp@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    121f <main+0x9f>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
test   %eax,%eax
js     1264 <main+0xe4>
lea    0xe0f(%rip),%rdi
mov    %rdi,0x10(%rsp)
lea    0xe06(%rip),%rax
mov    %rax,0x18(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x10(%rsp),%rsi
call   1090 <execvp@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
lea    0xdca(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbx
ret
lea    0xda6(%rip),%rdi
call   1080 <perror@plt>
jmp    11b8 <main+0x38>
lea    0xd95(%rip),%rdi
call   1080 <perror@plt>
jmp    11f4 <main+0x74>

LLVM IR (-O0):
@.str = [5 x i8] c"dup2\00"
@.str.1 = [5 x i8] c"more\00"
@.str.2 = [3 x i8] c"ls\00"
@.str.3 = [3 x i8] c"-l\00"
@.str.4 = [7 x i8] c"Done!\0A\00"
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 [10 x i8*]
%10 = alloca i32
%11 = alloca i32
%12 = alloca [10 x i8*]
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%15 = call i32 @pipe(i32* %14)
%16 = call i32 (...) @fork()
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %35
19: ; preds = %2
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%24 = load i32, i32* %23
%25 = call i32 @dup2(i32 %24, i32 0)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %19
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %28, %19
%31 = getelementptr inbounds [10 x i8*], [10 x i8*]* %9, i64 0, i64 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %31
%32 = getelementptr inbounds [10 x i8*], [10 x i8*]* %9, i64 0, i64 1
store i8* null, i8** %32
%33 = getelementptr inbounds [10 x i8*], [10 x i8*]* %9, i64 0, i64 0
%34 = call i32 @execvp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %33)
br label %35
35: ; preds = %30, %2
%36 = call i32 (...) @fork()
store i32 %36, i32* %10
%37 = load i32, i32* %10
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %53
39: ; preds = %35
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @dup2(i32 %41, i32 1)
store i32 %42, i32* %11
%43 = load i32, i32* %11
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %47
47: ; preds = %45, %39
%48 = getelementptr inbounds [10 x i8*], [10 x i8*]* %12, i64 0, i64 0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %48
%49 = getelementptr inbounds [10 x i8*], [10 x i8*]* %12, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %49
%50 = getelementptr inbounds [10 x i8*], [10 x i8*]* %12, i64 0, i64 2
store i8* null, i8** %50
%51 = getelementptr inbounds [10 x i8*], [10 x i8*]* %12, i64 0, i64 0
%52 = call i32 @execvp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %51)
br label %53
53: ; preds = %47, %35
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%55 = load i32, i32* %54
%56 = call i32 @close(i32 %55)
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%58 = load i32, i32* %57
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %7
%61 = call i32 @waitpid(i32 %60, i32* %13, i32 0)
%62 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"dup2\00"
@.str.1 = [5 x i8] c"more\00"
@.str.2 = [3 x i8] c"ls\00"
@.str.3 = [3 x i8] c"-l\00"
@str = [6 x i8] c"Done!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [10 x i8*]
%5 = alloca [10 x i8*]
%6 = alloca i32
%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 = call i32 (...) @fork()
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %26
12: ; preds = %2
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%14 = load i32, i32* %13
%15 = call i32 @close(i32 %14)
%16 = load i32, i32* %8
%17 = call i32 @dup2(i32 %16, i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %12
%22 = bitcast [10 x i8*]* %4 to i8*
%23 = getelementptr inbounds [10 x i8*], [10 x i8*]* %4, i64 0, i64 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %23
%24 = getelementptr inbounds [10 x i8*], [10 x i8*]* %4, i64 0, i64 1
store i8* null, i8** %24
%25 = call i32 @execvp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %23)
br label %26
26: ; preds = %21, %2
%27 = call i32 (...) @fork()
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %42
29: ; preds = %26
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @dup2(i32 %31, i32 1)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
%37 = bitcast [10 x i8*]* %5 to i8*
%38 = getelementptr inbounds [10 x i8*], [10 x i8*]* %5, i64 0, i64 0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %38
%39 = getelementptr inbounds [10 x i8*], [10 x i8*]* %5, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %39
%40 = getelementptr inbounds [10 x i8*], [10 x i8*]* %5, i64 0, i64 2
store i8* null, i8** %40
%41 = call i32 @execvp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %38)
br label %42
42: ; preds = %36, %26
%43 = load i32, i32* %8
%44 = call i32 @close(i32 %43)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = bitcast i32* %6 to i8*
%49 = call i32 @waitpid(i32 %10, i32* %6, i32 0)
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
ret i32 0
}