; LLVM IR from Py-Contributors/AlgorithmsAndDataStructure :: C++/Algorithms/RecursionAlgorithms/RecursiveFactorial.ll (Z18recursiveFactorialj)

; 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: mustprogress uwtable
define dso_local noundef i64 @_Z18recursiveFactorialj(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 ule i32 %4, 1
  br i1 %5, label %6, label %7

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

7:                                                ; preds = %1
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = zext i32 %8 to i64
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = sub i32 %10, 1
  %12 = call noundef i64 @_Z18recursiveFactorialj(i32 noundef %11)
  %13 = mul i64 %9, %12
  store i64 %13, ptr %2, align 8
  br label %14

14:                                               ; preds = %7, %6
  %15 = load i64, ptr %2, align 8
  ret i64 %15
}

attributes #0 = { mustprogress 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++ 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: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef i64 @_Z18recursiveFactorialj(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp ult i32 %0, 2
  br i1 %2, label %tailrecurse._crit_edge, label %tailrecurse.preheader

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

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

tailrecurse:                                      ; preds = %tailrecurse, %tailrecurse.preheader.new
  %indvars.iv = phi i64 [ %3, %tailrecurse.preheader.new ], [ %indvars.iv.next.7, %tailrecurse ]
  %accumulator.tr5 = phi i64 [ 1, %tailrecurse.preheader.new ], [ %14, %tailrecurse ]
  %niter = phi i64 [ 0, %tailrecurse.preheader.new ], [ %niter.next.7, %tailrecurse ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %7 = mul i64 %accumulator.tr5, %indvars.iv
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, -2
  %8 = mul i64 %7, %indvars.iv.next
  %indvars.iv.next.2 = add nsw i64 %indvars.iv, -3
  %9 = mul i64 %8, %indvars.iv.next.1
  %indvars.iv.next.3 = add nsw i64 %indvars.iv, -4
  %10 = mul i64 %9, %indvars.iv.next.2
  %indvars.iv.next.4 = add nsw i64 %indvars.iv, -5
  %11 = mul i64 %10, %indvars.iv.next.3
  %indvars.iv.next.5 = add nsw i64 %indvars.iv, -6
  %12 = mul i64 %11, %indvars.iv.next.4
  %indvars.iv.next.6 = add nsw i64 %indvars.iv, -7
  %13 = mul i64 %12, %indvars.iv.next.5
  %indvars.iv.next.7 = add nsw i64 %indvars.iv, -8
  %14 = mul i64 %13, %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 ], [ %14, %tailrecurse ]
  %indvars.iv.unr = phi i64 [ %3, %tailrecurse.preheader ], [ %indvars.iv.next.7, %tailrecurse ]
  %accumulator.tr5.unr = phi i64 [ 1, %tailrecurse.preheader ], [ %14, %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.tr5.epil = phi i64 [ %15, %tailrecurse.epil ], [ %accumulator.tr5.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
  %15 = mul i64 %accumulator.tr5.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 ], [ %15, %tailrecurse.epil ]
  ret i64 %accumulator.tr.lcssa
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn 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