; LLVM IR from ncduy0303/Competitive-Programming :: Data Structures/SQRT.ll (Z7mo_algov)

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

@n = external dso_local global i32, align 4
@ans = external dso_local global [200001 x i32], align 16
@cur = external dso_local global i32, align 4
@qs = external dso_local global [200001 x %struct.query], align 16

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z8subtracti(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 @_Z7mo_algov() #2 {
  %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 = load i32, ptr @n, align 4, !tbaa !5
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds %struct.query, ptr @qs, i64 %7
  %9 = getelementptr inbounds %struct.query, ptr %8, i64 1
  call void @_ZSt4sortIP5queryEvT_S2_(ptr noundef getelementptr inbounds (%struct.query, ptr @qs, i64 1), ptr noundef %9)
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  store i32 0, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 0, ptr %2, align 4, !tbaa !5
  call void @_Z3addi(i32 noundef 0)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 1, ptr %3, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %66, %0
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = load i32, ptr @n, align 4, !tbaa !5
  %13 = icmp sle i32 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %69

15:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [200001 x %struct.query], ptr @qs, i64 0, i64 %17
  %19 = getelementptr inbounds %struct.query, ptr %18, i32 0, i32 1
  %20 = load i32, ptr %19, align 4, !tbaa !9
  store i32 %20, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [200001 x %struct.query], ptr @qs, i64 0, i64 %22
  %24 = getelementptr inbounds %struct.query, ptr %23, i32 0, i32 2
  %25 = load i32, ptr %24, align 8, !tbaa !11
  store i32 %25, ptr %5, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %30, %15
  %27 = load i32, ptr %1, align 4, !tbaa !5
  %28 = load i32, ptr %4, align 4, !tbaa !5
  %29 = icmp slt i32 %27, %28
  br i1 %29, label %30, label %33

30:                                               ; preds = %26
  %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 @_Z8subtracti(i32 noundef %31)
  br label %26, !llvm.loop !12

33:                                               ; preds = %26
  br label %34

34:                                               ; preds = %38, %33
  %35 = load i32, ptr %1, align 4, !tbaa !5
  %36 = load i32, ptr %4, align 4, !tbaa !5
  %37 = icmp sgt i32 %35, %36
  br i1 %37, label %38, label %41

38:                                               ; preds = %34
  %39 = load i32, ptr %1, align 4, !tbaa !5
  %40 = add nsw i32 %39, -1
  store i32 %40, ptr %1, align 4, !tbaa !5
  call void @_Z3addi(i32 noundef %40)
  br label %34, !llvm.loop !14

41:                                               ; preds = %34
  br label %42

42:                                               ; preds = %46, %41
  %43 = load i32, ptr %2, align 4, !tbaa !5
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = icmp slt i32 %43, %44
  br i1 %45, label %46, label %49

46:                                               ; preds = %42
  %47 = load i32, ptr %2, align 4, !tbaa !5
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %2, align 4, !tbaa !5
  call void @_Z3addi(i32 noundef %48)
  br label %42, !llvm.loop !15

49:                                               ; preds = %42
  br label %50

50:                                               ; preds = %54, %49
  %51 = load i32, ptr %2, align 4, !tbaa !5
  %52 = load i32, ptr %5, align 4, !tbaa !5
  %53 = icmp sgt i32 %51, %52
  br i1 %53, label %54, label %57

54:                                               ; preds = %50
  %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 @_Z8subtracti(i32 noundef %55)
  br label %50, !llvm.loop !16

57:                                               ; preds = %50
  %58 = load i32, ptr @cur, align 4, !tbaa !5
  %59 = load i32, ptr %3, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [200001 x %struct.query], ptr @qs, i64 0, i64 %60
  %62 = getelementptr inbounds %struct.query, ptr %61, i32 0, i32 3
  %63 = load i32, ptr %62, align 4, !tbaa !17
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [200001 x i32], ptr @ans, i64 0, i64 %64
  store i32 %58, ptr %65, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %66

66:                                               ; preds = %57
  %67 = load i32, ptr %3, align 4, !tbaa !5
  %68 = add nsw i32 %67, 1
  store i32 %68, ptr %3, align 4, !tbaa !5
  br label %10, !llvm.loop !18

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIP5queryEvT_S2_(ptr noundef, ptr noundef) #3

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


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

@n = external dso_local local_unnamed_addr global i32, align 4
@ans = external dso_local local_unnamed_addr global [200001 x i32], align 16
@cur = external dso_local local_unnamed_addr global i32, align 4
@qs = external dso_local global [200001 x %struct.query], align 16

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z7mo_algov() local_unnamed_addr #1 {
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %2 = sext i32 %1 to i64
  %3 = getelementptr inbounds %struct.query, ptr @qs, i64 %2
  %4 = getelementptr inbounds i8, ptr %3, i64 16
  tail call void @_ZSt4sortIP5queryEvT_S2_(ptr noundef nonnull getelementptr inbounds (i8, ptr @qs, i64 16), ptr noundef nonnull %4)
  tail call void @_Z3addi(i32 noundef 0)
  %5 = load i32, ptr @n, align 4, !tbaa !5
  %.not33 = icmp slt i32 %5, 1
  br i1 %.not33, label %._crit_edge39, label %.lr.ph38

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

.lr.ph38:                                         ; preds = %0, %._crit_edge
  %indvars.iv = phi i64 [ %indvars.iv.next, %._crit_edge ], [ 1, %0 ]
  %.036 = phi i32 [ %.2.lcssa, %._crit_edge ], [ 0, %0 ]
  %.01735 = phi i32 [ %.219.lcssa, %._crit_edge ], [ 0, %0 ]
  %6 = getelementptr inbounds [200001 x %struct.query], ptr @qs, i64 0, i64 %indvars.iv
  %7 = getelementptr inbounds i8, ptr %6, i64 4
  %8 = load i32, ptr %7, align 4, !tbaa !9
  %9 = getelementptr inbounds i8, ptr %6, i64 8
  %10 = load i32, ptr %9, align 8, !tbaa !11
  %11 = icmp slt i32 %.036, %8
  br i1 %11, label %.lr.ph, label %.preheader22

.preheader22:                                     ; preds = %.lr.ph38
  %12 = icmp sgt i32 %.036, %8
  br i1 %12, label %.lr.ph25, label %.preheader21

.lr.ph:                                           ; preds = %.lr.ph38, %.lr.ph
  %.123 = phi i32 [ %13, %.lr.ph ], [ %.036, %.lr.ph38 ]
  %13 = add i32 %.123, 1
  tail call void @_Z8subtracti(i32 noundef %.123)
  %exitcond.not = icmp eq i32 %13, %8
  br i1 %exitcond.not, label %.preheader21, label %.lr.ph, !llvm.loop !12

.preheader21:                                     ; preds = %.lr.ph25, %.lr.ph, %.preheader22
  %.2.lcssa = phi i32 [ %.036, %.preheader22 ], [ %8, %.lr.ph ], [ %8, %.lr.ph25 ]
  %14 = icmp slt i32 %.01735, %10
  br i1 %14, label %.lr.ph28, label %.preheader

.lr.ph25:                                         ; preds = %.preheader22, %.lr.ph25
  %.224 = phi i32 [ %15, %.lr.ph25 ], [ %.036, %.preheader22 ]
  %15 = add nsw i32 %.224, -1
  tail call void @_Z3addi(i32 noundef %15)
  %16 = icmp sgt i32 %15, %8
  br i1 %16, label %.lr.ph25, label %.preheader21, !llvm.loop !14

.preheader:                                       ; preds = %.preheader21
  %17 = icmp sgt i32 %.01735, %10
  br i1 %17, label %.lr.ph31, label %._crit_edge

.lr.ph28:                                         ; preds = %.preheader21, %.lr.ph28
  %.11827 = phi i32 [ %18, %.lr.ph28 ], [ %.01735, %.preheader21 ]
  %18 = add nsw i32 %.11827, 1
  tail call void @_Z3addi(i32 noundef %18)
  %exitcond43.not = icmp eq i32 %18, %10
  br i1 %exitcond43.not, label %._crit_edge, label %.lr.ph28, !llvm.loop !15

.lr.ph31:                                         ; preds = %.preheader, %.lr.ph31
  %.21930 = phi i32 [ %19, %.lr.ph31 ], [ %.01735, %.preheader ]
  %19 = add nsw i32 %.21930, -1
  tail call void @_Z8subtracti(i32 noundef %.21930)
  %20 = icmp sgt i32 %19, %10
  br i1 %20, label %.lr.ph31, label %._crit_edge, !llvm.loop !16

._crit_edge:                                      ; preds = %.lr.ph31, %.lr.ph28, %.preheader
  %.219.lcssa = phi i32 [ %.01735, %.preheader ], [ %10, %.lr.ph28 ], [ %10, %.lr.ph31 ]
  %21 = load i32, ptr @cur, align 4, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %6, i64 12
  %23 = load i32, ptr %22, align 4, !tbaa !17
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [200001 x i32], ptr @ans, i64 0, i64 %24
  store i32 %21, ptr %25, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %26 = load i32, ptr @n, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %.not.not = icmp slt i64 %indvars.iv, %27
  br i1 %.not.not, label %.lr.ph38, label %._crit_edge39, !llvm.loop !18
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIP5queryEvT_S2_(ptr noundef, ptr 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 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 = { 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" }

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass