; LLVM IR from afrozchakure/Competitive-Programming-Solutions :: HackerRank/C (CalculateTheNthTerm)

; 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 %15

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

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

19:                                               ; preds = %15
  %20 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %20, ptr %5, align 4
  br label %48

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

25:                                               ; preds = %21
  %26 = load i32, ptr %9, align 4, !tbaa !5
  store i32 %26, ptr %5, align 4
  br label %48

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

48:                                               ; preds = %27, %25, %19, %13
  %49 = load i32, ptr %5, align 4
  ret i32 %49
}

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
  %.tr32 = phi i32 [ %13, %tailrecurse ], [ %0, %4 ]
  %accumulator.tr31 = phi i32 [ %15, %tailrecurse ], [ 0, %4 ]
  %6 = icmp eq i32 %.tr32, %2
  br i1 %6, label %._crit_edge, label %7

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

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

._crit_edge:                                      ; preds = %tailrecurse, %.lr.ph, %7, %4
  %accumulator.tr.lcssa = phi i32 [ 0, %4 ], [ %accumulator.tr31, %7 ], [ %accumulator.tr31, %.lr.ph ], [ %15, %tailrecurse ]
  %.0 = phi i32 [ %0, %4 ], [ %3, %7 ], [ %2, %.lr.ph ], [ %1, %tailrecurse ]
  %accumulator.ret.tr = add nsw i32 %.0, %accumulator.tr.lcssa
  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,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass