; LLVM IR from lzyrapx/Competitive-Programming :: ACdream/1084（数学）（阶乘K进制下0的个数）.ll (Z9getfactori)

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

@prime = external dso_local global [1000010 x i32], align 16
@cnt = external dso_local global i32, align 4
@fac = external dso_local global [1234 x i32], align 16
@num = external dso_local global i32, align 4
@expo = external dso_local global [1234 x i32], align 16

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z9getfactori(i32 noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  store i32 0, ptr @num, align 4, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 16 @expo, i8 0, i64 4936, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = call noundef double @_ZSt4sqrtIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef %5)
  %7 = fptosi double %6 to i32
  store i32 %7, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %62, %1
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = load i32, ptr @cnt, align 4, !tbaa !5
  %11 = icmp slt i32 %9, %10
  br i1 %11, label %12, label %19

12:                                               ; preds = %8
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [1000010 x i32], ptr @prime, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = load i32, ptr %3, align 4, !tbaa !5
  %18 = icmp sle i32 %16, %17
  br label %19

19:                                               ; preds = %12, %8
  %20 = phi i1 [ false, %8 ], [ %18, %12 ]
  br i1 %20, label %22, label %21

21:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %65

22:                                               ; preds = %19
  %23 = load i32, ptr %2, align 4, !tbaa !5
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [1000010 x i32], ptr @prime, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = srem i32 %23, %27
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %30, label %61

30:                                               ; preds = %22
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [1000010 x i32], ptr @prime, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = load i32, ptr @num, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [1234 x i32], ptr @fac, i64 0, i64 %36
  store i32 %34, ptr %37, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %46, %30
  %39 = load i32, ptr %2, align 4, !tbaa !5
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [1000010 x i32], ptr @prime, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = srem i32 %39, %43
  %45 = icmp eq i32 %44, 0
  br i1 %45, label %46, label %58

46:                                               ; preds = %38
  %47 = load i32, ptr @num, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [1234 x i32], ptr @expo, i64 0, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !5
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %49, align 4, !tbaa !5
  %52 = load i32, ptr %4, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [1000010 x i32], ptr @prime, i64 0, i64 %53
  %55 = load i32, ptr %54, align 4, !tbaa !5
  %56 = load i32, ptr %2, align 4, !tbaa !5
  %57 = sdiv i32 %56, %55
  store i32 %57, ptr %2, align 4, !tbaa !5
  br label %38, !llvm.loop !9

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

61:                                               ; preds = %58, %22
  br label %62

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

65:                                               ; preds = %21
  %66 = load i32, ptr %2, align 4, !tbaa !5
  %67 = icmp sgt i32 %66, 1
  br i1 %67, label %68, label %77

68:                                               ; preds = %65
  %69 = load i32, ptr %2, align 4, !tbaa !5
  %70 = load i32, ptr @num, align 4, !tbaa !5
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [1234 x i32], ptr @fac, i64 0, i64 %71
  store i32 %69, ptr %72, align 4, !tbaa !5
  %73 = load i32, ptr @num, align 4, !tbaa !5
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr @num, align 4, !tbaa !5
  %75 = sext i32 %73 to i64
  %76 = getelementptr inbounds [1234 x i32], ptr @expo, i64 0, i64 %75
  store i32 1, ptr %76, align 4, !tbaa !5
  br label %77

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt4sqrtIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef) #3

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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" }
attributes #3 = { inlinehint mustprogress 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 #4 = { 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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !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"

@prime = external dso_local local_unnamed_addr global [1000010 x i32], align 16
@cnt = external dso_local local_unnamed_addr global i32, align 4
@fac = external dso_local local_unnamed_addr global [1234 x i32], align 16
@num = external dso_local local_unnamed_addr global i32, align 4
@expo = external dso_local local_unnamed_addr global [1234 x i32], align 16

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z9getfactori(i32 noundef %0) local_unnamed_addr #1 {
  store i32 0, ptr @num, align 4, !tbaa !5
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(4936) @expo, i8 0, i64 4936, i1 false)
  %2 = tail call noundef double @_ZSt4sqrtIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef %0)
  %3 = fptosi double %2 to i32
  %4 = load i32, ptr @cnt, align 4, !tbaa !5
  %num.promoted = load i32, ptr @num, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph20.preheader, label %.critedge

.lr.ph20.preheader:                               ; preds = %1
  %wide.trip.count = zext nneg i32 %4 to i64
  br label %.lr.ph20

.lr.ph20:                                         ; preds = %.lr.ph20.preheader, %25
  %indvars.iv = phi i64 [ 0, %.lr.ph20.preheader ], [ %indvars.iv.next, %25 ]
  %.01417 = phi i32 [ %0, %.lr.ph20.preheader ], [ %.2, %25 ]
  %6 = phi i32 [ %num.promoted, %.lr.ph20.preheader ], [ %26, %25 ]
  %7 = getelementptr inbounds [1000010 x i32], ptr @prime, i64 0, i64 %indvars.iv
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %.not = icmp sgt i32 %8, %3
  br i1 %.not, label %.critedge, label %11

.critedge:                                        ; preds = %.lr.ph20, %25, %1
  %9 = phi i32 [ %num.promoted, %1 ], [ %26, %25 ], [ %6, %.lr.ph20 ]
  %.014.lcssa = phi i32 [ %0, %1 ], [ %.2, %25 ], [ %.01417, %.lr.ph20 ]
  %10 = icmp sgt i32 %.014.lcssa, 1
  br i1 %10, label %27, label %32

11:                                               ; preds = %.lr.ph20
  %12 = srem i32 %.01417, %8
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %.lr.ph, label %25

.lr.ph:                                           ; preds = %11
  %14 = sext i32 %6 to i64
  %15 = getelementptr inbounds [1234 x i32], ptr @fac, i64 0, i64 %14
  store i32 %8, ptr %15, align 4, !tbaa !5
  %16 = getelementptr inbounds [1234 x i32], ptr @expo, i64 0, i64 %14
  %.promoted = load i32, ptr %16, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %.lr.ph, %17
  %18 = phi i32 [ %.promoted, %.lr.ph ], [ %19, %17 ]
  %.116 = phi i32 [ %.01417, %.lr.ph ], [ %20, %17 ]
  %19 = add nsw i32 %18, 1
  %20 = sdiv i32 %.116, %8
  %21 = srem i32 %20, %8
  %22 = icmp eq i32 %21, 0
  br i1 %22, label %17, label %23, !llvm.loop !9

23:                                               ; preds = %17
  store i32 %19, ptr %16, align 4, !tbaa !5
  %24 = add nsw i32 %6, 1
  store i32 %24, ptr @num, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %11, %23
  %26 = phi i32 [ %24, %23 ], [ %6, %11 ]
  %.2 = phi i32 [ %20, %23 ], [ %.01417, %11 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.critedge, label %.lr.ph20, !llvm.loop !11

27:                                               ; preds = %.critedge
  %28 = sext i32 %9 to i64
  %29 = getelementptr inbounds [1234 x i32], ptr @fac, i64 0, i64 %28
  store i32 %.014.lcssa, ptr %29, align 4, !tbaa !5
  %30 = add nsw i32 %9, 1
  store i32 %30, ptr @num, align 4, !tbaa !5
  %31 = getelementptr inbounds [1234 x i32], ptr @expo, i64 0, i64 %28
  store i32 1, ptr %31, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %27, %.critedge
  ret void
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt4sqrtIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { mustprogress 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 = { inlinehint mustprogress 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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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