; LLVM IR from MinaFaried3/Assiut-University-Training---Newcomers :: j-sheet7_recursion/J-Factorial.ll (Z9factoriali)

; 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 @_Z9factoriali(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, 1
  br i1 %5, label %6, label %9

6:                                                ; preds = %1
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = sext i32 %7 to i64
  store i64 %8, ptr %2, align 8
  br label %16

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

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

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

tailrecurse.preheader:                            ; preds = %1
  %3 = sext 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.tr6 = 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 nsw i64 %accumulator.tr6, %indvars.iv
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, -2
  %8 = mul nsw i64 %7, %indvars.iv.next
  %indvars.iv.next.2 = add nsw i64 %indvars.iv, -3
  %9 = mul nsw i64 %8, %indvars.iv.next.1
  %indvars.iv.next.3 = add nsw i64 %indvars.iv, -4
  %10 = mul nsw i64 %9, %indvars.iv.next.2
  %indvars.iv.next.4 = add nsw i64 %indvars.iv, -5
  %11 = mul nsw i64 %10, %indvars.iv.next.3
  %indvars.iv.next.5 = add nsw i64 %indvars.iv, -6
  %12 = mul nsw i64 %11, %indvars.iv.next.4
  %indvars.iv.next.6 = add nsw i64 %indvars.iv, -7
  %13 = mul nsw i64 %12, %indvars.iv.next.5
  %indvars.iv.next.7 = add nsw i64 %indvars.iv, -8
  %14 = mul nsw 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.tr6.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.tr6.epil = phi i64 [ %15, %tailrecurse.epil ], [ %accumulator.tr6.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 nsw i64 %accumulator.tr6.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