#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*/ sleep(int);
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 parent!";
  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[0]);
      write(pfd[1], w_cont, strlen(w_cont));
      close(pfd[1]);
      wait(&status);
    } else {
      sleep(2);
      close(pfd[1]);
      read(pfd[0], r_cont, strlen(w_cont));
      printf("child process read: %s\n", r_cont);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0xe20(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe1d(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe1a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe18(%rip),%al
mov    %al,-0x18(%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    123f <main+0x7f>
lea    0xde9(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1263 <main+0xa3>
lea    0xdd3(%rip),%rdi
call   10a0 <perror@plt>
jmp    131a <main+0x15a>
cmpl   $0x0,-0x10(%rbp)
jle    12bc <main+0xfc>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x13c(%rbp)
lea    -0x30(%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   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
lea    -0x14(%rbp),%rdi
call   10d0 <wait@plt>
jmp    1315 <main+0x155>
mov    $0x2,%edi
call   10c0 <sleep@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x14c(%rbp)
lea    -0x130(%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   1090 <read@plt>
lea    -0x130(%rbp),%rsi
lea    0xd32(%rip),%rdi
call   1050 <printf@plt>
jmp    131a <main+0x15a>
jmp    131f <main+0x15f>
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)
movups 0xe40(%rip),%xmm0
movups %xmm0,0x19(%rsp)
movaps 0xe2b(%rip),%xmm0
movaps %xmm0,0x10(%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     1309 <main+0x159>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
js     12fb <main+0x14b>
je     12ad <main+0xfd>
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>
lea    0xc(%rsp),%rdi
call   10c0 <wait@plt>
jmp    12ee <main+0x13e>
mov    $0x2,%edi
call   10b0 <sleep@plt>
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>
lea    0xd5c(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd2a(%rip),%rdi
call   1090 <perror@plt>
jmp    12ee <main+0x13e>
lea    0xd09(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
@__const.main.w_cont = [25 x i8] c"Hello child, I'm parent!\00"
@.str = [19 x i8] c"create pipe failed\00"
@.str.1 = [22 x i8] c"create process failed\00"
@.str.2 = [24 x i8] c"child process read: %s\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i32
%3 = alloca [25 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 [25 x i8], [25 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 %41
20: ; preds = %15
%21 = icmp eq i32 %16, 0
br i1 %21, label %32, label %22
22: ; preds = %20
%23 = load i32, i32* %9
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @strlen(i8* %7)
%28 = call i32 @write(i32 %26, i8* %7, i32 %27)
%29 = load i32, i32* %25
%30 = call i32 @close(i32 %29)
%31 = call i32 @wait(i32* %2)
br label %41
32: ; preds = %20
%33 = call i32 @sleep(i32 2)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* %9
%38 = call i32 @strlen(i8* %7)
%39 = call i32 @read(i32 %37, i8* %8, i32 %38)
%40 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %41
41: ; preds = %18, %32, %22
ret i32 0
}