; LLVM IR from Lodour/ACM-CheatSheet :: src/2_数学/7_莫比乌斯反演/1_莫比乌斯.ll (Z5calmuv)

; 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 [100001 x i32], align 16
@tot = external dso_local global i32, align 4
@mu = external dso_local global [100001 x i32], align 16
@check = external dso_local global [100001 x i8], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z5calmuv() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 1, ptr getelementptr inbounds ([100001 x i32], ptr @mu, i64 0, i64 1), align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 2, ptr %1, align 4, !tbaa !5
  br label %4

4:                                                ; preds = %83, %0
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = icmp slt i32 %5, 100001
  br i1 %6, label %8, label %7

7:                                                ; preds = %4
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %86

8:                                                ; preds = %4
  %9 = load i32, ptr %1, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [100001 x i8], ptr @check, i64 0, i64 %10
  %12 = load i8, ptr %11, align 1, !tbaa !9, !range !11, !noundef !12
  %13 = trunc i8 %12 to i1
  br i1 %13, label %23, label %14

14:                                               ; preds = %8
  %15 = load i32, ptr %1, align 4, !tbaa !5
  %16 = load i32, ptr @tot, align 4, !tbaa !5
  %17 = add nsw i32 %16, 1
  store i32 %17, ptr @tot, align 4, !tbaa !5
  %18 = sext i32 %16 to i64
  %19 = getelementptr inbounds [100001 x i32], ptr @prime, i64 0, i64 %18
  store i32 %15, ptr %19, align 4, !tbaa !5
  %20 = load i32, ptr %1, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [100001 x i32], ptr @mu, i64 0, i64 %21
  store i32 -1, ptr %22, align 4, !tbaa !5
  br label %23

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

24:                                               ; preds = %78, %23
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = load i32, ptr @tot, align 4, !tbaa !5
  %27 = icmp slt i32 %25, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %24
  store i32 5, ptr %2, align 4
  br label %81

29:                                               ; preds = %24
  %30 = load i32, ptr %1, align 4, !tbaa !5
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [100001 x i32], ptr @prime, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = mul nsw i32 %30, %34
  %36 = icmp sge i32 %35, 100001
  br i1 %36, label %37, label %38

37:                                               ; preds = %29
  store i32 5, ptr %2, align 4
  br label %81

38:                                               ; preds = %29
  %39 = load i32, ptr %1, align 4, !tbaa !5
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [100001 x i32], ptr @prime, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = mul nsw i32 %39, %43
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [100001 x i8], ptr @check, i64 0, i64 %45
  store i8 1, ptr %46, align 1, !tbaa !9
  %47 = load i32, ptr %1, align 4, !tbaa !5
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [100001 x i32], ptr @prime, i64 0, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = srem i32 %47, %51
  %53 = icmp eq i32 %52, 0
  br i1 %53, label %54, label %63

54:                                               ; preds = %38
  %55 = load i32, ptr %1, align 4, !tbaa !5
  %56 = load i32, ptr %3, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [100001 x i32], ptr @prime, i64 0, i64 %57
  %59 = load i32, ptr %58, align 4, !tbaa !5
  %60 = mul nsw i32 %55, %59
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [100001 x i32], ptr @mu, i64 0, i64 %61
  store i32 0, ptr %62, align 4, !tbaa !5
  store i32 5, ptr %2, align 4
  br label %81

63:                                               ; preds = %38
  %64 = load i32, ptr %1, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [100001 x i32], ptr @mu, i64 0, i64 %65
  %67 = load i32, ptr %66, align 4, !tbaa !5
  %68 = sub nsw i32 0, %67
  %69 = load i32, ptr %1, align 4, !tbaa !5
  %70 = load i32, ptr %3, align 4, !tbaa !5
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [100001 x i32], ptr @prime, i64 0, i64 %71
  %73 = load i32, ptr %72, align 4, !tbaa !5
  %74 = mul nsw i32 %69, %73
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [100001 x i32], ptr @mu, i64 0, i64 %75
  store i32 %68, ptr %76, align 4, !tbaa !5
  br label %77

77:                                               ; preds = %63
  br label %78

78:                                               ; preds = %77
  %79 = load i32, ptr %3, align 4, !tbaa !5
  %80 = add nsw i32 %79, 1
  store i32 %80, ptr %3, align 4, !tbaa !5
  br label %24, !llvm.loop !13

