#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 */
scalar_t__ fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
void main(int args, char *argv[]) {
  int hp[2], ph[2];
  char pa_buf[2], ch_buf[80];
  pipe(hp);
  pipe(ph);
  pid_t pid = fork();
  if (pid == 0) {
    int n_rec = 0;
    while (n_rec < 10) {
      int by_re = read(ph[0], ch_buf, 80);
      ch_buf[by_re] = '\0';
      printf("Child:%d Bytes recieved from parent.\n", by_re);
      printf("Child:Msg:\t%s\n", ch_buf);
      printf("Child:\tRemaining:%i\n", 10 - n_rec);
      if (n_rec < 9)
        write(hp[1], "l", 1);
      else
        write(hp[1], "q", 1);
      n_rec++;
    }
  } else {
    int exit = 0;
    while (!exit) {
      char input[80];
      scanf("%s", input);
      write(ph[1], input, strlen(input));
      printf("Parent:Message: <<%s>> sent to child\n", input);
      printf("Parent:Waiting for confirmation\n");
      read(hp[0], pa_buf, 2);
      printf("Parent:confirmation recieved from child %s\n", pa_buf);
      if (pa_buf[0] == 'q')
        exit = 1;
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x88(%rbp)
jne    127c <main+0x10c>
movl   $0x0,-0x8c(%rbp)
cmpl   $0xa,-0x8c(%rbp)
jge    1277 <main+0x107>
mov    -0x20(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x50,%edx
call   1070 <read@plt>
mov    %eax,-0x90(%rbp)
movslq -0x90(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
mov    -0x90(%rbp),%esi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x80(%rbp),%rsi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xa,%esi
sub    -0x8c(%rbp),%esi
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x9,-0x8c(%rbp)
jge    124f <main+0xdf>
mov    -0x14(%rbp),%edi
lea    0xe0a(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1263 <main+0xf3>
mov    -0x14(%rbp),%edi
lea    0xdf3(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x8c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8c(%rbp)
jmp    11ba <main+0x4a>
jmp    1350 <main+0x1e0>
movl   $0x0,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    129f <main+0x12f>
jmp    134b <main+0x1db>
lea    -0xf0(%rbp),%rsi
lea    0xda1(%rip),%rdi
call   1080 <scanf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xfc(%rbp)
lea    -0xf0(%rbp),%rax
mov    %rax,-0xf8(%rbp)
lea    -0xf0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xfc(%rbp),%edi
mov    -0xf8(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    -0xf0(%rbp),%rsi
lea    0xd5a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0x2,%edx
call   1070 <read@plt>
lea    -0x22(%rbp),%rsi
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movsbl -0x22(%rbp),%eax
cmp    $0x71,%eax
jne    1346 <main+0x1d6>
movl   $0x1,-0x94(%rbp)
jmp    1286 <main+0x116>
jmp    1350 <main+0x1e0>
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
lea    0x18(%rsp),%rdi
call   1070 <pipe@plt>
lea    0x10(%rsp),%rdi
call   1070 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1248 <main+0xc8>
lea    0x20(%rsp),%rbx
lea    0xe93(%rip),%r15
lea    0xede(%rip),%r12
lea    0xe(%rsp),%rbp
lea    0xea6(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0xe67(%rip),%rdi
mov    %rbx,%rsi
call   1090 <scanf@plt>
mov    0x14(%rsp),%r14d
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12,%rdi
call   1030 <puts@plt>
mov    0x18(%rsp),%edi
mov    %rbp,%rsi
mov    $0x2,%edx
call   1080 <read@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
cmpb   $0x71,0xe(%rsp)
jne    11e0 <main+0x60>
jmp    12d8 <main+0x158>
mov    $0xa,%ebx
lea    0x20(%rsp),%r14
lea    0xda7(%rip),%r15
lea    0xdc6(%rip),%r12
lea    0xdce(%rip),%r13
lea    0xddc(%rip),%rbp
xchg   %ax,%ax
mov    0x10(%rsp),%edi
mov    %r14,%rsi
mov    $0x50,%edx
call   1080 <read@plt>
movslq %eax,%rsi
movb   $0x0,0x20(%rsp,%rsi,1)
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x1c(%rsp),%edi
cmp    $0x1,%ebx
je     12c7 <main+0x147>
mov    %rbp,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
add    $0xffffffff,%ebx
jmp    1270 <main+0xf0>
lea    0xd7e(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"Child:%d Bytes recieved from parent.\0A\00"
@.str.1 = [15 x i8] c"Child:Msg:\09%s\0A\00"
@.str.2 = [21 x i8] c"Child:\09Remaining:%i\0A\00"
@.str.3 = [2 x i8] c"l\00"
@.str.4 = [2 x i8] c"q\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [38 x i8] c"Parent:Message: <<%s>> sent to child\0A\00"
@.str.7 = [33 x i8] c"Parent:Waiting for confirmation\0A\00"
@.str.8 = [44 x i8] c"Parent:confirmation recieved from child %s\0A\00"
define void @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca [2 x i32]
%6 = alloca [2 x i32]
%7 = alloca [2 x i8]
%8 = alloca [80 x i8]
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [80 x i8]
store i32 %0, i32* %3
store i8** %1, i8*** %4
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%15 = call i32 @pipe(i32* %14)
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%17 = call i32 @pipe(i32* %16)
%18 = call i64 (...) @fork()
store i64 %18, i64* %9
%19 = load i64, i64* %9
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %54
21: ; preds = %2
store i32 0, i32* %10
br label %22
22: ; preds = %50, %21
%23 = load i32, i32* %10
%24 = icmp slt i32 %23, 10
br i1 %24, label %25, label %53
25: ; preds = %22
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%27 = load i32, i32* %26
%28 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%29 = call i32 @read(i32 %27, i8* %28, i32 80)
store i32 %29, i32* %11
%30 = load i32, i32* %11
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 %31
store i8 0, i8* %32
%33 = load i32, i32* %11
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %33)
%35 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %10
%38 = sub nsw i32 10, %37
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %10
%41 = icmp slt i32 %40, 9
br i1 %41, label %42, label %46
42: ; preds = %25
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @write(i32 %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %50
46: ; preds = %25
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%48 = load i32, i32* %47
%49 = call i32 @write(i32 %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
br label %50
50: ; preds = %46, %42
%51 = load i32, i32* %10
%52 = add nsw i32 %51, 1
store i32 %52, i32* %10
br label %22
53: ; preds = %22
br label %84
54: ; preds = %2
store i32 0, i32* %12
br label %55
55: ; preds = %82, %54
%56 = load i32, i32* %12
%57 = icmp ne i32 %56, 0
%58 = xor i1 %57, true
br i1 %58, label %59, label %83
59: ; preds = %55
%60 = getelementptr inbounds [80 x i8], [80 x i8]* %13, i64 0, i64 0
%61 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %60)
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%63 = load i32, i32* %62
%64 = getelementptr inbounds [80 x i8], [80 x i8]* %13, i64 0, i64 0
%65 = getelementptr inbounds [80 x i8], [80 x i8]* %13, i64 0, i64 0
%66 = call i32 @strlen(i8* %65)
%67 = call i32 @write(i32 %63, i8* %64, i32 %66)
%68 = getelementptr inbounds [80 x i8], [80 x i8]* %13, i64 0, i64 0
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0), i8* %68)
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0))
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%72 = load i32, i32* %71
%73 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%74 = call i32 @read(i32 %72, i8* %73, i32 2)
%75 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.8, i64 0, i64 0), i8* %75)
%77 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%78 = load i8, i8* %77
%79 = sext i8 %78 to i32
%80 = icmp eq i32 %79, 113
br i1 %80, label %81, label %82
81: ; preds = %59
store i32 1, i32* %12
br label %82
82: ; preds = %81, %59
br label %55
83: ; preds = %55
br label %84
84: ; preds = %83, %53
ret void
}

LLVM IR (-O2):
@.str = [38 x i8] c"Child:%d Bytes recieved from parent.\0A\00"
@.str.1 = [15 x i8] c"Child:Msg:\09%s\0A\00"
@.str.2 = [21 x i8] c"Child:\09Remaining:%i\0A\00"
@.str.3 = [2 x i8] c"l\00"
@.str.4 = [2 x i8] c"q\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [38 x i8] c"Parent:Message: <<%s>> sent to child\0A\00"
@.str.8 = [44 x i8] c"Parent:confirmation recieved from child %s\0A\00"
@str = [32 x i8] c"Parent:Waiting for confirmation\00"
define void @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [2 x i32]
%5 = alloca [2 x i8]
%6 = alloca [80 x i8]
%7 = alloca [80 x i8]
%8 = bitcast [2 x i32]* %3 to i8*
%9 = bitcast [2 x i32]* %4 to i8*
%10 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%15 = call i32 @pipe(i32* %14)
%16 = call i64 (...) @fork()
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %2
%19 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
br label %38
21: ; preds = %2
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
br label %23
23: ; preds = %35, %21
%24 = phi i32 [ 0, %21 ], [ %37, %35 ]
%25 = load i32, i32* %14
%26 = call i32 @read(i32 %25, i8* %11, i32 80)
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %27
store i8 0, i8* %28
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %26)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%31 = sub nuw nsw i32 10, %24
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = icmp eq i32 %24, 9
%34 = load i32, i32* %22
br i1 %33, label %50, label %35
35: ; preds = %23
%36 = call i32 @write(i32 %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%37 = add nuw nsw i32 %24, 1
br label %23
38: ; preds = %18, %38
%39 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %19)
%40 = load i32, i32* %20
%41 = call i32 @strlen(i8* %19)
%42 = call i32 @write(i32 %40, i8* %19, i32 %41)
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0), i8* %19)
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%45 = load i32, i32* %12
%46 = call i32 @read(i32 %45, i8* %10, i32 2)
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.8, i64 0, i64 0), i8* %10)
%48 = load i8, i8* %10
%49 = icmp eq i8 %48, 113
br i1 %49, label %52, label %38
50: ; preds = %23
%51 = call i32 @write(i32 %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
br label %52
52: ; preds = %38, %50
ret void
}