; LLVM IR from upupming/algorithm :: acwing/寒假每日一题（提高组）/week2/503. 借教室.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"

@n = external dso_local global i32, align 4
@r = external dso_local global [1000010 x i32], align 16
@d = external dso_local global [1000010 x i32], align 16
@s = external dso_local global [1000010 x i32], align 16
@t = external dso_local global [1000010 x i32], align 16
@b = external dso_local global [1000010 x i32], align 16

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

8:                                                ; preds = %27, %1
  %9 = load i32, ptr %4, 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 %4) #2
  br label %30

13:                                               ; preds = %8
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [1000010 x i32], ptr @r, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = sub nsw i32 %18, 1
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [1000010 x i32], ptr @r, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = sub nsw i32 %17, %22
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [1000010 x i32], ptr @b, i64 0, i64 %25
  store i32 %23, ptr %26, align 4, !tbaa !5
  br label %27

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

30:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 1, ptr %5, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %62, %30
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = icmp sle i32 %32, %33
  br i1 %34, label %36, label %35

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

36:                                               ; preds = %31
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [1000010 x i32], ptr @d, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [1000010 x i32], ptr @s, i64 0, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [1000010 x i32], ptr @b, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = sub nsw i32 %47, %40
  store i32 %48, ptr %46, align 4, !tbaa !5
  %49 = load i32, ptr %5, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [1000010 x i32], ptr @d, i64 0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !5
  %53 = load i32, ptr %5, align 4, !tbaa !5
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [1000010 x i32], ptr @t, i64 0, i64 %54
  %56 = load i32, ptr %55, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [1000010 x i32], ptr @b, i64 0, i64 %58
  %60 = load i32, ptr %59, align 4, !tbaa !5
  %61 = add nsw i32 %60, %52
  store i32 %61, ptr %59, align 4, !tbaa !5
  br label %62

62:                                               ; preds = %36
  %63 = load i32, ptr %5, align 4, !tbaa !5
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %5, align 4, !tbaa !5
  br label %31, !llvm.loop !11

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

66:                                               ; preds = %89, %65
  %67 = load i32, ptr %6, align 4, !tbaa !5
  %68 = load i32, ptr @n, align 4, !tbaa !5
  %69 = icmp sle i32 %67, %68
  br i1 %69, label %71, label %70

70:                                               ; preds = %66
  store i32 8, ptr %7, align 4
  br label %92

71:                                               ; preds = %66
  %72 = load i32, ptr %6, align 4, !tbaa !5
  %73 = sub nsw i32 %72, 1
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds [1000010 x i32], ptr @b, i64 0, i64 %74
  %76 = load i32, ptr %75, align 4, !tbaa !5
  %77 = load i32, ptr %6, align 4, !tbaa !5
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds [1000010 x i32], ptr @b, i64 0, i64 %78
  %80 = load i32, ptr %79, align 4, !tbaa !5
  %81 = add nsw i32 %80, %76
  store i32 %81, ptr %79, align 4, !tbaa !5
  %82 = load i32, ptr %6, align 4, !tbaa !5
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds [1000010 x i32], ptr @b, i64 0, i64 %83
  %85 = load i32, ptr %84, align 4, !tbaa !5
  %86 = icmp slt i32 %85, 0
  br i1 %86, label %87, label %88

87:                                               ; preds = %71
  store i1 false, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %92

88:                                               ; preds = %71
  br label %89

89:                                               ; preds = %88
  %90 = load i32, ptr %6, align 4, !tbaa !5
  %91 = add nsw i32 %90, 1
  store i32 %91, ptr %6, align 4, !tbaa !5
  br label %66, !llvm.loop !12

92:                                               ; preds = %87, %70
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %93 = load i32, ptr %7, align 4
  switch i32 %93, label %97 [
    i32 8, label %94
    i32 1, label %95
  ]

