#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, const char *argv[]) {
  pid_t pid;
  int mypipefd[2];
  int ret;
  char buf[20];
  ret = pipe(mypipefd);
  if (ret == -1) {
    perror("Pipe error");
    exit(1);
  }
  pid = fork();
  if (pid == 0) {
    printf("Child Process\n");
    close(mypipefd[0]);
    write(mypipefd[1], "Hello there!", 12);
  } else {
    printf("Parent Process\n");
    close(mypipefd[1]);
    read(mypipefd[0], buf, 15);
    printf("buf: %s\n", buf);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11c2 <main+0x42>
lea    0xe4d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1207 <main+0x87>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
lea    0xe22(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
jmp    1240 <main+0xc0>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xf,%edx
call   1070 <read@plt>
lea    -0x40(%rbp),%rsi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
je     1220 <main+0x90>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     11ee <main+0x5e>
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0xf,%edx
call   1080 <read@plt>
lea    0xe36(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1218 <main+0x88>
lea    0xe3b(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
lea    0xdfd(%rip),%rsi
mov    $0xc,%edx
call   1040 <write@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
ret
lea    0xdd9(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"Pipe error\00"
@.str.1 = [15 x i8] c"Child Process\0A\00"
@.str.2 = [13 x i8] c"Hello there!\00"
@.str.3 = [16 x i8] c"Parent Process\0A\00"
@.str.4 = [9 x i8] c"buf: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca [2 x i32]
%8 = alloca i32
%9 = alloca [20 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = call i64 (...) @fork()
store i64 %18, i64* %6
%19 = load i64, i64* %6
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %17
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @write(i32 %27, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 12)
br label %40
29: ; preds = %17
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%35 = load i32, i32* %34
%36 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%37 = call i32 @read(i32 %35, i8* %36, i32 15)
%38 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %29, %21
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"Pipe error\00"
@.str.2 = [13 x i8] c"Hello there!\00"
@.str.4 = [9 x i8] c"buf: %s\0A\00"
@str = [15 x i8] c"Parent Process\00"
@str.5 = [14 x i8] c"Child Process\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [20 x i8]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %13
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.5, i64 0, i64 0))
%18 = load i32, i32* %7
%19 = call i32 @close(i32 %18)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @write(i32 %21, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 12)
br label %31
23: ; preds = %13
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %7
%29 = call i32 @read(i32 %28, i8* %6, i32 15)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %31
31: ; preds = %23, %16
ret i32 0
}