; LLVM IR from Lodour/ACM-CheatSheet :: src/2_数学/7_莫比乌斯反演 (2_n个数中互质数对数)

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

@ans = external dso_local global i64, align 8
@n = external dso_local global i32, align 4
@x = external dso_local global i32, align 4
@_max = external dso_local global i32, align 4
@b = external dso_local global [100001 x i32], align 16
@mu = external dso_local global [100001 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z5calmuv() #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 norecurse uwtable
define dso_local noundef i32 @main() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  store i32 0, ptr %1, align 4
  call void @_Z5calmuv()
  br label %7

7:                                                ; preds = %76, %0
  %8 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  %9 = icmp eq i32 %8, 1
  br i1 %9, label %10, label %83

10:                                               ; preds = %7
  call void @llvm.memset.p0.i64(ptr align 16 @b, i8 0, i64 400004, i1 false)
  store i32 0, ptr @_max, align 4, !tbaa !5
  store i64 0, ptr @ans, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %29, %10
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = load i32, ptr @n, align 4, !tbaa !5
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  br label %32

16:                                               ; preds = %11
  %17 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @x)
  %18 = load i32, ptr @x, align 4, !tbaa !5
  %19 = load i32, ptr @_max, align 4, !tbaa !5
  %20 = icmp sgt i32 %18, %19
  br i1 %20, label %21, label %23

21:                                               ; preds = %16
  %22 = load i32, ptr @x, align 4, !tbaa !5
  store i32 %22, ptr @_max, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %21, %16
  %24 = load i32, ptr @x, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [100001 x i32], ptr @b, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %26, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %23
  %30 = load i32, ptr %2, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr %2, align 4, !tbaa !5
  br label %11, !llvm.loop !11

32:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %73, %32
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = load i32, ptr @_max, align 4, !tbaa !5
  %36 = icmp sle i32 %34, %35
  br i1 %36, label %38, label %37

37:                                               ; preds = %33
  store i32 7, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  br label %76

38:                                               ; preds = %33
  store i32 0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  store i64 %40, ptr %6, align 8, !tbaa !9
  br label %41

41:                                               ; preds = %53, %38
  %42 = load i64, ptr %6, align 8, !tbaa !9
  %43 = load i32, ptr @_max, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = icmp sle i64 %42, %44
  br i1 %45, label %47, label %46

46:                                               ; preds = %41
  store i32 10, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  br label %58

47:                                               ; preds = %41
  %48 = load i64, ptr %6, align 8, !tbaa !9
  %49 = getelementptr inbounds [100001 x i32], ptr @b, i64 0, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !5
  %51 = load i32, ptr %3, align 4, !tbaa !5
  %52 = add nsw i32 %51, %50
  store i32 %52, ptr %3, align 4, !tbaa !5
  br label %53

53:                                               ; preds = %47
  %54 = load i32, ptr %4, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = load i64, ptr %6, align 8, !tbaa !9
  %57 = add nsw i64 %56, %55
  store i64 %57, ptr %6, align 8, !tbaa !9
  br label %41, !llvm.loop !13

58:                                               ; preds = %46
  %59 = load i32, ptr %4, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [100001 x i32], ptr @mu, i64 0, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = mul nsw i64 1, %63
  %65 = load i32, ptr %3, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = mul nsw i64 %64, %66
  %68 = load i32, ptr %3, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = mul nsw i64 %67, %69
  %71 = load i64, ptr @ans, align 8, !tbaa !9
  %72 = add nsw i64 %71, %70
  store i64 %72, ptr @ans, align 8, !tbaa !9
  br label %73

73:                                               ; preds = %58
  %74 = load i32, ptr %4, align 4, !tbaa !5
  %75 = add nsw i32 %74, 1
  store i32 %75, ptr %4, align 4, !tbaa !5
  br label %33, !llvm.loop !14

76:                                               ; preds = %37
  %77 = load i64, ptr @ans, align 8, !tbaa !9
  %78 = load i32, ptr getelementptr inbounds ([100001 x i32], ptr @b, i64 0, i64 1), align 4, !tbaa !5
  %79 = sext i32 %78 to i64
  %80 = sub nsw i64 %77, %79
  %81 = sdiv i64 %80, 2
  %82 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i64 noundef %81)
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  br label %7, !llvm.loop !15

83:                                               ; preds = %7
  ret i32 0
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #3

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

