#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*/ dup2(int, int);
int /*<<< orphan*/ execve(char *, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main() {
  int pid;
  int fd[2];
  pipe(fd);
  int status;
  char buff[100];
  strcpy(buff, "test pipe");
  pid = fork();
  if (pid == 0) {
    dup2(1, fd[1]);
    execve("bin/ls", NULL, NULL);
  } else {
    waitpid(pid, &status, 0);
    dup2(0, fd[0]);
    dup2(fd[1], 1);
    execve("bin/scan_pipe", NULL, NULL);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
call   1050 <pipe@plt>
lea    -0x80(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1030 <strcpy@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11c4 <main+0x64>
mov    -0xc(%rbp),%esi
mov    $0x1,%edi
call   1040 <dup2@plt>
lea    0xe57(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1060 <execve@plt>
jmp    11fc <main+0x9c>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    -0x10(%rbp),%esi
xor    %edi,%edi
call   1040 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
lea    0xe21(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1060 <execve@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x80,%rsp
lea    0x4(%rsp),%rdi
call   1050 <pipe@plt>
lea    0xe87(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1030 <strcpy@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11b9 <main+0x59>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    0x4(%rsp),%esi
xor    %edi,%edi
call   1040 <dup2@plt>
lea    0xe63(%rip),%rbx
mov    $0x1,%esi
mov    0x8(%rsp),%edi
jmp    11c9 <main+0x69>
lea    0xe4a(%rip),%rbx
mov    $0x1,%edi
mov    0x8(%rsp),%esi
call   1040 <dup2@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <execve@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"test pipe\00"
@.str.1 = [7 x i8] c"bin/ls\00"
@.str.2 = [14 x i8] c"bin/scan_pipe\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca [100 x i8]
store i32 0, i32* %1
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%9 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%10 = call i32 (...) @fork()
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %0
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%15 = load i32, i32* %14
%16 = call i32 @dup2(i32 1, i32 %15)
%17 = call i32 @execve(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32* null, i32* null)
br label %28
18: ; preds = %0
%19 = load i32, i32* %2
%20 = call i32 @waitpid(i32 %19, i32* %4, i32 0)
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%22 = load i32, i32* %21
%23 = call i32 @dup2(i32 0, i32 %22)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @dup2(i32 %25, i32 1)
%27 = call i32 @execve(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32* null, i32* null)
br label %28
28: ; preds = %18, %13
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"test pipe\00"
@.str.1 = [7 x i8] c"bin/ls\00"
@.str.2 = [14 x i8] c"bin/scan_pipe\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = bitcast i32* %2 to i8*
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%9 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%10 = call i32 (...) @fork()
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%14 = load i32, i32* %13
br label %21
15: ; preds = %0
%16 = call i32 @waitpid(i32 %10, i32* %2, i32 0)
%17 = load i32, i32* %5
%18 = call i32 @dup2(i32 0, i32 %17)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%20 = load i32, i32* %19
br label %21
21: ; preds = %15, %12
%22 = phi i32 [ 1, %15 ], [ %14, %12 ]
%23 = phi i32 [ %20, %15 ], [ 1, %12 ]
%24 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %15 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %12 ]
%25 = call i32 @dup2(i32 %23, i32 %22)
%26 = call i32 @execve(i8* %24, i32* null, i32* null)
ret i32 0
}