; LLVM IR from upupming/algorithm :: acwing/算法基础课/第四讲 数学知识/求组合数/888. 求组合数 IV.ll (Z12get_primes_li)

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

@primes = external dso_local global [5010 x i32], align 16
@cnt = external dso_local global i32, align 4
@st = external dso_local global [5010 x i8], align 16

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

6:                                                ; preds = %57, %1
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = icmp sle i32 %7, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %6
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %60

11:                                               ; preds = %6
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [5010 x i8], ptr @st, i64 0, i64 %13
  %15 = load i8, ptr %14, align 1, !tbaa !9, !range !11, !noundef !12
  %16 = trunc i8 %15 to i1
  br i1 %16, label %23, label %17

17:                                               ; preds = %11
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = load i32, ptr @cnt, align 4, !tbaa !5
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr @cnt, align 4, !tbaa !5
  %21 = sext i32 %19 to i64
  %22 = getelementptr inbounds [5010 x i32], ptr @primes, i64 0, i64 %21
  store i32 %18, ptr %22, align 4, !tbaa !5
  br label %23

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

24:                                               ; preds = %52, %23
  %25 = load i32, ptr %5, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [5010 x i32], ptr @primes, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = sdiv i32 %29, %30
  %32 = icmp sle i32 %28, %31
  br i1 %32, label %34, label %33

33:                                               ; preds = %24
  store i32 5, ptr %4, align 4
  br label %55

34:                                               ; preds = %24
  %35 = load i32, ptr %5, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [5010 x i32], ptr @primes, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = load i32, ptr %3, align 4, !tbaa !5
  %40 = mul nsw i32 %38, %39
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [5010 x i8], ptr @st, i64 0, i64 %41
  store i8 1, ptr %42, align 1, !tbaa !9
  %43 = load i32, ptr %3, align 4, !tbaa !5
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [5010 x i32], ptr @primes, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = srem i32 %43, %47
  %49 = icmp eq i32 %48, 0
  br i1 %49, label %50, label %51

50:                                               ; preds = %34
  store i32 5, ptr %4, align 4
  br label %55

51:                                               ; preds = %34
  br label %52

52:                                               ; preds = %51
  %53 = load i32, ptr %5, align 4, !tbaa !5
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %5, align 4, !tbaa !5
  br label %24, !llvm.loop !13

55:                                               ; preds = %50, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %56

56:                                               ; preds = %55
  br label %57

57:                                               ; preds = %56
  %58 = load i32, ptr %3, align 4, !tbaa !5
  %59 = add nsw i32 %58, 1
  store i32 %59, ptr %3, align 4, !tbaa !5
  br label %6, !llvm.loop !15

60:                                               ; preds = %10
  ret void
}

; 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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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

@primes = external dso_local local_unnamed_addr global [5010 x i32], align 16
@cnt = external dso_local local_unnamed_addr global i32, align 4
@st = external dso_local local_unnamed_addr global [5010 x i8], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z12get_primes_li(i32 noundef %0) local_unnamed_addr #0 {
  %.not20 = icmp slt i32 %0, 2
  br i1 %.not20, label %._crit_edge24, label %.lr.ph23.preheader

.lr.ph23.preheader:                               ; preds = %1
  %cnt.promoted = load i32, ptr @cnt, align 4, !tbaa !5
  %2 = add nuw i32 %0, 1
  %wide.trip.count = zext i32 %2 to i64
  br label %.lr.ph23

._crit_edge24:                                    ; preds = %._crit_edge, %1
  ret void

.lr.ph23:                                         ; preds = %.lr.ph23.preheader, %._crit_edge
  %indvars.iv26 = phi i64 [ 2, %.lr.ph23.preheader ], [ %indvars.iv.next27, %._crit_edge ]
  %3 = phi i32 [ %cnt.promoted, %.lr.ph23.preheader ], [ %13, %._crit_edge ]
  %4 = getelementptr inbounds [5010 x i8], ptr @st, i64 0, i64 %indvars.iv26
  %5 = load i8, ptr %4, align 1, !tbaa !9, !range !11, !noundef !12
  %6 = trunc nuw i8 %5 to i1
  br i1 %6, label %.lr.ph23._crit_edge, label %7

.lr.ph23._crit_edge:                              ; preds = %.lr.ph23
  %.pre = trunc nuw nsw i64 %indvars.iv26 to i32
  br label %12

7:                                                ; preds = %.lr.ph23
  %8 = add nsw i32 %3, 1
  store i32 %8, ptr @cnt, align 4, !tbaa !5
  %9 = sext i32 %3 to i64
  %10 = getelementptr inbounds [5010 x i32], ptr @primes, i64 0, i64 %9
  %11 = trunc nuw nsw i64 %indvars.iv26 to i32
  store i32 %11, ptr %10, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %.lr.ph23._crit_edge, %7
  %.pre-phi = phi i32 [ %.pre, %.lr.ph23._crit_edge ], [ %11, %7 ]
  %13 = phi i32 [ %3, %.lr.ph23._crit_edge ], [ %8, %7 ]
  %14 = udiv i32 %0, %.pre-phi
  %15 = load i32, ptr @primes, align 16, !tbaa !5
  %.not1617 = icmp sgt i32 %15, %14
  br i1 %.not1617, label %._crit_edge, label %.lr.ph

16:                                               ; preds = %.lr.ph
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %17 = getelementptr inbounds [5010 x i32], ptr @primes, i64 0, i64 %indvars.iv.next
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %.not16 = icmp sgt i32 %18, %14
  br i1 %.not16, label %._crit_edge, label %.lr.ph, !llvm.loop !13

.lr.ph:                                           ; preds = %12, %16
  %indvars.iv = phi i64 [ %indvars.iv.next, %16 ], [ 0, %12 ]
  %19 = phi i32 [ %18, %16 ], [ %15, %12 ]
  %20 = mul nsw i32 %19, %.pre-phi
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [5010 x i8], ptr @st, i64 0, i64 %21
  store i8 1, ptr %22, align 1, !tbaa !9
  %23 = srem i32 %.pre-phi, %19
  %24 = icmp eq i32 %23, 0
  br i1 %24, label %._crit_edge, label %16

._crit_edge:                                      ; preds = %16, %.lr.ph, %12
  %indvars.iv.next27 = add nuw nsw i64 %indvars.iv26, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next27, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge24, label %.lr.ph23, !llvm.loop !15
}

attributes #0 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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