; LLVM IR from upupming/algorithm :: acwing/寒假每日一题（入门组）/week3/1371. 货币系统.ll (Z14loop_optimizedv)

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

%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@n = external dso_local global i64, align 8
@m = external dso_local global i64, align 8
@v = external dso_local global [10010 x i64], align 16
@g = external dso_local global [10010 x i64], align 16
@_ZSt4cout = external global %"class.std::basic_ostream", align 8

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

4:                                                ; preds = %41, %0
  %5 = load i32, ptr %1, align 4, !tbaa !9
  %6 = sext i32 %5 to i64
  %7 = load i64, ptr @n, align 8, !tbaa !5
  %8 = icmp sle i64 %6, %7
  br i1 %8, label %10, label %9

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

10:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %11 = load i32, ptr %1, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [10010 x i64], ptr @v, i64 0, i64 %12
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %3, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %37, %10
  %17 = load i32, ptr %3, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = load i64, ptr @m, align 8, !tbaa !5
  %20 = icmp sle i64 %18, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %16
  store i32 5, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %40

22:                                               ; preds = %16
  %23 = load i32, ptr %3, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = load i32, ptr %1, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [10010 x i64], ptr @v, i64 0, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = sub nsw i64 %24, %28
  %30 = getelementptr inbounds [10010 x i64], ptr @g, i64 0, i64 %29
  %31 = load i64, ptr %30, align 8, !tbaa !5
  %32 = load i32, ptr %3, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [10010 x i64], ptr @g, i64 0, i64 %33
  %35 = load i64, ptr %34, align 8, !tbaa !5
  %36 = add nsw i64 %35, %31
  store i64 %36, ptr %34, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %22
  %38 = load i32, ptr %3, align 4, !tbaa !9
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %3, align 4, !tbaa !9
  br label %16, !llvm.loop !11

40:                                               ; preds = %21
  br label %41

41:                                               ; preds = %40
  %42 = load i32, ptr %1, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %1, align 4, !tbaa !9
  br label %4, !llvm.loop !13

44:                                               ; preds = %9
  %45 = load i64, ptr @m, align 8, !tbaa !5
  %46 = getelementptr inbounds [10010 x i64], ptr @g, i64 0, i64 %45
  %47 = load i64, ptr %46, align 8, !tbaa !5
  %48 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %47)
  %49 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8) %48, ptr noundef @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
  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

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef) #0 align 2

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) #0 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(ptr noundef nonnull align 8 dereferenceable(8)) #2

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 = { 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !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"

%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@n = external dso_local local_unnamed_addr global i64, align 8
@m = external dso_local local_unnamed_addr global i64, align 8
@v = external dso_local local_unnamed_addr global [10010 x i64], align 16
@g = external dso_local local_unnamed_addr global [10010 x i64], align 16
@_ZSt4cout = external global %"class.std::basic_ostream", align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_Z14loop_optimizedv() local_unnamed_addr #0 {
  store i64 1, ptr @g, align 16, !tbaa !5
  %1 = load i64, ptr @n, align 8, !tbaa !5
  %.not14 = icmp slt i64 %1, 1
  %.pre = load i64, ptr @m, align 8, !tbaa !5
  br i1 %.not14, label %._crit_edge18, label %.lr.ph17.preheader

.lr.ph17.preheader:                               ; preds = %0
  %2 = shl i64 %.pre, 3
  %3 = add i64 %2, 8
  %scevgep23 = getelementptr i8, ptr @g, i64 %3
  %scevgep25 = getelementptr i8, ptr @g, i64 %3
  %4 = add i64 %.pre, 1
  br label %.lr.ph17

._crit_edge18:                                    ; preds = %._crit_edge, %0
  %5 = getelementptr inbounds [10010 x i64], ptr @g, i64 0, i64 %.pre
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %7 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %6)
  %8 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8) %7, ptr noundef nonnull @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
  ret void

.lr.ph17:                                         ; preds = %.lr.ph17.preheader, %._crit_edge
  %indvars.iv20 = phi i64 [ %indvars.iv.next21, %._crit_edge ], [ 1, %.lr.ph17.preheader ]
  %9 = getelementptr inbounds [10010 x i64], ptr @v, i64 0, i64 %indvars.iv20
  %10 = load i64, ptr %9, align 8, !tbaa !5
  %sext = shl i64 %10, 32
  %11 = ashr exact i64 %sext, 32
  %.not1112 = icmp slt i64 %.pre, %11
  br i1 %.not1112, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.lr.ph17
  %12 = sub i64 %4, %11
  %min.iters.check = icmp ult i64 %12, 6
  br i1 %min.iters.check, label %.lr.ph.preheader30, label %vector.memcheck

.lr.ph.preheader30:                               ; preds = %middle.block, %vector.memcheck, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ %11, %vector.memcheck ], [ %11, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph

vector.memcheck:                                  ; preds = %.lr.ph.preheader
  %13 = ashr exact i64 %sext, 29
  %scevgep = getelementptr i8, ptr @g, i64 %13
  %14 = shl i64 %10, 3
  %15 = sub i64 %13, %14
  %scevgep24 = getelementptr i8, ptr @g, i64 %15
  %16 = mul i64 %10, -8
  %scevgep26 = getelementptr i8, ptr %scevgep25, i64 %16
  %bound0 = icmp ult ptr %scevgep, %scevgep26
  %bound1 = icmp ult ptr %scevgep24, %scevgep23
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %.lr.ph.preheader30, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i64 %12, -4
  %ind.end = add i64 %11, %n.vec
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = add i64 %11, %index
  %17 = sub nsw i64 %offset.idx, %10
  %18 = getelementptr inbounds [10010 x i64], ptr @g, i64 0, i64 %17
  %19 = getelementptr inbounds i8, ptr %18, i64 16
  %wide.load = load <2 x i64>, ptr %18, align 8, !tbaa !5, !alias.scope !9
  %wide.load27 = load <2 x i64>, ptr %19, align 8, !tbaa !5, !alias.scope !9
  %20 = getelementptr inbounds [10010 x i64], ptr @g, i64 0, i64 %offset.idx
  %21 = getelementptr inbounds i8, ptr %20, i64 16
  %wide.load28 = load <2 x i64>, ptr %20, align 8, !tbaa !5, !alias.scope !12, !noalias !9
  %wide.load29 = load <2 x i64>, ptr %21, align 8, !tbaa !5, !alias.scope !12, !noalias !9
  %22 = add nsw <2 x i64> %wide.load28, %wide.load
  %23 = add nsw <2 x i64> %wide.load29, %wide.load27
  store <2 x i64> %22, ptr %20, align 8, !tbaa !5, !alias.scope !12, !noalias !9
  store <2 x i64> %23, ptr %21, align 8, !tbaa !5, !alias.scope !12, !noalias !9
  %index.next = add nuw i64 %index, 4
  %24 = icmp eq i64 %index.next, %n.vec
  br i1 %24, label %middle.block, label %vector.body, !llvm.loop !14

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %12, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader30

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %.lr.ph17
  %indvars.iv.next21 = add nuw nsw i64 %indvars.iv20, 1
  %exitcond = icmp eq i64 %indvars.iv20, %1
  br i1 %exitcond, label %._crit_edge18, label %.lr.ph17, !llvm.loop !18

.lr.ph:                                           ; preds = %.lr.ph.preheader30, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader30 ]
  %25 = sub nsw i64 %indvars.iv, %10
  %26 = getelementptr inbounds [10010 x i64], ptr @g, i64 0, i64 %25
  %27 = load i64, ptr %26, align 8, !tbaa !5
  %28 = getelementptr inbounds [10010 x i64], ptr @g, i64 0, i64 %indvars.iv
  %29 = load i64, ptr %28, align 8, !tbaa !5
  %30 = add nsw i64 %29, %27
  store i64 %30, ptr %28, align 8, !tbaa !5
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %.not11.not = icmp sgt i64 %.pre, %indvars.iv
  br i1 %.not11.not, label %.lr.ph, label %._crit_edge, !llvm.loop !19
}

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) local_unnamed_addr #0 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(ptr noundef nonnull align 8 dereferenceable(8)) #1

attributes #0 = { 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 #1 = { inlinehint 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" }

!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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10}
!10 = distinct !{!10, !11}
!11 = distinct !{!11, !"LVerDomain"}
!12 = !{!13}
!13 = distinct !{!13, !11}
!14 = distinct !{!14, !15, !16, !17}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!"llvm.loop.isvectorized", i32 1}
!17 = !{!"llvm.loop.unroll.runtime.disable"}
!18 = distinct !{!18, !15}
!19 = distinct !{!19, !15, !16}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,SROAPass,SimplifyCFGPass,TailCallElimPass