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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ ReadProcess(int *);
int /*<<< orphan*/ WriteProcess(int *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int main() {
  int fd[2];
  pid_t pid;
  if (pipe(fd) < 0) {
    fprintf(stderr, "Create Pipe Error %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if ((pid = fork()) < 0) {
    fprintf(stderr, " Fork Error : %s \n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (pid == 0) {
    WriteProcess(fd);
  } else {
    if ((pid = fork()) < 0) {
      fprintf(stderr, " Fork Error : %s \n", strerror(errno));
      exit(EXIT_FAILURE);
    }
    if (pid == 0) {
      ReadProcess(fd);
    } else {
      wait(0);
      wait(0);
      printf("-------------End of Program-----------\n");
      return 0;
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jge    11ec <main+0x5c>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe27(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    123b <main+0xab>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xdee(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1254 <main+0xc4>
lea    -0xc(%rbp),%rdi
call   1070 <WriteProcess@plt>
jmp    12e7 <main+0x157>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    12a3 <main+0x113>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xd86(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12bc <main+0x12c>
lea    -0xc(%rbp),%rdi
call   1030 <ReadProcess@plt>
jmp    12e2 <main+0x152>
xor    %edi,%edi
call   10a0 <wait@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
lea    0xd58(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e7 <main+0x157>
jmp    12e7 <main+0x157>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
js     121a <main+0x8a>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     11fa <main+0x6a>
je     11dc <main+0x4c>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     11fa <main+0x6a>
je     11e8 <main+0x58>
xor    %edi,%edi
call   10a0 <wait@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
lea    0xe54(%rip),%rdi
call   1040 <puts@plt>
jmp    11f2 <main+0x62>
lea    0x8(%rsp),%rdi
call   1070 <WriteProcess@plt>
jmp    11f2 <main+0x62>
lea    0x8(%rsp),%rdi
call   1030 <ReadProcess@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2ddf(%rip),%rax
mov    (%rax),%ebx
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdfe(%rip),%rsi
jmp    1238 <main+0xa8>
mov    0x2dbf(%rip),%rax
mov    (%rax),%ebx
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdc8(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Create Pipe Error %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [19 x i8] c" Fork Error : %s \0A\00"
@.str.2 = [40 x i8] c"-------------End of Program-----------\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i64
store i32 0, i32* %1
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%5 = call i64 @pipe(i32* %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = call i64 (...) @fork()
store i64 %15, i64* %3
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %14
%25 = load i64, i64* %3
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%29 = call i32 @WriteProcess(i32* %28)
br label %51
30: ; preds = %24
%31 = call i64 (...) @fork()
store i64 %31, i64* %3
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %30
%41 = load i64, i64* %3
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%45 = call i32 @ReadProcess(i32* %44)
br label %50
46: ; preds = %40
%47 = call i32 @wait(i32 0)
%48 = call i32 @wait(i32 0)
%49 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %51
50: ; preds = %43
br label %51
51: ; preds = %46, %50, %27
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Create Pipe Error %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [19 x i8] c" Fork Error : %s \0A\00"
@str = [39 x i8] c"-------------End of Program-----------\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 i64 @pipe(i32* %3)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = call i8* @strerror(i32 %8)
%10 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = call i64 (...) @fork()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %13
%24 = icmp eq i64 %14, 0
br i1 %24, label %25, label %27
25: ; preds = %23
%26 = call i32 @WriteProcess(i32* %3)
br label %45
27: ; preds = %23
%28 = call i64 (...) @fork()
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %33)
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %27
%38 = icmp eq i64 %28, 0
br i1 %38, label %39, label %41
39: ; preds = %37
%40 = call i32 @ReadProcess(i32* %3)
br label %45
41: ; preds = %37
%42 = call i32 @wait(i32 0)
%43 = call i32 @wait(i32 0)
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
br label %45
45: ; preds = %25, %39, %41
ret i32 0
}