#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*/ readmessage;
typedef int /*<<< orphan*/ pipe2writemessage;
typedef int /*<<< orphan*/ pipe1writemessage;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int fork();
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int pipefds1[2], pipefds2[2];
  int returnstatus1, returnstatus2;
  int pid;
  char pipe1writemessage[20] = "Hi";
  char pipe2writemessage[20] = "Hello";
  char readmessage[20];
  returnstatus1 = pipe(pipefds1);
  if (returnstatus1 == -1) {
    printf("Unable to create pipe 1 \n");
    return 1;
  }
  returnstatus2 = pipe(pipefds2);
  if (returnstatus2 == -1) {
    printf("Unable to create pipe 2 \n");
    return 1;
  }
  pid = fork();
  if (pid != 0) {
    close(pipefds1[0]);
    close(pipefds2[1]);
    printf("In Parent: Writing to pipe 1 – Message is %s\n", pipe1writemessage);
    write(pipefds1[1], pipe1writemessage, sizeof(pipe1writemessage));
    read(pipefds2[0], readmessage, sizeof(readmessage));
    printf("In Parent: Reading from pipe 2 – Message is %s\n", readmessage);
  } else {
    close(pipefds1[1]);
    close(pipefds2[0]);
    read(pipefds1[0], readmessage, sizeof(readmessage));
    printf("In Child: Reading from pipe 1 – Message is %s\n", readmessage);
    printf("In Child: Writing to pipe 2 – Message is %s\n", pipe2writemessage);
    write(pipefds2[1], pipe2writemessage, sizeof(pipe2writemessage));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe87(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe84(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe82(%rip),%eax
mov    %eax,-0x30(%rbp)
mov    0xe88(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    0xe85(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    0xe83(%rip),%eax
mov    %eax,-0x50(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11e0 <main+0x80>
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dc <main+0x17c>
lea    -0x14(%rbp),%rdi
call   1060 <pipe@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1210 <main+0xb0>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dc <main+0x17c>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     127f <main+0x11f>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
lea    -0x40(%rbp),%rsi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x14,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x14,%edx
call   1070 <read@plt>
lea    -0x80(%rbp),%rsi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12d5 <main+0x175>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x14,%edx
call   1070 <read@plt>
lea    -0x80(%rbp),%rsi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x60(%rbp),%rsi
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x14,%edx
call   1030 <write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x68,%rsp
movaps 0xe82(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movl   $0x0,0x40(%rsp)
movaps 0xe8e(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x20(%rsp)
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
je     1229 <main+0xb9>
mov    %rsp,%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
je     1232 <main+0xc2>
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     1245 <main+0xd5>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
lea    0xe53(%rip),%rdi
lea    0x30(%rsp),%r14
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    $0x14,%edx
call   1040 <write@plt>
mov    (%rsp),%edi
lea    0x50(%rsp),%r14
mov    %r14,%rsi
mov    $0x14,%edx
call   1080 <read@plt>
lea    0xe47(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12a6 <main+0x136>
lea    0xedf(%rip),%rdi
jmp    1239 <main+0xc9>
lea    0xebd(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    12a6 <main+0x136>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    (%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
lea    0x50(%rsp),%r14
mov    %r14,%rsi
mov    $0x14,%edx
call   1080 <read@plt>
lea    0xe23(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe41(%rip),%rdi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rsp),%edi
mov    %r14,%rsi
mov    $0x14,%edx
call   1040 <write@plt>
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.pipe1writemessage = [20 x i8] c"Hi\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.pipe2writemessage = [20 x i8] c"Hello\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [26 x i8] c"Unable to create pipe 1 \0A\00"
@.str.1 = [26 x i8] c"Unable to create pipe 2 \0A\00"
@.str.2 = [48 x i8] c"In Parent: Writing to pipe 1 \E2\80\93 Message is %s\0A\00"
@.str.3 = [50 x i8] c"In Parent: Reading from pipe 2 \E2\80\93 Message is %s\0A\00"
@.str.4 = [49 x i8] c"In Child: Reading from pipe 1 \E2\80\93 Message is %s\0A\00"
@.str.5 = [47 x i8] c"In Child: Writing to pipe 2 \E2\80\93 Message is %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [20 x i8]
%8 = alloca [20 x i8]
%9 = alloca [20 x i8]
store i32 0, i32* %1
%10 = bitcast [20 x i8]* %7 to i8*
%11 = bitcast [20 x i8]* %8 to i8*
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %68
18: ; preds = %0
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%20 = call i32 @pipe(i32* %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %68
25: ; preds = %18
%26 = call i32 (...) @fork()
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %48
29: ; preds = %25
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%39 = load i32, i32* %38
%40 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%41 = call i32 @write(i32 %39, i8* %40, i32 20)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%43 = load i32, i32* %42
%44 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%45 = call i32 @read(i32 %43, i8* %44, i32 20)
%46 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i8* %46)
br label %67
48: ; preds = %25
%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)
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%53 = load i32, i32* %52
%54 = call i32 @close(i32 %53)
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%56 = load i32, i32* %55
%57 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%58 = call i32 @read(i32 %56, i8* %57, i32 20)
%59 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i8* %59)
%61 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i8* %61)
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%64 = load i32, i32* %63
%65 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%66 = call i32 @write(i32 %64, i8* %65, i32 20)
br label %67
67: ; preds = %48, %29
store i32 0, i32* %1
br label %68
68: ; preds = %67, %23, %16
%69 = load i32, i32* %1
ret i32 %69
}

LLVM IR (-O2):
@__const.main.pipe1writemessage = [20 x i8] c"Hi\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.pipe2writemessage = [20 x i8] c"Hello\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str.2 = [48 x i8] c"In Parent: Writing to pipe 1 \E2\80\93 Message is %s\0A\00"
@.str.3 = [50 x i8] c"In Parent: Reading from pipe 2 \E2\80\93 Message is %s\0A\00"
@.str.4 = [49 x i8] c"In Child: Reading from pipe 1 \E2\80\93 Message is %s\0A\00"
@.str.5 = [47 x i8] c"In Child: Writing to pipe 2 \E2\80\93 Message is %s\0A\00"
@str = [25 x i8] c"Unable to create pipe 2 \00"
@str.6 = [25 x i8] c"Unable to create pipe 1 \00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [2 x i32]
%3 = alloca [20 x i8]
%4 = alloca [20 x i8]
%5 = alloca [20 x i8]
%6 = bitcast [2 x i32]* %1 to i8*
%7 = bitcast [2 x i32]* %2 to i8*
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.6, i64 0, i64 0))
br label %51
16: ; preds = %0
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%18 = call i32 @pipe(i32* %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %51
22: ; preds = %16
%23 = call i32 (...) @fork()
%24 = icmp eq i32 %23, 0
br i1 %24, label %38, label %25
25: ; preds = %22
%26 = load i32, i32* %11
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @write(i32 %33, i8* %8, i32 20)
%35 = load i32, i32* %17
%36 = call i32 @read(i32 %35, i8* %10, i32 20)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i8* %10)
br label %51
38: ; preds = %22
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %17
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %11
%45 = call i32 @read(i32 %44, i8* %10, i32 20)
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i8* %10)
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i8* %9)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @write(i32 %49, i8* %9, i32 20)
br label %51
51: ; preds = %25, %38, %20, %14
%52 = phi i32 [ 1, %14 ], [ 1, %20 ], [ 0, %38 ], [ 0, %25 ]
ret i32 %52
}