#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;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int read(int, char *, int);
int strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int pipe_desc[2];
  if (pipe(pipe_desc) == -1) {
    perror("pipe");
    exit(EXIT_FAILURE);
  }
  pid_t pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if (pid == 0) {
    close(pipe_desc[0]);
    char *msg = "hello";
    write(pipe_desc[1], msg, strlen(msg));
    close(pipe_desc[1]);
    exit(0);
  }
  close(pipe_desc[1]);
  char buffer[64];
  int n_read = read(pipe_desc[0], buffer, 64);
  while (n_read != 0) {
    write(1, buffer, n_read);
    n_read = read(pipe_desc[0], buffer, 64);
  }
  close(pipe_desc[0]);
  wait(NULL);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11d5 <main+0x45>
lea    0xe3e(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e12(%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    1203 <main+0x73>
lea    0xe15(%rip),%rdi
call   1080 <perror@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    125a <main+0xca>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
lea    0xdee(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x84(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x84(%rbp),%edi
mov    -0x80(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x40,%edx
call   1070 <read@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
je     12aa <main+0x11a>
lea    -0x70(%rbp),%rsi
mov    -0x74(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x40,%edx
call   1070 <read@plt>
mov    %eax,-0x74(%rbp)
jmp    1276 <main+0xe6>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x58,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     121e <main+0x8e>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1227 <main+0x97>
cmp    $0xffffffff,%eax
je     125f <main+0xcf>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x40,%edx
call   1070 <read@plt>
test   %eax,%eax
je     1204 <main+0x74>
lea    0x10(%rsp),%rbx
nop
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1070 <read@plt>
test   %eax,%eax
jne    11e0 <main+0x50>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret
lea    0xddb(%rip),%rdi
jmp    1266 <main+0xd6>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%ebx
lea    0xdcf(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xd9f(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d6e(%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 = [6 x i8] c"hello\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 i8*
%9 = alloca [64 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = call i32 (...) @fork()
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %18
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %43
29: ; preds = %26
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8** %8
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%34 = load i32, i32* %33
%35 = load i8*, i8** %8
%36 = load i8*, i8** %8
%37 = call i32 @strlen(i8* %36)
%38 = call i32 @write(i32 %34, i8* %35, i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = call i32 @exit(i32 0)
unreachable
43: ; preds = %26
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%48 = load i32, i32* %47
%49 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%50 = call i32 @read(i32 %48, i8* %49, i32 64)
store i32 %50, i32* %10
br label %51
51: ; preds = %54, %43
%52 = load i32, i32* %10
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %62
54: ; preds = %51
%55 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%56 = load i32, i32* %10
%57 = call i32 @write(i32 1, i8* %55, i32 %56)
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%59 = load i32, i32* %58
%60 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%61 = call i32 @read(i32 %59, i8* %60, i32 64)
store i32 %61, i32* %10
br label %51
62: ; preds = %51
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%64 = load i32, i32* %63
%65 = call i32 @close(i32 %64)
%66 = call i32 @wait(i32* null)
%67 = load i32, i32* %3
ret i32 %67
}

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 = [6 x i8] c"hello\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [64 x i8]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = call i32 (...) @fork()
switch i32 %14, label %29 [
i32 -1, label %15
i32 0, label %19
]
15: ; preds = %13
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %13
%20 = load i32, i32* %6
%21 = call i32 @close(i32 %20)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @write(i32 %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %22
%27 = call i32 @close(i32 %26)
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %13
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%34 = load i32, i32* %6
%35 = call i32 @read(i32 %34, i8* %33, i32 64)
%36 = icmp eq i32 %35, 0
br i1 %36, label %43, label %37
37: ; preds = %29, %37
%38 = phi i32 [ %41, %37 ], [ %35, %29 ]
%39 = call i32 @write(i32 1, i8* %33, i32 %38)
%40 = load i32, i32* %6
%41 = call i32 @read(i32 %40, i8* %33, i32 64)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %37
43: ; preds = %37, %29
%44 = load i32, i32* %6
%45 = call i32 @close(i32 %44)
%46 = call i32 @wait(i32* null)
ret i32 0
}