#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*/ close(int);
int dup(int);
int /*<<< orphan*/ execl(char *, char *, char *, ...);
scalar_t__ fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *);
int main() {
  int fdes[2];
  if (pipe(fdes) == 0) {
    pid_t pid = fork();
    if (pid == 0) {
      close(1);
      fdes[1] = dup(fdes[1]);
      close(fdes[0]);
      execl("/bin/ls", "/bin/ls", "-l", NULL);
    } else {
      close(0);
      fdes[0] = dup(fdes[0]);
      close(fdes[1]);
      execl("/bin/sort", "/bin/sort", NULL);
    }
  } else
    printf("Pipe error\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jne    120a <main+0xaa>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d6 <main+0x76>
mov    $0x1,%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
lea    0xe44(%rip),%rsi
lea    0xe45(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execl@plt>
jmp    1205 <main+0xa5>
xor    %edi,%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
lea    0xe14(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execl@plt>
jmp    1216 <main+0xb6>
lea    0xe04(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    %rsp,%rdi
call   1060 <pipe@plt>
test   %rax,%rax
je     117c <main+0x1c>
lea    0xea0(%rip),%rdi
call   1030 <puts@plt>
jmp    11f1 <main+0x91>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11b8 <main+0x58>
xor    %edi,%edi
call   1050 <close@plt>
mov    (%rsp),%edi
call   1040 <dup@plt>
mov    %eax,(%rsp)
mov    0x4(%rsp),%edi
call   1050 <close@plt>
lea    0xe61(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1070 <execl@plt>
jmp    11f1 <main+0x91>
mov    $0x1,%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
call   1040 <dup@plt>
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
call   1050 <close@plt>
lea    0xe22(%rip),%rdi
lea    0xe23(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1070 <execl@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [8 x i8] c"/bin/ls\00"
@.str.1 = [3 x i8] c"-l\00"
@.str.2 = [10 x i8] c"/bin/sort\00"
@.str.3 = [12 x i8] c"Pipe error\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i64
store i32 0, i32* %1
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%5 = call i64 @pipe(i32* %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %32
7: ; preds = %0
%8 = call i64 (...) @fork()
store i64 %8, i64* %3
%9 = load i64, i64* %3
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %7
%12 = call i32 @close(i32 1)
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%14 = load i32, i32* %13
%15 = call i32 @dup(i32 %14)
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
store i32 %15, i32* %16
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%18 = load i32, i32* %17
%19 = call i32 @close(i32 %18)
%20 = call i32 (i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* null)
br label %31
21: ; preds = %7
%22 = call i32 @close(i32 0)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%24 = load i32, i32* %23
%25 = call i32 @dup(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
store i32 %25, i32* %26
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = call i32 (i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* null)
br label %31
31: ; preds = %21, %11
br label %34
32: ; preds = %0
%33 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %32, %31
%35 = load i32, i32* %1
ret i32 %35
}

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