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

/* Variables and functions */
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd[2] = {};
  int ret = 0;
  ret = pipe(fd);
  if (ret < 0) {
    perror("pipe");
  }
  pid_t pid1, pid2;
  pid1 = fork();
  if (pid1 < 0) {
    perror("fork");
  }
  if (pid1 == 0) {
    char buf[100] = "Child process 1 is sending a message!\n";
    write(fd[1], buf, strlen(buf));
  } else {
    char read_buf1[100] = "";
    read(fd[0], read_buf1, sizeof(read_buf1));
    printf("read_buf is %s\n", read_buf1);
    pid2 = fork();
    if (pid2 < 0) {
      perror("fork");
    }
    if (pid2 == 0) {
      char buf[100] = "Child process 2 is sending a message!\n";
      write(fd[1], buf, strlen(buf));
    } else {
      char read_buf2[100] = "";
      read(fd[0], read_buf2, sizeof(read_buf2));
      printf("read_buf is %s\n", read_buf2);
    }
  }
  wait(NULL);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x210,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
movl   $0x0,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11f2 <main+0x52>
lea    0xe13(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1214 <main+0x74>
lea    0xdf6(%rip),%rdi
call   10a0 <perror@plt>
cmpq   $0x0,-0x28(%rbp)
jne    1273 <main+0xd3>
lea    -0xa0(%rbp),%rdi
lea    0xdf3(%rip),%rsi
mov    $0x64,%edx
call   1090 <memcpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1fc(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0x1f8(%rbp)
lea    -0xa0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x1fc(%rbp),%edi
mov    -0x1f8(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    136d <main+0x1cd>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x64,%edx
call   1080 <read@plt>
lea    -0x110(%rbp),%rsi
lea    0xd62(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    12cf <main+0x12f>
lea    0xd3b(%rip),%rdi
call   10a0 <perror@plt>
cmpq   $0x0,-0x30(%rbp)
jne    132e <main+0x18e>
lea    -0x180(%rbp),%rdi
lea    0xda8(%rip),%rsi
mov    $0x64,%edx
call   1090 <memcpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20c(%rbp)
lea    -0x180(%rbp),%rax
mov    %rax,-0x208(%rbp)
lea    -0x180(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x20c(%rbp),%edi
mov    -0x208(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1368 <main+0x1c8>
lea    -0x1f0(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x1f0(%rbp),%rsi
mov    $0x64,%edx
call   1080 <read@plt>
lea    -0x1f0(%rbp),%rsi
lea    0xca7(%rip),%rdi
call   1050 <printf@plt>
jmp    136d <main+0x1cd>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
xor    %eax,%eax
add    $0x210,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0xe8,%rsp
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %eax,%eax
js     137e <main+0x1fe>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     138f <main+0x20f>
je     128a <main+0x10a>
xorps  %xmm0,%xmm0
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movl   $0x0,0xe0(%rsp)
mov    0x8(%rsp),%edi
lea    0x80(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x64,%edx
call   1070 <read@plt>
lea    0xdf1(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     13a0 <main+0x220>
je     12fd <main+0x17d>
xorps  %xmm0,%xmm0
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x70(%rsp)
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x64,%edx
call   1070 <read@plt>
lea    0xd8d(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
jmp    136a <main+0x1ea>
movaps 0xddf(%rip),%xmm0
movaps %xmm0,0xd0(%rsp)
movaps 0xdc0(%rip),%xmm0
movaps %xmm0,0xc0(%rsp)
movaps 0xda1(%rip),%xmm0
movaps %xmm0,0xb0(%rsp)
movaps 0xd82(%rip),%xmm0
movaps %xmm0,0xa0(%rsp)
movaps 0xd63(%rip),%xmm0
movaps %xmm0,0x90(%rsp)
movaps 0xd44(%rip),%xmm0
movaps %xmm0,0x80(%rsp)
movl   $0x0,0xe0(%rsp)
mov    0xc(%rsp),%ebx
lea    0x80(%rsp),%r14
jmp    1356 <main+0x1d6>
movaps 0xddc(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movaps 0xdc0(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xda4(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xd88(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xd6c(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xd50(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x70(%rsp)
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>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %eax,%eax
add    $0xe8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xc7b(%rip),%rdi
call   1080 <perror@plt>
jmp    11a5 <main+0x25>
lea    0xc6f(%rip),%rdi
call   1080 <perror@plt>
jmp    11bb <main+0x3b>
lea    0xc5e(%rip),%rdi
call   1080 <perror@plt>
jmp    1237 <main+0xb7>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@__const.main.buf = [100 x i8] c"Child process 1 is sending a message!\0A\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\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\00\00\00\00\00\00\00\00\00\00"
@.str.2 = [16 x i8] c"read_buf is %s\0A\00"
@__const.main.buf.3 = [100 x i8] c"Child process 2 is sending a message!\0A\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\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\00\00\00\00\00\00\00\00\00\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca [100 x i8]
%11 = alloca [100 x i8]
%12 = alloca [100 x i8]
%13 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = bitcast [2 x i32]* %6 to i8*
store i32 0, i32* %7
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = call i64 (...) @fork()
store i64 %22, i64* %8
%23 = load i64, i64* %8
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
%28 = load i64, i64* %8
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %27
%31 = bitcast [100 x i8]* %10 to i8*
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%33 = load i32, i32* %32
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @write(i32 %33, i8* %34, i32 %36)
br label %71
38: ; preds = %27
%39 = bitcast [100 x i8]* %11 to i8*
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%41 = load i32, i32* %40
%42 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 100)
%44 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%45 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %44)
%46 = call i64 (...) @fork()
store i64 %46, i64* %9
%47 = load i64, i64* %9
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %38
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %51
51: ; preds = %49, %38
%52 = load i64, i64* %9
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %62
54: ; preds = %51
%55 = bitcast [100 x i8]* %12 to i8*
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%57 = load i32, i32* %56
%58 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%59 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%60 = call i32 @strlen(i8* %59)
%61 = call i32 @write(i32 %57, i8* %58, i32 %60)
br label %70
62: ; preds = %51
%63 = bitcast [100 x i8]* %13 to i8*
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%65 = load i32, i32* %64
%66 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%67 = call i32 @read(i32 %65, i8* %66, i32 100)
%68 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%69 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %68)
br label %70
70: ; preds = %62, %54
br label %71
71: ; preds = %70, %30
%72 = call i32 @wait(i32* null)
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@__const.main.buf = [100 x i8] c"Child process 1 is sending a message!\0A\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\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\00\00\00\00\00\00\00\00\00\00"
@.str.2 = [16 x i8] c"read_buf is %s\0A\00"
@__const.main.buf.3 = [100 x i8] c"Child process 2 is sending a message!\0A\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\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\00\00\00\00\00\00\00\00\00\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to [2 x i32]*
%5 = alloca [100 x i8]
%6 = alloca [100 x i8]
%7 = alloca [100 x i8]
%8 = alloca [100 x i8]
%9 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%10 = bitcast i64* %3 to i32*
%11 = call i32 @pipe(i32* %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = call i64 (...) @fork()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %28
20: ; preds = %15
%21 = icmp eq i64 %16, 0
br i1 %21, label %22, label %28
22: ; preds = %20
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @strlen(i8* %23)
%27 = call i32 @write(i32 %25, i8* %23, i32 %26)
br label %51
28: ; preds = %18, %20
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%30 = load i32, i32* %10
%31 = call i32 @read(i32 %30, i8* %29, i32 100)
%32 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%33 = call i64 (...) @fork()
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %45
37: ; preds = %28
%38 = icmp eq i64 %33, 0
br i1 %38, label %39, label %45
39: ; preds = %37
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @strlen(i8* %40)
%44 = call i32 @write(i32 %42, i8* %40, i32 %43)
br label %50
45: ; preds = %35, %37
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%47 = load i32, i32* %10
%48 = call i32 @read(i32 %47, i8* %46, i32 100)
%49 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %46)
br label %50
50: ; preds = %45, %39
br label %51
51: ; preds = %50, %22
%52 = call i32 @wait(i32* null)
ret i32 0
}