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

; 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.segtree = type { i32, %"class.std::vector.5" }
%"class.std::vector.5" = type { %"struct.std::_Vector_base.6" }
%"struct.std::_Vector_base.6" = type { %"struct.std::_Vector_base<segtree::tdata, std::allocator<segtree::tdata>>::_Vector_impl" }
%"struct.std::_Vector_base<segtree::tdata, std::allocator<segtree::tdata>>::_Vector_impl" = type { %"struct.std::_Vector_base<segtree::tdata, std::allocator<segtree::tdata>>::_Vector_impl_data" }
%"struct.std::_Vector_base<segtree::tdata, std::allocator<segtree::tdata>>::_Vector_impl_data" = type { ptr, ptr, ptr }
%"struct.segtree::tdata" = type { i64 }

@par = external dso_local global [10001 x i32], align 16
@chainHead = external dso_local global [10001 x i32], align 16
@chainId = external dso_local global [10001 x i32], align 16
@pos = external dso_local global [10001 x i32], align 16
@st = external dso_local global %struct.segtree, 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 uwtable
define dso_local noundef i64 @_Z8query_upii(i32 noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca %"struct.segtree::tdata", align 8
  %7 = alloca %"struct.segtree::tdata", align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store i64 0, ptr %5, align 8, !tbaa !9
  br label %8

8:                                                ; preds = %39, %2
  br label %9

9:                                                ; preds = %8
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = icmp eq i32 %10, %11
  br i1 %12, label %13, label %14

13:                                               ; preds = %9
  br label %69

14:                                               ; preds = %9
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [10001 x i32], ptr @chainId, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [10001 x i32], ptr @chainId, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = icmp eq i32 %18, %22
  br i1 %23, label %24, label %39

24:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [10001 x i32], ptr @pos, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = add nsw i32 %28, 1
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [10001 x i32], ptr @pos, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = call i64 @_ZN7segtree5queryEii(ptr noundef nonnull align 8 dereferenceable(32) @st, i32 noundef %29, i32 noundef %33)
  %35 = getelementptr inbounds %"struct.segtree::tdata", ptr %6, i32 0, i32 0
  store i64 %34, ptr %35, align 8
  %36 = getelementptr inbounds %"struct.segtree::tdata", ptr %6, i32 0, i32 0
  %37 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %5, ptr noundef nonnull align 8 dereferenceable(8) %36)
  %38 = load i64, ptr %37, align 8, !tbaa !9
  store i64 %38, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %69

39:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [10001 x i32], ptr @chainId, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [10001 x i32], ptr @chainHead, i64 0, i64 %44
  %46 = load i32, ptr %45, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [10001 x i32], ptr @pos, i64 0, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !5
  %50 = load i32, ptr %3, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [10001 x i32], ptr @pos, i64 0, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !5
  %54 = call i64 @_ZN7segtree5queryEii(ptr noundef nonnull align 8 dereferenceable(32) @st, i32 noundef %49, i32 noundef %53)
  %55 = getelementptr inbounds %"struct.segtree::tdata", ptr %7, i32 0, i32 0
  store i64 %54, ptr %55, align 8
  %56 = getelementptr inbounds %"struct.segtree::tdata", ptr %7, i32 0, i32 0
  %57 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %5, ptr noundef nonnull align 8 dereferenceable(8) %56)
  %58 = load i64, ptr %57, align 8, !tbaa !9
  store i64 %58, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  %59 = load i32, ptr %3, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [10001 x i32], ptr @chainId, i64 0, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [10001 x i32], ptr @chainHead, i64 0, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [10001 x i32], ptr @par, i64 0, i64 %66
  %68 = load i32, ptr %67, align 4, !tbaa !5
  store i32 %68, ptr %3, align 4, !tbaa !5
  br label %8, !llvm.loop !11

69:                                               ; preds = %24, %13
  %70 = load i64, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret i64 %70
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #2

