; LLVM IR from UtkarshPathrabe/Competitive-Coding :: HackerRank Solutions/C/Functions (Calculate the Nth term)

; 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 = icmp eq i32 %10, 1
  br i1 %11, label %12, label %14

12:                                               ; preds = %4
  %13 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %13, ptr %5, align 4
  br label %45

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

17:                                               ; preds = %14
  %18 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %18, ptr %5, align 4
  br label %45

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

22:                                               ; preds = %19
  %23 = load i32, ptr %9, align 4, !tbaa !5
  store i32 %23, 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, %22, %17, %12
  %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 {
  br label %tailrecurse

tailrecurse:                                      ; preds = %5, %4
  %accumulator.tr = phi i32 [ 0, %4 ], [ %12, %5 ]
  %.tr = phi i32 [ %0, %4 ], [ %10, %5 ]
  switch i32 %.tr, label %5 [
    i32 1, label %.loopexit.loopexit
    i32 2, label %.loopexit.loopexit37
    i32 3, label %.loopexit
  ]

5:                                                ; preds = %tailrecurse
  %6 = add nsw i32 %.tr, -1
  %7 = tail call i32 @find_nth_term(i32 noundef %6, i32 noundef %1, i32 noundef %2, i32 noundef %3)
  %8 = add nsw i32 %.tr, -2
  %9 = tail call i32 @find_nth_term(i32 noundef %8, i32 noundef %1, i32 noundef %2, i32 noundef %3)
  %10 = add nsw i32 %.tr, -3
  %11 = add i32 %7, %accumulator.tr
  %12 = add i32 %11, %9
  br label %tailrecurse

.loopexit.loopexit:                               ; preds = %tailrecurse
  br label %.loopexit

.loopexit.loopexit37:                             ; preds = %tailrecurse
  br label %.loopexit

.loopexit:                                        ; preds = %tailrecurse, %.loopexit.loopexit37, %.loopexit.loopexit
  %.0 = phi i32 [ %1, %.loopexit.loopexit ], [ %2, %.loopexit.loopexit37 ], [ %3, %tailrecurse ]
  %accumulator.ret.tr = add nsw i32 %.0, %accumulator.tr
  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: GVNPass,GlobalOptPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass