#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 */
struct stat {
  int st_size;
  int /*<<< orphan*/ st_mode;
  int /*<<< orphan*/ member_0;
};
typedef int pid_t;

/* Variables and functions */
int S_ISFIFO(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_sys(char *);
int fork();
scalar_t__ fstat(int, struct stat *);
int getpid();
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int ret = 0;
  char buf[2] = {0};
  int fd[2] = {0};
  if (pipe(fd) < 0)
    err_sys("pipe");
  printf("parent %lu start\n", getpid());
  struct stat fs = {0};
  if (fstat(fd[0], &fs) < 0)
    err_sys("fstat");
  printf("fd[0] st_size: %d\n", fs.st_size);
  printf("S_ISFIFO(fd[0]): %d\n", S_ISFIFO(fs.st_mode));
  if (fstat(fd[1], &fs) < 0)
    err_sys("fstat");
  printf("fd[1] st_size: %d\n", fs.st_size);
  printf("S_ISFIFO(fd[1]): %d\n", S_ISFIFO(fs.st_mode));
  pid_t pid = fork();
  if (pid == 0) {
    close(fd[1]);
    ret = read(fd[0], buf, 1);
    printf("child read %c\n", buf[0]);
    ret = read(fd[0], buf, 1);
    printf("child read %c\n", buf[0]);
  } else if (pid > 0) {
    printf("start child %lu\n", pid);
    close(fd[0]);
    ret = write(fd[1], "bB", 2);
    printf("parent write %d with b\n", ret);
  } else
    err_sys("fork");
  sleep(20);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x16(%rbp),%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1080 <memset@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
lea    -0x20(%rbp),%rdi
call   10a0 <pipe@plt>
cmp    $0x0,%rax
jge    121c <main+0x5c>
lea    0xde9(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xdd9(%rip),%rdi
call   1070 <printf@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10d0 <fstat@plt>
cmp    $0x0,%rax
jge    1263 <main+0xa3>
lea    0xdb9(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x30(%rbp),%esi
lea    0xdb0(%rip),%rdi
call   1070 <printf@plt>
mov    -0x2c(%rbp),%edi
call   1060 <S_ISFIFO@plt>
mov    %eax,%esi
lea    0xdad(%rip),%rdi
call   1070 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10d0 <fstat@plt>
cmp    $0x0,%rax
jge    12aa <main+0xea>
lea    0xd72(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x30(%rbp),%esi
lea    0xd91(%rip),%rdi
call   1070 <printf@plt>
mov    -0x2c(%rbp),%edi
call   1060 <S_ISFIFO@plt>
mov    %eax,%esi
lea    0xd8e(%rip),%rdi
call   1070 <printf@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    1338 <main+0x178>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
mov    -0x20(%rbp),%edi
lea    -0x16(%rbp),%rsi
mov    $0x1,%edx
call   10b0 <read@plt>
mov    %eax,-0x14(%rbp)
movsbl -0x16(%rbp),%esi
lea    0xd63(%rip),%rdi
call   1070 <printf@plt>
mov    -0x20(%rbp),%edi
lea    -0x16(%rbp),%rsi
mov    $0x1,%edx
call   10b0 <read@plt>
mov    %eax,-0x14(%rbp)
movsbl -0x16(%rbp),%esi
lea    0xd3f(%rip),%rdi
call   1070 <printf@plt>
jmp    1395 <main+0x1d5>
cmpl   $0x0,-0x34(%rbp)
jle    1384 <main+0x1c4>
mov    -0x34(%rbp),%esi
lea    0xd30(%rip),%rdi
call   1070 <printf@plt>
mov    -0x20(%rbp),%edi
call   1090 <close@plt>
mov    -0x1c(%rbp),%edi
lea    0xd2a(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xd16(%rip),%rdi
call   1070 <printf@plt>
jmp    1390 <main+0x1d0>
lea    0xd1d(%rip),%rdi
call   1030 <err_sys@plt>
jmp    1395 <main+0x1d5>
mov    $0x14,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movw   $0x0,0xe(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x10(%rsp),%rdi
call   1090 <pipe@plt>
test   %rax,%rax
jns    11e2 <main+0x32>
lea    0xe23(%rip),%rdi
call   1030 <err_sys@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe15(%rip),%rdi
mov    %eax,%esi
call   1070 <printf@plt>
movl   $0x0,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x10(%rsp),%edi
lea    0x18(%rsp),%rsi
call   10c0 <fstat@plt>
test   %rax,%rax
jns    1227 <main+0x77>
lea    0xdf5(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x18(%rsp),%esi
lea    0xdeb(%rip),%rdi
call   1070 <printf@plt>
mov    0x1c(%rsp),%edi
call   1060 <S_ISFIFO@plt>
lea    0xde9(%rip),%rdi
mov    %eax,%esi
call   1070 <printf@plt>
mov    0x14(%rsp),%edi
lea    0x18(%rsp),%rsi
call   10c0 <fstat@plt>
test   %rax,%rax
jns    126d <main+0xbd>
lea    0xdaf(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x18(%rsp),%esi
lea    0xdcd(%rip),%rdi
call   1070 <printf@plt>
mov    0x1c(%rsp),%edi
call   1060 <S_ISFIFO@plt>
lea    0xdcb(%rip),%rdi
mov    %eax,%esi
call   1070 <printf@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     12e1 <main+0x131>
jle    1334 <main+0x184>
lea    0xdd0(%rip),%rdi
mov    %eax,%esi
call   1070 <printf@plt>
mov    0x10(%rsp),%edi
call   1080 <close@plt>
mov    0x14(%rsp),%edi
lea    0xdc6(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
lea    0xdb8(%rip),%rdi
mov    %eax,%esi
call   1070 <printf@plt>
jmp    1340 <main+0x190>
mov    0x14(%rsp),%edi
call   1080 <close@plt>
mov    0x10(%rsp),%edi
lea    0xe(%rsp),%r14
mov    %r14,%rsi
mov    $0x1,%edx
call   10a0 <read@plt>
movsbl 0xe(%rsp),%esi
lea    0xd61(%rip),%rbx
mov    %rbx,%rdi
call   1070 <printf@plt>
mov    0x10(%rsp),%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   10a0 <read@plt>
movsbl 0xe(%rsp),%esi
mov    %rbx,%rdi
call   1070 <printf@plt>
jmp    1340 <main+0x190>
lea    0xd6d(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0x14,%edi
call   10b0 <sleep@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.stat = type { i32, i32, i32 }
@.str = [5 x i8] c"pipe\00"
@.str.1 = [18 x i8] c"parent %lu start\0A\00"
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [19 x i8] c"fd[0] st_size: %d\0A\00"
@.str.4 = [21 x i8] c"S_ISFIFO(fd[0]): %d\0A\00"
@.str.5 = [19 x i8] c"fd[1] st_size: %d\0A\00"
@.str.6 = [21 x i8] c"S_ISFIFO(fd[1]): %d\0A\00"
@.str.7 = [15 x i8] c"child read %c\0A\00"
@.str.8 = [17 x i8] c"start child %lu\0A\00"
@.str.9 = [3 x i8] c"bB\00"
@.str.10 = [24 x i8] c"parent write %d with b\0A\00"
@.str.11 = [5 x i8] c"fork\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i16
%4 = alloca i64
%5 = bitcast i64* %4 to [2 x i32]*
%6 = alloca %struct.stat
%7 = bitcast i16* %3 to i8*
store i16 0, i16* %3
%8 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%9 = bitcast i64* %4 to i32*
%10 = call i64 @pipe(i32* %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @err_sys(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = call i32 (...) @getpid()
%16 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = bitcast %struct.stat* %6 to i8*
%18 = load i32, i32* %9
%19 = call i64 @fstat(i32 %18, %struct.stat* %6)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 @err_sys(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %14
%24 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 1
%28 = load i32, i32* %27
%29 = call i32 @S_ISFIFO(i32 %28)
%30 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i64 @fstat(i32 %32, %struct.stat* %6)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %23
%36 = call i32 @err_sys(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %23
%38 = load i32, i32* %24
%39 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %27
%41 = call i32 @S_ISFIFO(i32 %40)
%42 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %41)
%43 = call i32 (...) @fork()
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %58
45: ; preds = %37
%46 = load i32, i32* %31
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %9
%49 = call i32 @read(i32 %48, i8* %7, i32 1)
%50 = load i8, i8* %7
%51 = sext i8 %50 to i32
%52 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i32 %51)
%53 = load i32, i32* %9
%54 = call i32 @read(i32 %53, i8* %7, i32 1)
%55 = load i8, i8* %7
%56 = sext i8 %55 to i32
%57 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i32 %56)
br label %69
58: ; preds = %37
%59 = icmp sgt i32 %43, 0
br i1 %59, label %60, label %67
60: ; preds = %58
%61 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i32 %43)
%62 = load i32, i32* %9
%63 = call i32 @close(i32 %62)
%64 = load i32, i32* %31
%65 = call i32 @write(i32 %64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i32 2)
%66 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0), i32 %65)
br label %69
67: ; preds = %58
%68 = call i32 @err_sys(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.11, i64 0, i64 0))
br label %69
69: ; preds = %60, %67, %45
%70 = call i32 @sleep(i32 20)
ret i32 0
}