#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*/ buff;

/* Variables and functions */
int fork();
int getpid();
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int pfd[2];
  char buff[50];
  int n = 0, pid = -1;
  if (pipe(pfd) != 0) {
    printf("Error\n");
    return -1;
  } else {
    pid = fork();
    if (pid > 0) {
      printf("Parent Process %d says hello to child\n", getpid());
      write(pfd[1], "Hello World!", 12);
    } else if (pid == 0) {
      n = read(pfd[0], buff, sizeof(buff));
      buff[n] = '\0';
      printf("Child Process %d has got the message : %s\n", getpid(), buff);
    } else {
      printf("Error : In calling fork()");
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0xffffffff,-0x48(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     11aa <main+0x4a>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <main+0x100>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jle    11ee <main+0x8e>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    0xe4f(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
jmp    1254 <main+0xf4>
cmpl   $0x0,-0x48(%rbp)
jne    1235 <main+0xd5>
mov    -0xc(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x32,%edx
call   1070 <read@plt>
mov    %eax,-0x44(%rbp)
movslq -0x44(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    -0x40(%rbp),%rdx
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    124f <main+0xef>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <main+0x100>
jmp    1254 <main+0xf4>
jmp    1259 <main+0xf9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x48,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
je     1199 <main+0x29>
lea    0xeec(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    11d2 <main+0x62>
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jle    11dc <main+0x6c>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe4c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0xc(%rsp),%edi
lea    0xe5f(%rip),%rsi
mov    $0xc,%edx
call   1040 <write@plt>
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret
je     11ee <main+0x7e>
lea    0xe7a(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1192 <main+0x22>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
mov    $0x32,%edx
call   1080 <read@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe19(%rip),%rdi
mov    %eax,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11d2 <main+0x62>

LLVM IR (-O0):
@.str = [7 x i8] c"Error\0A\00"
@.str.1 = [39 x i8] c"Parent Process %d says hello to child\0A\00"
@.str.2 = [13 x i8] c"Hello World!\00"
@.str.3 = [43 x i8] c"Child Process %d has got the message : %s\0A\00"
@.str.4 = [26 x i8] c"Error : In calling fork()\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [50 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %4
store i32 -1, i32* %5
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %40
11: ; preds = %0
%12 = call i32 (...) @fork()
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %11
%16 = call i32 (...) @getpid()
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @write(i32 %19, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 12)
br label %38
21: ; preds = %11
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %35
24: ; preds = %21
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%28 = call i32 @read(i32 %26, i8* %27, i32 50)
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 %30
store i8 0, i8* %31
%32 = call i32 (...) @getpid()
%33 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %32, i8* %33)
br label %37
35: ; preds = %21
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %40
37: ; preds = %24
br label %38
38: ; preds = %37, %15
br label %39
39: ; preds = %38
store i32 0, i32* %1
br label %40
40: ; preds = %39, %35, %9
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
@.str.1 = [39 x i8] c"Parent Process %d says hello to child\0A\00"
@.str.2 = [13 x i8] c"Hello World!\00"
@.str.3 = [43 x i8] c"Child Process %d has got the message : %s\0A\00"
@.str.4 = [26 x i8] c"Error : In calling fork()\00"
@str = [6 x i8] c"Error\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [50 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %0
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %30
10: ; preds = %0
%11 = call i32 (...) @fork()
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = call i32 (...) @getpid()
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @write(i32 %17, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 12)
br label %30
19: ; preds = %10
%20 = icmp eq i32 %11, 0
br i1 %20, label %21, label %28
21: ; preds = %19
%22 = load i32, i32* %5
%23 = call i32 @read(i32 %22, i8* %4, i32 50)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 %24
store i8 0, i8* %25
%26 = call i32 (...) @getpid()
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %26, i8* %4)
br label %30
28: ; preds = %19
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %30
30: ; preds = %21, %13, %28, %8
%31 = phi i32 [ -1, %8 ], [ -1, %28 ], [ 0, %13 ], [ 0, %21 ]
ret i32 %31
}