#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 /*<<< orphan*/ dup2(int, int);
int execlp(char *, char *, char *, ...);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int fd[2] = {};
  int ret_pipe = 0;
  ret_pipe = pipe(fd);
  if (ret_pipe < 0) {
    perror("pipe");
  }
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    perror("fork");
  }
  if (pid == 0) {
    dup2(fd[1], 1);
    int ret_son = 0;
    ret_son = execlp("ls", "ls", "--help", NULL);
    if (ret_son < 0) {
      perror("execlp");
    }
    exit(-1);
  } else {
    wait(NULL);
    close(fd[1]);
    int ret_father = 0;
    dup2(fd[0], 0);
    ret_father = execlp("more", "more", NULL);
    if (ret_father < 0) {
      perror("execlp");
    }
  }
  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    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
movl   $0x0,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11df <main+0x4f>
lea    0xe26(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1201 <main+0x71>
lea    0xe09(%rip),%rdi
call   1070 <perror@plt>
cmpq   $0x0,-0x28(%rbp)
jne    125f <main+0xcf>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
movl   $0x0,-0x2c(%rbp)
lea    0xde3(%rip),%rsi
lea    0xddf(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1090 <execlp@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1255 <main+0xc5>
lea    0xdc4(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
lea    0xd93(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
mov    $0x0,%al
call   1090 <execlp@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    12af <main+0x11f>
lea    0xd6a(%rip),%rdi
call   1070 <perror@plt>
jmp    12b4 <main+0x124>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
movq   $0x0,(%rsp)
mov    %rsp,%rdi
call   1050 <pipe@plt>
test   %eax,%eax
js     11d8 <main+0x58>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11e6 <main+0x66>
je     1202 <main+0x82>
xor    %edi,%edi
call   1090 <wait@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
lea    0xe57(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1080 <execlp@plt>
test   %eax,%eax
js     11f4 <main+0x74>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe21(%rip),%rdi
call   1060 <perror@plt>
jmp    1195 <main+0x15>
lea    0xe18(%rip),%rdi
call   1060 <perror@plt>
jmp    11a3 <main+0x23>
lea    0xe19(%rip),%rdi
call   1060 <perror@plt>
jmp    11d4 <main+0x54>
mov    0x4(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
lea    0xdf3(%rip),%rdi
lea    0xdef(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1080 <execlp@plt>
test   %eax,%eax
jns    123a <main+0xba>
lea    0xddf(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [3 x i8] c"ls\00"
@.str.3 = [7 x i8] c"--help\00"
@.str.4 = [7 x i8] c"execlp\00"
@.str.5 = [5 x i8] c"more\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to [2 x i32]*
%5 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%6 = bitcast i64* %3 to i32*
%7 = call i32 @pipe(i32* %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %28
16: ; preds = %11
%17 = icmp eq i64 %12, 0
br i1 %17, label %18, label %28
18: ; preds = %16
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @dup2(i32 %20, i32 1)
%22 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* null)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %14, %16
%29 = call i32 @wait(i32* null)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %6
%34 = call i32 @dup2(i32 %33, i32 0)
%35 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* null)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %28
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %37, %28
ret i32 0
}