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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int fork();
int getpid();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int *);
int write(int, char *, int);
int main(void) {
  pid_t pid = -1;
  int ret = -1;
  int status = -1;
  int fd[2] = {0};
  char buf[128] = {0};
  int i = 0;
  ret = pipe(fd);
  if (-1 == ret) {
    perror("pipe failed: ");
    goto _OUT;
  }
  for (i = 0; i < 2; i++) {
    pid = fork();
    if (-1 == (ret = pid)) {
      perror("fork failed: ");
      goto _OUT;
    } else if (pid == 0) {
      break;
    }
  }
  if (0 == pid) {
    printf("child: %d\n", getpid());
    if (0 == i) {
      close(fd[0]);
      memset(buf, 0, sizeof(buf));
      ret = write(fd[1], "howaylee", sizeof("howaylee"));
      printf("write finish\n");
      if (-1 == ret) {
        perror("write failed: ");
        goto _OUT;
      }
    }
    if (1 == i) {
      close(fd[1]);
      sleep(5);
      ret = read(fd[0], buf, sizeof(buf));
      printf("read finish\n");
      if (-1 == ret) {
        perror("read failed: ");
        goto _OUT;
      }
      printf("ret=%d buf = %s\n", ret, buf);
    }
  }
  if (0 < pid) {
    wait(&status);
    wait(&status);
  }
_OUT:
  close(fd[0]);
  close(fd[1]);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
movl   $0x0,-0xa4(%rbp)
lea    -0x18(%rbp),%rdi
call   1080 <pipe@plt>
mov    %eax,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    122f <main+0x7f>
lea    0xddb(%rip),%rdi
call   10a0 <perror@plt>
jmp    13db <main+0x22b>
movl   $0x0,-0xa4(%rbp)
cmpl   $0x2,-0xa4(%rbp)
jge    12a1 <main+0xf1>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%ecx
mov    %ecx,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1274 <main+0xc4>
lea    0xda4(%rip),%rdi
call   10a0 <perror@plt>
jmp    13db <main+0x22b>
cmpl   $0x0,-0x8(%rbp)
jne    1283 <main+0xd3>
jmp    12a1 <main+0xf1>
jmp    1288 <main+0xd8>
jmp    128d <main+0xdd>
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    1239 <main+0x89>
xor    %eax,%eax
cmp    -0x8(%rbp),%eax
jne    13b9 <main+0x209>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
cmp    -0xa4(%rbp),%eax
jne    1335 <main+0x185>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
lea    0xd31(%rip),%rsi
mov    $0x9,%edx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
lea    0xd26(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    1330 <main+0x180>
lea    0xd18(%rip),%rdi
call   10a0 <perror@plt>
jmp    13db <main+0x22b>
jmp    1335 <main+0x185>
mov    $0x1,%eax
cmp    -0xa4(%rbp),%eax
jne    13b4 <main+0x204>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
mov    -0x18(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1090 <read@plt>
mov    %eax,-0xc(%rbp)
lea    0xcd7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    139c <main+0x1ec>
lea    0xcc8(%rip),%rdi
call   10a0 <perror@plt>
jmp    13db <main+0x22b>
mov    -0xc(%rbp),%esi
lea    -0xa0(%rbp),%rdx
lea    0xcbb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13b9 <main+0x209>
xor    %eax,%eax
cmp    -0x8(%rbp),%eax
jge    13d6 <main+0x226>
lea    -0x10(%rbp),%rdi
call   10c0 <wait@plt>
lea    -0x10(%rbp),%rdi
call   10c0 <wait@plt>
jmp    13db <main+0x22b>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0xc(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x98,%rsp
movl   $0xffffffff,0xc(%rsp)
ff
movq   $0x0,(%rsp)
xorps  %xmm0,%xmm0
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 %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
mov    %rsp,%rdi
call   1090 <pipe@plt>
cmp    $0xffffffff,%eax
je     1334 <main+0x174>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     1269 <main+0xa9>
cmp    $0xffffffff,%eax
je     133d <main+0x17d>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     12ce <main+0x10e>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     133d <main+0x17d>
test   %ebx,%ebx
jle    1357 <main+0x197>
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   10d0 <wait@plt>
mov    %r14,%rdi
call   10d0 <wait@plt>
jmp    1357 <main+0x197>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xda5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rsp),%edi
call   1080 <close@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
mov    0x4(%rsp),%edi
lea    0xd83(%rip),%rsi
mov    $0x9,%edx
call   1040 <write@plt>
mov    %eax,%ebx
lea    0xdb3(%rip),%rdi
call   1030 <puts@plt>
cmp    $0xffffffff,%ebx
jne    1357 <main+0x197>
lea    0xd64(%rip),%rdi
jmp    134d <main+0x18d>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd40(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x4(%rsp),%edi
call   1080 <close@plt>
mov    $0x5,%edi
call   10c0 <sleep@plt>
mov    (%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x80,%edx
call   10a0 <read@plt>
mov    %eax,%ebx
lea    0xd4b(%rip),%rdi
call   1030 <puts@plt>
cmp    $0xffffffff,%ebx
je     1346 <main+0x186>
lea    0xd29(%rip),%rdi
lea    0x10(%rsp),%rdx
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1357 <main+0x197>
lea    0xcc5(%rip),%rdi
jmp    134d <main+0x18d>
lea    0xcca(%rip),%rdi
jmp    134d <main+0x18d>
lea    0xcf2(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%ebx
mov    (%rsp),%edi
call   1080 <close@plt>
mov    0x4(%rsp),%edi
call   1080 <close@plt>
mov    %ebx,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"pipe failed: \00"
@.str.1 = [14 x i8] c"fork failed: \00"
@.str.2 = [11 x i8] c"child: %d\0A\00"
@.str.3 = [9 x i8] c"howaylee\00"
@.str.4 = [14 x i8] c"write finish\0A\00"
@.str.5 = [15 x i8] c"write failed: \00"
@.str.6 = [13 x i8] c"read finish\0A\00"
@.str.7 = [14 x i8] c"read failed: \00"
@.str.8 = [17 x i8] c"ret=%d buf = %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2 x i32]
%6 = alloca [128 x i8]
%7 = alloca i32
store i32 0, i32* %1
store i32 -1, i32* %2
store i32 -1, i32* %3
store i32 -1, i32* %4
%8 = bitcast [2 x i32]* %5 to i8*
%9 = bitcast [128 x i8]* %6 to i8*
store i32 0, i32* %7
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp eq i32 -1, %12
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %87
16: ; preds = %0
store i32 0, i32* %7
br label %17
17: ; preds = %32, %16
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 2
br i1 %19, label %20, label %35
20: ; preds = %17
%21 = call i32 (...) @fork()
store i32 %21, i32* %2
%22 = load i32, i32* %2
store i32 %22, i32* %3
%23 = icmp eq i32 -1, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %87
26: ; preds = %20
%27 = load i32, i32* %2
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26
br label %35
30: ; preds = %26
br label %31
31: ; preds = %30
br label %32
32: ; preds = %31
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
br label %17
35: ; preds = %29, %17
%36 = load i32, i32* %2
%37 = icmp eq i32 0, %36
br i1 %37, label %38, label %80
38: ; preds = %35
%39 = call i32 (...) @getpid()
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %7
%42 = icmp eq i32 0, %41
br i1 %42, label %43, label %58
43: ; preds = %38
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%48 = call i32 @memset(i8* %47, i32 0, i32 128)
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @write(i32 %50, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 9)
store i32 %51, i32* %3
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%53 = load i32, i32* %3
%54 = icmp eq i32 -1, %53
br i1 %54, label %55, label %57
55: ; preds = %43
%56 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %87
57: ; preds = %43
br label %58
58: ; preds = %57, %38
%59 = load i32, i32* %7
%60 = icmp eq i32 1, %59
br i1 %60, label %61, label %79
61: ; preds = %58
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%63 = load i32, i32* %62
%64 = call i32 @close(i32 %63)
%65 = call i32 @sleep(i32 5)
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%67 = load i32, i32* %66
%68 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%69 = call i32 @read(i32 %67, i8* %68, i32 128)
store i32 %69, i32* %3
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%71 = load i32, i32* %3
%72 = icmp eq i32 -1, %71
br i1 %72, label %73, label %75
73: ; preds = %61
%74 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %87
75: ; preds = %61
%76 = load i32, i32* %3
%77 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i32 %76, i8* %77)
br label %79
79: ; preds = %75, %58
br label %80
80: ; preds = %79, %35
%81 = load i32, i32* %2
%82 = icmp slt i32 0, %81
br i1 %82, label %83, label %86
83: ; preds = %80
%84 = call i32 @wait(i32* %4)
%85 = call i32 @wait(i32* %4)
br label %86
86: ; preds = %83, %80
br label %87
87: ; preds = %86, %73, %55, %24, %14
%88 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%89 = load i32, i32* %88
%90 = call i32 @close(i32 %89)
%91 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%92 = load i32, i32* %91
%93 = call i32 @close(i32 %92)
%94 = load i32, i32* %3
ret i32 %94
}

LLVM IR (-O2):
@.str = [14 x i8] c"pipe failed: \00"
@.str.1 = [14 x i8] c"fork failed: \00"
@.str.2 = [11 x i8] c"child: %d\0A\00"
@.str.3 = [9 x i8] c"howaylee\00"
@.str.5 = [15 x i8] c"write failed: \00"
@.str.7 = [14 x i8] c"read failed: \00"
@.str.8 = [17 x i8] c"ret=%d buf = %s\0A\00"
@str = [12 x i8] c"read finish\00"
@str.9 = [13 x i8] c"write finish\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = bitcast i64* %2 to [2 x i32]*
%4 = alloca [128 x i8]
%5 = bitcast i32* %1 to i8*
store i32 -1, i32* %1
%6 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%8 = bitcast i64* %2 to i32*
%9 = call i32 @pipe(i32* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %13, label %11
11: ; preds = %0
%12 = call i32 (...) @fork()
switch i32 %12, label %17 [
i32 -1, label %15
i32 0, label %21
]
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %52
15: ; preds = %17, %11
%16 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %52
17: ; preds = %11
%18 = call i32 (...) @fork()
switch i32 %18, label %19 [
i32 -1, label %15
i32 0, label %34
]
19: ; preds = %17
%20 = icmp sgt i32 %18, 0
br i1 %20, label %49, label %52
21: ; preds = %11
%22 = call i32 (...) @getpid()
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %8
%25 = call i32 @close(i32 %24)
%26 = call i32 @memset(i8* %7, i32 0, i32 128)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @write(i32 %28, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 9)
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.9, i64 0, i64 0))
%31 = icmp eq i32 %29, -1
br i1 %31, label %32, label %52
32: ; preds = %21
%33 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %52
34: ; preds = %17
%35 = call i32 (...) @getpid()
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = call i32 @sleep(i32 5)
%41 = load i32, i32* %8
%42 = call i32 @read(i32 %41, i8* %7, i32 128)
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%44 = icmp eq i32 %42, -1
br i1 %44, label %45, label %47
45: ; preds = %34
%46 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %52
47: ; preds = %34
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i32 %42, i8* %7)
br label %52
49: ; preds = %19
%50 = call i32 @wait(i32* %1)
%51 = call i32 @wait(i32* %1)
br label %52
52: ; preds = %21, %47, %19, %49, %45, %32, %15, %13
%53 = phi i32 [ -1, %13 ], [ -1, %15 ], [ -1, %32 ], [ -1, %45 ], [ %18, %49 ], [ %18, %19 ], [ %29, %21 ], [ %42, %47 ]
%54 = load i32, i32* %8
%55 = call i32 @close(i32 %54)
%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)
ret i32 %53
}