; LLVM IR from upupming/algorithm :: acwing/算法竞赛进阶指南/0x08-总结与练习/121. 赶牛入圈.ll (Z5validi)

; 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::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@c = external dso_local global i32, align 4
@sum = external dso_local global [1010 x [1010 x i32]], align 16
@numbers = external dso_local global %"class.std::vector", align 8

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

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

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

9:                                                ; preds = %100, %1
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) @numbers) #2
  %13 = icmp ult i64 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %9
  store i32 2, ptr %6, align 4
  br label %103

15:                                               ; preds = %9
  br label %16

16:                                               ; preds = %30, %15
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @numbers, i64 noundef %18) #2
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = add nsw i32 %21, 1
  %23 = sext i32 %22 to i64
  %24 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @numbers, i64 noundef %23) #2
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = sub nsw i32 %20, %25
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = sub nsw i32 %27, 1
  %29 = icmp sgt i32 %26, %28
  br i1 %29, label %30, label %33

30:                                               ; preds = %16
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %4, align 4, !tbaa !5
  br label %16, !llvm.loop !9

33:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 1, ptr %8, align 4, !tbaa !5
  br label %34

34:                                               ; preds = %94, %33
  %35 = load i32, ptr %8, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) @numbers) #2
  %38 = icmp ult i64 %36, %37
  br i1 %38, label %40, label %39

39:                                               ; preds = %34
  store i32 7, ptr %6, align 4
  br label %97

40:                                               ; preds = %34
  br label %41

41:                                               ; preds = %55, %40
  %42 = load i32, ptr %8, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @numbers, i64 noundef %43) #2
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = load i32, ptr %7, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  %48 = sext i32 %47 to i64
  %49 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @numbers, i64 noundef %48) #2
  %50 = load i32, ptr %49, align 4, !tbaa !5
  %51 = sub nsw i32 %45, %50
  %52 = load i32, ptr %3, align 4, !tbaa !5
  %53 = sub nsw i32 %52, 1
  %54 = icmp sgt i32 %51, %53
  br i1 %54, label %55, label %58

55:                                               ; preds = %41
  %56 = load i32, ptr %7, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %7, align 4, !tbaa !5
  br label %41, !llvm.loop !11

58:                                               ; preds = %41
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [1010 x [1010 x i32]], ptr @sum, i64 0, i64 %60
  %62 = load i32, ptr %8, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [1010 x i32], ptr %61, i64 0, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !5
  %66 = load i32, ptr %5, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [1010 x [1010 x i32]], ptr @sum, i64 0, i64 %67
  %69 = load i32, ptr %7, align 4, !tbaa !5
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [1010 x i32], ptr %68, i64 0, i64 %70
  %72 = load i32, ptr %71, align 4, !tbaa !5
  %73 = sub nsw i32 %65, %72
  %74 = load i32, ptr %4, align 4, !tbaa !5
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [1010 x [1010 x i32]], ptr @sum, i64 0, i64 %75
  %77 = load i32, ptr %8, align 4, !tbaa !5
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds [1010 x i32], ptr %76, i64 0, i64 %78
  %80 = load i32, ptr %79, align 4, !tbaa !5
  %81 = sub nsw i32 %73, %80
  %82 = load i32, ptr %4, align 4, !tbaa !5
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds [1010 x [1010 x i32]], ptr @sum, i64 0, i64 %83
  %85 = load i32, ptr %7, align 4, !tbaa !5
  %86 = sext i32 %85 to i64
  %87 = getelementptr inbounds [1010 x i32], ptr %84, i64 0, i64 %86
  %88 = load i32, ptr %87, align 4, !tbaa !5
  %89 = add nsw i32 %81, %88
  %90 = load i32, ptr @c, align 4, !tbaa !5
  %91 = icmp sge i32 %89, %90
  br i1 %91, label %92, label %93

92:                                               ; preds = %58
  store i1 true, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %97

93:                                               ; preds = %58
  br label %94

94:                                               ; preds = %93
  %95 = load i32, ptr %8, align 4, !tbaa !5
  %96 = add nsw i32 %95, 1
  store i32 %96, ptr %8, align 4, !tbaa !5
  br label %34, !llvm.loop !12

97:                                               ; preds = %92, %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  %98 = load i32, ptr %6, align 4
  switch i32 %98, label %103 [
    i32 7, label %99
  ]

99:                                               ; preds = %97
  br label %100

100:                                              ; preds = %99
  %101 = load i32, ptr %5, align 4, !tbaa !5
  %102 = add nsw i32 %101, 1
  store i32 %102, ptr %5, align 4, !tbaa !5
  br label %9, !llvm.loop !13

103:                                              ; preds = %97, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %104 = load i32, ptr %6, align 4
  switch i32 %104, label %108 [
    i32 2, label %105
    i32 1, label %106
  ]

