#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, "This is a test.\n");
    fprintf(stream, "Hello, world.\n");
    fprintf(stream, "My dog has fleas.\n");
    fprintf(stream, "This program is great.\n");
    fprintf(stream, "One fish, two fish.\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    1278 <main+0xe8>
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    0xdf5(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xdf4(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xdf7(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xdff(%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     1266 <main+0xd6>
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    $0x10,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
lea    0xe2e(%rip),%rdi
mov    $0xe,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
lea    0xe24(%rip),%rdi
mov    $0x12,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
lea    0xe1e(%rip),%rdi
mov    $0x17,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
lea    0xe1d(%rip),%rdi
mov    $0x14,%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    1291 <main+0x101>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
mov    0x2d67(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
lea    0xd79(%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 = [17 x i8] c"This is a test.\0A\00"
@.str.3 = [15 x i8] c"Hello, world.\0A\00"
@.str.4 = [19 x i8] c"My dog has fleas.\0A\00"
@.str.5 = [24 x i8] c"This program is great.\0A\00"
@.str.6 = [21 x i8] c"One fish, two fish.\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 %43
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 ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32*, i32** %4
%29 = call i32 @fprintf(i32* %28, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%30 = load i32*, i32** %4
%31 = call i32 @fprintf(i32* %30, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%32 = load i32*, i32** %4
%33 = call i32 @fprintf(i32* %32, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%34 = load i32*, i32** %4
%35 = call i32 @fprintf(i32* %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
%36 = load i32*, i32** %4
%37 = call i32 @fflush(i32* %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = load i64, i64* %3
%42 = call i32 @waitpid(i64 %41, i32* null, i32 0)
br label %43
43: ; 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 = [17 x i8] c"This is a test.\0A\00"
@.str.3 = [15 x i8] c"Hello, world.\0A\00"
@.str.4 = [19 x i8] c"My dog has fleas.\0A\00"
@.str.5 = [24 x i8] c"This program is great.\0A\00"
@.str.6 = [21 x i8] c"One fish, two fish.\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 %30
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 ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i64 16, i64 1, i32* %20)
%22 = call i64 @fwrite(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i64 14, i64 1, i32* %20)
%23 = call i64 @fwrite(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i64 18, i64 1, i32* %20)
%24 = call i64 @fwrite(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i64 23, i64 1, i32* %20)
%25 = call i64 @fwrite(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i64 20, i64 1, i32* %20)
%26 = call i32 @fflush(i32* %20)
%27 = load i32, i32* %18
%28 = call i32 @close(i32 %27)
%29 = call i32 @waitpid(i64 %5, i32* null, i32 0)
br label %30
30: ; preds = %15, %7
ret i32 0
}