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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, int, char const *);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
int write(int, char const *, int /*<<< orphan*/);
int main() {
  pid_t pid;
  int pipe_fd[2];
  char buf[256];
  const char data[] = "Pipe Test Program";
  int real_read, real_write;
  memset((void *)buf, 0, sizeof(buf));
  if (pipe(pipe_fd) < 0) {
    perror("fail to Pipe ");
    exit(0);
  }
  if ((pid = fork()) == 0) {
    close(pipe_fd[1]);
    sleep(1 * 3);
    if ((real_read = read(pipe_fd[0], buf, 256)) > 0) {
      printf("%d bytes read form the pipe is %s\n", real_read, buf);
    }
    close(pipe_fd[0]);
    exit(0);
  } else if (pid > 0) {
    close(pipe_fd[0]);
    sleep(1);
    if ((real_write = write(pipe_fd[1], data, strlen(data))) != -1) {
      printf("parent write %d bytes : %s \n", real_write, data);
    }
    close(pipe_fd[1]);
    waitpid(pid, NULL, 0);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    0xe2e(%rip),%rax
mov    %rax,-0x130(%rbp)
mov    0xe28(%rip),%rax
mov    %rax,-0x128(%rbp)
mov    0xe22(%rip),%ax
mov    %ax,-0x120(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    -0x10(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0x0,%rax
jge    122e <main+0x6e>
lea    0xdf0(%rip),%rdi
call   10b0 <perror@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    12a0 <main+0xe0>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    $0x3,%edi
call   10d0 <sleep@plt>
mov    -0x10(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1090 <read@plt>
mov    %eax,-0x134(%rbp)
cmp    $0x0,%eax
jle    1291 <main+0xd1>
mov    -0x134(%rbp),%esi
lea    -0x110(%rbp),%rdx
lea    0xd94(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    133a <main+0x17a>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x144(%rbp)
lea    -0x130(%rbp),%rax
mov    %rax,-0x140(%rbp)
lea    -0x130(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x144(%rbp),%edi
mov    -0x140(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x138(%rbp)
cmp    $0xffffffff,%eax
je     131c <main+0x15c>
mov    -0x138(%rbp),%esi
lea    -0x130(%rbp),%rdx
lea    0xd2c(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <waitpid@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
jmp    133f <main+0x17f>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x130,%rsp
movaps 0xe2e(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movw   $0x6d,0x20(%rsp)
lea    0x30(%rsp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
test   %rax,%rax
js     121a <main+0x5a>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
je     122d <main+0x6d>
jg     127a <main+0xba>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf1(%rip),%rdi
call   10b0 <perror@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    $0x3,%edi
call   10d0 <sleep@plt>
mov    0x8(%rsp),%edi
lea    0x30(%rsp),%rsi
mov    $0x100,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    126a <main+0xaa>
lea    0xdc2(%rip),%rdi
lea    0x30(%rsp),%rdx
mov    %eax,%esi
call   1050 <printf@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    0xc(%rsp),%ebp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     12c5 <main+0x105>
lea    0xd8a(%rip),%rdi
lea    0x10(%rsp),%rdx
mov    %eax,%esi
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <waitpid@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@__const.main.data = [18 x i8] c"Pipe Test Program\00"
@.str = [14 x i8] c"fail to Pipe \00"
@.str.1 = [35 x i8] c"%d bytes read form the pipe is %s\0A\00"
@.str.2 = [29 x i8] c"parent write %d bytes : %s \0A\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca [2 x i32]
%3 = alloca [256 x i8]
%4 = alloca [18 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = bitcast [18 x i8]* %4 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i32 0, i32 256)
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%11 = call i64 @pipe(i32* %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %0
%17 = call i64 (...) @fork()
store i64 %17, i64* %1
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %38
19: ; preds = %16
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
%23 = call i32 @sleep(i32 3)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%25 = load i32, i32* %24
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 256)
store i32 %27, i32* %5
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %19
%30 = load i32, i32* %5
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%32 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %30, i8* %31)
br label %33
33: ; preds = %29, %19
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = call i32 @exit(i32 0)
unreachable
38: ; preds = %16
%39 = load i64, i64* %1
%40 = icmp sgt i64 %39, 0
br i1 %40, label %41, label %64
41: ; preds = %38
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = call i32 @sleep(i32 1)
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%47 = load i32, i32* %46
%48 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 0
%49 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @write(i32 %47, i8* %48, i32 %50)
store i32 %51, i32* %6
%52 = icmp ne i32 %51, -1
br i1 %52, label %53, label %57
53: ; preds = %41
%54 = load i32, i32* %6
%55 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 0
%56 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %54, i8* %55)
br label %57
57: ; preds = %53, %41
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
%61 = load i64, i64* %1
%62 = call i32 @waitpid(i64 %61, i32* null, i32 0)
%63 = call i32 @exit(i32 0)
unreachable
64: ; preds = %38
br label %65
65: ; preds = %64
ret i32 0
}

LLVM IR (-O2):
@__const.main.data = [18 x i8] c"Pipe Test Program\00"
@.str = [14 x i8] c"fail to Pipe \00"
@.str.1 = [35 x i8] c"%d bytes read form the pipe is %s\0A\00"
@.str.2 = [29 x i8] c"parent write %d bytes : %s \0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [256 x i8]
%3 = alloca [18 x i8]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [18 x i8], [18 x i8]* %3, i64 0, i64 0
%7 = call i32 @memset(i8* %5, i32 0, i32 256)
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %0
%15 = call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %31
17: ; preds = %14
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @close(i32 %19)
%21 = call i32 @sleep(i32 3)
%22 = load i32, i32* %8
%23 = call i32 @read(i32 %22, i8* %5, i32 256)
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %23, i8* %5)
br label %27
27: ; preds = %25, %17
%28 = load i32, i32* %8
%29 = call i32 @close(i32 %28)
%30 = call i32 @exit(i32 0)
unreachable
31: ; preds = %14
%32 = icmp sgt i64 %15, 0
br i1 %32, label %33, label %49
33: ; preds = %31
%34 = load i32, i32* %8
%35 = call i32 @close(i32 %34)
%36 = call i32 @sleep(i32 1)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @strlen(i8* %6)
%40 = call i32 @write(i32 %38, i8* %6, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %44, label %42
42: ; preds = %33
%43 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %40, i8* %6)
br label %44
44: ; preds = %42, %33
%45 = load i32, i32* %37
%46 = call i32 @close(i32 %45)
%47 = call i32 @waitpid(i64 %15, i32* null, i32 0)
%48 = call i32 @exit(i32 0)
unreachable
49: ; preds = %31
ret i32 0
}