105:                                              ; preds = %103
  store i1 false, ptr %2, align 1
  br label %106

106:                                              ; preds = %105, %103
  %107 = load i1, ptr %2, align 1
  ret i1 %107

108:                                              ; preds = %103
  unreachable
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24)) #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) #1 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = 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"

%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@c = external dso_local local_unnamed_addr global i32, align 4
@sum = external dso_local local_unnamed_addr global [1010 x [1010 x i32]], align 16
@numbers = external dso_local global %"class.std::vector", align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z5validi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) @numbers) #1
  %.not3543 = icmp ugt i64 %2, 1
  br i1 %.not3543, label %.preheader39, label %._crit_edge

.preheader39:                                     ; preds = %1, %.thread
  %indvars.iv59 = phi i64 [ %indvars.iv.next60, %.thread ], [ 1, %1 ]
  %.02644 = phi i64 [ %indvars.iv, %.thread ], [ 0, %1 ]
  %sext = shl i64 %.02644, 32
  %3 = ashr exact i64 %sext, 32
  br label %4

4:                                                ; preds = %.preheader39, %4
  %indvars.iv = phi i64 [ %3, %.preheader39 ], [ %indvars.iv.next, %4 ]
  %5 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @numbers, i64 noundef %indvars.iv59) #1
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %7 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @numbers, i64 noundef %indvars.iv.next) #1
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = sub nsw i32 %6, %8
  %.not = icmp slt i32 %9, %0
  br i1 %.not, label %.preheader38, label %4, !llvm.loop !9

.preheader38:                                     ; preds = %4
  %10 = tail call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) @numbers) #1
  %.not3340 = icmp ugt i64 %10, 1
  br i1 %.not3340, label %.preheader.lr.ph, label %.thread

.preheader.lr.ph:                                 ; preds = %.preheader38
  %sext61 = shl i64 %indvars.iv, 32
  %11 = ashr exact i64 %sext61, 32
  br label %.preheader

12:                                               ; preds = %21
  %indvars.iv.next57 = add nuw nsw i64 %indvars.iv56, 1
  %13 = tail call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) @numbers) #1
  %.not33 = icmp ugt i64 %13, %indvars.iv.next57
  br i1 %.not33, label %.preheader, label %.thread, !llvm.loop !11

.preheader:                                       ; preds = %.preheader.lr.ph, %12
  %indvars.iv56 = phi i64 [ 1, %.preheader.lr.ph ], [ %indvars.iv.next57, %12 ]
  %.02241 = phi i64 [ 0, %.preheader.lr.ph ], [ %indvars.iv53, %12 ]
  %sext62 = shl i64 %.02241, 32
  %14 = ashr exact i64 %sext62, 32
  br label %15

15:                                               ; preds = %.preheader, %15
  %indvars.iv53 = phi i64 [ %14, %.preheader ], [ %indvars.iv.next54, %15 ]
  %16 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @numbers, i64 noundef %indvars.iv56) #1
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %indvars.iv.next54 = add nsw i64 %indvars.iv53, 1
  %18 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @numbers, i64 noundef %indvars.iv.next54) #1
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = sub nsw i32 %17, %19
  %.not31 = icmp slt i32 %20, %0
  br i1 %.not31, label %21, label %15, !llvm.loop !12

21:                                               ; preds = %15
  %22 = getelementptr inbounds [1010 x [1010 x i32]], ptr @sum, i64 0, i64 %indvars.iv59, i64 %indvars.iv56
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %sext63 = shl i64 %indvars.iv53, 32
  %24 = ashr exact i64 %sext63, 32
  %25 = getelementptr inbounds [1010 x [1010 x i32]], ptr @sum, i64 0, i64 %indvars.iv59, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = getelementptr inbounds [1010 x [1010 x i32]], ptr @sum, i64 0, i64 %11, i64 %indvars.iv56
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = getelementptr inbounds [1010 x [1010 x i32]], ptr @sum, i64 0, i64 %11, i64 %24
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = add i32 %26, %28
  %32 = sub i32 %23, %31
  %33 = add nsw i32 %32, %30
  %34 = load i32, ptr @c, align 4, !tbaa !5
  %.not32 = icmp slt i32 %33, %34
  br i1 %.not32, label %12, label %._crit_edge

.thread:                                          ; preds = %12, %.preheader38
  %indvars.iv.next60 = add nuw nsw i64 %indvars.iv59, 1
  %35 = tail call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) @numbers) #1
  %.not35 = icmp ugt i64 %35, %indvars.iv.next60
  br i1 %.not35, label %.preheader39, label %._crit_edge, !llvm.loop !13

._crit_edge:                                      ; preds = %.thread, %21, %1
  %.not35.lcssa = phi i1 [ false, %1 ], [ true, %21 ], [ false, %.thread ]
  ret i1 %.not35.lcssa
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) local_unnamed_addr #0 align 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 = { 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}


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