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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fdopen(int, char *);
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ reader(int /*<<< orphan*/ *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ writer(char *, int, int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int pipefd[2];
  pid_t cpid;
  if (pipe(pipefd) == -1) {
    perror("pipe");
    exit(EXIT_FAILURE);
  }
  cpid = fork();
  if (cpid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if (cpid == 0) {
    FILE *stream = NULL;
    close(pipefd[1]);
    stream = fdopen(pipefd[0], "r");
    reader(stream);
    close(pipefd[0]);
    exit(EXIT_SUCCESS);
  } else {
    FILE *stream = NULL;
    close(pipefd[0]);
    stream = fdopen(pipefd[1], "w");
    writer("Hello, world!", 10, stream);
    close(pipefd[1]);
    wait(NULL);
    exit(EXIT_SUCCESS);
  }
  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
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11d2 <main+0x42>
lea    0xe41(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1200 <main+0x70>
lea    0xe18(%rip),%rdi
call   1080 <perror@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    124c <main+0xbc>
movq   $0x0,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
lea    0xde6(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <reader@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
lea    0xda6(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdx
lea    0xd94(%rip),%rdi
mov    $0xa,%esi
call   1070 <writer@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    %rsp,%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11a7 <main+0x17>
lea    0xe5b(%rip),%rdi
jmp    11be <main+0x2e>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11d1 <main+0x41>
cmp    $0xffffffff,%eax
jne    11fb <main+0x6b>
lea    0xe47(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
lea    0xe26(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,%rdi
call   1030 <reader@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
jmp    1237 <main+0xa7>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
lea    0xdfe(%rip),%rsi
call   1060 <fdopen@plt>
lea    0xdf4(%rip),%rdi
mov    $0xa,%esi
mov    %rax,%rdx
call   1070 <writer@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

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

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