94:                                               ; preds = %92
  store i1 true, ptr %2, align 1
  br label %95

95:                                               ; preds = %94, %92
  %96 = load i1, ptr %2, align 1
  ret i1 %96

97:                                               ; preds = %92
  unreachable
}

; 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

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 = { 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
@r = external dso_local local_unnamed_addr global [1000010 x i32], align 16
@d = external dso_local local_unnamed_addr global [1000010 x i32], align 16
@s = external dso_local local_unnamed_addr global [1000010 x i32], align 16
@t = external dso_local local_unnamed_addr global [1000010 x i32], align 16
@b = external dso_local local_unnamed_addr global [1000010 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_Z5validi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %.not25 = icmp slt i32 %2, 1
  br i1 %.not25, label %.preheader24, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %3 = add nuw i32 %2, 1
  %wide.trip.count = zext i32 %3 to i64
  %.pre = load i32, ptr @r, align 16, !tbaa !5
  %4 = zext nneg i32 %2 to i64
  %min.iters.check = icmp ult i32 %2, 8
  br i1 %min.iters.check, label %.lr.ph.preheader53, label %vector.ph

.lr.ph.preheader53:                               ; preds = %middle.block, %.lr.ph.preheader
  %.ph = phi i32 [ %.pre, %.lr.ph.preheader ], [ %vector.recur.extract, %middle.block ]
  %indvars.iv.ph = phi i64 [ 1, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %4, 2147483640
  %ind.end = or disjoint i64 %n.vec, 1
  %vector.recur.init = insertelement <4 x i32> poison, i32 %.pre, i64 3
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vector.recur = phi <4 x i32> [ %vector.recur.init, %vector.ph ], [ %wide.load51, %vector.body ]
  %offset.idx = or disjoint i64 %index, 1
  %5 = getelementptr inbounds [1000010 x i32], ptr @r, i64 0, i64 %offset.idx
  %6 = getelementptr inbounds i8, ptr %5, i64 16
  %wide.load = load <4 x i32>, ptr %5, align 4, !tbaa !5
  %wide.load51 = load <4 x i32>, ptr %6, align 4, !tbaa !5
  %7 = shufflevector <4 x i32> %vector.recur, <4 x i32> %wide.load, <4 x i32> <i32 3, i32 4, i32 5, i32 6>
  %8 = shufflevector <4 x i32> %wide.load, <4 x i32> %wide.load51, <4 x i32> <i32 3, i32 4, i32 5, i32 6>
  %9 = sub nsw <4 x i32> %wide.load, %7
  %10 = sub nsw <4 x i32> %wide.load51, %8
  %11 = getelementptr inbounds [1000010 x i32], ptr @b, i64 0, i64 %offset.idx
  %12 = getelementptr inbounds i8, ptr %11, i64 16
  store <4 x i32> %9, ptr %11, align 4, !tbaa !5
  store <4 x i32> %10, ptr %12, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %13 = icmp eq i64 %index.next, %n.vec
  br i1 %13, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %vector.recur.extract = extractelement <4 x i32> %wide.load51, i64 3
  %cmp.n = icmp eq i64 %n.vec, %4
  br i1 %cmp.n, label %.preheader24, label %.lr.ph.preheader53

.preheader24:                                     ; preds = %.lr.ph, %middle.block, %1
  %.not2227 = icmp slt i32 %0, 1
  br i1 %.not2227, label %.preheader, label %.lr.ph29.preheader

.lr.ph29.preheader:                               ; preds = %.preheader24
  %14 = add nuw i32 %0, 1
  %wide.trip.count41 = zext i32 %14 to i64
  br label %.lr.ph29

.lr.ph:                                           ; preds = %.lr.ph.preheader53, %.lr.ph
  %15 = phi i32 [ %17, %.lr.ph ], [ %.ph, %.lr.ph.preheader53 ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader53 ]
  %16 = getelementptr inbounds [1000010 x i32], ptr @r, i64 0, i64 %indvars.iv
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = sub nsw i32 %17, %15
  %19 = getelementptr inbounds [1000010 x i32], ptr @b, i64 0, i64 %indvars.iv
  store i32 %18, ptr %19, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.preheader24, label %.lr.ph, !llvm.loop !13

.preheader:                                       ; preds = %.lr.ph29, %.preheader24
  br i1 %.not25, label %._crit_edge, label %.lr.ph33.preheader

.lr.ph33.preheader:                               ; preds = %.preheader
  %20 = zext nneg i32 %2 to i64
  %21 = add nuw i32 %2, 1
  %wide.trip.count45 = zext i32 %21 to i64
  %.pre47 = load i32, ptr @b, align 16, !tbaa !5
  %22 = load i32, ptr getelementptr inbounds (i8, ptr @b, i64 4), align 4, !tbaa !5
  %23 = add nsw i32 %22, %.pre47
  store i32 %23, ptr getelementptr inbounds (i8, ptr @b, i64 4), align 4, !tbaa !5
  %24 = icmp slt i32 %23, 0
  br i1 %24, label %._crit_edge, label %.lr.ph49

.lr.ph29:                                         ; preds = %.lr.ph29.preheader, %.lr.ph29
  %indvars.iv38 = phi i64 [ 1, %.lr.ph29.preheader ], [ %indvars.iv.next39, %.lr.ph29 ]
  %25 = getelementptr inbounds [1000010 x i32], ptr @d, i64 0, i64 %indvars.iv38
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = getelementptr inbounds [1000010 x i32], ptr @s, i64 0, i64 %indvars.iv38
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [1000010 x i32], ptr @b, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = sub nsw i32 %31, %26
  store i32 %32, ptr %30, align 4, !tbaa !5
  %33 = getelementptr inbounds [1000010 x i32], ptr @t, i64 0, i64 %indvars.iv38
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [1000010 x i32], ptr @b, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = add nsw i32 %38, %26
  store i32 %39, ptr %37, align 4, !tbaa !5
  %indvars.iv.next39 = add nuw nsw i64 %indvars.iv38, 1
  %exitcond42.not = icmp eq i64 %indvars.iv.next39, %wide.trip.count41
  br i1 %exitcond42.not, label %.preheader, label %.lr.ph29, !llvm.loop !14

.lr.ph49:                                         ; preds = %.lr.ph33.preheader, %.lr.ph33
  %40 = phi i32 [ %43, %.lr.ph33 ], [ %23, %.lr.ph33.preheader ]
  %indvars.iv4348 = phi i64 [ %indvars.iv.next44, %.lr.ph33 ], [ 1, %.lr.ph33.preheader ]
  %indvars.iv.next44 = add nuw nsw i64 %indvars.iv4348, 1
  %exitcond46 = icmp eq i64 %indvars.iv.next44, %wide.trip.count45
  br i1 %exitcond46, label %._crit_edge.loopexit, label %.lr.ph33, !llvm.loop !15

.lr.ph33:                                         ; preds = %.lr.ph49
  %41 = getelementptr inbounds [1000010 x i32], ptr @b, i64 0, i64 %indvars.iv.next44
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = add nsw i32 %42, %40
  store i32 %43, ptr %41, align 4, !tbaa !5
  %44 = icmp slt i32 %43, 0
  br i1 %44, label %._crit_edge.loopexit, label %.lr.ph49, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %.lr.ph49, %.lr.ph33
  %.not23.le = icmp uge i64 %indvars.iv4348, %20
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.lr.ph33.preheader, %.preheader
  %.not23.lcssa = phi i1 [ true, %.preheader ], [ false, %.lr.ph33.preheader ], [ %.not23.le, %._crit_edge.loopexit ]
  ret i1 %.not23.lcssa
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, 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" }

!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, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !12, !11}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !10}


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