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

/* Variables and functions */
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
scalar_t__ write(int, char *, int);
int main(void) {
  int pipe_fd[2];
  pid_t pid;
  char buffer[80];
  if (pipe(pipe_fd) < 0) {
    perror("create pipe error");
    return 1;
  }
  pid = fork();
  if (pid < 0) {
    perror("fork error");
    return 2;
  } else if (pid == 0) {
    puts("child process.");
    close(pipe_fd[1]);
    sleep(3);
    read(pipe_fd[0], buffer, sizeof(buffer));
    printf("child read from pipe is: %s\n", buffer);
    close(pipe_fd[0]);
  } else {
    puts("parent process.");
    close(pipe_fd[0]);
    sleep(2);
    write(pipe_fd[1], "hello", 5);
    if (write(pipe_fd[1], " pipe", 5) > 0) {
      printf("Parent write to pipe.\n");
    }
    close(pipe_fd[1]);
    sleep(3);
    waitpid(pid, NULL, 0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11da <main+0x3a>
lea    0xe37(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ec <main+0x14c>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1208 <main+0x68>
lea    0xe1b(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12ec <main+0x14c>
cmpq   $0x0,-0x18(%rbp)
jne    1261 <main+0xc1>
lea    0xe03(%rip),%rdi
call   1030 <puts@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    $0x3,%edi
call   10b0 <sleep@plt>
mov    -0xc(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x50,%edx
call   1080 <read@plt>
lea    -0x70(%rbp),%rsi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
jmp    12e0 <main+0x140>
lea    0xde1(%rip),%rdi
call   1030 <puts@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   10b0 <sleep@plt>
mov    -0x8(%rbp),%edi
lea    0xdd0(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
lea    0xdc2(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jle    12bf <main+0x11f>
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    $0x3,%edi
call   10b0 <sleep@plt>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
jmp    12e5 <main+0x145>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x68,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
js     12ab <main+0x10b>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     12be <main+0x11e>
je     1250 <main+0xb0>
mov    %rax,%r14
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   10b0 <sleep@plt>
mov    0xc(%rsp),%edi
lea    0xe5c(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
lea    0xe4d(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
test   %rax,%rax
jle    122d <main+0x8d>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    $0x3,%edi
call   10b0 <sleep@plt>
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
jmp    12a1 <main+0x101>
lea    0xdc6(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    $0x3,%edi
call   10b0 <sleep@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
mov    $0x50,%edx
call   1080 <read@plt>
lea    0xda0(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd4e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%ebx
jmp    12a1 <main+0x101>
lea    0xd4d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x2,%ebx
jmp    12a1 <main+0x101>

LLVM IR (-O0):
@.str = [18 x i8] c"create pipe error\00"
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [15 x i8] c"child process.\00"
@.str.3 = [29 x i8] c"child read from pipe is: %s\0A\00"
@.str.4 = [16 x i8] c"parent process.\00"
@.str.5 = [6 x i8] c"hello\00"
@.str.6 = [6 x i8] c" pipe\00"
@.str.7 = [23 x i8] c"Parent write to pipe.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i64
%4 = alloca [80 x i8]
store i32 0, i32* %1
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %58
10: ; preds = %0
%11 = call i64 (...) @fork()
store i64 %11, i64* %3
%12 = load i64, i64* %3
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %58
16: ; preds = %10
%17 = load i64, i64* %3
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %34
19: ; preds = %16
%20 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%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 [80 x i8], [80 x i8]* %4, i64 0, i64 0
%28 = call i32 @read(i32 %26, i8* %27, i32 80)
%29 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
br label %56
34: ; preds = %16
%35 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = call i32 @sleep(i32 2)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i64 @write(i32 %41, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 5)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i64 @write(i32 %44, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 5)
%46 = icmp sgt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %34
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
br label %49
49: ; preds = %47, %34
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
%53 = call i32 @sleep(i32 3)
%54 = load i64, i64* %3
%55 = call i32 @waitpid(i64 %54, i32* null, i32 0)
br label %56
56: ; preds = %49, %19
br label %57
57: ; preds = %56
store i32 0, i32* %1
br label %58
58: ; preds = %57, %14, %8
%59 = load i32, i32* %1
ret i32 %59
}

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