#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 pid_t;

/* Variables and functions */
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int wait(int *);
int main() {
  pid_t pid, wpid;
  int n = 0, status = 0, i = 0;
  unsigned short proceed = 0;
  static int fib, pfib, ppfib;
  printf("Fibchild - fibonacci series \n");
  printf("Give the length of fibonacci series to be printed: ");
  scanf("%d", &n);
  printf("\n");
  i = 1;
  proceed = 1;
  while (i <= n && proceed) {
    pid = fork();
    proceed = 0;
    printf("Child process created: %d \n", pid);
    if (!pid) {
      printf("Child process executing: %d \n", getpid());
      ppfib = pfib;
      pfib = fib;
      if (i < 3) {
        fib = 1;
      } else {
        fib = pfib + ppfib;
      }
      printf("fib = %d\t", fib);
      printf("pfib = %d\t", pfib);
      printf("ppfib = %d\n", ppfib);
    } else if (pid > 0) {
      printf("Parent process executing: %d \n", getpid());
      wpid = wait(&status);
      proceed = 1;
      printf("Child process ended: %d \n\n", wpid);
    }
    i++;
  }
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movw   $0x0,-0x1a(%rbp)
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeb5(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <scanf@plt>
lea    0xf31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x18(%rbp)
movw   $0x1,-0x1a(%rbp)
mov    -0x18(%rbp),%ecx
xor    %eax,%eax
cmp    -0x10(%rbp),%ecx
mov    %al,-0x1b(%rbp)
jg     11df <main+0x8f>
movzwl -0x1a(%rbp),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x1b(%rbp)
mov    -0x1b(%rbp),%al
test   $0x1,%al
jne    11ef <main+0x9f>
jmp    130c <main+0x1bc>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x8(%rbp)
movw   $0x0,-0x1a(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jne    12b5 <main+0x165>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e19(%rip),%eax
mov    %eax,0x2e17(%rip)
mov    0x2e09(%rip),%eax
mov    %eax,0x2e07(%rip)
cmpl   $0x3,-0x18(%rbp)
jge    1262 <main+0x112>
movl   $0x1,0x2def(%rip)
jmp    1274 <main+0x124>
mov    0x2de8(%rip),%eax
add    0x2de6(%rip),%eax
mov    %eax,0x2dd8(%rip)
mov    0x2dd2(%rip),%esi
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dc2(%rip),%esi
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2db2(%rip),%esi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12fe <main+0x1ae>
cmpl   $0x0,-0x8(%rbp)
jle    12f9 <main+0x1a9>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x14(%rbp),%rdi
call   1060 <wait@plt>
mov    %eax,-0xc(%rbp)
movw   $0x1,-0x1a(%rbp)
mov    -0xc(%rbp),%esi
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12fe <main+0x1ae>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11c1 <main+0x71>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movl   $0x0,(%rsp)
movl   $0x0,0x4(%rsp)
lea    0xf31(%rip),%rdi
call   1040 <puts@plt>
lea    0xe63(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe89(%rip),%rdi
mov    %rsp,%rsi
call   1070 <scanf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
cmpl   $0x0,(%rsp)
jle    12ba <main+0x14a>
mov    $0x1,%ebx
lea    0xe64(%rip),%r13
lea    0xeae(%rip),%r14
lea    0x4(%rsp),%r15
lea    0xec1(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebp
mov    %r13,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %ebp,%ebp
je     1242 <main+0xd2>
jle    12ba <main+0x14a>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
call   1080 <wait@plt>
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x1(%rbx),%eax
cmp    (%rsp),%ebx
mov    %eax,%ebx
jl     11f0 <main+0x80>
jmp    12ba <main+0x14a>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe03(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e01(%rip),%eax
mov    %eax,0x2dff(%rip)
mov    0x2df1(%rip),%ecx
mov    %ecx,0x2def(%rip)
add    %eax,%ecx
cmp    $0x3,%ebx
mov    $0x1,%esi
cmovae %ecx,%esi
mov    %esi,0x2dd8(%rip)
lea    0xde7(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2dc8(%rip),%esi
lea    0xdd2(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2db8(%rip),%esi
lea    0xdc9(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@main.fib = internal global i32 0
@main.pfib = internal global i32 0
@main.ppfib = internal global i32 0
@.str = [30 x i8] c"Fibchild - fibonacci series \0A\00"
@.str.1 = [52 x i8] c"Give the length of fibonacci series to be printed: \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [28 x i8] c"Child process created: %d \0A\00"
@.str.5 = [30 x i8] c"Child process executing: %d \0A\00"
@.str.6 = [10 x i8] c"fib = %d\09\00"
@.str.7 = [11 x i8] c"pfib = %d\09\00"
@.str.8 = [12 x i8] c"ppfib = %d\0A\00"
@.str.9 = [31 x i8] c"Parent process executing: %d \0A\00"
@.str.10 = [27 x i8] c"Child process ended: %d \0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i16
store i32 0, i32* %1
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
store i16 0, i16* %7
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %6
store i16 1, i16* %7
br label %12
12: ; preds = %57, %0
%13 = load i32, i32* %6
%14 = load i32, i32* %4
%15 = icmp sle i32 %13, %14
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i16, i16* %7
%18 = zext i16 %17 to i32
%19 = icmp ne i32 %18, 0
br label %20
20: ; preds = %16, %12
%21 = phi i1 [ false, %12 ], [ %19, %16 ]
br i1 %21, label %22, label %60
22: ; preds = %20
%23 = call i32 (...) @fork()
store i32 %23, i32* %2
store i16 0, i16* %7
%24 = load i32, i32* %2
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %2
%27 = icmp ne i32 %26, 0
br i1 %27, label %47, label %28
28: ; preds = %22
%29 = call i32 (...) @getpid()
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @main.pfib
store i32 %31, i32* @main.ppfib
%32 = load i32, i32* @main.fib
store i32 %32, i32* @main.pfib
%33 = load i32, i32* %6
%34 = icmp slt i32 %33, 3
br i1 %34, label %35, label %36
35: ; preds = %28
store i32 1, i32* @main.fib
br label %40
36: ; preds = %28
%37 = load i32, i32* @main.pfib
%38 = load i32, i32* @main.ppfib
%39 = add nsw i32 %37, %38
store i32 %39, i32* @main.fib
br label %40
40: ; preds = %36, %35
%41 = load i32, i32* @main.fib
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %41)
%43 = load i32, i32* @main.pfib
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i32 %43)
%45 = load i32, i32* @main.ppfib
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i32 %45)
br label %57
47: ; preds = %22
%48 = load i32, i32* %2
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %47
%51 = call i32 (...) @getpid()
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0), i32 %51)
%53 = call i32 @wait(i32* %5)
store i32 %53, i32* %3
store i16 1, i16* %7
%54 = load i32, i32* %3
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %50, %47
br label %57
57: ; preds = %56, %40
%58 = load i32, i32* %6
%59 = add nsw i32 %58, 1
store i32 %59, i32* %6
br label %12
60: ; preds = %20
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@main.fib = internal unnamed_addr global i32 0
@main.pfib = internal unnamed_addr global i32 0
@main.ppfib = internal unnamed_addr global i32 0
@.str.1 = [52 x i8] c"Give the length of fibonacci series to be printed: \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.4 = [28 x i8] c"Child process created: %d \0A\00"
@.str.5 = [30 x i8] c"Child process executing: %d \0A\00"
@.str.6 = [10 x i8] c"fib = %d\09\00"
@.str.7 = [11 x i8] c"pfib = %d\09\00"
@.str.8 = [12 x i8] c"ppfib = %d\0A\00"
@.str.9 = [31 x i8] c"Parent process executing: %d \0A\00"
@.str.10 = [27 x i8] c"Child process ended: %d \0A\0A\00"
@str = [29 x i8] c"Fibchild - fibonacci series \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%4 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%8 = call i32 @putchar(i32 10)
%9 = load i32, i32* %1
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %39
11: ; preds = %0, %31
%12 = phi i32 [ %36, %31 ], [ 1, %0 ]
%13 = call i32 (...) @fork()
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %13, 0
br i1 %15, label %16, label %29
16: ; preds = %11
%17 = call i32 (...) @getpid()
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %17)
%19 = load i32, i32* @main.pfib
store i32 %19, i32* @main.ppfib
%20 = load i32, i32* @main.fib
store i32 %20, i32* @main.pfib
%21 = icmp ult i32 %12, 3
%22 = add nsw i32 %20, %19
%23 = select i1 %21, i32 1, i32 %22
store i32 %23, i32* @main.fib
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @main.pfib
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i32 %25)
%27 = load i32, i32* @main.ppfib
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i32 %27)
br label %39
29: ; preds = %11
%30 = icmp sgt i32 %13, 0
br i1 %30, label %31, label %39
31: ; preds = %29
%32 = call i32 (...) @getpid()
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0), i32 %32)
%34 = call i32 @wait(i32* %2)
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0), i32 %34)
%36 = add nuw nsw i32 %12, 1
%37 = load i32, i32* %1
%38 = icmp slt i32 %12, %37
br i1 %38, label %11, label %39
39: ; preds = %29, %31, %16, %0
%40 = call i32 @putchar(i32 10)
ret i32 0
}