declare i32 @printf(ptr noundef, ...) #3

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 = { mustprogress norecurse 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 = { "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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { 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 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}
!15 = distinct !{!15, !12}


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

@ans = external dso_local local_unnamed_addr global i64, align 8
@n = external dso_local global i32, align 4
@x = external dso_local global i32, align 4
@_max = external dso_local local_unnamed_addr global i32, align 4
@b = external dso_local local_unnamed_addr global [100001 x i32], align 16
@mu = external dso_local local_unnamed_addr global [100001 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z5calmuv() local_unnamed_addr #0

; Function Attrs: mustprogress norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  tail call void @_Z5calmuv()
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %2 = icmp eq i32 %1, 1
  br i1 %2, label %.lr.ph28, label %._crit_edge29

.lr.ph28:                                         ; preds = %0, %33
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(400004) @b, i8 0, i64 400004, i1 false)
  store i32 0, ptr @_max, align 4, !tbaa !5
  store i64 0, ptr @ans, align 8, !tbaa !9
  %3 = load i32, ptr @n, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %.preheader._crit_edge

.preheader:                                       ; preds = %13
  %.not23 = icmp slt i32 %14, 1
  br i1 %.not23, label %.preheader._crit_edge, label %.lr.ph25

.preheader._crit_edge:                            ; preds = %.lr.ph28, %.preheader
  %.pre = load i64, ptr @ans, align 8, !tbaa !9
  br label %33

.lr.ph25:                                         ; preds = %.preheader
  %ans.promoted = load i64, ptr @ans, align 8, !tbaa !9
  %5 = zext nneg i32 %14 to i64
  %6 = zext nneg i32 %14 to i64
  %xtraiter = and i64 %6, 1
  %7 = icmp eq i32 %14, 1
  br i1 %7, label %._crit_edge26.unr-lcssa, label %.lr.ph25.new

.lr.ph25.new:                                     ; preds = %.lr.ph25
  %unroll_iter = and i64 %6, 2147483646
  br label %.lr.ph22.preheader

.lr.ph:                                           ; preds = %.lr.ph28, %13
  %.01518 = phi i32 [ %19, %13 ], [ 0, %.lr.ph28 ]
  %8 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @x)
  %9 = load i32, ptr @x, align 4, !tbaa !5
  %10 = load i32, ptr @_max, align 4, !tbaa !5
  %11 = icmp sgt i32 %9, %10
  br i1 %11, label %12, label %13

12:                                               ; preds = %.lr.ph
  store i32 %9, ptr @_max, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %12, %.lr.ph
  %14 = phi i32 [ %9, %12 ], [ %10, %.lr.ph ]
  %15 = sext i32 %9 to i64
  %16 = getelementptr inbounds [100001 x i32], ptr @b, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr %16, align 4, !tbaa !5
  %19 = add nuw nsw i32 %.01518, 1
  %20 = load i32, ptr @n, align 4, !tbaa !5
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %.lr.ph, label %.preheader, !llvm.loop !11

._crit_edge26.unr-lcssa:                          ; preds = %._crit_edge.1, %.lr.ph25
  %.lcssa38.ph = phi i64 [ poison, %.lr.ph25 ], [ %60, %._crit_edge.1 ]
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph25 ], [ %indvars.iv.next.1, %._crit_edge.1 ]
  %.unr = phi i64 [ %ans.promoted, %.lr.ph25 ], [ %60, %._crit_edge.1 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge26, label %.lr.ph22.epil

.lr.ph22.epil:                                    ; preds = %._crit_edge26.unr-lcssa, %.lr.ph22.epil
  %.021.epil = phi i64 [ %25, %.lr.ph22.epil ], [ %indvars.iv.unr, %._crit_edge26.unr-lcssa ]
  %.01420.epil = phi i32 [ %24, %.lr.ph22.epil ], [ 0, %._crit_edge26.unr-lcssa ]
  %22 = getelementptr inbounds [100001 x i32], ptr @b, i64 0, i64 %.021.epil
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = add nsw i32 %23, %.01420.epil
  %25 = add nuw nsw i64 %.021.epil, %indvars.iv.unr
  %.not17.epil = icmp ugt i64 %25, %5
  br i1 %.not17.epil, label %._crit_edge.epil, label %.lr.ph22.epil, !llvm.loop !13

._crit_edge.epil:                                 ; preds = %.lr.ph22.epil
  %26 = getelementptr inbounds [100001 x i32], ptr @mu, i64 0, i64 %indvars.iv.unr
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = sext i32 %24 to i64
  %30 = mul nsw i64 %29, %29
  %31 = mul i64 %30, %28
  %32 = add nsw i64 %31, %.unr
  br label %._crit_edge26

._crit_edge26:                                    ; preds = %._crit_edge26.unr-lcssa, %._crit_edge.epil
  %.lcssa38 = phi i64 [ %.lcssa38.ph, %._crit_edge26.unr-lcssa ], [ %32, %._crit_edge.epil ]
  store i64 %.lcssa38, ptr @ans, align 8, !tbaa !9
  br label %33

33:                                               ; preds = %.preheader._crit_edge, %._crit_edge26
  %34 = phi i64 [ %.pre, %.preheader._crit_edge ], [ %.lcssa38, %._crit_edge26 ]
  %35 = load i32, ptr getelementptr inbounds (i8, ptr @b, i64 4), align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = sub nsw i64 %34, %36
  %38 = sdiv i64 %37, 2
  %39 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i64 noundef %38)
  %40 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %41 = icmp eq i32 %40, 1
  br i1 %41, label %.lr.ph28, label %._crit_edge29, !llvm.loop !14

