; LLVM IR from Mohammed-Shoaib/Coding-Problems :: HackerRank/C/Easy (E0009)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind uwtable
define dso_local i32 @find_nth_term(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !5
  %10 = load i32, ptr %6, align 4, !tbaa !5
  %11 = load i32, ptr %7, align 4, !tbaa !5
  %12 = icmp eq i32 %10, %11
  br i1 %12, label %13, label %14

13:                                               ; preds = %4
  store i32 1, ptr %5, align 4
  br label %45

14:                                               ; preds = %4
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = load i32, ptr %8, align 4, !tbaa !5
  %17 = icmp eq i32 %15, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %14
  store i32 2, ptr %5, align 4
  br label %45

19:                                               ; preds = %14
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = load i32, ptr %9, align 4, !tbaa !5
  %22 = icmp eq i32 %20, %21
  br i1 %22, label %23, label %24

23:                                               ; preds = %19
  store i32 3, ptr %5, align 4
  br label %45

24:                                               ; preds = %19
  %25 = load i32, ptr %6, align 4, !tbaa !5
  %26 = sub nsw i32 %25, 1
  %27 = load i32, ptr %7, align 4, !tbaa !5
  %28 = load i32, ptr %8, align 4, !tbaa !5
  %29 = load i32, ptr %9, align 4, !tbaa !5
  %30 = call i32 @find_nth_term(i32 noundef %26, i32 noundef %27, i32 noundef %28, i32 noundef %29)
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = sub nsw i32 %31, 2
  %33 = load i32, ptr %7, align 4, !tbaa !5
  %34 = load i32, ptr %8, align 4, !tbaa !5
  %35 = load i32, ptr %9, align 4, !tbaa !5
  %36 = call i32 @find_nth_term(i32 noundef %32, i32 noundef %33, i32 noundef %34, i32 noundef %35)
  %37 = add nsw i32 %30, %36
  %38 = load i32, ptr %6, align 4, !tbaa !5
  %39 = sub nsw i32 %38, 3
  %40 = load i32, ptr %7, align 4, !tbaa !5
  %41 = load i32, ptr %8, align 4, !tbaa !5
  %42 = load i32, ptr %9, align 4, !tbaa !5
  %43 = call i32 @find_nth_term(i32 noundef %39, i32 noundef %40, i32 noundef %41, i32 noundef %42)
  %44 = add nsw i32 %37, %43
  store i32 %44, ptr %5, align 4
  br label %45

45:                                               ; preds = %24, %23, %18, %13
  %46 = load i32, ptr %5, align 4
  ret i32 %46
}

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nofree nosync nounwind memory(none) uwtable
define dso_local i32 @find_nth_term(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp eq i32 %0, %1
  br i1 %5, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %tailrecurse
  %.tr27 = phi i32 [ %13, %tailrecurse ], [ %0, %4 ]
  %accumulator.tr26 = phi i32 [ %15, %tailrecurse ], [ 0, %4 ]
  %6 = icmp eq i32 %.tr27, %2
  br i1 %6, label %._crit_edge.loopexit, label %7

7:                                                ; preds = %.lr.ph
  %8 = icmp eq i32 %.tr27, %3
  br i1 %8, label %._crit_edge.loopexit, label %tailrecurse

tailrecurse:                                      ; preds = %7
  %9 = add nsw i32 %.tr27, -1
  %10 = tail call i32 @find_nth_term(i32 noundef %9, i32 noundef %1, i32 noundef %2, i32 noundef %3)
  %11 = add nsw i32 %.tr27, -2
  %12 = tail call i32 @find_nth_term(i32 noundef %11, i32 noundef %1, i32 noundef %2, i32 noundef %3)
  %13 = add nsw i32 %.tr27, -3
  %14 = add i32 %10, %accumulator.tr26
  %15 = add i32 %14, %12
  %16 = icmp eq i32 %13, %1
  br i1 %16, label %._crit_edge.loopexit, label %.lr.ph

._crit_edge.loopexit:                             ; preds = %7, %.lr.ph, %tailrecurse
  %accumulator.tr.lcssa.ph = phi i32 [ %15, %tailrecurse ], [ %accumulator.tr26, %.lr.ph ], [ %accumulator.tr26, %7 ]
  %.0.ph = phi i32 [ 1, %tailrecurse ], [ 2, %.lr.ph ], [ 3, %7 ]
  %17 = add nsw i32 %.0.ph, %accumulator.tr.lcssa.ph
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %4
  %accumulator.ret.tr = phi i32 [ 1, %4 ], [ %17, %._crit_edge.loopexit ]
  ret i32 %accumulator.ret.tr
}

attributes #0 = { nofree nosync nounwind memory(none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}


; Effective O3 passes: GlobalOptPass,InstCombinePass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass