; LLVM IR from MinhasKamal/AlgorithmImplementations :: playWithNumbers/factorial (FactorsOfFactorial)

; 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"

@n = external dso_local global [25 x i32], align 16

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local void @varify(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %8 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %8, ptr %3, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %13, %1
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = srem i32 %10, 2
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %13, label %18

13:                                               ; preds = %9
  %14 = load i32, ptr @n, align 16, !tbaa !5
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr @n, align 16, !tbaa !5
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = sdiv i32 %16, 2
  store i32 %17, ptr %3, align 4, !tbaa !5
  br label %9, !llvm.loop !9

18:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 1, ptr %6, align 4, !tbaa !5
  store i32 3, ptr %4, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %62, %18
  %20 = load i32, ptr %4, align 4, !tbaa !5
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = icmp sle i32 %20, %21
  br i1 %22, label %23, label %65

23:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 1, ptr %7, align 4, !tbaa !5
  store i32 3, ptr %5, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %36, %23
  %25 = load i32, ptr %5, align 4, !tbaa !5
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = sdiv i32 %26, 3
  %28 = icmp sle i32 %25, %27
  br i1 %28, label %29, label %39

29:                                               ; preds = %24
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = srem i32 %30, %31
  %33 = icmp eq i32 %32, 0
  br i1 %33, label %34, label %35

34:                                               ; preds = %29
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %39

35:                                               ; preds = %29
  br label %36

36:                                               ; preds = %35
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = add nsw i32 %37, 2
  store i32 %38, ptr %5, align 4, !tbaa !5
  br label %24, !llvm.loop !11

39:                                               ; preds = %34, %24
  %40 = load i32, ptr %7, align 4, !tbaa !5
  %41 = icmp eq i32 %40, 1
  br i1 %41, label %42, label %61

42:                                               ; preds = %39
  %43 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %43, ptr %3, align 4, !tbaa !5
  br label %44

44:                                               ; preds = %49, %42
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = load i32, ptr %4, align 4, !tbaa !5
  %47 = srem i32 %45, %46
  %48 = icmp eq i32 %47, 0
  br i1 %48, label %49, label %58

49:                                               ; preds = %44
  %50 = load i32, ptr %6, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [25 x i32], ptr @n, i64 0, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !5
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %52, align 4, !tbaa !5
  %55 = load i32, ptr %3, align 4, !tbaa !5
  %56 = load i32, ptr %4, align 4, !tbaa !5
  %57 = sdiv i32 %55, %56
  store i32 %57, ptr %3, align 4, !tbaa !5
  br label %44, !llvm.loop !12

58:                                               ; preds = %44
  %59 = load i32, ptr %6, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %6, align 4, !tbaa !5
  br label %61

61:                                               ; preds = %58, %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %4, align 4, !tbaa !5
  %64 = add nsw i32 %63, 2
  store i32 %64, ptr %4, align 4, !tbaa !5
  br label %19, !llvm.loop !13

65:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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" }
attributes #2 = { nounwind }

!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"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !10}


; 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"

@n = external dso_local local_unnamed_addr global [25 x i32], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @varify(i32 noundef %0) local_unnamed_addr #0 {
  %2 = and i32 %0, 1
  %3 = icmp eq i32 %2, 0
  br i1 %3, label %.lr.ph.preheader, label %.preheader26

.lr.ph.preheader:                                 ; preds = %1
  %n.promoted = load i32, ptr @n, align 16, !tbaa !5
  br label %.lr.ph

..preheader26_crit_edge:                          ; preds = %.lr.ph
  store i32 %5, ptr @n, align 16, !tbaa !5
  br label %.preheader26

.preheader26:                                     ; preds = %..preheader26_crit_edge, %1
  %.not34 = icmp slt i32 %0, 3
  br i1 %.not34, label %._crit_edge, label %.preheader

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.02227 = phi i32 [ %6, %.lr.ph ], [ %0, %.lr.ph.preheader ]
  %4 = phi i32 [ %5, %.lr.ph ], [ %n.promoted, %.lr.ph.preheader ]
  %5 = add nsw i32 %4, 1
  %6 = ashr exact i32 %.02227, 1
  %7 = and i32 %.02227, 2
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %.lr.ph, label %..preheader26_crit_edge, !llvm.loop !9

.preheader:                                       ; preds = %.preheader26, %.loopexit
  %.01937 = phi i32 [ %.1, %.loopexit ], [ 1, %.preheader26 ]
  %.02135 = phi i32 [ %25, %.loopexit ], [ 3, %.preheader26 ]
  %9 = udiv i32 %.02135, 3
  %.not2528 = icmp ult i32 %.02135, 9
  br i1 %.not2528, label %.critedge.preheader, label %.lr.ph30

10:                                               ; preds = %.lr.ph30
  %11 = add nuw nsw i32 %.02029, 2
  %.not25 = icmp ugt i32 %11, %9
  br i1 %.not25, label %.critedge.preheader, label %.lr.ph30, !llvm.loop !11

.critedge.preheader:                              ; preds = %10, %.preheader
  %12 = urem i32 %0, %.02135
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %.lr.ph32, label %23

.lr.ph32:                                         ; preds = %.critedge.preheader
  %14 = sext i32 %.01937 to i64
  %15 = getelementptr inbounds [25 x i32], ptr @n, i64 0, i64 %14
  %.promoted = load i32, ptr %15, align 4, !tbaa !5
  br label %.critedge

.lr.ph30:                                         ; preds = %.preheader, %10
  %.02029 = phi i32 [ %11, %10 ], [ 3, %.preheader ]
  %16 = urem i32 %.02135, %.02029
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %.loopexit, label %10

.critedge:                                        ; preds = %.lr.ph32, %.critedge
  %18 = phi i32 [ %.promoted, %.lr.ph32 ], [ %19, %.critedge ]
  %.12331 = phi i32 [ %0, %.lr.ph32 ], [ %20, %.critedge ]
  %19 = add nsw i32 %18, 1
  %20 = sdiv i32 %.12331, %.02135
  %21 = srem i32 %20, %.02135
  %22 = icmp eq i32 %21, 0
  br i1 %22, label %.critedge, label %.critedge._crit_edge, !llvm.loop !12

.critedge._crit_edge:                             ; preds = %.critedge
  store i32 %19, ptr %15, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %.critedge._crit_edge, %.critedge.preheader
  %24 = add nsw i32 %.01937, 1
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph30, %23
  %.1 = phi i32 [ %24, %23 ], [ %.01937, %.lr.ph30 ]
  %25 = add nuw nsw i32 %.02135, 2
  %.not = icmp sgt i32 %25, %0
  br i1 %.not, label %._crit_edge, label %.preheader, !llvm.loop !13

._crit_edge:                                      ; preds = %.loopexit, %.preheader26
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: 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 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !10}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass