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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ wait();
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  int pfd[2];
  int pid;
  char w_cont[] = " hello sundy !";
  char r_cont[255];
  if (pipe(pfd) < 0) {
    perror("create pipe failed");
    return -1;
  } else {
    if ((pid = fork()) < 0) {
      perror("create process failed");
    } else if (pid > 0) {
      close(pfd[0]);
      write(pfd[1], w_cont, strlen(w_cont));
      close(pfd[1]);
      wait();
    } else {
      sleep(2);
      close(pfd[1]);
      read(pfd[0], r_cont, 255);
      printf("child process read:%s\n", r_cont);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe47(%rip),%rax
mov    %rax,-0x1f(%rbp)
mov    0xe45(%rip),%eax
mov    %eax,-0x17(%rbp)
mov    0xe3f(%rip),%ax
mov    %ax,-0x13(%rbp)
mov    0xe37(%rip),%al
mov    %al,-0x11(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    1205 <main+0x65>
lea    0xe1b(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <main+0x12a>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1229 <main+0x89>
lea    0xe03(%rip),%rdi
call   1090 <perror@plt>
jmp    12be <main+0x11e>
cmpl   $0x0,-0x10(%rbp)
jle    1280 <main+0xe0>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x12c(%rbp)
lea    -0x1f(%rbp),%rax
mov    %rax,-0x128(%rbp)
lea    -0x1f(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x12c(%rbp),%edi
mov    -0x128(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   10b0 <wait@plt>
jmp    12b9 <main+0x119>
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1080 <read@plt>
lea    -0x120(%rbp),%rsi
lea    0xd84(%rip),%rdi
call   1050 <printf@plt>
jmp    12be <main+0x11e>
jmp    12c3 <main+0x123>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x128,%rsp
movabs $0x212079646e7573,%rax
mov    %rax,0x17(%rsp)
movabs $0x73206f6c6c656820,%rax
6f 20 73
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
js     126f <main+0xcf>
xor    %ebx,%ebx
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     1282 <main+0xe2>
je     1228 <main+0x88>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   10b0 <wait@plt>
jmp    1262 <main+0xc2>
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
lea    0x20(%rsp),%rbx
mov    %rbx,%rsi
mov    $0xff,%edx
call   1080 <read@plt>
lea    0xde0(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd99(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1262 <main+0xc2>
lea    0xd99(%rip),%rdi
call   1090 <perror@plt>
jmp    1262 <main+0xc2>

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

LLVM IR (-O2):
@__const.main.w_cont = [15 x i8] c" hello sundy !\00"
@.str = [19 x i8] c"create pipe failed\00"
@.str.1 = [22 x i8] c"create process failed\00"
@.str.2 = [23 x i8] c"child process read:%s\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [15 x i8]
%3 = alloca [255 x i8]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = getelementptr inbounds [15 x i8], [15 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [255 x i8], [255 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 %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %37
12: ; preds = %0
%13 = call i32 (...) @fork()
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %37
17: ; preds = %12
%18 = icmp eq i32 %13, 0
br i1 %18, label %29, label %19
19: ; preds = %17
%20 = load i32, i32* %7
%21 = call i32 @close(i32 %20)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @strlen(i8* %5)
%25 = call i32 @write(i32 %23, i8* %5, i32 %24)
%26 = load i32, i32* %22
%27 = call i32 @close(i32 %26)
%28 = call i32 (...) @wait()
br label %37
29: ; preds = %17
%30 = call i32 @sleep(i32 2)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %7
%35 = call i32 @read(i32 %34, i8* %6, i32 255)
%36 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %6)
br label %37
37: ; preds = %19, %29, %15, %10
%38 = phi i32 [ -1, %10 ], [ 0, %15 ], [ 0, %29 ], [ 0, %19 ]
ret i32 %38
}