#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*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
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;
  int pipe_fd[2];
  char buf[128] = {0};
  const char data[] = "Pipe Test Program";
  int real_read, real_write;
  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, 128)) > 0) {
      printf("Read data from pipe is %s\n", 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("Write to pipe is: %s\n", data);
    }
    close(pipe_fd[1]);
    waitpid(pid, NULL, 0);
    exit(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    0xe24(%rip),%rax
mov    %rax,-0xc0(%rbp)
mov    0xe1e(%rip),%rax
mov    %rax,-0xb8(%rbp)
mov    0xe18(%rip),%ax
mov    %ax,-0xb0(%rbp)
lea    -0x18(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0x0,%rax
jge    122a <main+0x7a>
lea    0xdf9(%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,-0x10(%rbp)
cmp    $0x0,%rax
jne    1298 <main+0xe8>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    $0x3,%edi
call   10c0 <sleep@plt>
mov    -0x18(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1090 <read@plt>
mov    %eax,-0xc4(%rbp)
cmp    $0x0,%eax
jle    1289 <main+0xd9>
lea    -0xa0(%rbp),%rsi
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    132e <main+0x17e>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0xd4(%rbp)
lea    -0xc0(%rbp),%rax
mov    %rax,-0xd0(%rbp)
lea    -0xc0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xd4(%rbp),%edi
mov    -0xd0(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0xc8(%rbp)
cmp    $0xffffffff,%eax
je     1310 <main+0x160>
lea    -0xc0(%rbp),%rsi
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <waitpid@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    1333 <main+0x183>
xor    %eax,%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0xb0,%rsp
xorps  %xmm0,%xmm0
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 0xe0a(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movw   $0x6d,0x20(%rsp)
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
test   %rax,%rax
js     122d <main+0x7d>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     1243 <main+0x93>
jg     1290 <main+0xe0>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe0f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    $0x3,%edi
call   10c0 <sleep@plt>
mov    0x8(%rsp),%edi
lea    0x30(%rsp),%rsi
mov    $0x80,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    1280 <main+0xd0>
lea    0xd9e(%rip),%rdi
lea    0x30(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10c0 <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     12db <main+0x12b>
lea    0xd5e(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <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   10b0 <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 = [27 x i8] c"Read data from pipe is %s\0A\00"
@.str.2 = [22 x i8] c"Write to pipe is: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [2 x i32]
%4 = alloca [128 x i8]
%5 = alloca [18 x i8]
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = bitcast [128 x i8]* %4 to i8*
%9 = bitcast [18 x i8]* %5 to i8*
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %3, 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 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = call i64 (...) @fork()
store i64 %17, i64* %2
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %37
19: ; preds = %16
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %3, 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]* %3, i64 0, i64 0
%25 = load i32, i32* %24
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 128)
store i32 %27, i32* %6
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %19
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %29, %19
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %16
%38 = load i64, i64* %2
%39 = icmp sgt i64 %38, 0
br i1 %39, label %40, label %62
40: ; preds = %37
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = call i32 @sleep(i32 1)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%46 = load i32, i32* %45
%47 = getelementptr inbounds [18 x i8], [18 x i8]* %5, i64 0, i64 0
%48 = getelementptr inbounds [18 x i8], [18 x i8]* %5, i64 0, i64 0
%49 = call i32 @strlen(i8* %48)
%50 = call i32 @write(i32 %46, i8* %47, i32 %49)
store i32 %50, i32* %7
%51 = icmp ne i32 %50, -1
br i1 %51, label %52, label %55
52: ; preds = %40
%53 = getelementptr inbounds [18 x i8], [18 x i8]* %5, i64 0, i64 0
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %52, %40
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%57 = load i32, i32* %56
%58 = call i32 @close(i32 %57)
%59 = load i64, i64* %2
%60 = call i32 @waitpid(i64 %59, i32* null, i32 0)
%61 = call i32 @exit(i32 0)
unreachable
62: ; preds = %37
br label %63
63: ; preds = %62
ret i32 0
}

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