; LLVM IR from sunkafei/xcpc-algorithm-templates :: 算法/树算法/快速求树中与结点x距离不超过k的点权和.ll (Z5queryii)

; 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.TreeSet = type { [16500000 x %"struct.TreeSet::node"], i32, i32, i32 }
%"struct.TreeSet::node" = type { i32, i32, i32 }

@pa = external dso_local global [110000 x i32], align 16
@root = external dso_local global [110000 x i32], align 16
@root2 = external dso_local global [110000 x i32], align 16
@tree = external dso_local global %struct.TreeSet, align 4

; 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
declare dso_local noundef i32 @_Z4distii(i32 noundef, i32 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z5queryii(i32 noundef %0, i32 noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = 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 8, ptr %5) #3
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [110000 x i32], ptr @root, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = call noundef i32 @_ZN7TreeSet3sumEii(ptr noundef nonnull align 4 dereferenceable(198000012) @tree, i32 noundef %11, i32 noundef %12)
  %14 = sext i32 %13 to i64
  store i64 %14, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %15 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %15, ptr %6, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %61, %2
  %17 = load i32, ptr %6, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [110000 x i32], ptr @pa, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %23, label %22

22:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %66

23:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = load i32, ptr %6, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [110000 x i32], ptr @pa, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = call noundef i32 @_Z4distii(i32 noundef %24, i32 noundef %28)
  store i32 %29, ptr %7, align 4, !tbaa !5
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = sub nsw i32 %30, %31
  %33 = icmp sge i32 %32, 0
  br i1 %33, label %34, label %60

34:                                               ; preds = %23
  %35 = load i32, ptr %6, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [110000 x i32], ptr @pa, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [110000 x i32], ptr @root, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = sub nsw i32 %42, %43
  %45 = call noundef i32 @_ZN7TreeSet3sumEii(ptr noundef nonnull align 4 dereferenceable(198000012) @tree, i32 noundef %41, i32 noundef %44)
  %46 = sext i32 %45 to i64
  %47 = load i64, ptr %5, align 8, !tbaa !9
  %48 = add nsw i64 %47, %46
  store i64 %48, ptr %5, align 8, !tbaa !9
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [110000 x i32], ptr @root2, i64 0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !5
  %53 = load i32, ptr %4, align 4, !tbaa !5
  %54 = load i32, ptr %7, align 4, !tbaa !5
  %55 = sub nsw i32 %53, %54
  %56 = call noundef i32 @_ZN7TreeSet3sumEii(ptr noundef nonnull align 4 dereferenceable(198000012) @tree, i32 noundef %52, i32 noundef %55)
  %57 = sext i32 %56 to i64
  %58 = load i64, ptr %5, align 8, !tbaa !9
  %59 = sub nsw i64 %58, %57
  store i64 %59, ptr %5, align 8, !tbaa !9
  br label %60

60:                                               ; preds = %34, %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %6, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [110000 x i32], ptr @pa, i64 0, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !5
  store i32 %65, ptr %6, align 4, !tbaa !5
  br label %16, !llvm.loop !11

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN7TreeSet3sumEii(ptr noundef nonnull align 4 dereferenceable(198000012), i32 noundef, i32 noundef) #1 align 2

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 = { 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 = { 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.TreeSet = type { [16500000 x %"struct.TreeSet::node"], i32, i32, i32 }
%"struct.TreeSet::node" = type { i32, i32, i32 }

@pa = external dso_local local_unnamed_addr global [110000 x i32], align 16
@root = external dso_local local_unnamed_addr global [110000 x i32], align 16
@root2 = external dso_local local_unnamed_addr global [110000 x i32], align 16
@tree = external dso_local global %struct.TreeSet, align 4

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z5queryii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = sext i32 %0 to i64
  %4 = getelementptr inbounds [110000 x i32], ptr @root, i64 0, i64 %3
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = tail call noundef i32 @_ZN7TreeSet3sumEii(ptr noundef nonnull align 4 dereferenceable(198000012) @tree, i32 noundef %5, i32 noundef %1)
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds [110000 x i32], ptr @pa, i64 0, i64 %3
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %.not21 = icmp eq i32 %9, 0
  br i1 %.not21, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %29, %2
  %.0.lcssa = phi i64 [ %7, %2 ], [ %.1, %29 ]
  ret i64 %.0.lcssa

.lr.ph:                                           ; preds = %2, %29
  %10 = phi i32 [ %33, %29 ], [ %9, %2 ]
  %11 = phi ptr [ %32, %29 ], [ %8, %2 ]
  %12 = phi i64 [ %31, %29 ], [ %3, %2 ]
  %.022 = phi i64 [ %.1, %29 ], [ %7, %2 ]
  %13 = tail call noundef i32 @_Z4distii(i32 noundef %0, i32 noundef %10)
  %14 = sub nsw i32 %1, %13
  %15 = icmp sgt i32 %14, -1
  br i1 %15, label %16, label %29

16:                                               ; preds = %.lr.ph
  %17 = load i32, ptr %11, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [110000 x i32], ptr @root, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = tail call noundef i32 @_ZN7TreeSet3sumEii(ptr noundef nonnull align 4 dereferenceable(198000012) @tree, i32 noundef %20, i32 noundef %14)
  %22 = sext i32 %21 to i64
  %23 = add nsw i64 %.022, %22
  %24 = getelementptr inbounds [110000 x i32], ptr @root2, i64 0, i64 %12
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = tail call noundef i32 @_ZN7TreeSet3sumEii(ptr noundef nonnull align 4 dereferenceable(198000012) @tree, i32 noundef %25, i32 noundef %14)
  %27 = sext i32 %26 to i64
  %28 = sub i64 %23, %27
  br label %29

29:                                               ; preds = %16, %.lr.ph
  %.1 = phi i64 [ %28, %16 ], [ %.022, %.lr.ph ]
  %30 = load i32, ptr %11, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [110000 x i32], ptr @pa, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %.not = icmp eq i32 %33, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN7TreeSet3sumEii(ptr noundef nonnull align 4 dereferenceable(198000012), i32 noundef, i32 noundef) local_unnamed_addr #0 align 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" }

!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,TailCallElimPass