.lr.ph22.preheader:                               ; preds = %._crit_edge.1, %.lr.ph25.new
  %indvars.iv = phi i64 [ 1, %.lr.ph25.new ], [ %indvars.iv.next.1, %._crit_edge.1 ]
  %42 = phi i64 [ %ans.promoted, %.lr.ph25.new ], [ %60, %._crit_edge.1 ]
  %niter = phi i64 [ 0, %.lr.ph25.new ], [ %niter.next.1, %._crit_edge.1 ]
  br label %.lr.ph22

._crit_edge:                                      ; preds = %.lr.ph22
  %43 = getelementptr inbounds [100001 x i32], ptr @mu, i64 0, i64 %indvars.iv
  %44 = load i32, ptr %43, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = sext i32 %63 to i64
  %47 = mul nsw i64 %46, %46
  %48 = mul i64 %47, %45
  %49 = add nsw i64 %48, %42
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  br label %.lr.ph22.1

.lr.ph22.1:                                       ; preds = %.lr.ph22.1, %._crit_edge
  %.021.1 = phi i64 [ %53, %.lr.ph22.1 ], [ %indvars.iv.next, %._crit_edge ]
  %.01420.1 = phi i32 [ %52, %.lr.ph22.1 ], [ 0, %._crit_edge ]
  %50 = getelementptr inbounds [100001 x i32], ptr @b, i64 0, i64 %.021.1
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = add nsw i32 %51, %.01420.1
  %53 = add nuw nsw i64 %.021.1, %indvars.iv.next
  %.not17.1 = icmp ugt i64 %53, %5
  br i1 %.not17.1, label %._crit_edge.1, label %.lr.ph22.1, !llvm.loop !13

._crit_edge.1:                                    ; preds = %.lr.ph22.1
  %54 = getelementptr inbounds [100001 x i32], ptr @mu, i64 0, i64 %indvars.iv.next
  %55 = load i32, ptr %54, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = sext i32 %52 to i64
  %58 = mul nsw i64 %57, %57
  %59 = mul i64 %58, %56
  %60 = add nsw i64 %59, %49
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add nuw i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge26.unr-lcssa, label %.lr.ph22.preheader, !llvm.loop !15

.lr.ph22:                                         ; preds = %.lr.ph22.preheader, %.lr.ph22
  %.021 = phi i64 [ %64, %.lr.ph22 ], [ %indvars.iv, %.lr.ph22.preheader ]
  %.01420 = phi i32 [ %63, %.lr.ph22 ], [ 0, %.lr.ph22.preheader ]
  %61 = getelementptr inbounds [100001 x i32], ptr @b, i64 0, i64 %.021
  %62 = load i32, ptr %61, align 4, !tbaa !5
  %63 = add nsw i32 %62, %.01420
  %64 = add nuw nsw i64 %.021, %indvars.iv
  %.not17 = icmp ugt i64 %64, %5
  br i1 %.not17, label %._crit_edge, label %.lr.ph22, !llvm.loop !13

._crit_edge29:                                    ; preds = %33, %0
  ret i32 0
}

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

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

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

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 = { mustprogress norecurse 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 = { nofree nounwind "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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

!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 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}
!15 = distinct !{!15, !12}


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