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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, char);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  int pid;
  int fd[2];
  if (pipe(fd) == -1) {
    perror("pipe");
    exit(EXIT_FAILURE);
  }
  pid = fork();
  if (pid < 0) {
    perror("failed to create child...");
    exit(EXIT_FAILURE);
  } else if (pid == 0) {
    char ch;
    close(fd[1]);
    while (read(fd[0], &ch, sizeof(ch)) > 0) {
      printf("%c", ch);
    }
    close(fd[0]);
    exit(EXIT_SUCCESS);
  } else {
    char msg[] = "test pipe ...\n";
    close(fd[0]);
    write(fd[1], msg, strlen(msg));
    close(fd[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)
lea    -0x10(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11db <main+0x3b>
lea    0xe38(%rip),%rdi
call   1090 <perror@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1209 <main+0x69>
lea    0xe0f(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1261 <main+0xc1>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0x0,%rax
jle    124b <main+0xab>
lea    0xde2(%rip),%rdi
movsbl -0x11(%rbp),%esi
call   1050 <printf@plt>
jmp    121b <main+0x7b>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0xdba(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xdb8(%rip),%eax
mov    %eax,-0x18(%rbp)
mov    0xdb2(%rip),%ax
mov    %ax,-0x14(%rbp)
mov    0xdaa(%rip),%al
mov    %al,-0x12(%rbp)
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsp,%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11bd <main+0x1d>
lea    0xe45(%rip),%rdi
jmp    11cf <main+0x2f>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
jns    11e2 <main+0x42>
lea    0xe36(%rip),%rdi
call   1090 <perror@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jne    123c <main+0x9c>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    (%rsp),%edi
lea    0x8(%rsp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
test   %rax,%rax
jle    1232 <main+0x92>
lea    0xe14(%rip),%r14
lea    0x8(%rsp),%rbx
movsbl 0x8(%rsp),%esi
mov    %r14,%rdi
call   1050 <printf@plt>
mov    (%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
test   %rax,%rax
jg     1210 <main+0x70>
mov    (%rsp),%edi
call   1060 <close@plt>
jmp    128f <main+0xef>
movabs $0xa2e2e2e206570,%rax
mov    %rax,0xf(%rsp)
movabs $0x7069702074736574,%rax
70 69 70
mov    %rax,0x8(%rsp)
mov    (%rsp),%edi
call   1060 <close@plt>
mov    0x4(%rsp),%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10b0 <wait@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [26 x i8] c"failed to create child...\00"
@.str.2 = [3 x i8] c"%c\00"
@EXIT_SUCCESS = global i32 0
@__const.main.msg = [15 x i8] c"test pipe ...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i32]
%4 = alloca i8
%5 = alloca [15 x i8]
store i32 0, i32* %1
%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 = %0
%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 = %0
%14 = call i32 (...) @fork()
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %13
%22 = load i32, i32* %2
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %42
24: ; preds = %21
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %33, %24
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%30 = load i32, i32* %29
%31 = call i64 @read(i32 %30, i8* %4, i32 1)
%32 = icmp sgt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i8, i8* %4
%35 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8 signext %34)
br label %28
36: ; preds = %28
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %3, 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 = %21
%43 = bitcast [15 x i8]* %5 to i8*
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%48 = load i32, i32* %47
%49 = getelementptr inbounds [15 x i8], [15 x i8]* %5, i64 0, i64 0
%50 = getelementptr inbounds [15 x i8], [15 x i8]* %5, i64 0, i64 0
%51 = call i32 @strlen(i8* %50)
%52 = call i32 @write(i32 %48, i8* %49, i32 %51)
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
%56 = call i32 @wait(i32* null)
%57 = load i32, i32* @EXIT_SUCCESS
%58 = call i32 @exit(i32 %57)
unreachable
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [26 x i8] c"failed to create child...\00"
@.str.2 = [3 x i8] c"%c\00"
@EXIT_SUCCESS = global i32 0
@__const.main.msg = [15 x i8] c"test pipe ...\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i8
%3 = alloca [15 x i8]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, 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 = %0
%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 = %0
%13 = call i32 (...) @fork()
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %12
%20 = icmp eq i32 %13, 0
br i1 %20, label %21, label %39
21: ; preds = %19
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %5
%26 = call i64 @read(i32 %25, i8* %2, i32 1)
%27 = icmp sgt i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %21, %28
%29 = load i8, i8* %2
%30 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8 signext %29)
%31 = load i32, i32* %5
%32 = call i64 @read(i32 %31, i8* %2, i32 1)
%33 = icmp sgt i64 %32, 0
br i1 %33, label %28, label %34
34: ; preds = %28, %21
%35 = load i32, i32* %5
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* @EXIT_SUCCESS
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %19
%40 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%41 = load i32, i32* %5
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @strlen(i8* %40)
%46 = call i32 @write(i32 %44, i8* %40, i32 %45)
%47 = load i32, i32* %43
%48 = call i32 @close(i32 %47)
%49 = call i32 @wait(i32* null)
%50 = load i32, i32* @EXIT_SUCCESS
%51 = call i32 @exit(i32 %50)
unreachable
}