; LLVM IR from mahendramahara/DSA-in-C-Programing :: RECURSION (Factorial)

; 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 i64 @factorial(i32 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %9, label %6

6:                                                ; preds = %1
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = icmp eq i32 %7, 1
  br i1 %8, label %9, label %10

9:                                                ; preds = %6, %1
  store i64 1, ptr %2, align 8
  br label %17

10:                                               ; preds = %6
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = sub nsw i32 %13, 1
  %15 = call i64 @factorial(i32 noundef %14)
  %16 = mul i64 %12, %15
  store i64 %16, ptr %2, align 8
  br label %17

17:                                               ; preds = %10, %9
  %18 = load i64, ptr %2, align 8
  ret i64 %18
}

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 norecurse nosync nounwind memory(none) uwtable
define dso_local i64 @factorial(i32 noundef %0) local_unnamed_addr #0 {
  %or.cond7 = icmp ult i32 %0, 2
  br i1 %or.cond7, label %tailrecurse._crit_edge, label %tailrecurse.preheader

tailrecurse.preheader:                            ; preds = %1
  %2 = sext i32 %0 to i64
  %3 = add nsw i64 %2, -1
  %4 = add nsw i64 %2, -2
  %xtraiter = and i64 %3, 7
  %5 = icmp ult i64 %4, 7
  br i1 %5, label %tailrecurse._crit_edge.loopexit.unr-lcssa, label %tailrecurse.preheader.new

tailrecurse.preheader.new:                        ; preds = %tailrecurse.preheader
  %unroll_iter = and i64 %3, -8
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse, %tailrecurse.preheader.new
  %indvars.iv = phi i64 [ %2, %tailrecurse.preheader.new ], [ %indvars.iv.next.7, %tailrecurse ]
  %accumulator.tr8 = phi i64 [ 1, %tailrecurse.preheader.new ], [ %13, %tailrecurse ]
  %niter = phi i64 [ 0, %tailrecurse.preheader.new ], [ %niter.next.7, %tailrecurse ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %6 = mul i64 %accumulator.tr8, %indvars.iv
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, -2
  %7 = mul i64 %6, %indvars.iv.next
  %indvars.iv.next.2 = add nsw i64 %indvars.iv, -3
  %8 = mul i64 %7, %indvars.iv.next.1
  %indvars.iv.next.3 = add nsw i64 %indvars.iv, -4
  %9 = mul i64 %8, %indvars.iv.next.2
  %indvars.iv.next.4 = add nsw i64 %indvars.iv, -5
  %10 = mul i64 %9, %indvars.iv.next.3
  %indvars.iv.next.5 = add nsw i64 %indvars.iv, -6
  %11 = mul i64 %10, %indvars.iv.next.4
  %indvars.iv.next.6 = add nsw i64 %indvars.iv, -7
  %12 = mul i64 %11, %indvars.iv.next.5
  %indvars.iv.next.7 = add nsw i64 %indvars.iv, -8
  %13 = mul i64 %12, %indvars.iv.next.6
  %niter.next.7 = add i64 %niter, 8
  %niter.ncmp.7 = icmp eq i64 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %tailrecurse._crit_edge.loopexit.unr-lcssa, label %tailrecurse

tailrecurse._crit_edge.loopexit.unr-lcssa:        ; preds = %tailrecurse, %tailrecurse.preheader
  %.lcssa.ph = phi i64 [ poison, %tailrecurse.preheader ], [ %13, %tailrecurse ]
  %indvars.iv.unr = phi i64 [ %2, %tailrecurse.preheader ], [ %indvars.iv.next.7, %tailrecurse ]
  %accumulator.tr8.unr = phi i64 [ 1, %tailrecurse.preheader ], [ %13, %tailrecurse ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %tailrecurse._crit_edge, label %tailrecurse.epil

tailrecurse.epil:                                 ; preds = %tailrecurse._crit_edge.loopexit.unr-lcssa, %tailrecurse.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %tailrecurse.epil ], [ %indvars.iv.unr, %tailrecurse._crit_edge.loopexit.unr-lcssa ]
  %accumulator.tr8.epil = phi i64 [ %14, %tailrecurse.epil ], [ %accumulator.tr8.unr, %tailrecurse._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %tailrecurse.epil ], [ 0, %tailrecurse._crit_edge.loopexit.unr-lcssa ]
  %indvars.iv.next.epil = add nsw i64 %indvars.iv.epil, -1
  %14 = mul i64 %accumulator.tr8.epil, %indvars.iv.epil
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %tailrecurse._crit_edge, label %tailrecurse.epil, !llvm.loop !5

tailrecurse._crit_edge:                           ; preds = %tailrecurse._crit_edge.loopexit.unr-lcssa, %tailrecurse.epil, %1
  %accumulator.tr.lcssa = phi i64 [ 1, %1 ], [ %.lcssa.ph, %tailrecurse._crit_edge.loopexit.unr-lcssa ], [ %14, %tailrecurse.epil ]
  ret i64 %accumulator.tr.lcssa
}

attributes #0 = { nofree norecurse 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)"}
!5 = distinct !{!5, !6}
!6 = !{!"llvm.loop.unroll.disable"}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass