#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 /*<<< orphan*/ buf;

/* Variables and functions */
int STDOUT_FILENO;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int pipe(int *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd[2];
  int len;
  char buf[100];
  int rtn = pipe(fd);
  bzero(buf, sizeof(buf));
  if (rtn == -1) {
    printf("%m\n");
    exit(-1);
  }
  int pid = fork();
  if (pid == 0) {
    close(fd[1]);
    while ((len = read(fd[0], buf, sizeof(buf))) > 0) {
      write(STDOUT_FILENO, buf, len);
    }
  } else {
    close(fd[0]);
    sprintf(buf, "%s\n", "hello, pipe!");
    write(fd[1], buf, strlen(buf));
    close(fd[1]);
    waitpid(pid, NULL, 0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
mov    %eax,-0x84(%rbp)
lea    -0x80(%rbp),%rdi
mov    $0x64,%esi
call   10a0 <bzero@plt>
cmpl   $0xffffffff,-0x84(%rbp)
jne    1202 <main+0x52>
lea    0xe0d(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x88(%rbp)
cmpl   $0x0,-0x88(%rbp)
jne    1260 <main+0xb0>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1080 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    125b <main+0xab>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    -0x80(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
jmp    1224 <main+0x74>
jmp    12c9 <main+0x119>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
lea    -0x80(%rbp),%rdi
lea    0xd91(%rip),%rsi
lea    0xd8e(%rip),%rdx
call   10b0 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x94(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0x90(%rbp)
lea    -0x80(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x94(%rbp),%edi
mov    -0x90(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x88(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%rdi
mov    $0x64,%esi
call   10a0 <bzero@plt>
cmp    $0xffffffff,%ebx
je     12a0 <main+0xf0>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     123e <main+0x8e>
mov    %eax,%ebx
mov    0x8(%rsp),%edi
call   1060 <close@plt>
lea    0xe08(%rip),%rsi
lea    0xe05(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   10b0 <sprintf@plt>
mov    0xc(%rsp),%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
jmp    1292 <main+0xe2>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x64,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    1292 <main+0xe2>
mov    0x2d8b(%rip),%rbp
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     1270 <main+0xc0>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd59(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%m\0A\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [13 x i8] c"hello, pipe!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
store i32 %8, i32* %5
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%10 = call i32 @bzero(i8* %9, i32 100)
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %0
%17 = call i32 (...) @fork()
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %36
20: ; preds = %16
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @close(i32 %22)
br label %24
24: ; preds = %30, %20
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%28 = call i32 @read(i32 %26, i8* %27, i32 100)
store i32 %28, i32* %3
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %24
%31 = load i32, i32* @STDOUT_FILENO
%32 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%33 = load i32, i32* %3
%34 = call i32 @write(i32 %31, i8* %32, i32 %33)
br label %24
35: ; preds = %24
br label %53
36: ; preds = %16
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%41 = call i32 @sprintf(i8* %40, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%43 = load i32, i32* %42
%44 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%45 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%46 = call i32 @strlen(i8* %45)
%47 = call i32 @write(i32 %43, i8* %44, i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %6
%52 = call i32 @waitpid(i32 %51, i32* null, i32 0)
br label %53
53: ; preds = %36, %35
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%m\0A\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [13 x i8] c"hello, pipe!\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [100 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = call i32 @bzero(i8* %4, i32 100)
%8 = icmp eq i32 %6, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %0
%13 = call i32 (...) @fork()
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %29
15: ; preds = %12
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @close(i32 %17)
%19 = load i32, i32* %5
%20 = call i32 @read(i32 %19, i8* %4, i32 100)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %40
22: ; preds = %15, %22
%23 = phi i32 [ %27, %22 ], [ %20, %15 ]
%24 = load i32, i32* @STDOUT_FILENO
%25 = call i32 @write(i32 %24, i8* %4, i32 %23)
%26 = load i32, i32* %5
%27 = call i32 @read(i32 %26, i8* %4, i32 100)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %22, label %40
29: ; preds = %12
%30 = load i32, i32* %5
%31 = call i32 @close(i32 %30)
%32 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @strlen(i8* %4)
%36 = call i32 @write(i32 %34, i8* %4, i32 %35)
%37 = load i32, i32* %33
%38 = call i32 @close(i32 %37)
%39 = call i32 @waitpid(i32 %13, i32* null, i32 0)
br label %40
40: ; preds = %22, %15, %29
ret i32 0
}