#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 getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
scalar_t__ wait(int *);
int main() {
  pid_t pid, wpid;
  int n = 0, status = 0, i = 0;
  unsigned short proceed = 0;
  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();
    if (!pid) {
      proceed = 0;
      printf("Child process executing: %d \t", getpid());
      int j = 0;
      unsigned long fib = 0, pre_fib = 0;
      for (j = 1; j <= i; j++) {
        if (j < 3) {
          fib = 1;
          pre_fib = 1;
        } else {
          unsigned long temp = fib;
          fib = pre_fib + fib;
          pre_fib = temp;
        }
      }
      printf("fib(%d) = %lu\n", i, fib);
    } else if (pid > 0) {
      proceed = 1;
    }
    i++;
  }
  i = n;
  while (pid > 0 && i > 0) {
    wpid = wait(&status);
    printf("Child process ended: %d with status=%d\n\n", wpid, status);
    i--;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movw   $0x0,-0x26(%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    -0x1c(%rbp),%rsi
call   1050 <scanf@plt>
lea    0xefc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x24(%rbp)
movw   $0x1,-0x26(%rbp)
mov    -0x24(%rbp),%ecx
xor    %eax,%eax
cmp    -0x1c(%rbp),%ecx
mov    %al,-0x49(%rbp)
jg     11df <main+0x8f>
movzwl -0x26(%rbp),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    11ef <main+0x9f>
jmp    12d8 <main+0x188>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12b4 <main+0x164>
movw   $0x0,-0x26(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jg     129a <main+0x14a>
cmpl   $0x3,-0x2c(%rbp)
jge    126b <main+0x11b>
movq   $0x1,-0x38(%rbp)
movq   $0x1,-0x40(%rbp)
jmp    1287 <main+0x137>
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    128c <main+0x13c>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1240 <main+0xf0>
mov    -0x24(%rbp),%esi
mov    -0x38(%rbp),%rdx
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12ca <main+0x17a>
cmpq   $0x0,-0x10(%rbp)
jle    12c5 <main+0x175>
movw   $0x1,-0x26(%rbp)
jmp    12ca <main+0x17a>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11c1 <main+0x71>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x4a(%rbp)
jle    12f8 <main+0x1a8>
cmpl   $0x0,-0x24(%rbp)
setg   %al
mov    %al,-0x4a(%rbp)
mov    -0x4a(%rbp),%al
test   $0x1,%al
jne    1308 <main+0x1b8>
jmp    1338 <main+0x1e8>
lea    -0x20(%rbp),%rdi
call   1060 <wait@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    12de <main+0x18e>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movl   $0x0,(%rsp)
movl   $0x0,0x4(%rsp)
lea    0xf00(%rip),%rdi
call   1040 <puts@plt>
lea    0xe67(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe8d(%rip),%rdi
mov    %rsp,%rsi
call   1070 <scanf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
cmpl   $0x0,(%rsp)
jle    1301 <main+0x191>
mov    $0x1,%r15d
nopl   0x0(%rax)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %rax,%r14
test   %rax,%rax
je     11fc <main+0x8c>
lea    0x1(%r15),%eax
mov    (%rsp),%ebp
cmp    %ebp,%r15d
mov    %eax,%r15d
jl     11d0 <main+0x60>
test   %r14,%r14
jg     12c8 <main+0x158>
jmp    1301 <main+0x191>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe2b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    -0x1(%r15),%eax
cmp    $0x3,%eax
jae    1227 <main+0xb7>
mov    $0x1,%ecx
xor    %eax,%eax
jmp    126e <main+0xfe>
mov    %r15d,%edx
and    $0xfffffffc,%edx
mov    $0x1,%ecx
xor    %ebx,%ebx
mov    $0x1,%esi
xor    %eax,%eax
nopl   0x0(%rax,%rax,1)
mov    %ecx,%edi
add    %rax,%rbx
cmp    $0x3,%ecx
cmovb  %rsi,%rbx
cmovb  %rsi,%rax
add    %rbx,%rax
cmp    $0x2,%ecx
cmovb  %rsi,%rax
cmovb  %rsi,%rbx
add    %rax,%rbx
add    %rbx,%rax
lea    0x4(%rdi),%ecx
add    $0x3,%edi
cmp    %edx,%edi
jne    1240 <main+0xd0>
mov    %rax,%rdx
test   $0x3,%r15b
je     12af <main+0x13f>
movzbl %r15b,%esi
and    $0x3,%esi
mov    $0x1,%edi
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    %rax,%rbx
cmp    $0x3,%ecx
mov    %rbx,%rdx
cmovb  %rdi,%rdx
cmovb  %rdi,%rax
add    $0x1,%ecx
mov    %rax,%rbx
mov    %rdx,%rax
add    $0xffffffff,%esi
jne    1290 <main+0x120>
lea    0xd9f(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rsp),%ebp
test   %r14,%r14
jle    1301 <main+0x191>
test   %ebp,%ebp
jle    1301 <main+0x191>
add    $0x1,%ebp
lea    0x4(%rsp),%r14
lea    0xd89(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1080 <wait@plt>
mov    0x4(%rsp),%edx
mov    %rbx,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
ja     12e0 <main+0x170>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.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 = [30 x i8] c"Child process executing: %d \09\00"
@.str.5 = [15 x i8] c"fib(%d) = %lu\0A\00"
@.str.6 = [41 x i8] c"Child process ended: %d with status=%d\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i16
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
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
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%15 = 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 %16
16: ; preds = %60, %0
%17 = load i32, i32* %6
%18 = load i32, i32* %4
%19 = icmp sle i32 %17, %18
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i16, i16* %7
%22 = zext i16 %21 to i32
%23 = icmp ne i32 %22, 0
br label %24
24: ; preds = %20, %16
%25 = phi i1 [ false, %16 ], [ %23, %20 ]
br i1 %25, label %26, label %63
26: ; preds = %24
%27 = call i64 (...) @fork()
store i64 %27, i64* %2
%28 = load i64, i64* %2
%29 = icmp ne i64 %28, 0
br i1 %29, label %55, label %30
30: ; preds = %26
store i16 0, i16* %7
%31 = call i32 (...) @getpid()
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %31)
store i32 0, i32* %8
store i64 0, i64* %9
store i64 0, i64* %10
store i32 1, i32* %8
br label %33
33: ; preds = %48, %30
%34 = load i32, i32* %8
%35 = load i32, i32* %6
%36 = icmp sle i32 %34, %35
br i1 %36, label %37, label %51
37: ; preds = %33
%38 = load i32, i32* %8
%39 = icmp slt i32 %38, 3
br i1 %39, label %40, label %41
40: ; preds = %37
store i64 1, i64* %9
store i64 1, i64* %10
br label %47
41: ; preds = %37
%42 = load i64, i64* %9
store i64 %42, i64* %11
%43 = load i64, i64* %10
%44 = load i64, i64* %9
%45 = add i64 %43, %44
store i64 %45, i64* %9
%46 = load i64, i64* %11
store i64 %46, i64* %10
br label %47
47: ; preds = %41, %40
br label %48
48: ; preds = %47
%49 = load i32, i32* %8
%50 = add nsw i32 %49, 1
store i32 %50, i32* %8
br label %33
51: ; preds = %33
%52 = load i32, i32* %6
%53 = load i64, i64* %9
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %52, i64 %53)
br label %60
55: ; preds = %26
%56 = load i64, i64* %2
%57 = icmp sgt i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %55
store i16 1, i16* %7
br label %59
59: ; preds = %58, %55
br label %60
60: ; preds = %59, %51
%61 = load i32, i32* %6
%62 = add nsw i32 %61, 1
store i32 %62, i32* %6
br label %16
63: ; preds = %24
%64 = load i32, i32* %4
store i32 %64, i32* %6
br label %65
65: ; preds = %73, %63
%66 = load i64, i64* %2
%67 = icmp sgt i64 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load i32, i32* %6
%70 = icmp sgt i32 %69, 0
br label %71
71: ; preds = %68, %65
%72 = phi i1 [ false, %65 ], [ %70, %68 ]
br i1 %72, label %73, label %80
73: ; preds = %71
%74 = call i64 @wait(i32* %5)
store i64 %74, i64* %3
%75 = load i64, i64* %3
%76 = load i32, i32* %5
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0), i64 %75, i32 %76)
%78 = load i32, i32* %6
%79 = add nsw i32 %78, -1
store i32 %79, i32* %6
br label %65
80: ; preds = %71
ret i32 0
}

