#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 /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, char);
int rand();
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int p2c[2], c2p[2];
  pipe(p2c);
  pipe(c2p);
  char c = 'a';
  if (fork() == 0) {
    close(p2c[1]);
    close(c2p[0]);
    while (1) {
      read(p2c[0], &c, sizeof(char));
      printf("Child: %c\n", c);
      c -= 'a';
      c++;
      c = c % 26;
      c += 'a';
      write(c2p[1], &c, sizeof(char));
    }
    exit(0);
  }
  close(p2c[0]);
  close(c2p[1]);
  while (1) {
    sleep(rand() % 2);
    write(p2c[1], &c, sizeof(char));
    read(c2p[0], &c, sizeof(char));
    printf("Dad: %c\n", c);
  }
  wait(0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
lea    -0x14(%rbp),%rdi
call   1060 <pipe@plt>
movb   $0x61,-0x15(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    1233 <main+0xb3>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
lea    0xe22(%rip),%rdi
movsbl -0x15(%rbp),%esi
call   1040 <printf@plt>
movsbl -0x15(%rbp),%eax
sub    $0x61,%eax
mov    %eax,-0x24(%rbp)
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
add    $0x1,%al
mov    %al,-0x1d(%rbp)
mov    %al,-0x15(%rbp)
movsbl -0x15(%rbp),%eax
mov    $0x1a,%ecx
cltd
idiv   %ecx
mov    %dl,%al
mov    %al,-0x15(%rbp)
movsbl -0x15(%rbp),%eax
add    $0x61,%eax
mov    %eax,-0x1c(%rbp)
mov    %al,-0x15(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    11c6 <main+0x46>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1080 <sleep@plt>
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
lea    0xd89(%rip),%rdi
movsbl -0x15(%rbp),%esi
call   1040 <printf@plt>
jmp    1243 <main+0xc3>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0x18(%rsp),%rdi
call   1060 <pipe@plt>
lea    0x10(%rsp),%rdi
call   1060 <pipe@plt>
movb   $0x61,0xf(%rsp)
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     121b <main+0x9b>
mov    0x18(%rsp),%edi
call   1050 <close@plt>
mov    0x14(%rsp),%edi
call   1050 <close@plt>
lea    0xf(%rsp),%rbx
lea    0xe3f(%rip),%r14
nopl   0x0(%rax)
xor    %eax,%eax
call   10a0 <rand@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
and    $0xfffffffe,%ecx
sub    %ecx,%eax
mov    %eax,%edi
call   1080 <sleep@plt>
mov    0x1c(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    0x10(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movsbl 0xf(%rsp),%esi
mov    %r14,%rdi
call   1040 <printf@plt>
jmp    11d0 <main+0x50>
mov    0x1c(%rsp),%edi
call   1050 <close@plt>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
lea    0xf(%rsp),%r15
lea    0xdc7(%rip),%r14
nopl   0x0(%rax)
mov    0x18(%rsp),%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movsbl 0xf(%rsp),%ebx
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
lea    -0x60(%rbx),%eax
movsbl %al,%eax
imul   $0x4f,%eax,%ecx
movzwl %cx,%edx
shr    $0xb,%ecx
shr    $0xf,%edx
add    %cl,%dl
movzbl %dl,%ecx
lea    (%rcx,%rcx,4),%edx
lea    (%rdx,%rdx,4),%edx
add    %ecx,%edx
sub    %dl,%al
add    $0x61,%al
mov    %al,0xf(%rsp)
mov    0x14(%rsp),%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1240 <main+0xc0>

LLVM IR (-O0):
@.str = [11 x i8] c"Child: %c\0A\00"
@.str.1 = [9 x i8] c"Dad: %c\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [2 x i32]
%4 = alloca i8
store i32 0, i32* %1
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
store i8 97, i8* %4
%9 = call i64 (...) @fork()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %41
11: ; preds = %0
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%13 = load i32, i32* %12
%14 = call i32 @close(i32 %13)
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%16 = load i32, i32* %15
%17 = call i32 @close(i32 %16)
br label %18
18: ; preds = %11, %18
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%20 = load i32, i32* %19
%21 = call i32 @read(i32 %20, i8* %4, i32 1)
%22 = load i8, i8* %4
%23 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 signext %22)
%24 = load i8, i8* %4
%25 = sext i8 %24 to i32
%26 = sub nsw i32 %25, 97
%27 = trunc i32 %26 to i8
store i8 %27, i8* %4
%28 = load i8, i8* %4
%29 = add i8 %28, 1
store i8 %29, i8* %4
%30 = load i8, i8* %4
%31 = sext i8 %30 to i32
%32 = srem i32 %31, 26
%33 = trunc i32 %32 to i8
store i8 %33, i8* %4
%34 = load i8, i8* %4
%35 = sext i8 %34 to i32
%36 = add nsw i32 %35, 97
%37 = trunc i32 %36 to i8
store i8 %37, i8* %4
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @write(i32 %39, i8* %4, i32 1)
br label %18
41: ; preds = %0
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
br label %48
48: ; preds = %41, %48
%49 = call i32 (...) @rand()
%50 = srem i32 %49, 2
%51 = call i32 @sleep(i32 %50)
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%53 = load i32, i32* %52
%54 = call i32 @write(i32 %53, i8* %4, i32 1)
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%56 = load i32, i32* %55
%57 = call i32 @read(i32 %56, i8* %4, i32 1)
%58 = load i8, i8* %4
%59 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8 signext %58)
br label %48
}

LLVM IR (-O2):
@.str = [11 x i8] c"Child: %c\0A\00"
@.str.1 = [9 x i8] c"Dad: %c\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [2 x i32]
%3 = alloca i8
%4 = bitcast [2 x i32]* %1 to i8*
%5 = bitcast [2 x i32]* %2 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
store i8 97, i8* %3
%10 = call i64 (...) @fork()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %29
12: ; preds = %0
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%14 = load i32, i32* %13
%15 = call i32 @close(i32 %14)
%16 = load i32, i32* %8
%17 = call i32 @close(i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
br label %19
19: ; preds = %19, %12
%20 = load i32, i32* %6
%21 = call i32 @read(i32 %20, i8* %3, i32 1)
%22 = load i8, i8* %3
%23 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 signext %22)
%24 = add i8 %22, -96
%25 = srem i8 %24, 26
%26 = add nsw i8 %25, 97
store i8 %26, i8* %3
%27 = load i32, i32* %18
%28 = call i32 @write(i32 %27, i8* %3, i32 1)
br label %19
29: ; preds = %0
%30 = load i32, i32* %6
%31 = call i32 @close(i32 %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
br label %36
36: ; preds = %36, %29
%37 = call i32 (...) @rand()
%38 = srem i32 %37, 2
%39 = call i32 @sleep(i32 %38)
%40 = load i32, i32* %35
%41 = call i32 @write(i32 %40, i8* %3, i32 1)
%42 = load i32, i32* %8
%43 = call i32 @read(i32 %42, i8* %3, i32 1)
%44 = load i8, i8* %3
%45 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8 signext %44)
br label %36
}