#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*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ read(int, int *, int);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, int *, int);
int main() {
  int n, pa, ab, bp;
  if (fork() == 0) {
    pa = open("pa", O_RDONLY);
    ab = open("ab", O_WRONLY);
    do {
      read(pa, &n, sizeof(int));
      if (n % 2 == 0)
        n /= 2;
      write(ab, &n, sizeof(int));
    } while (n > 100);
    close(pa);
    close(ab);
    exit(0);
  }
  if (fork() == 0) {
    ab = open("ab", O_RDONLY);
    bp = open("bp", O_WRONLY);
    do {
      read(ab, &n, sizeof(int));
      if (n % 3 == 0)
        n /= 3;
      write(bp, &n, sizeof(int));
    } while (n > 100);
    close(ab);
    close(bp);
    exit(0);
  }
  pa = open("pa", O_WRONLY);
  bp = open("bp", O_RDONLY);
  scanf("%d", &n);
  do {
    write(pa, &n, sizeof(int));
    read(bp, &n, sizeof(int));
    printf("Father: %d\n", n);
    n -= 5;
  } while (n > 100);
  close(pa);
  close(bp);
  wait(0);
  wait(0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jne    1245 <main+0xb5>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
lea    0xe40(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
lea    0xe2b(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1213 <main+0x83>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmpl   $0x64,-0x8(%rbp)
jg     11e0 <main+0x50>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jne    12eb <main+0x15b>
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
lea    0xd9d(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x10(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
lea    0xd88(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    -0x8(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12b9 <main+0x129>
mov    -0x8(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmpl   $0x64,-0x8(%rbp)
jg     1286 <main+0xf6>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    0x2cde(%rip),%rax
mov    (%rax),%esi
lea    0xd05(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
mov    0x2cde(%rip),%rax
mov    (%rax),%esi
lea    0xcf3(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
lea    0xce7(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1070 <scanf@plt>
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    -0x8(%rbp),%esi
lea    0xcb5(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
sub    $0x5,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x64,-0x8(%rbp)
jg     132b <main+0x19b>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     1275 <main+0xe5>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     12f4 <main+0x164>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    0xe39(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r14d
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    0xe27(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
lea    0xe1c(%rip),%rdi
lea    0x4(%rsp),%rbx
mov    %rbx,%rsi
call   1070 <scanf@plt>
lea    0xe0b(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    0x4(%rsp),%esi
mov    %r15,%rdi
call   1040 <printf@plt>
mov    0x4(%rsp),%eax
lea    -0x5(%rax),%ecx
mov    %ecx,0x4(%rsp)
cmp    $0x69,%eax
jg     1210 <main+0x80>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
lea    0xd7b(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r14d
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
lea    0xd66(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r15d
lea    0x4(%rsp),%rbx
jmp    12c9 <main+0x139>
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x64,%ebp
jle    1386 <main+0x1f6>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    0x4(%rsp),%ebp
test   $0x1,%bpl
jne    12b0 <main+0x120>
mov    %ebp,%eax
shr    $0x1f,%eax
add    %ebp,%eax
sar    %eax
mov    %eax,0x4(%rsp)
mov    %eax,%ebp
jmp    12b0 <main+0x120>
mov    0x2ced(%rip),%rax
mov    (%rax),%esi
lea    0xcff(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r14d
mov    0x2cbd(%rip),%rax
mov    (%rax),%esi
lea    0xcea(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r15d
lea    0x4(%rsp),%rbx
jmp    1345 <main+0x1b5>
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x64,%ebp
jle    1386 <main+0x1f6>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
movslq 0x4(%rsp),%rbp
imul   $0xaaaaaaab,%ebp,%eax
add    $0x2aaaaaaa,%eax
cmp    $0x55555554,%eax
ja     1330 <main+0x1a0>
imul   $0x55555556,%rbp,%rbp
mov    %rbp,%rax
shr    $0x3f,%rax
shr    $0x20,%rbp
add    %eax,%ebp
mov    %ebp,0x4(%rsp)
jmp    1330 <main+0x1a0>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %r15d,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"pa\00"
@O_RDONLY = global i32 0
@.str.1 = [3 x i8] c"ab\00"
@O_WRONLY = global i32 0
@.str.2 = [3 x i8] c"bp\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [12 x i8] c"Father: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i64 (...) @fork()
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %34
8: ; preds = %0
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* @O_WRONLY
%12 = call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %11)
store i32 %12, i32* %4
br label %13
13: ; preds = %25, %8
%14 = load i32, i32* %3
%15 = call i32 @read(i32 %14, i32* %2, i32 4)
%16 = load i32, i32* %2
%17 = srem i32 %16, 2
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* %2
%21 = sdiv i32 %20, 2
store i32 %21, i32* %2
br label %22
22: ; preds = %19, %13
%23 = load i32, i32* %4
%24 = call i32 @write(i32 %23, i32* %2, i32 4)
br label %25
25: ; preds = %22
%26 = load i32, i32* %2
%27 = icmp sgt i32 %26, 100
br i1 %27, label %13, label %28
28: ; preds = %25
%29 = load i32, i32* %3
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %4
%32 = call i32 @close(i32 %31)
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %0
%35 = call i64 (...) @fork()
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %63
37: ; preds = %34
%38 = load i32, i32* @O_RDONLY
%39 = call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %38)
store i32 %39, i32* %4
%40 = load i32, i32* @O_WRONLY
%41 = call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %40)
store i32 %41, i32* %5
br label %42
42: ; preds = %54, %37
%43 = load i32, i32* %4
%44 = call i32 @read(i32 %43, i32* %2, i32 4)
%45 = load i32, i32* %2
%46 = srem i32 %45, 3
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %42
%49 = load i32, i32* %2
%50 = sdiv i32 %49, 3
store i32 %50, i32* %2
br label %51
51: ; preds = %48, %42
%52 = load i32, i32* %5
%53 = call i32 @write(i32 %52, i32* %2, i32 4)
br label %54
54: ; preds = %51
%55 = load i32, i32* %2
%56 = icmp sgt i32 %55, 100
br i1 %56, label %42, label %57
57: ; preds = %54
%58 = load i32, i32* %4
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %5
%61 = call i32 @close(i32 %60)
%62 = call i32 @exit(i32 0)
unreachable
63: ; preds = %34
%64 = load i32, i32* @O_WRONLY
%65 = call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %64)
store i32 %65, i32* %3
%66 = load i32, i32* @O_RDONLY
%67 = call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %66)
store i32 %67, i32* %5
%68 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %2)
br label %69
69: ; preds = %78, %63
%70 = load i32, i32* %3
%71 = call i32 @write(i32 %70, i32* %2, i32 4)
%72 = load i32, i32* %5
%73 = call i32 @read(i32 %72, i32* %2, i32 4)
%74 = load i32, i32* %2
%75 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %74)
%76 = load i32, i32* %2
%77 = sub nsw i32 %76, 5
store i32 %77, i32* %2
br label %78
78: ; preds = %69
%79 = load i32, i32* %2
%80 = icmp sgt i32 %79, 100
br i1 %80, label %69, label %81
81: ; preds = %78
%82 = load i32, i32* %3
%83 = call i32 @close(i32 %82)
%84 = load i32, i32* %5
%85 = call i32 @close(i32 %84)
%86 = call i32 @wait(i32 0)
%87 = call i32 @wait(i32 0)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"pa\00"
@O_RDONLY = global i32 0
@.str.1 = [3 x i8] c"ab\00"
@O_WRONLY = global i32 0
@.str.2 = [3 x i8] c"bp\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [12 x i8] c"Father: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %25
5: ; preds = %0
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @O_WRONLY
%9 = tail call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %17, %5
%11 = call i32 @read(i32 %7, i32* %1, i32 4)
%12 = load i32, i32* %1
%13 = and i32 %12, 1
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = sdiv i32 %12, 2
store i32 %16, i32* %1
br label %17
17: ; preds = %15, %10
%18 = phi i32 [ %16, %15 ], [ %12, %10 ]
%19 = call i32 @write(i32 %9, i32* %1, i32 4)
%20 = icmp sgt i32 %18, 100
br i1 %20, label %10, label %21
21: ; preds = %17
%22 = tail call i32 @close(i32 %7)
%23 = tail call i32 @close(i32 %9)
%24 = tail call i32 @exit(i32 0)
unreachable
25: ; preds = %0
%26 = tail call i64 (...) @fork()
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %48
28: ; preds = %25
%29 = load i32, i32* @O_RDONLY
%30 = tail call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @O_WRONLY
%32 = tail call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %40, %28
%34 = call i32 @read(i32 %30, i32* %1, i32 4)
%35 = load i32, i32* %1
%36 = srem i32 %35, 3
%37 = sdiv i32 %35, 3
%38 = icmp eq i32 %36, 0
br i1 %38, label %39, label %40
39: ; preds = %33
store i32 %37, i32* %1
br label %40
40: ; preds = %39, %33
%41 = phi i32 [ %37, %39 ], [ %35, %33 ]
%42 = call i32 @write(i32 %32, i32* %1, i32 4)
%43 = icmp sgt i32 %41, 100
br i1 %43, label %33, label %44
44: ; preds = %40
%45 = tail call i32 @close(i32 %30)
%46 = tail call i32 @close(i32 %32)
%47 = tail call i32 @exit(i32 0)
unreachable
48: ; preds = %25
%49 = load i32, i32* @O_WRONLY
%50 = tail call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %49)
%51 = load i32, i32* @O_RDONLY
%52 = tail call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %51)
%53 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %1)
br label %54
54: ; preds = %54, %48
%55 = call i32 @write(i32 %50, i32* %1, i32 4)
%56 = call i32 @read(i32 %52, i32* %1, i32 4)
%57 = load i32, i32* %1
%58 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %57)
%59 = load i32, i32* %1
%60 = add nsw i32 %59, -5
store i32 %60, i32* %1
%61 = icmp sgt i32 %59, 105
br i1 %61, label %54, label %62
62: ; preds = %54
%63 = call i32 @close(i32 %50)
%64 = call i32 @close(i32 %52)
%65 = call i32 @wait(i32 0)
%66 = call i32 @wait(i32 0)
ret i32 0
}