#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
int main() {
  int fds[2];
  pid_t pid;
  pipe(fds);
  pid = fork();
  if (pid == (pid_t)0) {
    close(fds[1]);
    dup2(fds[0], STDIN_FILENO);
    execlp("sort", "sort", 0);
  } else {
    FILE *stream;
    close(fds[0]);
    stream = fdopen(fds[1], "w");
    fprintf(stream, "Hello pipe and dup.\n");
    fprintf(stream, "Advanded Linux Programming is good.\n");
    fprintf(stream, "Is APUE better?\n");
    fflush(stream);
    close(fds[1]);
    waitpid(pid, NULL, 0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ed <main+0x5d>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
lea    0xe22(%rip),%rsi
xor    %edx,%edx
mov    %rsi,%rdi
call   10a0 <execlp@plt>
jmp    1258 <main+0xc8>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
lea    0xe06(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdf4(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xdf9(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xe0e(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1070 <fflush@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsp,%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     1234 <main+0xa4>
mov    %rax,%r14
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
lea    0xe43(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,%rbx
lea    0xe36(%rip),%rdi
mov    $0x14,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
lea    0xe32(%rip),%rdi
mov    $0x24,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
lea    0xe3e(%rip),%rdi
mov    $0x10,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fflush@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    %r14,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
jmp    125f <main+0xcf>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
lea    0xdab(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10a0 <execlp@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@.str = [5 x i8] c"sort\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [21 x i8] c"Hello pipe and dup.\0A\00"
@.str.3 = [37 x i8] c"Advanded Linux Programming is good.\0A\00"
@.str.4 = [17 x i8] c"Is APUE better?\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i64
%4 = alloca i32*
store i32 0, i32* %1
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = call i64 (...) @fork()
store i64 %7, i64* %3
%8 = load i64, i64* %3
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %0
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%12 = load i32, i32* %11
%13 = call i32 @close(i32 %12)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%15 = load i32, i32* %14
%16 = load i32, i32* @STDIN_FILENO
%17 = call i32 @dup2(i32 %15, i32 %16)
%18 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 0)
br label %39
19: ; preds = %0
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32* @fdopen(i32 %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %4
%26 = load i32*, i32** %4
%27 = call i32 @fprintf(i32* %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32*, i32** %4
%29 = call i32 @fprintf(i32* %28, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%30 = load i32*, i32** %4
%31 = call i32 @fprintf(i32* %30, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%32 = load i32*, i32** %4
%33 = call i32 @fflush(i32* %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = load i64, i64* %3
%38 = call i32 @waitpid(i64 %37, i32* null, i32 0)
br label %39
39: ; preds = %19, %10
ret i32 0
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@.str = [5 x i8] c"sort\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [21 x i8] c"Hello pipe and dup.\0A\00"
@.str.3 = [37 x i8] c"Advanded Linux Programming is good.\0A\00"
@.str.4 = [17 x i8] c"Is APUE better?\0A\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 i32 @pipe(i32* %3)
%5 = call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %15
7: ; preds = %0
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%9 = load i32, i32* %8
%10 = call i32 @close(i32 %9)
%11 = load i32, i32* %3
%12 = load i32, i32* @STDIN_FILENO
%13 = call i32 @dup2(i32 %11, i32 %12)
%14 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 0)
br label %28
15: ; preds = %0
%16 = load i32, i32* %3
%17 = call i32 @close(i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32* @fdopen(i32 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = call i64 @fwrite(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64 20, i64 1, i32* %20)
%22 = call i64 @fwrite(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i64 36, i64 1, i32* %20)
%23 = call i64 @fwrite(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i64 16, i64 1, i32* %20)
%24 = call i32 @fflush(i32* %20)
%25 = load i32, i32* %18
%26 = call i32 @close(i32 %25)
%27 = call i32 @waitpid(i64 %5, i32* null, i32 0)
br label %28
28: ; preds = %15, %7
ret i32 0
}