#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);
scalar_t__ fork();
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
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 = -1;
  int pipe_fd[2];
  char buf[(256)] = {0};
  const char data[] = "Pipe Test Program";
  int real_read = 0;
  int real_write = 0;
  memset((void *)buf, 0, sizeof(buf));
  if (pipe(pipe_fd) < 0) {
    printf("Pipe create error\n");
    exit(1);
  }
  if ((pid = fork()) == 0) {
    close(pipe_fd[1]);
    sleep((1) * 3);
    if ((real_read = read(pipe_fd[0], buf, (256))) > 0) {
      printf("I am child progress.I have read %d bytes from the pipe: '%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("I am parent progress.I have wrote %d bytes to the pipe:'%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
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
mov    0xe23(%rip),%rax
mov    %rax,-0x130(%rbp)
mov    0xe1d(%rip),%rax
mov    %rax,-0x128(%rbp)
mov    0xe17(%rip),%ax
mov    %ax,-0x120(%rbp)
movl   $0x0,-0x134(%rbp)
movl   $0x0,-0x138(%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    1252 <main+0xa2>
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    12c6 <main+0x116>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    $0x3,%edi
call   10c0 <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    12b7 <main+0x107>
mov    -0x134(%rbp),%esi
lea    -0x110(%rbp),%rdx
lea    0xd75(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    1362 <main+0x1b2>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10c0 <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     1344 <main+0x194>
mov    -0x138(%rbp),%esi
lea    -0x130(%rbp),%rdx
lea    0xd26(%rip),%rdi
mov    $0x0,%al
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   10b0 <exit@plt>
jmp    1367 <main+0x1b7>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x130,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps 0xdba(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movw   $0x6d,0x20(%rsp)
lea    0x30(%rsp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
lea    0x8(%rsp),%rdi
call   1090 <pipe@plt>
test   %rax,%rax
js     128e <main+0xce>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
je     12a4 <main+0xe4>
jg     12f3 <main+0x133>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf8(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
mov    $0x3,%edi
call   10d0 <sleep@plt>
mov    0x8(%rsp),%edi
lea    0x30(%rsp),%rsi
mov    $0x100,%edx
call   10a0 <read@plt>
test   %eax,%eax
jle    12e3 <main+0x123>
lea    0xd3d(%rip),%rdi
lea    0x30(%rsp),%rdx
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    0xc(%rsp),%ebp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1340 <main+0x180>
lea    0xd1e(%rip),%rdi
lea    0x10(%rsp),%rdx
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <waitpid@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

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

LLVM IR (-O2):
@__const.main.data = [18 x i8] c"Pipe Test Program\00"
@.str.1 = [62 x i8] c"I am child progress.I have read %d bytes from the pipe: '%s'\0A\00"
@.str.2 = [61 x i8] c"I am parent progress.I have wrote %d bytes to the pipe:'%s'\0A\00"
@str = [18 x i8] c"Pipe create error\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 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 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
}