#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 /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, char *, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  pid_t pid;
  int fd[2], w;
  pipe(fd);
  pid = fork();
  if (pid == 0) {
    printf("i'm the child used for ls \n");
    dup2(fd[1], STDOUT_FILENO);
    close(fd[0]);
    execlp("ls", "ls", "-la", NULL);
  }
  pid = fork();
  if (pid == 0) {
    printf("i'm in the second child, which will be used to run grep\n");
    dup2(fd[0], STDIN_FILENO);
    close(fd[1]);
    execlp("grep", "grep", ".c", NULL);
  }
  wait(&w);
  return 0;
}


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)
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e4 <main+0x74>
lea    0xe54(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
lea    0xe4b(%rip),%rsi
lea    0xe47(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   1070 <execlp@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1239 <main+0xc9>
lea    0xe22(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
lea    0xe36(%rip),%rsi
lea    0xe34(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   1070 <execlp@plt>
lea    -0x24(%rbp),%rdi
call   1080 <wait@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0xc(%rsp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    11c9 <main+0x59>
lea    0xeb1(%rip),%rdi
call   1030 <puts@plt>
mov    0x10(%rsp),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0xe48(%rip),%rdi
lea    0xe44(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
call   1070 <execlp@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    1214 <main+0xa4>
lea    0xe2e(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
lea    0xe3a(%rip),%rdi
lea    0xdfd(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
call   1070 <execlp@plt>
lea    0x14(%rsp),%rdi
call   1080 <wait@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"i'm the child used for ls \0A\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [4 x i8] c"-la\00"
@.str.3 = [57 x i8] c"i'm in the second child, which will be used to run grep\0A\00"
@STDIN_FILENO = global i32 0
@.str.4 = [5 x i8] c"grep\00"
@.str.5 = [3 x i8] c".c\00"
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 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = call i64 (...) @fork()
store i64 %11, i64* %6
%12 = load i64, i64* %6
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %7, 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]* %7, i64 0, i64 0
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
%23 = 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), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* null)
br label %24
24: ; preds = %14, %2
%25 = call i64 (...) @fork()
store i64 %25, i64* %6
%26 = load i64, i64* %6
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %38
28: ; preds = %24
%29 = call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0))
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%31 = load i32, i32* %30
%32 = load i32, i32* @STDIN_FILENO
%33 = call i32 @dup2(i32 %31, i32 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* null)
br label %38
38: ; preds = %28, %24
%39 = call i32 @wait(i32* %8)
ret i32 0
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [4 x i8] c"-la\00"
@STDIN_FILENO = global i32 0
@.str.4 = [5 x i8] c"grep\00"
@.str.5 = [3 x i8] c".c\00"
@str = [56 x i8] c"i'm in the second child, which will be used to run grep\00"
@str.6 = [27 x i8] c"i'm the child used for ls \00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = bitcast [2 x i32]* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = call i64 (...) @fork()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %2
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.6, i64 0, i64 0))
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%14 = load i32, i32* %13
%15 = load i32, i32* @STDOUT_FILENO
%16 = call i32 @dup2(i32 %14, i32 %15)
%17 = load i32, i32* %7
%18 = call i32 @close(i32 %17)
%19 = 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), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* null)
br label %20
20: ; preds = %11, %2
%21 = call i64 (...) @fork()
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %20
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str, i64 0, i64 0))
%25 = load i32, i32* %7
%26 = load i32, i32* @STDIN_FILENO
%27 = call i32 @dup2(i32 %25, i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
%31 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* null)
br label %32
32: ; preds = %23, %20
%33 = call i32 @wait(i32* %4)
ret i32 0
}