; LLVM IR from lzyrapx/Competitive-Programming :: ACdream/1108（莫队算法）.ll (Z5solvev)

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

%struct.Query = type { i32, i32, i32, i32 }

@querys = external dso_local global [100010 x %struct.Query], align 16
@num = external dso_local global [100010 x i32], align 16
@sum = external dso_local global [100010 x i32], align 16
@ans = external dso_local global [100010 x i32], align 16
@m = external dso_local global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z13binary_searchi(i32 noundef) #0

; 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
define dso_local void @_Z5solvev() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  call void @llvm.memset.p0.i64(ptr align 16 @sum, i8 0, i64 400040, i1 false)
  call void @llvm.memset.p0.i64(ptr align 16 @num, i8 0, i64 400040, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #5
  store i32 1, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %4

4:                                                ; preds = %71, %0
  %5 = load i32, ptr %3, align 4, !tbaa !5
  %6 = load i32, ptr @m, align 4, !tbaa !5
  %7 = icmp slt i32 %5, %6
  br i1 %7, label %9, label %8

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

9:                                                ; preds = %4
  br label %10

10:                                               ; preds = %18, %9
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [100010 x %struct.Query], ptr @querys, i64 0, i64 %13
  %15 = getelementptr inbounds %struct.Query, ptr %14, i32 0, i32 1
  %16 = load i32, ptr %15, align 4, !tbaa !9
  %17 = icmp slt i32 %11, %16
  br i1 %17, label %18, label %21

18:                                               ; preds = %10
  %19 = load i32, ptr %2, align 4, !tbaa !5
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr %2, align 4, !tbaa !5
  call void @_Z3Addi(i32 noundef %20)
  br label %10, !llvm.loop !11

21:                                               ; preds = %10
  br label %22

22:                                               ; preds = %30, %21
  %23 = load i32, ptr %1, align 4, !tbaa !5
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [100010 x %struct.Query], ptr @querys, i64 0, i64 %25
  %27 = getelementptr inbounds %struct.Query, ptr %26, i32 0, i32 0
  %28 = load i32, ptr %27, align 16, !tbaa !13
  %29 = icmp sgt i32 %23, %28
  br i1 %29, label %30, label %33

30:                                               ; preds = %22
  %31 = load i32, ptr %1, align 4, !tbaa !5
  %32 = add nsw i32 %31, -1
  store i32 %32, ptr %1, align 4, !tbaa !5
  call void @_Z3Addi(i32 noundef %32)
  br label %22, !llvm.loop !14

33:                                               ; preds = %22
  br label %34

34:                                               ; preds = %42, %33
  %35 = load i32, ptr %1, align 4, !tbaa !5
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [100010 x %struct.Query], ptr @querys, i64 0, i64 %37
  %39 = getelementptr inbounds %struct.Query, ptr %38, i32 0, i32 0
  %40 = load i32, ptr %39, align 16, !tbaa !13
  %41 = icmp slt i32 %35, %40
  br i1 %41, label %42, label %45

42:                                               ; preds = %34
  %43 = load i32, ptr %1, align 4, !tbaa !5
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %1, align 4, !tbaa !5
  call void @_Z3Deci(i32 noundef %43)
  br label %34, !llvm.loop !15

45:                                               ; preds = %34
  br label %46

46:                                               ; preds = %54, %45
  %47 = load i32, ptr %2, align 4, !tbaa !5
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [100010 x %struct.Query], ptr @querys, i64 0, i64 %49
  %51 = getelementptr inbounds %struct.Query, ptr %50, i32 0, i32 1
  %52 = load i32, ptr %51, align 4, !tbaa !9
  %53 = icmp sgt i32 %47, %52
  br i1 %53, label %54, label %57

54:                                               ; preds = %46
  %55 = load i32, ptr %2, align 4, !tbaa !5
  %56 = add nsw i32 %55, -1
  store i32 %56, ptr %2, align 4, !tbaa !5
  call void @_Z3Deci(i32 noundef %55)
  br label %46, !llvm.loop !16

57:                                               ; preds = %46
  %58 = load i32, ptr %3, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [100010 x %struct.Query], ptr @querys, i64 0, i64 %59
  %61 = getelementptr inbounds %struct.Query, ptr %60, i32 0, i32 2
  %62 = load i32, ptr %61, align 8, !tbaa !17
  %63 = call noundef i32 @_Z13binary_searchi(i32 noundef %62)
  %64 = load i32, ptr %3, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [100010 x %struct.Query], ptr @querys, i64 0, i64 %65
  %67 = getelementptr inbounds %struct.Query, ptr %66, i32 0, i32 3
  %68 = load i32, ptr %67, align 4, !tbaa !18
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [100010 x i32], ptr @ans, i64 0, i64 %69
  store i32 %63, ptr %70, align 4, !tbaa !5
  br label %71

71:                                               ; preds = %57
  %72 = load i32, ptr %3, align 4, !tbaa !5
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %3, align 4, !tbaa !5
  br label %4, !llvm.loop !19

74:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #5
  ret void
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z3Addi(i32 noundef) #4

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z3Deci(i32 noundef) #4

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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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 #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 = !{!10, !6, i64 4}
!10 = !{!"_ZTS5Query", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!10, !6, i64 0}
!14 = distinct !{!14, !12}
!15 = distinct !{!15, !12}
!16 = distinct !{!16, !12}
!17 = !{!10, !6, i64 8}
!18 = !{!10, !6, i64 12}
!19 = distinct !{!19, !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"

%struct.Query = type { i32, i32, i32, i32 }

@querys = external dso_local local_unnamed_addr global [100010 x %struct.Query], align 16
@num = external dso_local local_unnamed_addr global [100010 x i32], align 16
@sum = external dso_local local_unnamed_addr global [100010 x i32], align 16
@ans = external dso_local local_unnamed_addr global [100010 x i32], align 16
@m = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z13binary_searchi(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z5solvev() local_unnamed_addr #0 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(400040) @sum, i8 0, i64 400040, i1 false)
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(400040) @num, i8 0, i64 400040, i1 false)
  %1 = load i32, ptr @m, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.preheader22, label %._crit_edge40

.preheader22:                                     ; preds = %0, %._crit_edge
  %indvars.iv = phi i64 [ %indvars.iv.next, %._crit_edge ], [ 0, %0 ]
  %.01638 = phi i32 [ %.2.lcssa, %._crit_edge ], [ 0, %0 ]
  %.01737 = phi i32 [ %.219.lcssa, %._crit_edge ], [ 1, %0 ]
  %3 = getelementptr inbounds [100010 x %struct.Query], ptr @querys, i64 0, i64 %indvars.iv
  %4 = getelementptr inbounds i8, ptr %3, i64 4
  %5 = load i32, ptr %4, align 4, !tbaa !9
  %6 = icmp slt i32 %.01638, %5
  br i1 %6, label %.lr.ph, label %.preheader21

._crit_edge40:                                    ; preds = %._crit_edge, %0
  ret void

.preheader21:                                     ; preds = %.lr.ph, %.preheader22
  %.1.lcssa = phi i32 [ %.01638, %.preheader22 ], [ %9, %.lr.ph ]
  %7 = load i32, ptr %3, align 16, !tbaa !11
  %8 = icmp sgt i32 %.01737, %7
  br i1 %8, label %.lr.ph29, label %.preheader20

.lr.ph:                                           ; preds = %.preheader22, %.lr.ph
  %.125 = phi i32 [ %9, %.lr.ph ], [ %.01638, %.preheader22 ]
  %9 = add nsw i32 %.125, 1
  tail call void @_Z3Addi(i32 noundef %9)
  %10 = load i32, ptr %4, align 4, !tbaa !9
  %11 = icmp slt i32 %9, %10
  br i1 %11, label %.lr.ph, label %.preheader21, !llvm.loop !12

.preheader20:                                     ; preds = %.lr.ph29, %.preheader21
  %12 = phi i32 [ %7, %.preheader21 ], [ %15, %.lr.ph29 ]
  %.118.lcssa = phi i32 [ %.01737, %.preheader21 ], [ %14, %.lr.ph29 ]
  %13 = icmp slt i32 %.118.lcssa, %12
  br i1 %13, label %.lr.ph32, label %.preheader

.lr.ph29:                                         ; preds = %.preheader21, %.lr.ph29
  %.11828 = phi i32 [ %14, %.lr.ph29 ], [ %.01737, %.preheader21 ]
  %14 = add nsw i32 %.11828, -1
  tail call void @_Z3Addi(i32 noundef %14)
  %15 = load i32, ptr %3, align 16, !tbaa !11
  %16 = icmp sgt i32 %14, %15
  br i1 %16, label %.lr.ph29, label %.preheader20, !llvm.loop !14

.preheader:                                       ; preds = %.lr.ph32, %.preheader20
  %.219.lcssa = phi i32 [ %.118.lcssa, %.preheader20 ], [ %19, %.lr.ph32 ]
  %17 = load i32, ptr %4, align 4, !tbaa !9
  %18 = icmp sgt i32 %.1.lcssa, %17
  br i1 %18, label %.lr.ph35, label %._crit_edge

.lr.ph32:                                         ; preds = %.preheader20, %.lr.ph32
  %.21931 = phi i32 [ %19, %.lr.ph32 ], [ %.118.lcssa, %.preheader20 ]
  %19 = add nsw i32 %.21931, 1
  tail call void @_Z3Deci(i32 noundef %.21931)
  %20 = load i32, ptr %3, align 16, !tbaa !11
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %.lr.ph32, label %.preheader, !llvm.loop !15

.lr.ph35:                                         ; preds = %.preheader, %.lr.ph35
  %.234 = phi i32 [ %22, %.lr.ph35 ], [ %.1.lcssa, %.preheader ]
  %22 = add nsw i32 %.234, -1
  tail call void @_Z3Deci(i32 noundef %.234)
  %23 = load i32, ptr %4, align 4, !tbaa !9
  %24 = icmp sgt i32 %22, %23
  br i1 %24, label %.lr.ph35, label %._crit_edge, !llvm.loop !16

._crit_edge:                                      ; preds = %.lr.ph35, %.preheader
  %.2.lcssa = phi i32 [ %.1.lcssa, %.preheader ], [ %22, %.lr.ph35 ]
  %25 = getelementptr inbounds i8, ptr %3, i64 8
  %26 = load i32, ptr %25, align 8, !tbaa !17
  %27 = tail call noundef i32 @_Z13binary_searchi(i32 noundef %26)
  %28 = getelementptr inbounds i8, ptr %3, i64 12
  %29 = load i32, ptr %28, align 4, !tbaa !18
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [100010 x i32], ptr @ans, i64 0, i64 %30
  store i32 %27, ptr %31, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %32 = load i32, ptr @m, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = icmp slt i64 %indvars.iv.next, %33
  br i1 %34, label %.preheader22, label %._crit_edge40, !llvm.loop !19
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z3Addi(i32 noundef) local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z3Deci(i32 noundef) local_unnamed_addr #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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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" }

!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 = !{!10, !6, i64 4}
!10 = !{!"_ZTS5Query", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12}
!11 = !{!10, !6, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}
!15 = distinct !{!15, !13}
!16 = distinct !{!16, !13}
!17 = !{!10, !6, i64 8}
!18 = !{!10, !6, i64 12}
!19 = distinct !{!19, !13}


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