; LLVM IR from upupming/algorithm :: acwing/算法竞赛进阶指南/0x29-总结与练习/194. 涂满它！.ll (Z9cntColorsv)

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

@n = external dso_local global i32, align 4
@a = external dso_local global [10 x [10 x i32]], align 16
@v = external dso_local global [10 x [10 x i32]], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z9cntColorsv() #0 {
  %1 = alloca [6 x i32], align 16
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 24, ptr %1) #3
  call void @llvm.memset.p0.i64(ptr align 16 %1, i8 0, i64 24, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %42, %0
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = load i32, ptr @n, align 4, !tbaa !5
  %10 = icmp slt i32 %8, %9
  br i1 %10, label %12, label %11

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

12:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %38, %12
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = load i32, ptr @n, align 4, !tbaa !5
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  store i32 5, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %41

18:                                               ; preds = %13
  %19 = load i32, ptr %2, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [10 x [10 x i32]], ptr @v, i64 0, i64 %20
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [10 x i32], ptr %21, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = icmp ne i32 %25, 1
  br i1 %26, label %27, label %37

27:                                               ; preds = %18
  %28 = load i32, ptr %2, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [10 x [10 x i32]], ptr @a, i64 0, i64 %29
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [10 x i32], ptr %30, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [6 x i32], ptr %1, i64 0, i64 %35
  store i32 1, ptr %36, align 4, !tbaa !5
  br label %37

37:                                               ; preds = %27, %18
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %4, align 4, !tbaa !5
  br label %13, !llvm.loop !9

41:                                               ; preds = %17
  br label %42

42:                                               ; preds = %41
  %43 = load i32, ptr %2, align 4, !tbaa !5
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %2, align 4, !tbaa !5
  br label %7, !llvm.loop !11

45:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %57, %45
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = icmp slt i32 %47, 6
  br i1 %48, label %50, label %49

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

50:                                               ; preds = %46
  %51 = load i32, ptr %6, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [6 x i32], ptr %1, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = load i32, ptr %5, align 4, !tbaa !5
  %56 = add nsw i32 %55, %54
  store i32 %56, ptr %5, align 4, !tbaa !5
  br label %57

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

60:                                               ; preds = %49
  %61 = load i32, ptr %5, align 4, !tbaa !5
  store i32 1, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 24, ptr %1) #3
  ret i32 %61
}

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"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}
!12 = distinct !{!12, !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"

@n = external dso_local local_unnamed_addr global i32, align 4
@a = external dso_local local_unnamed_addr global [10 x [10 x i32]], align 16
@v = external dso_local local_unnamed_addr global [10 x [10 x i32]], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z9cntColorsv() local_unnamed_addr #0 {
  %1 = alloca [6 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %1) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(24) %1, i8 0, i64 24, i1 false)
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.preheader16.us.preheader, label %.preheader

.preheader16.us.preheader:                        ; preds = %0
  %wide.trip.count25 = zext nneg i32 %2 to i64
  %xtraiter = and i64 %wide.trip.count25, 1
  %4 = icmp eq i32 %2, 1
  %unroll_iter = and i64 %wide.trip.count25, 2147483646
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader16.us

.preheader16.us:                                  ; preds = %.preheader16.us.preheader, %._crit_edge.us
  %indvars.iv22 = phi i64 [ 0, %.preheader16.us.preheader ], [ %indvars.iv.next23, %._crit_edge.us ]
  br i1 %4, label %._crit_edge.us.unr-lcssa, label %.preheader16.us.new

.preheader16.us.new:                              ; preds = %.preheader16.us, %20
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %20 ], [ 0, %.preheader16.us ]
  %niter = phi i64 [ %niter.next.1, %20 ], [ 0, %.preheader16.us ]
  %5 = getelementptr inbounds [10 x [10 x i32]], ptr @v, i64 0, i64 %indvars.iv22, i64 %indvars.iv
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %.not.us = icmp eq i32 %6, 1
  br i1 %.not.us, label %12, label %7

7:                                                ; preds = %.preheader16.us.new
  %8 = getelementptr inbounds [10 x [10 x i32]], ptr @a, i64 0, i64 %indvars.iv22, i64 %indvars.iv
  %9 = load i32, ptr %8, align 8, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [6 x i32], ptr %1, i64 0, i64 %10
  store i32 1, ptr %11, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %7, %.preheader16.us.new
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %13 = getelementptr inbounds [10 x [10 x i32]], ptr @v, i64 0, i64 %indvars.iv22, i64 %indvars.iv.next
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %.not.us.1 = icmp eq i32 %14, 1
  br i1 %.not.us.1, label %20, label %15

15:                                               ; preds = %12
  %16 = getelementptr inbounds [10 x [10 x i32]], ptr @a, i64 0, i64 %indvars.iv22, i64 %indvars.iv.next
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [6 x i32], ptr %1, i64 0, i64 %18
  store i32 1, ptr %19, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %15, %12
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.us.unr-lcssa, label %.preheader16.us.new, !llvm.loop !9

._crit_edge.us.unr-lcssa:                         ; preds = %20, %.preheader16.us
  %indvars.iv.unr = phi i64 [ 0, %.preheader16.us ], [ %indvars.iv.next.1, %20 ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.us.unr-lcssa
  %21 = getelementptr inbounds [10 x [10 x i32]], ptr @v, i64 0, i64 %indvars.iv22, i64 %indvars.iv.unr
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %.not.us.epil = icmp eq i32 %22, 1
  br i1 %.not.us.epil, label %._crit_edge.us, label %23

23:                                               ; preds = %.epil.preheader
  %24 = getelementptr inbounds [10 x [10 x i32]], ptr @a, i64 0, i64 %indvars.iv22, i64 %indvars.iv.unr
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [6 x i32], ptr %1, i64 0, i64 %26
  store i32 1, ptr %27, align 4, !tbaa !5
  br label %._crit_edge.us

._crit_edge.us:                                   ; preds = %.epil.preheader, %23, %._crit_edge.us.unr-lcssa
  %indvars.iv.next23 = add nuw nsw i64 %indvars.iv22, 1
  %exitcond26.not = icmp eq i64 %indvars.iv.next23, %wide.trip.count25
  br i1 %exitcond26.not, label %.preheader.loopexit, label %.preheader16.us, !llvm.loop !11

.preheader.loopexit:                              ; preds = %._crit_edge.us
  %28 = load <4 x i32>, ptr %1, align 16, !tbaa !5
  %.phi.trans.insert36 = getelementptr inbounds i8, ptr %1, i64 16
  %.pre37 = load i32, ptr %.phi.trans.insert36, align 16, !tbaa !5
  %.phi.trans.insert38 = getelementptr inbounds i8, ptr %1, i64 20
  %.pre39 = load i32, ptr %.phi.trans.insert38, align 4, !tbaa !5
  %29 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %28)
  %op.rdx = add i32 %29, %.pre37
  %op.rdx40 = add i32 %op.rdx, %.pre39
  br label %.preheader

.preheader:                                       ; preds = %.preheader.loopexit, %0
  %30 = phi i32 [ %op.rdx40, %.preheader.loopexit ], [ 0, %0 ]
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %1) #4
  ret i32 %30
}

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

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

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #3

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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}


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