#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 /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
scalar_t__ strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main() {
  int parent[2];
  int child[2];
  if (pipe(parent) == -1) {
    printf("parent pipe broken...\n");
  }
  if (pipe(child) == -1) {
    printf("child pipe broken...\n");
  }
  int pid = fork();
  if (pid < 0) {
    perror("fail: ");
  }
  if (pid == 0) {
    char buffer[20];
    close(parent[1]);
    read(parent[0], buffer, 20);
    printf("child process receive message from parent: %s\n", buffer);
    close(parent[0]);
    close(child[0]);
    char sendbuf[] = "Hi, daddy!";
    write(child[1], sendbuf, strlen(sendbuf) + 1);
    close(child[1]);
    exit(0);
  } else {
    char sendbuf[] = "Hi, my baby.";
    close(parent[0]);
    write(parent[1], sendbuf, strlen(sendbuf) + 1);
    wait(NULL);
    close(child[1]);
    char recvbuf[20];
    read(child[0], recvbuf, 20);
    printf("parent process receive message from child: %s\n", recvbuf);
    close(child[0]);
    close(parent[1]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11d2 <main+0x32>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x14(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11f2 <main+0x52>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1212 <main+0x72>
lea    0xe20(%rip),%rdi
call   1090 <perror@plt>
cmpl   $0x0,-0x18(%rbp)
jne    12af <main+0x10f>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x14,%edx
call   1080 <read@plt>
lea    -0x30(%rbp),%rsi
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0xe05(%rip),%rax
mov    %rax,-0x3b(%rbp)
mov    0xe02(%rip),%ax
mov    %ax,-0x33(%rbp)
mov    0xdfa(%rip),%al
mov    %al,-0x31(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x6c(%rbp)
lea    -0x3b(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    -0x3b(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0xdb8(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xdb6(%rip),%eax
mov    %eax,-0x40(%rbp)
mov    0xdb1(%rip),%al
mov    %al,-0x3c(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x7c(%rbp)
lea    -0x48(%rbp),%rax
mov    %rax,-0x78(%rbp)
lea    -0x48(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x14,%edx
call   1080 <read@plt>
lea    -0x60(%rbp),%rsi
lea    0xd50(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x38,%rsp
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11d2 <main+0x22>
lea    0xec5(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11eb <main+0x3b>
lea    0xe97(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
js     1297 <main+0xe7>
je     12a8 <main+0xf8>
movabs $0x2e796261622079,%rax
mov    %rax,0x15(%rsp)
movabs $0x6220796d202c6948,%rax
79 20 62
mov    %rax,0x10(%rsp)
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <write@plt>
xor    %edi,%edi
call   10c0 <wait@plt>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
mov    (%rsp),%edi
lea    0x20(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x14,%edx
call   1090 <read@plt>
lea    0xddc(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd62(%rip),%rdi
call   10a0 <perror@plt>
jmp    1200 <main+0x50>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
lea    0x20(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x14,%edx
call   1090 <read@plt>
lea    0xd39(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    (%rsp),%edi
call   1070 <close@plt>
movabs $0x64646164202c6948,%rax
61 64 64
mov    %rax,0x10(%rsp)
movl   $0x217964,0x17(%rsp)
mov    0x4(%rsp),%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <write@plt>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"parent pipe broken...\0A\00"
@.str.1 = [22 x i8] c"child pipe broken...\0A\00"
@.str.2 = [7 x i8] c"fail: \00"
@.str.3 = [47 x i8] c"child process receive message from parent: %s\0A\00"
@__const.main.sendbuf = [11 x i8] c"Hi, daddy!\00"
@__const.main.sendbuf.4 = [13 x i8] c"Hi, my baby.\00"
@.str.5 = [47 x i8] c"parent process receive message from child: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca [20 x i8]
%6 = alloca [11 x i8]
%7 = alloca [13 x i8]
%8 = alloca [20 x i8]
store i32 0, i32* %1
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %0
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = call i32 (...) @fork()
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %57
29: ; preds = %26
%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 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%34 = load i32, i32* %33
%35 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 20)
%37 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = bitcast [11 x i8]* %6 to i8*
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%47 = load i32, i32* %46
%48 = getelementptr inbounds [11 x i8], [11 x i8]* %6, i64 0, i64 0
%49 = getelementptr inbounds [11 x i8], [11 x i8]* %6, i64 0, i64 0
%50 = call i64 @strlen(i8* %49)
%51 = add nsw i64 %50, 1
%52 = call i32 @write(i32 %47, i8* %48, i64 %51)
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
%56 = call i32 @exit(i32 0)
unreachable
57: ; preds = %26
%58 = bitcast [13 x i8]* %7 to i8*
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%60 = load i32, i32* %59
%61 = call i32 @close(i32 %60)
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%63 = load i32, i32* %62
%64 = getelementptr inbounds [13 x i8], [13 x i8]* %7, i64 0, i64 0
%65 = getelementptr inbounds [13 x i8], [13 x i8]* %7, i64 0, i64 0
%66 = call i64 @strlen(i8* %65)
%67 = add nsw i64 %66, 1
%68 = call i32 @write(i32 %63, i8* %64, i64 %67)
%69 = call i32 @wait(i32* null)
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%71 = load i32, i32* %70
%72 = call i32 @close(i32 %71)
%73 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%74 = load i32, i32* %73
%75 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%76 = call i32 @read(i32 %74, i8* %75, i32 20)
%77 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i8* %77)
%79 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%80 = load i32, i32* %79
%81 = call i32 @close(i32 %80)
%82 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%83 = load i32, i32* %82
%84 = call i32 @close(i32 %83)
br label %85
85: ; preds = %57
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [7 x i8] c"fail: \00"
@.str.3 = [47 x i8] c"child process receive message from parent: %s\0A\00"
@__const.main.sendbuf = [11 x i8] c"Hi, daddy!\00"
@__const.main.sendbuf.4 = [13 x i8] c"Hi, my baby.\00"
@.str.5 = [47 x i8] c"parent process receive message from child: %s\0A\00"
@str = [21 x i8] c"child pipe broken...\00"
@str.6 = [22 x i8] c"parent pipe broken...\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [2 x i32]
%3 = alloca [20 x i8]
%4 = alloca [11 x i8]
%5 = alloca [13 x i8]
%6 = alloca [20 x i8]
%7 = bitcast [2 x i32]* %1 to i8*
%8 = bitcast [2 x i32]* %2 to i8*
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.6, i64 0, i64 0))
br label %14
14: ; preds = %12, %0
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = call i32 (...) @fork()
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %48
25: ; preds = %20
%26 = icmp eq i32 %21, 0
br i1 %26, label %27, label %48
27: ; preds = %25
%28 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %9
%33 = call i32 @read(i32 %32, i8* %28, i32 20)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %28)
%35 = load i32, i32* %9
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* %15
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [11 x i8], [11 x i8]* %4, i64 0, i64 0
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i64 @strlen(i8* %39)
%43 = add nsw i64 %42, 1
%44 = call i32 @write(i32 %41, i8* %39, i64 %43)
%45 = load i32, i32* %40
%46 = call i32 @close(i32 %45)
%47 = call i32 @exit(i32 0)
unreachable
48: ; preds = %23, %25
%49 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%50 = load i32, i32* %9
%51 = call i32 @close(i32 %50)
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%53 = load i32, i32* %52
%54 = call i64 @strlen(i8* %49)
%55 = add nsw i64 %54, 1
%56 = call i32 @write(i32 %53, i8* %49, i64 %55)
%57 = call i32 @wait(i32* null)
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
%61 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%62 = load i32, i32* %15
%63 = call i32 @read(i32 %62, i8* %61, i32 20)
%64 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i8* %61)
%65 = load i32, i32* %15
%66 = call i32 @close(i32 %65)
%67 = load i32, i32* %52
%68 = call i32 @close(i32 %67)
ret i32 0
}