; Function Attrs: mustprogress uwtable
declare dso_local i64 @_ZN7segtree5queryEii(ptr noundef nonnull align 8 dereferenceable(32), i32 noundef, i32 noundef) #1 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #3 = { 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, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; 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.segtree = type { i32, %"class.std::vector.5" }
%"class.std::vector.5" = type { %"struct.std::_Vector_base.6" }
%"struct.std::_Vector_base.6" = type { %"struct.std::_Vector_base<segtree::tdata, std::allocator<segtree::tdata>>::_Vector_impl" }
%"struct.std::_Vector_base<segtree::tdata, std::allocator<segtree::tdata>>::_Vector_impl" = type { %"struct.std::_Vector_base<segtree::tdata, std::allocator<segtree::tdata>>::_Vector_impl_data" }
%"struct.std::_Vector_base<segtree::tdata, std::allocator<segtree::tdata>>::_Vector_impl_data" = type { ptr, ptr, ptr }
%"struct.segtree::tdata" = type { i64 }

@par = external dso_local local_unnamed_addr global [10001 x i32], align 16
@chainHead = external dso_local local_unnamed_addr global [10001 x i32], align 16
@chainId = external dso_local local_unnamed_addr global [10001 x i32], align 16
@pos = external dso_local local_unnamed_addr global [10001 x i32], align 16
@st = external dso_local global %struct.segtree, align 8

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z8query_upii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = alloca i64, align 8
  %4 = alloca %"struct.segtree::tdata", align 8
  %5 = alloca %"struct.segtree::tdata", align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  store i64 0, ptr %3, align 8, !tbaa !5
  %6 = icmp eq i32 %0, %1
  br i1 %6, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %7 = sext i32 %1 to i64
  %8 = getelementptr inbounds [10001 x i32], ptr @chainId, i64 0, i64 %7
  br label %9

9:                                                ; preds = %.lr.ph, %24
  %.011 = phi i32 [ %0, %.lr.ph ], [ %42, %24 ]
  %10 = sext i32 %.011 to i64
  %11 = getelementptr inbounds [10001 x i32], ptr @chainId, i64 0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !9
  %13 = load i32, ptr %8, align 4, !tbaa !9
  %14 = icmp eq i32 %12, %13
  br i1 %14, label %15, label %24

15:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  %16 = getelementptr inbounds [10001 x i32], ptr @pos, i64 0, i64 %7
  %17 = load i32, ptr %16, align 4, !tbaa !9
  %18 = add nsw i32 %17, 1
  %19 = getelementptr inbounds [10001 x i32], ptr @pos, i64 0, i64 %10
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = call i64 @_ZN7segtree5queryEii(ptr noundef nonnull align 8 dereferenceable(32) @st, i32 noundef %18, i32 noundef %20)
  store i64 %21, ptr %4, align 8
  %22 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %3, ptr noundef nonnull align 8 dereferenceable(8) %4)
  %23 = load i64, ptr %22, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  br label %.loopexit

24:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #3
  %25 = sext i32 %12 to i64
  %26 = getelementptr inbounds [10001 x i32], ptr @chainHead, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [10001 x i32], ptr @pos, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !9
  %31 = getelementptr inbounds [10001 x i32], ptr @pos, i64 0, i64 %10
  %32 = load i32, ptr %31, align 4, !tbaa !9
  %33 = call i64 @_ZN7segtree5queryEii(ptr noundef nonnull align 8 dereferenceable(32) @st, i32 noundef %30, i32 noundef %32)
  store i64 %33, ptr %5, align 8
  %34 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %3, ptr noundef nonnull align 8 dereferenceable(8) %5)
  %35 = load i64, ptr %34, align 8, !tbaa !5
  store i64 %35, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #3
  %36 = load i32, ptr %11, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [10001 x i32], ptr @chainHead, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [10001 x i32], ptr @par, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !9
  %43 = icmp eq i32 %42, %1
  br i1 %43, label %.loopexit, label %9, !llvm.loop !11

.loopexit:                                        ; preds = %24, %2, %15
  %44 = phi i64 [ 0, %2 ], [ %23, %15 ], [ %35, %24 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  ret i64 %44
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
declare dso_local i64 @_ZN7segtree5queryEii(ptr noundef nonnull align 8 dereferenceable(32), i32 noundef, i32 noundef) local_unnamed_addr #1 align 2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #3 = { 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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