LLVM IR (-O2):
@.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 = [30 x i8] c"Child process executing: %d \09\00"
@.str.5 = [15 x i8] c"fib(%d) = %lu\0A\00"
@.str.6 = [41 x i8] c"Child process ended: %d with status=%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 %16, label %82
11: ; preds = %70, %66
%12 = phi i32 [ %69, %66 ], [ %72, %70 ]
%13 = icmp sgt i64 %19, 0
%14 = icmp sgt i32 %12, 0
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %75, label %82
16: ; preds = %0, %70
%17 = phi i32 [ %74, %70 ], [ 2, %0 ]
%18 = phi i32 [ %71, %70 ], [ 1, %0 ]
%19 = call i64 (...) @fork()
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %70
21: ; preds = %16
%22 = call i32 (...) @getpid()
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%24 = add i32 %17, -1
%25 = add i32 %17, -2
%26 = and i32 %24, 3
%27 = icmp ult i32 %25, 3
br i1 %27, label %48, label %28
28: ; preds = %21
%29 = and i32 %24, -4
br label %30
30: ; preds = %30, %28
%31 = phi i64 [ 0, %28 ], [ %43, %30 ]
%32 = phi i64 [ 0, %28 ], [ %44, %30 ]
%33 = phi i32 [ 1, %28 ], [ %45, %30 ]
%34 = phi i32 [ 0, %28 ], [ %46, %30 ]
%35 = icmp ult i32 %33, 3
%36 = add i64 %31, %32
%37 = select i1 %35, i64 1, i64 %36
%38 = select i1 %35, i64 1, i64 %32
%39 = icmp ult i32 %33, 2
%40 = add i64 %38, %37
%41 = select i1 %39, i64 1, i64 %40
%42 = select i1 %39, i64 1, i64 %37
%43 = add i64 %42, %41
%44 = add i64 %41, %43
%45 = add nuw i32 %33, 4
%46 = add i32 %34, 4
%47 = icmp eq i32 %46, %29
br i1 %47, label %48, label %30
48: ; preds = %30, %21
%49 = phi i64 [ undef, %21 ], [ %44, %30 ]
%50 = phi i64 [ 0, %21 ], [ %43, %30 ]
%51 = phi i64 [ 0, %21 ], [ %44, %30 ]
%52 = phi i32 [ 1, %21 ], [ %45, %30 ]
%53 = icmp eq i32 %26, 0
br i1 %53, label %66, label %54
54: ; preds = %48, %54
%55 = phi i64 [ %62, %54 ], [ %50, %48 ]
%56 = phi i64 [ %61, %54 ], [ %51, %48 ]
%57 = phi i32 [ %63, %54 ], [ %52, %48 ]
%58 = phi i32 [ %64, %54 ], [ 0, %48 ]
%59 = icmp ult i32 %57, 3
%60 = add i64 %55, %56
%61 = select i1 %59, i64 1, i64 %60
%62 = select i1 %59, i64 1, i64 %56
%63 = add nuw i32 %57, 1
%64 = add i32 %58, 1
%65 = icmp eq i32 %64, %26
br i1 %65, label %66, label %54
66: ; preds = %54, %48
%67 = phi i64 [ %49, %48 ], [ %61, %54 ]
%68 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %18, i64 %67)
%69 = load i32, i32* %1
br label %11
70: ; preds = %16
%71 = add nuw nsw i32 %18, 1
%72 = load i32, i32* %1
%73 = icmp slt i32 %18, %72
%74 = add nuw i32 %17, 1
br i1 %73, label %16, label %11
75: ; preds = %11, %75
%76 = phi i32 [ %80, %75 ], [ %12, %11 ]
%77 = call i64 @wait(i32* %2)
%78 = load i32, i32* %2
%79 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0), i64 %77, i32 %78)
%80 = add nsw i32 %76, -1
%81 = icmp ugt i32 %76, 1
br i1 %81, label %75, label %82
82: ; preds = %75, %0, %11
ret i32 0
}