#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);
int fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ wait(int *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  int pfd[2];
  int pid;
  int status = 0;
  char w_cont[] = "Hello child, I'm child!";
  char r_cont[255] = {0};
  if (pipe(pfd) < 0) {
    perror("create pipe failed");
    exit(1);
  } else {
    if ((pid = fork()) < 0) {
      perror("create process failed");
    } else if (pid > 0) {
      close(pfd[1]);
      read(pfd[0], r_cont, strlen(w_cont));
      close(pfd[0]);
      wait(&status);
      printf("parent process read: %s\n", r_cont);
    } else {
      close(pfd[0]);
      write(pfd[1], w_cont, strlen(w_cont));
      close(pfd[1]);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0xe30(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe2d(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe2a(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0xff,%edx
call   1060 <memset@plt>
lea    -0xc(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0x0,%rax
jge    1226 <main+0x76>
lea    0xe01(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    124a <main+0x9a>
lea    0xdeb(%rip),%rdi
call   10a0 <perror@plt>
jmp    12ff <main+0x14f>
cmpl   $0x0,-0x10(%rbp)
jle    12b9 <main+0x109>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x13c(%rbp)
lea    -0x130(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x13c(%rbp),%edi
mov    -0x138(%rbp),%rsi
mov    %eax,%edx
call   1090 <read@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
lea    -0x14(%rbp),%rdi
call   10c0 <wait@plt>
lea    -0x130(%rbp),%rsi
lea    0xd92(%rip),%rdi
call   1050 <printf@plt>
jmp    12fa <main+0x14a>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x148(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x14c(%rbp),%edi
mov    -0x148(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
jmp    12ff <main+0x14f>
jmp    1304 <main+0x154>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x138,%rsp
movl   $0x0,0xc(%rsp)
movaps 0xe47(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movabs $0x21646c69686320,%rax
mov    %rax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x11f(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
lea    0x4(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
js     12fb <main+0x15b>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     12ed <main+0x14d>
je     12b1 <main+0x111>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0x4(%rsp),%ebx
lea    0x10(%rsp),%rdi
call   1040 <strlen@plt>
lea    0x30(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1080 <read@plt>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
lea    0xc(%rsp),%rdi
call   10b0 <wait@plt>
lea    0xd9a(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
jmp    12e0 <main+0x140>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%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>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd37(%rip),%rdi
call   1090 <perror@plt>
jmp    12e0 <main+0x140>
lea    0xd16(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@__const.main.w_cont = [24 x i8] c"Hello child, I'm child!\00"
@.str = [19 x i8] c"create pipe failed\00"
@.str.1 = [22 x i8] c"create process failed\00"
@.str.2 = [25 x i8] c"parent process read: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca [24 x i8]
%6 = alloca [255 x i8]
store i32 0, i32* %1
store i32 0, i32* %4
%7 = bitcast [24 x i8]* %5 to i8*
%8 = bitcast [255 x i8]* %6 to i8*
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%10 = call i64 @pipe(i32* %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = call i32 (...) @fork()
store i32 %16, i32* %3
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %53
20: ; preds = %15
%21 = load i32, i32* %3
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %39
23: ; preds = %20
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%28 = load i32, i32* %27
%29 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%30 = getelementptr inbounds [24 x i8], [24 x i8]* %5, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = call i32 @read(i32 %28, i8* %29, i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = call i32 @wait(i32* %4)
%37 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%38 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %37)
br label %52
39: ; preds = %20
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%44 = load i32, i32* %43
%45 = getelementptr inbounds [24 x i8], [24 x i8]* %5, i64 0, i64 0
%46 = getelementptr inbounds [24 x i8], [24 x i8]* %5, i64 0, i64 0
%47 = call i32 @strlen(i8* %46)
%48 = call i32 @write(i32 %44, i8* %45, i32 %47)
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
br label %52
52: ; preds = %39, %23
br label %53
53: ; preds = %52, %18
br label %54
54: ; preds = %53
ret i32 0
}

LLVM IR (-O2):
@__const.main.w_cont = [24 x i8] c"Hello child, I'm child!\00"
@.str = [19 x i8] c"create pipe failed\00"
@.str.1 = [22 x i8] c"create process failed\00"
@.str.2 = [25 x i8] c"parent process read: %s\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i32
%3 = alloca [24 x i8]
%4 = alloca [255 x i8]
%5 = bitcast [2 x i32]* %1 to i8*
%6 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%7 = getelementptr inbounds [24 x i8], [24 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%10 = call i64 @pipe(i32* %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = call i32 (...) @fork()
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %42
20: ; preds = %15
%21 = icmp eq i32 %16, 0
br i1 %21, label %33, label %22
22: ; preds = %20
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %9
%27 = call i32 @strlen(i8* %7)
%28 = call i32 @read(i32 %26, i8* %8, i32 %27)
%29 = load i32, i32* %9
%30 = call i32 @close(i32 %29)
%31 = call i32 @wait(i32* %2)
%32 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %42
33: ; preds = %20
%34 = load i32, i32* %9
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @strlen(i8* %7)
%39 = call i32 @write(i32 %37, i8* %7, i32 %38)
%40 = load i32, i32* %36
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %18, %33, %22
ret i32 0
}