; LLVM IR from upupming/algorithm :: acwing/算法竞赛进阶指南/0x51-线性DP/273. 分级.ll (Z4workv)

; 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 [2010 x i32], align 16
@b = external dso_local global [2010 x i32], align 16
@f = external dso_local global [2010 x [2010 x i32]], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z4workv() #0 {
  %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 i32, align 4
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #5
  store i32 1, ptr %1, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %21, %0
  %9 = load i32, ptr %1, align 4, !tbaa !5
  %10 = load i32, ptr @n, align 4, !tbaa !5
  %11 = icmp sle i32 %9, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #5
  br label %24

13:                                               ; preds = %8
  %14 = load i32, ptr %1, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [2010 x i32], ptr @a, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = load i32, ptr %1, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [2010 x i32], ptr @b, i64 0, i64 %19
  store i32 %17, ptr %20, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %13
  %22 = load i32, ptr %1, align 4, !tbaa !5
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %1, align 4, !tbaa !5
  br label %8, !llvm.loop !9

24:                                               ; preds = %12
  %25 = load i32, ptr @n, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr @b, i64 %26
  %28 = getelementptr inbounds i32, ptr %27, i64 1
  call void @_ZSt4sortIPiEvT_S1_(ptr noundef getelementptr inbounds (i32, ptr @b, i64 1), ptr noundef %28)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %72, %24
  %30 = load i32, ptr %2, align 4, !tbaa !5
  %31 = load i32, ptr @n, align 4, !tbaa !5
  %32 = icmp sle i32 %30, %31
  br i1 %32, label %34, label %33

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

34:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  store i32 1061109567, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  store i32 1, ptr %5, align 4, !tbaa !5
  br label %35

35:                                               ; preds = %68, %34
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = load i32, ptr @n, align 4, !tbaa !5
  %38 = icmp sle i32 %36, %37
  br i1 %38, label %40, label %39

39:                                               ; preds = %35
  store i32 8, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %71

40:                                               ; preds = %35
  %41 = load i32, ptr %2, align 4, !tbaa !5
  %42 = sub nsw i32 %41, 1
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [2010 x [2010 x i32]], ptr @f, i64 0, i64 %43
  %45 = load i32, ptr %5, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [2010 x i32], ptr %44, i64 0, i64 %46
  %48 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %4, ptr noundef nonnull align 4 dereferenceable(4) %47)
  %49 = load i32, ptr %48, align 4, !tbaa !5
  store i32 %49, ptr %4, align 4, !tbaa !5
  %50 = load i32, ptr %4, align 4, !tbaa !5
  %51 = load i32, ptr %2, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [2010 x i32], ptr @a, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = load i32, ptr %5, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [2010 x i32], ptr @b, i64 0, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !5
  %59 = sub nsw i32 %54, %58
  %60 = call i32 @llvm.abs.i32(i32 %59, i1 true)
  %61 = add nsw i32 %50, %60
  %62 = load i32, ptr %2, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [2010 x [2010 x i32]], ptr @f, i64 0, i64 %63
  %65 = load i32, ptr %5, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [2010 x i32], ptr %64, i64 0, i64 %66
  store i32 %61, ptr %67, align 4, !tbaa !5
  br label %68

68:                                               ; preds = %40
  %69 = load i32, ptr %5, align 4, !tbaa !5
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %5, align 4, !tbaa !5
  br label %35, !llvm.loop !11

71:                                               ; preds = %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  br label %72

72:                                               ; preds = %71
  %73 = load i32, ptr %2, align 4, !tbaa !5
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %2, align 4, !tbaa !5
  br label %29, !llvm.loop !12

75:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  store i32 1061109567, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  store i32 1, ptr %7, align 4, !tbaa !5
  br label %76

76:                                               ; preds = %90, %75
  %77 = load i32, ptr %7, align 4, !tbaa !5
  %78 = load i32, ptr @n, align 4, !tbaa !5
  %79 = icmp sle i32 %77, %78
  br i1 %79, label %81, label %80

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

81:                                               ; preds = %76
  %82 = load i32, ptr @n, align 4, !tbaa !5
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds [2010 x [2010 x i32]], ptr @f, i64 0, i64 %83
  %85 = load i32, ptr %7, align 4, !tbaa !5
  %86 = sext i32 %85 to i64
  %87 = getelementptr inbounds [2010 x i32], ptr %84, i64 0, i64 %86
  %88 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %6, ptr noundef nonnull align 4 dereferenceable(4) %87)
  %89 = load i32, ptr %88, align 4, !tbaa !5
  store i32 %89, ptr %6, align 4, !tbaa !5
  br label %90

90:                                               ; preds = %81
  %91 = load i32, ptr %7, align 4, !tbaa !5
  %92 = add nsw i32 %91, 1
  store i32 %92, ptr %7, align 4, !tbaa !5
  br label %76, !llvm.loop !13

93:                                               ; preds = %80
  %94 = load i32, ptr %6, align 4, !tbaa !5
  store i32 1, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  ret i32 %94
}

; 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: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPiEvT_S1_(ptr noundef, ptr noundef) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #4

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 = { inlinehint 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 #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !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 [2010 x i32], align 16
@b = external dso_local global [2010 x i32], align 16
@f = external dso_local global [2010 x [2010 x i32]], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z4workv() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = load i32, ptr @n, align 4, !tbaa !5
  %.not25 = icmp slt i32 %3, 1
  br i1 %.not25, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %0
  %4 = zext nneg i32 %3 to i64
  %5 = shl nuw nsw i64 %4, 2
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 4 getelementptr inbounds (i8, ptr @b, i64 4), ptr nonnull align 4 getelementptr inbounds (i8, ptr @a, i64 4), i64 %5, i1 false), !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.preheader, %0
  %6 = sext i32 %3 to i64
  %7 = getelementptr inbounds i32, ptr @b, i64 %6
  %8 = getelementptr inbounds i8, ptr %7, i64 4
  tail call void @_ZSt4sortIPiEvT_S1_(ptr noundef nonnull getelementptr inbounds (i8, ptr @b, i64 4), ptr noundef nonnull %8)
  %9 = load i32, ptr @n, align 4, !tbaa !5
  %.not2232 = icmp slt i32 %9, 1
  br i1 %.not2232, label %._crit_edge36.thread, label %.lr.ph35.split

._crit_edge36.thread:                             ; preds = %._crit_edge
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #6
  br label %._crit_edge41

._crit_edge36:                                    ; preds = %._crit_edge31
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #6
  store i32 1061109567, ptr %2, align 4, !tbaa !5
  %.not2337 = icmp slt i32 %13, 1
  br i1 %.not2337, label %._crit_edge41, label %.lr.ph40

.lr.ph35.split:                                   ; preds = %._crit_edge, %._crit_edge31
  %10 = phi i32 [ %13, %._crit_edge31 ], [ %9, %._crit_edge ]
  %indvars.iv45 = phi i64 [ %indvars.iv.next46, %._crit_edge31 ], [ 1, %._crit_edge ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #6
  store i32 1061109567, ptr %1, align 4, !tbaa !5
  %.not2427 = icmp slt i32 %10, 1
  br i1 %.not2427, label %.lr.ph35.split.._crit_edge31_crit_edge, label %.lr.ph30

.lr.ph35.split.._crit_edge31_crit_edge:           ; preds = %.lr.ph35.split
  %.pre = sext i32 %10 to i64
  br label %._crit_edge31

.lr.ph30:                                         ; preds = %.lr.ph35.split
  %11 = add nsw i64 %indvars.iv45, -1
  %12 = getelementptr inbounds [2010 x i32], ptr @a, i64 0, i64 %indvars.iv45
  br label %14

._crit_edge31:                                    ; preds = %14, %.lr.ph35.split.._crit_edge31_crit_edge
  %.pre-phi = phi i64 [ %.pre, %.lr.ph35.split.._crit_edge31_crit_edge ], [ %26, %14 ]
  %13 = phi i32 [ %10, %.lr.ph35.split.._crit_edge31_crit_edge ], [ %25, %14 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #6
  %indvars.iv.next46 = add nuw nsw i64 %indvars.iv45, 1
  %.not22.not = icmp slt i64 %indvars.iv45, %.pre-phi
  br i1 %.not22.not, label %.lr.ph35.split, label %._crit_edge36, !llvm.loop !9

14:                                               ; preds = %.lr.ph30, %14
  %indvars.iv = phi i64 [ 1, %.lr.ph30 ], [ %indvars.iv.next, %14 ]
  %15 = getelementptr inbounds [2010 x [2010 x i32]], ptr @f, i64 0, i64 %11, i64 %indvars.iv
  %16 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %1, ptr noundef nonnull align 4 dereferenceable(4) %15)
  %17 = load i32, ptr %16, align 4, !tbaa !5
  store i32 %17, ptr %1, align 4, !tbaa !5
  %18 = load i32, ptr %12, align 4, !tbaa !5
  %19 = getelementptr inbounds [2010 x i32], ptr @b, i64 0, i64 %indvars.iv
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = sub nsw i32 %18, %20
  %22 = call i32 @llvm.abs.i32(i32 %21, i1 true)
  %23 = add nsw i32 %22, %17
  %24 = getelementptr inbounds [2010 x [2010 x i32]], ptr @f, i64 0, i64 %indvars.iv45, i64 %indvars.iv
  store i32 %23, ptr %24, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %25 = load i32, ptr @n, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %.not24.not = icmp slt i64 %indvars.iv, %26
  br i1 %.not24.not, label %14, label %._crit_edge31, !llvm.loop !12

._crit_edge41:                                    ; preds = %.lr.ph40, %._crit_edge36.thread, %._crit_edge36
  %27 = phi i32 [ 1061109567, %._crit_edge36 ], [ 1061109567, %._crit_edge36.thread ], [ %32, %.lr.ph40 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #6
  ret i32 %27

.lr.ph40:                                         ; preds = %._crit_edge36, %.lr.ph40
  %indvars.iv48 = phi i64 [ %indvars.iv.next49, %.lr.ph40 ], [ 1, %._crit_edge36 ]
  %28 = phi i32 [ %33, %.lr.ph40 ], [ %13, %._crit_edge36 ]
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [2010 x [2010 x i32]], ptr @f, i64 0, i64 %29, i64 %indvars.iv48
  %31 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %2, ptr noundef nonnull align 4 dereferenceable(4) %30)
  %32 = load i32, ptr %31, align 4, !tbaa !5
  store i32 %32, ptr %2, align 4, !tbaa !5
  %indvars.iv.next49 = add nuw nsw i64 %indvars.iv48, 1
  %33 = load i32, ptr @n, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %.not23.not = icmp slt i64 %indvars.iv48, %34
  br i1 %.not23.not, label %.lr.ph40, label %._crit_edge41, !llvm.loop !13
}

; 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPiEvT_S1_(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #3

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #4

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #5

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 = { mustprogress 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 = { inlinehint 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 #4 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #6 = { 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, !11}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.unswitch.partial.disable"}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !10}


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