#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 O_NONBLOCK;
int O_RDONLY;
int O_WRONLY;
int S_IFIFO;
scalar_t__ fork();
int /*<<< orphan*/ mknod(char *, int, int /*<<< orphan*/);
int open(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ wait(int *);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  char filename[] = "fifo";
  mknod(filename, S_IFIFO | 0644, 0);
  pid_t child = fork();
  if (child > 0) {
    int fd = open(filename, O_WRONLY);
    int i;
    for (i = 0; i < 10; ++i) {
      char buffer[40] = "parent  write ";
      size_t length = strlen(buffer);
      char i_ascii = '0' + i;
      buffer[length] = i_ascii;
      buffer[length + 1] = '\n';
      buffer[length + 2] = 0;
      write(1, buffer, strlen(buffer));
      write(fd, &i_ascii, 1);
    }
    wait(&i);
  } else {
    int fd = open(filename, O_RDONLY | O_NONBLOCK);
    pid_t grandchild = fork();
    int i;
    for (i = 0; i < 10; ++i) {
      char buffer[40] = {
          0,
      };
      strcat(buffer, grandchild ? "child    " : "grandson ");
      strcat(buffer, "read ");
      size_t length = strlen(buffer);
      int bytes = read(fd, buffer + length, 1);
      buffer[length + bytes] = '\n';
      buffer[length + bytes + 1] = 0;
      write(1, buffer, strlen(buffer));
      usleep(grandchild ? 23 : 39);
    }
    wait(&i);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe38(%rip),%eax
mov    %eax,-0x9(%rbp)
mov    0xe33(%rip),%al
mov    %al,-0x5(%rbp)
lea    -0x9(%rbp),%rdi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
or     $0x1a4,%esi
xor    %edx,%edx
call   1070 <mknod@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    12c8 <main+0x118>
lea    -0x9(%rbp),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0xa,-0x20(%rbp)
jge    12ba <main+0x10a>
lea    -0x50(%rbp),%rdi
lea    0xdeb(%rip),%rsi
mov    $0x28,%edx
call   1080 <memcpy@plt>
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%eax
add    $0x30,%eax
mov    %al,-0x59(%rbp)
mov    -0x59(%rbp),%cl
mov    -0x58(%rbp),%rax
mov    %cl,-0x50(%rbp,%rax,1)
mov    -0x58(%rbp),%rax
movb   $0xa,-0x4f(%rbp,%rax,1)
mov    -0x58(%rbp),%rax
movb   $0x0,-0x4e(%rbp,%rax,1)
lea    -0x50(%rbp),%rax
mov    %rax,-0xb8(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xb8(%rbp),%rsi
mov    %eax,%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x1c(%rbp),%edi
lea    -0x59(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1220 <main+0x70>
lea    -0x20(%rbp),%rdi
call   10b0 <wait@plt>
jmp    1414 <main+0x264>
lea    -0x9(%rbp),%rdi
mov    0x2d0d(%rip),%rax
mov    (%rax),%esi
mov    0x2d0c(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x60(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x68(%rbp)
movl   $0x0,-0x6c(%rbp)
cmpl   $0xa,-0x6c(%rbp)
jge    140b <main+0x25b>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x68(%rbp),%rcx
lea    0xce8(%rip),%rsi
lea    0xcd7(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rsi
call   10a0 <strcat@plt>
lea    -0xa0(%rbp),%rdi
lea    0xcd0(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0xa0(%rbp),%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,-0xa8(%rbp)
mov    -0x60(%rbp),%edi
lea    -0xa0(%rbp),%rsi
add    -0xa8(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0xac(%rbp)
mov    -0xa8(%rbp),%rax
movslq -0xac(%rbp),%rcx
add    %rcx,%rax
movb   $0xa,-0xa0(%rbp,%rax,1)
0a
mov    -0xa8(%rbp),%rax
movslq -0xac(%rbp),%rcx
add    %rcx,%rax
movb   $0x0,-0x9f(%rbp,%rax,1)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xc0(%rbp)
lea    -0xa0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xc0(%rbp),%rsi
mov    %eax,%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x68(%rbp),%rcx
mov    $0x27,%edi
mov    $0x17,%eax
cmp    $0x0,%rcx
cmovne %eax,%edi
call   10d0 <usleep@plt>
mov    -0x6c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x6c(%rbp)
jmp    12f8 <main+0x148>
lea    -0x6c(%rbp),%rdi
call   10b0 <wait@plt>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movb   $0x0,0xc(%rsp)
movl   $0x6f666966,0x8(%rsp)
6f
mov    0x2e1e(%rip),%rax
mov    $0x1a4,%esi
or     (%rax),%esi
lea    0x8(%rsp),%rdi
xor    %edx,%edx
call   1060 <mknod@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jle    1278 <main+0xe8>
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
call   1070 <open@plt>
mov    %eax,%r15d
mov    $0xffffffff,%ebx
lea    0x10(%rsp),%rbp
lea    0x3(%rsp),%r14
nopw   0x0(%rax,%rax,1)
movaps 0xe29(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe0d(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x30(%rsp)
mov    %rbp,%rdi
call   1040 <strlen@plt>
cltq
lea    0x31(%rbx),%ecx
mov    %cl,0x3(%rsp)
mov    %cl,0x10(%rsp,%rax,1)
movw   $0xa,0x11(%rsp,%rax,1)
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
lea    0x2(%rbx),%eax
mov    %eax,0x4(%rsp)
add    $0x1,%ebx
cmp    $0x9,%ebx
jb     1200 <main+0x70>
jmp    135e <main+0x1ce>
mov    0x2d61(%rip),%rax
mov    0x2d62(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0x8(%rsp),%rdi
call   1070 <open@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   10a0 <fork@plt>
xor    %r13d,%r13d
test   %rax,%rax
sete   %r13b
lea    0xd60(%rip),%rax
lea    0xd4f(%rip),%r15
cmove  %rax,%r15
shl    $0x4,%r13d
add    $0x17,%r13d
mov    $0xffffffff,%ebp
lea    0x10(%rsp),%rbx
nopl   0x0(%rax)
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x30(%rsp)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rdi
lea    0xd1e(%rip),%rsi
call   1080 <strcat@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
movslq %eax,%r12
lea    (%rsp,%r12,1),%rsi
add    $0x10,%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cltq
add    %r12,%rax
movw   $0xa,0x10(%rsp,%rax,1)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r13d,%edi
call   10b0 <usleep@plt>
lea    0x2(%rbp),%eax
mov    %eax,0x4(%rsp)
add    $0x1,%ebp
cmp    $0x9,%ebp
jb     12d0 <main+0x140>
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.filename = [5 x i8] c"fifo\00"
@S_IFIFO = global i32 0
@O_WRONLY = global i32 0
@__const.main.buffer = [40 x i8] c"parent write \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str = [10 x i8] c"child \00"
@.str.1 = [10 x i8] c"grandson \00"
@.str.2 = [6 x i8] c"read \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [5 x i8]
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca [40 x i8]
%7 = alloca i64
%8 = alloca i8
%9 = alloca i32
%10 = alloca i64
%11 = alloca i32
%12 = alloca [40 x i8]
%13 = alloca i64
%14 = alloca i32
store i32 0, i32* %1
%15 = bitcast [5 x i8]* %2 to i8*
%16 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%17 = load i32, i32* @S_IFIFO
%18 = or i32 %17, 420
%19 = call i32 @mknod(i8* %16, i32 %18, i32 0)
%20 = call i64 (...) @fork()
store i64 %20, i64* %3
%21 = load i64, i64* %3
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %58
23: ; preds = %0
%24 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%25 = load i32, i32* @O_WRONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %4
store i32 0, i32* %5
br label %27
27: ; preds = %53, %23
%28 = load i32, i32* %5
%29 = icmp slt i32 %28, 10
br i1 %29, label %30, label %56
30: ; preds = %27
%31 = bitcast [40 x i8]* %6 to i8*
%32 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
%34 = sext i32 %33 to i64
store i64 %34, i64* %7
%35 = load i32, i32* %5
%36 = add nsw i32 48, %35
%37 = trunc i32 %36 to i8
store i8 %37, i8* %8
%38 = load i8, i8* %8
%39 = load i64, i64* %7
%40 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 %39
store i8 %38, i8* %40
%41 = load i64, i64* %7
%42 = add i64 %41, 1
%43 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 %42
store i8 10, i8* %43
%44 = load i64, i64* %7
%45 = add i64 %44, 2
%46 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 %45
store i8 0, i8* %46
%47 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 0
%48 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 0
%49 = call i32 @strlen(i8* %48)
%50 = call i32 @write(i32 1, i8* %47, i32 %49)
%51 = load i32, i32* %4
%52 = call i32 @write(i32 %51, i8* %8, i32 1)
br label %53
53: ; preds = %30
%54 = load i32, i32* %5
%55 = add nsw i32 %54, 1
store i32 %55, i32* %5
br label %27
56: ; preds = %27
%57 = call i32 @wait(i32* %5)
br label %111
58: ; preds = %0
%59 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%60 = load i32, i32* @O_RDONLY
%61 = load i32, i32* @O_NONBLOCK
%62 = or i32 %60, %61
%63 = call i32 @open(i8* %59, i32 %62)
store i32 %63, i32* %9
%64 = call i64 (...) @fork()
store i64 %64, i64* %10
store i32 0, i32* %11
br label %65
65: ; preds = %106, %58
%66 = load i32, i32* %11
%67 = icmp slt i32 %66, 10
br i1 %67, label %68, label %109
68: ; preds = %65
%69 = bitcast [40 x i8]* %12 to i8*
%70 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%71 = load i64, i64* %10
%72 = icmp ne i64 %71, 0
%73 = zext i1 %72 to i64
%74 = select i1 %72, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0)
%75 = call i32 @strcat(i8* %70, i8* %74)
%76 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%77 = call i32 @strcat(i8* %76, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%78 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%79 = call i32 @strlen(i8* %78)
%80 = sext i32 %79 to i64
store i64 %80, i64* %13
%81 = load i32, i32* %9
%82 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%83 = load i64, i64* %13
%84 = getelementptr inbounds i8, i8* %82, i64 %83
%85 = call i32 @read(i32 %81, i8* %84, i32 1)
store i32 %85, i32* %14
%86 = load i64, i64* %13
%87 = load i32, i32* %14
%88 = sext i32 %87 to i64
%89 = add i64 %86, %88
%90 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 %89
store i8 10, i8* %90
%91 = load i64, i64* %13
%92 = load i32, i32* %14
%93 = sext i32 %92 to i64
%94 = add i64 %91, %93
%95 = add i64 %94, 1
%96 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 %95
store i8 0, i8* %96
%97 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%98 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%99 = call i32 @strlen(i8* %98)
%100 = call i32 @write(i32 1, i8* %97, i32 %99)
%101 = load i64, i64* %10
%102 = icmp ne i64 %101, 0
%103 = zext i1 %102 to i64
%104 = select i1 %102, i32 23, i32 39
%105 = call i32 @usleep(i32 %104)
br label %106
106: ; preds = %68
%107 = load i32, i32* %11
%108 = add nsw i32 %107, 1
store i32 %108, i32* %11
br label %65
109: ; preds = %65
%110 = call i32 @wait(i32* %11)
br label %111
111: ; preds = %109, %56
ret i32 0
}

LLVM IR (-O2):
@__const.main.filename = [5 x i8] c"fifo\00"
@S_IFIFO = global i32 0
@O_WRONLY = global i32 0
@__const.main.buffer = [40 x i8] c"parent write \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str = [10 x i8] c"child \00"
@.str.1 = [10 x i8] c"grandson \00"
@.str.2 = [6 x i8] c"read \00"
define i32 @main() {
%1 = alloca [5 x i8]
%2 = alloca i32
%3 = alloca [40 x i8]
%4 = alloca i8
%5 = alloca i32
%6 = alloca [40 x i8]
%7 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%8 = load i32, i32* @S_IFIFO
%9 = or i32 %8, 420
%10 = call i32 @mknod(i8* %7, i32 %9, i32 0)
%11 = call i64 (...) @fork()
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %36
13: ; preds = %0
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* %7, i32 %14)
%16 = bitcast i32* %2 to i8*
%17 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
br label %18
18: ; preds = %13, %18
%19 = phi i32 [ 0, %13 ], [ %32, %18 ]
%20 = call i32 @strlen(i8* %17)
%21 = sext i32 %20 to i64
%22 = trunc i32 %19 to i8
%23 = add nuw nsw i8 %22, 48
store i8 %23, i8* %4
%24 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 %21
store i8 %23, i8* %24
%25 = add nsw i64 %21, 1
%26 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 %25
store i8 10, i8* %26
%27 = add nsw i64 %21, 2
%28 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 %27
store i8 0, i8* %28
%29 = call i32 @strlen(i8* %17)
%30 = call i32 @write(i32 1, i8* %17, i32 %29)
%31 = call i32 @write(i32 %15, i8* %4, i32 1)
%32 = add nuw nsw i32 %19, 1
store i32 %32, i32* %2
%33 = icmp ult i32 %19, 9
br i1 %33, label %18, label %34
34: ; preds = %18
%35 = call i32 @wait(i32* %2)
br label %67
36: ; preds = %0
%37 = load i32, i32* @O_RDONLY
%38 = load i32, i32* @O_NONBLOCK
%39 = or i32 %38, %37
%40 = call i32 @open(i8* %7, i32 %39)
%41 = call i64 (...) @fork()
%42 = bitcast i32* %5 to i8*
%43 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 0
%44 = icmp eq i64 %41, 0
%45 = select i1 %44, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0)
%46 = select i1 %44, i32 39, i32 23
br label %47
47: ; preds = %36, %47
%48 = phi i32 [ 0, %36 ], [ %63, %47 ]
%49 = call i32 @strcat(i8* %43, i8* %45)
%50 = call i32 @strcat(i8* %43, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%51 = call i32 @strlen(i8* %43)
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 %52
%54 = call i32 @read(i32 %40, i8* %53, i32 1)
%55 = sext i32 %54 to i64
%56 = add nsw i64 %55, %52
%57 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 %56
store i8 10, i8* %57
%58 = add nsw i64 %56, 1
%59 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 %58
store i8 0, i8* %59
%60 = call i32 @strlen(i8* %43)
%61 = call i32 @write(i32 1, i8* %43, i32 %60)
%62 = call i32 @usleep(i32 %46)
%63 = add nuw nsw i32 %48, 1
store i32 %63, i32* %5
%64 = icmp ult i32 %48, 9
br i1 %64, label %47, label %65
65: ; preds = %47
%66 = call i32 @wait(i32* %5)
br label %67
67: ; preds = %65, %34
ret i32 0
}