#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*/ EXIT_FAILURE;
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 *, ...);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
void *fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(void) {
  int fd[2];
  pipe(fd);
  int child1 = fork();
  if (child1 == 0) {
    close(fd[0]);
    dup2(fd[1], STDOUT_FILENO);
    execlp("ls", "ls", "-al", NULL);
    printf("execlp() failed.\n");
    exit(EXIT_FAILURE);
  }
  pid_t child2 = fork();
  if (child2 == 0) {
    close(fd[1]);
    dup2(fd[0], STDIN_FILENO);
    execlp("tr", "tr", "s", "X", NULL);
    printf("execlp() failed.\n");
    exit(EXIT_FAILURE);
  }
  close(fd[0]);
  close(fd[1]);
  int status;
  waitpid(child2, &status, 0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x8(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11f4 <main+0x74>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
lea    0xe3b(%rip),%rsi
lea    0xe37(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1090 <execlp@plt>
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1261 <main+0xe1>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
lea    0xdef(%rip),%rsi
lea    0xdd0(%rip),%rdx
lea    0xde4(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
mov    $0x0,%al
call   1090 <execlp@plt>
lea    0xdb9(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0x4(%rsp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11d2 <main+0x52>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1209 <main+0x89>
mov    %rax,%rbx
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1070 <waitpid@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
lea    0xe0c(%rip),%rdi
lea    0xe08(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1090 <execlp@plt>
jmp    1246 <main+0xc6>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
lea    0xddc(%rip),%rdi
lea    0xdcf(%rip),%rdx
lea    0xdd1(%rip),%rcx
mov    %rdi,%rsi
xor    %r8d,%r8d
xor    %eax,%eax
call   1090 <execlp@plt>
lea    0xdbf(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@.str = [3 x i8] c"ls\00"
@.str.1 = [4 x i8] c"-al\00"
@.str.2 = [18 x i8] c"execlp() failed.\0A\00"
@EXIT_FAILURE = global i32 0
@STDIN_FILENO = global i32 0
@.str.3 = [3 x i8] c"tr\00"
@.str.4 = [2 x i8] c"s\00"
@.str.5 = [2 x i8] c"X\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = call i8* (...) @fork()
%8 = ptrtoint i8* %7 to i32
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %23
11: ; preds = %0
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%13 = load i32, i32* %12
%14 = call i32 @close(i32 %13)
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%16 = load i32, i32* %15
%17 = load i32, i32* @STDOUT_FILENO
%18 = call i32 @dup2(i32 %16, i32 %17)
%19 = 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 ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* null)
%20 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %0
%24 = call i8* (...) @fork()
%25 = ptrtoint i8* %24 to i64
store i64 %25, i64* %3
%26 = load i64, i64* %3
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %40
28: ; preds = %23
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%33 = load i32, i32* %32
%34 = load i32, i32* @STDIN_FILENO
%35 = call i32 @dup2(i32 %33, i32 %34)
%36 = call i32 (i8*, i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i8* null)
%37 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %23
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = load i64, i64* %3
%48 = call i32 @waitpid(i64 %47, i32* %4, i32 0)
ret i32 0
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [3 x i8] c"ls\00"
@.str.1 = [4 x i8] c"-al\00"
@EXIT_FAILURE = global i32 0
@STDIN_FILENO = global i32 0
@.str.3 = [3 x i8] c"tr\00"
@.str.4 = [2 x i8] c"s\00"
@.str.5 = [2 x i8] c"X\00"
@str.6 = [17 x i8] c"execlp() failed.\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i32
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = call i8* (...) @fork()
%7 = ptrtoint i8* %6 to i64
%8 = trunc i64 %7 to i32
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %21
10: ; preds = %0
%11 = load i32, i32* %4
%12 = call i32 @close(i32 %11)
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%14 = load i32, i32* %13
%15 = load i32, i32* @STDOUT_FILENO
%16 = call i32 @dup2(i32 %14, i32 %15)
%17 = 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 ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* null)
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.6, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %0
%22 = call i8* (...) @fork()
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %35
24: ; preds = %21
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %4
%29 = load i32, i32* @STDIN_FILENO
%30 = call i32 @dup2(i32 %28, i32 %29)
%31 = call i32 (i8*, i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i8* null)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.6, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %21
%36 = ptrtoint i8* %22 to i64
%37 = load i32, i32* %4
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = bitcast i32* %2 to i8*
%43 = call i32 @waitpid(i64 %36, i32* %2, i32 0)
ret i32 0
}