81:                                               ; preds = %54, %37, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %82

82:                                               ; preds = %81
  br label %83

83:                                               ; preds = %82
  %84 = load i32, ptr %1, align 4, !tbaa !5
  %85 = add nsw i32 %84, 1
  store i32 %85, ptr %1, align 4, !tbaa !5
  br label %4, !llvm.loop !15

86:                                               ; preds = %7
  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"

@prime = external dso_local local_unnamed_addr global [100001 x i32], align 16
@tot = external dso_local local_unnamed_addr global i32, align 4
@mu = external dso_local local_unnamed_addr global [100001 x i32], align 16
@check = external dso_local local_unnamed_addr global [100001 x i8], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z5calmuv() local_unnamed_addr #0 {
  store i32 1, ptr getelementptr inbounds (i8, ptr @mu, i64 4), align 4, !tbaa !5
  %tot.promoted = load i32, ptr @tot, align 4, !tbaa !5
  br label %2

1:                                                ; preds = %.loopexit
  ret void

2:                                                ; preds = %0, %.loopexit
  %indvars.iv25 = phi i64 [ 2, %0 ], [ %indvars.iv.next26, %.loopexit ]
  %3 = phi i32 [ %tot.promoted, %0 ], [ %14, %.loopexit ]
  %4 = getelementptr inbounds [100001 x i8], ptr @check, i64 0, i64 %indvars.iv25
  %5 = load i8, ptr %4, align 1, !tbaa !9, !range !11, !noundef !12
  %6 = trunc nuw i8 %5 to i1
  br i1 %6, label %13, label %7

7:                                                ; preds = %2
  %8 = add nsw i32 %3, 1
  store i32 %8, ptr @tot, align 4, !tbaa !5
  %9 = sext i32 %3 to i64
  %10 = getelementptr inbounds [100001 x i32], ptr @prime, i64 0, i64 %9
  %11 = trunc nuw nsw i64 %indvars.iv25 to i32
  store i32 %11, ptr %10, align 4, !tbaa !5
  %12 = getelementptr inbounds [100001 x i32], ptr @mu, i64 0, i64 %indvars.iv25
  store i32 -1, ptr %12, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %7, %2
  %14 = phi i32 [ %8, %7 ], [ %3, %2 ]
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %13
  %16 = getelementptr inbounds [100001 x i32], ptr @mu, i64 0, i64 %indvars.iv25
  %wide.trip.count = zext nneg i32 %14 to i64
  %17 = trunc nuw nsw i64 %indvars.iv25 to i32
  br label %18

18:                                               ; preds = %.lr.ph, %30
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %30 ]
  %19 = getelementptr inbounds [100001 x i32], ptr @prime, i64 0, i64 %indvars.iv
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = mul nsw i32 %20, %17
  %22 = icmp sgt i32 %21, 100000
  br i1 %22, label %.loopexit, label %23

23:                                               ; preds = %18
  %24 = sext i32 %21 to i64
  %25 = getelementptr inbounds [100001 x i8], ptr @check, i64 0, i64 %24
  store i8 1, ptr %25, align 1, !tbaa !9
  %26 = srem i32 %17, %20
  %27 = icmp eq i32 %26, 0
  br i1 %27, label %28, label %30

28:                                               ; preds = %23
  %29 = getelementptr inbounds [100001 x i32], ptr @mu, i64 0, i64 %24
  store i32 0, ptr %29, align 4, !tbaa !5
  br label %.loopexit

30:                                               ; preds = %23
  %31 = load i32, ptr %16, align 4, !tbaa !5
  %32 = sub nsw i32 0, %31
  %33 = getelementptr inbounds [100001 x i32], ptr @mu, i64 0, i64 %24
  store i32 %32, ptr %33, align 4, !tbaa !5
  %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 %.loopexit, label %18, !llvm.loop !13

.loopexit:                                        ; preds = %30, %18, %13, %28
  %indvars.iv.next26 = add nuw nsw i64 %indvars.iv25, 1
  %exitcond28.not = icmp eq i64 %indvars.iv.next26, 100001
  br i1 %exitcond28.not, label %1, label %2, !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,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass