#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;
  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");
  for (i = 1; i <= n; i++) {
    pid = fork();
    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);
      printf("Child process ended: %d \n", wpid);
    }
  }
  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)
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xebb(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <scanf@plt>
lea    0xf36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     12d7 <main+0x187>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1281 <main+0x131>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e4d(%rip),%eax
mov    %eax,0x2e4b(%rip)
mov    0x2e3d(%rip),%eax
mov    %eax,0x2e3b(%rip)
cmpl   $0x3,-0x18(%rbp)
jge    122e <main+0xde>
movl   $0x1,0x2e23(%rip)
jmp    1240 <main+0xf0>
mov    0x2e1c(%rip),%eax
add    0x2e1a(%rip),%eax
mov    %eax,0x2e0c(%rip)
mov    0x2e06(%rip),%esi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2df6(%rip),%esi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2de6(%rip),%esi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c4 <main+0x174>
cmpl   $0x0,-0x8(%rbp)
jle    12bf <main+0x16f>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x14(%rbp),%rdi
call   1060 <wait@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c4 <main+0x174>
jmp    12c9 <main+0x179>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11b5 <main+0x65>
lea    0xdff(%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    0xf30(%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    12b6 <main+0x146>
lea    0xe69(%rip),%rbx
lea    0xeb3(%rip),%rbp
lea    0x4(%rsp),%r14
lea    0xec6(%rip),%r15
xor    %r13d,%r13d
jmp    126f <main+0xff>
nopl   0x0(%rax)
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe55(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e53(%rip),%eax
mov    %eax,0x2e51(%rip)
mov    0x2e43(%rip),%esi
mov    %esi,0x2e41(%rip)
add    %eax,%esi
cmp    $0x3,%r13d
mov    $0x1,%eax
cmovb  %eax,%esi
mov    %esi,0x2e29(%rip)
lea    0xe38(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e19(%rip),%esi
lea    0xe23(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e09(%rip),%esi
lea    0xe1a(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    (%rsp),%r13d
jge    12b6 <main+0x146>
add    $0x1,%r13d
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%r12d
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %r12d,%r12d
je     11f0 <main+0x80>
jle    1269 <main+0xf9>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %rbp,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1080 <wait@plt>
mov    %r15,%rdi
mov    %eax,%esi
jmp    1262 <main+0xf2>
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 = [26 x i8] c"Child process ended: %d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %6
br label %11
11: ; preds = %51, %0
%12 = load i32, i32* %6
%13 = load i32, i32* %4
%14 = icmp sle i32 %12, %13
br i1 %14, label %15, label %54
15: ; preds = %11
%16 = call i32 (...) @fork()
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %17)
%19 = load i32, i32* %2
%20 = icmp ne i32 %19, 0
br i1 %20, label %40, label %21
21: ; preds = %15
%22 = call i32 (...) @getpid()
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %22)
%24 = load i32, i32* @main.pfib
store i32 %24, i32* @main.ppfib
%25 = load i32, i32* @main.fib
store i32 %25, i32* @main.pfib
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 3
br i1 %27, label %28, label %29
28: ; preds = %21
store i32 1, i32* @main.fib
br label %33
29: ; preds = %21
%30 = load i32, i32* @main.pfib
%31 = load i32, i32* @main.ppfib
%32 = add nsw i32 %30, %31
store i32 %32, i32* @main.fib
br label %33
33: ; preds = %29, %28
%34 = load i32, i32* @main.fib
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %34)
%36 = load i32, i32* @main.pfib
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i32 %36)
%38 = load i32, i32* @main.ppfib
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i32 %38)
br label %50
40: ; preds = %15
%41 = load i32, i32* %2
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %40
%44 = call i32 (...) @getpid()
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0), i32 %44)
%46 = call i32 @wait(i32* %5)
store i32 %46, i32* %3
%47 = load i32, i32* %3
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.10, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %43, %40
br label %50
50: ; preds = %49, %33
br label %51
51: ; preds = %50
%52 = load i32, i32* %6
%53 = add nsw i32 %52, 1
store i32 %53, i32* %6
br label %11
54: ; preds = %11
%55 = 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 = [26 x i8] c"Child process ended: %d \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 slt i32 %9, 1
br i1 %10, label %40, label %11
11: ; preds = %0, %36
%12 = phi i32 [ %37, %36 ], [ 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 %36
29: ; preds = %11
%30 = icmp sgt i32 %13, 0
br i1 %30, label %31, label %36
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 ([26 x i8], [26 x i8]* @.str.10, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %16, %31, %29
%37 = add nuw nsw i32 %12, 1
%38 = load i32, i32* %1
%39 = icmp slt i32 %12, %38
br i1 %39, label %11, label %40
40: ; preds = %36, %0
%41 = call i32 @putchar(i32 10)
ret i32 0
}