; LLVM IR from CZWin32768/ACMICPC-Templates-in-Latex :: atl/codes/Tree/Persistent-Segment-Tree.ll (ZN3PST5queryEii)

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

@_ZN3PST1nE = external dso_local global i32, align 4
@_ZN3PST4lsonE = external dso_local global [4000200 x i32], align 16
@_ZN3PST4rsonE = external dso_local global [4000200 x i32], align 16
@_ZN3PST1cE = external dso_local global [4000200 x i32], align 16

; 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 i32 @_ZN3PST5queryEii(i32 noundef %0, i32 noundef %1) #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
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 1, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %9 = load i32, ptr @_ZN3PST1nE, align 4, !tbaa !5
  store i32 %9, ptr %7, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %44, %2
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = load i32, ptr %7, align 4, !tbaa !5
  %13 = icmp slt i32 %11, %12
  br i1 %13, label %14, label %45

14:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = load i32, ptr %7, align 4, !tbaa !5
  %17 = add nsw i32 %15, %16
  %18 = ashr i32 %17, 1
  store i32 %18, ptr %8, align 4, !tbaa !5
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = load i32, ptr %8, align 4, !tbaa !5
  %21 = icmp sle i32 %19, %20
  br i1 %21, label %22, label %28

22:                                               ; preds = %14
  %23 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %23, ptr %7, align 4, !tbaa !5
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [4000200 x i32], ptr @_ZN3PST4lsonE, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  store i32 %27, ptr %3, align 4, !tbaa !5
  br label %44

28:                                               ; preds = %14
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [4000200 x i32], ptr @_ZN3PST4lsonE, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [4000200 x i32], ptr @_ZN3PST1cE, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !5
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = add nsw i32 %36, %35
  store i32 %37, ptr %5, align 4, !tbaa !5
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [4000200 x i32], ptr @_ZN3PST4rsonE, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  store i32 %41, ptr %3, align 4, !tbaa !5
  %42 = load i32, ptr %8, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %6, align 4, !tbaa !5
  br label %44

44:                                               ; preds = %28, %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %10, !llvm.loop !9

45:                                               ; preds = %10
  %46 = load i32, ptr %5, align 4, !tbaa !5
  %47 = load i32, ptr %3, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [4000200 x i32], ptr @_ZN3PST1cE, i64 0, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !5
  %51 = add nsw i32 %46, %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %51
}

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


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

@_ZN3PST1nE = external dso_local local_unnamed_addr global i32, align 4
@_ZN3PST4lsonE = external dso_local local_unnamed_addr global [4000200 x i32], align 16
@_ZN3PST4rsonE = external dso_local local_unnamed_addr global [4000200 x i32], align 16
@_ZN3PST1cE = external dso_local local_unnamed_addr global [4000200 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_ZN3PST5queryEii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr @_ZN3PST1nE, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, %1
  br i1 %4, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2, %17
  %.026 = phi i32 [ %.1, %17 ], [ %0, %2 ]
  %.01425 = phi i32 [ %.115, %17 ], [ %3, %2 ]
  %.01624 = phi i32 [ %.117, %17 ], [ 1, %2 ]
  %.01823 = phi i32 [ %.119, %17 ], [ 0, %2 ]
  %5 = add nsw i32 %.01425, %.01624
  %6 = ashr i32 %5, 1
  %.not = icmp slt i32 %6, %1
  %7 = sext i32 %.026 to i64
  %8 = getelementptr inbounds [4000200 x i32], ptr @_ZN3PST4lsonE, i64 0, i64 %7
  br i1 %.not, label %9, label %17

9:                                                ; preds = %.lr.ph
  %10 = load i32, ptr %8, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [4000200 x i32], ptr @_ZN3PST1cE, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = add nsw i32 %13, %.01823
  %15 = getelementptr inbounds [4000200 x i32], ptr @_ZN3PST4rsonE, i64 0, i64 %7
  %16 = add nsw i32 %6, 1
  br label %17

17:                                               ; preds = %.lr.ph, %9
  %.119 = phi i32 [ %14, %9 ], [ %.01823, %.lr.ph ]
  %.117 = phi i32 [ %16, %9 ], [ %.01624, %.lr.ph ]
  %.115 = phi i32 [ %.01425, %9 ], [ %6, %.lr.ph ]
  %.1.in = phi ptr [ %15, %9 ], [ %8, %.lr.ph ]
  %.1 = load i32, ptr %.1.in, align 4, !tbaa !5
  %18 = icmp sgt i32 %.115, %1
  br i1 %18, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %17, %2
  %.018.lcssa = phi i32 [ 0, %2 ], [ %.119, %17 ]
  %.0.lcssa = phi i32 [ %0, %2 ], [ %.1, %17 ]
  %19 = sext i32 %.0.lcssa to i64
  %20 = getelementptr inbounds [4000200 x i32], ptr @_ZN3PST1cE, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = add nsw i32 %21, %.018.lcssa
  ret i32 %22
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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}
!10 = !{!"llvm.loop.mustprogress"}


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