; LLVM IR from AllAlgorithms/c :: algorithms/math/factorial (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 @multiplyNumbers(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 sge i32 %4, 1
  br i1 %5, label %6, label %13

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

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

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

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 @multiplyNumbers(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp sgt i32 %0, 0
  br i1 %2, label %tailrecurse.preheader, label %tailrecurse._crit_edge

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

tailrecurse.preheader.new:                        ; preds = %tailrecurse.preheader
  %unroll_iter = and i64 %3, 2147483640
  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 ], [ %12, %tailrecurse ]
  %niter = phi i64 [ 0, %tailrecurse.preheader.new ], [ %niter.next.7, %tailrecurse ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %5 = mul nuw nsw i64 %accumulator.tr5, %indvars.iv
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, -2
  %6 = mul nuw nsw i64 %5, %indvars.iv.next
  %indvars.iv.next.2 = add nsw i64 %indvars.iv, -3
  %7 = mul nuw nsw i64 %6, %indvars.iv.next.1
  %indvars.iv.next.3 = add nsw i64 %indvars.iv, -4
  %8 = mul nuw nsw i64 %7, %indvars.iv.next.2
  %indvars.iv.next.4 = add nsw i64 %indvars.iv, -5
  %9 = mul nuw nsw i64 %8, %indvars.iv.next.3
  %indvars.iv.next.5 = add nsw i64 %indvars.iv, -6
  %10 = mul nuw nsw i64 %9, %indvars.iv.next.4
  %indvars.iv.next.6 = add nsw i64 %indvars.iv, -7
  %11 = mul nuw nsw i64 %10, %indvars.iv.next.5
  %indvars.iv.next.7 = add nsw i64 %indvars.iv, -8
  %12 = mul nuw nsw i64 %11, %indvars.iv.next.6
  %niter.next.7 = add i64 %niter, 8
  %niter.ncmp.7.not = icmp eq i64 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7.not, 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 ], [ %12, %tailrecurse ]
  %indvars.iv.unr = phi i64 [ %3, %tailrecurse.preheader ], [ %indvars.iv.next.7, %tailrecurse ]
  %accumulator.tr5.unr = phi i64 [ 1, %tailrecurse.preheader ], [ %12, %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 [ %13, %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
  %13 = mul nuw nsw 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 ], [ %13, %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: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass