; LLVM IR from sunkafei/xcpc-algorithm-templates :: 算法/数据结构/树链剖分.ll (Z5queryiii)

; 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.segment = type { i32, i32, i32, i32, i32, i32 }

@ans = external dso_local global i32, align 4
@t = external dso_local global [440000 x %struct.segment], 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: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z8pushdowni(i32 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5queryiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !5
  %8 = load i32, ptr %5, align 4, !tbaa !5
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [440000 x %struct.segment], ptr @t, i64 0, i64 %10
  %12 = getelementptr inbounds %struct.segment, ptr %11, i32 0, i32 0
  %13 = load i32, ptr %12, align 8, !tbaa !9
  %14 = icmp sle i32 %8, %13
  br i1 %14, label %15, label %31

15:                                               ; preds = %3
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [440000 x %struct.segment], ptr @t, i64 0, i64 %18
  %20 = getelementptr inbounds %struct.segment, ptr %19, i32 0, i32 1
  %21 = load i32, ptr %20, align 4, !tbaa !11
  %22 = icmp sge i32 %16, %21
  br i1 %22, label %23, label %31

23:                                               ; preds = %15
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [440000 x %struct.segment], ptr @t, i64 0, i64 %25
  %27 = getelementptr inbounds %struct.segment, ptr %26, i32 0, i32 4
  %28 = load i32, ptr %27, align 8, !tbaa !12
  %29 = load i32, ptr @ans, align 4, !tbaa !5
  %30 = add nsw i32 %29, %28
  store i32 %30, ptr @ans, align 4, !tbaa !5
  br label %69

31:                                               ; preds = %15, %3
  %32 = load i32, ptr %4, align 4, !tbaa !5
  call void @_Z8pushdowni(i32 noundef %32)
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [440000 x %struct.segment], ptr @t, i64 0, i64 %34
  %36 = getelementptr inbounds %struct.segment, ptr %35, i32 0, i32 0
  %37 = load i32, ptr %36, align 8, !tbaa !9
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [440000 x %struct.segment], ptr @t, i64 0, i64 %39
  %41 = getelementptr inbounds %struct.segment, ptr %40, i32 0, i32 1
  %42 = load i32, ptr %41, align 4, !tbaa !11
  %43 = add nsw i32 %37, %42
  %44 = ashr i32 %43, 1
  store i32 %44, ptr %7, align 4, !tbaa !5
  %45 = load i32, ptr %5, align 4, !tbaa !5
  %46 = load i32, ptr %7, align 4, !tbaa !5
  %47 = icmp sle i32 %45, %46
  br i1 %47, label %48, label %56

48:                                               ; preds = %31
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [440000 x %struct.segment], ptr @t, i64 0, i64 %50
  %52 = getelementptr inbounds %struct.segment, ptr %51, i32 0, i32 2
  %53 = load i32, ptr %52, align 8, !tbaa !13
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = load i32, ptr %6, align 4, !tbaa !5
  call void @_Z5queryiii(i32 noundef %53, i32 noundef %54, i32 noundef %55)
  br label %56

56:                                               ; preds = %48, %31
  %57 = load i32, ptr %6, align 4, !tbaa !5
  %58 = load i32, ptr %7, align 4, !tbaa !5
  %59 = icmp sgt i32 %57, %58
  br i1 %59, label %60, label %68

60:                                               ; preds = %56
  %61 = load i32, ptr %4, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [440000 x %struct.segment], ptr @t, i64 0, i64 %62
  %64 = getelementptr inbounds %struct.segment, ptr %63, i32 0, i32 3
  %65 = load i32, ptr %64, align 4, !tbaa !14
  %66 = load i32, ptr %5, align 4, !tbaa !5
  %67 = load i32, ptr %6, align 4, !tbaa !5
  call void @_Z5queryiii(i32 noundef %65, i32 noundef %66, i32 noundef %67)
  br label %68

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

69:                                               ; preds = %68, %23
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !6, i64 0}
!10 = !{!"_ZTS7segment", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20}
!11 = !{!10, !6, i64 4}
!12 = !{!10, !6, i64 16}
!13 = !{!10, !6, i64 8}
!14 = !{!10, !6, i64 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.segment = type { i32, i32, i32, i32, i32, i32 }

@ans = external dso_local local_unnamed_addr global i32, align 4
@t = external dso_local local_unnamed_addr global [440000 x %struct.segment], align 16

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z5queryiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  br label %tailrecurse

tailrecurse:                                      ; preds = %26, %3
  %.tr = phi i32 [ %0, %3 ], [ %28, %26 ]
  %4 = sext i32 %.tr to i64
  %5 = getelementptr inbounds [440000 x %struct.segment], ptr @t, i64 0, i64 %4
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %.not = icmp slt i32 %6, %1
  br i1 %.not, label %15, label %7

7:                                                ; preds = %tailrecurse
  %8 = getelementptr inbounds i8, ptr %5, i64 4
  %9 = load i32, ptr %8, align 4, !tbaa !10
  %.not19 = icmp sgt i32 %9, %2
  br i1 %.not19, label %15, label %10

10:                                               ; preds = %7
  %11 = getelementptr inbounds i8, ptr %5, i64 16
  %12 = load i32, ptr %11, align 8, !tbaa !11
  %13 = load i32, ptr @ans, align 4, !tbaa !12
  %14 = add nsw i32 %13, %12
  store i32 %14, ptr @ans, align 4, !tbaa !12
  br label %.loopexit

15:                                               ; preds = %7, %tailrecurse
  tail call void @_Z8pushdowni(i32 noundef %.tr)
  %16 = load i32, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %5, i64 4
  %18 = load i32, ptr %17, align 4, !tbaa !10
  %19 = add nsw i32 %18, %16
  %20 = ashr i32 %19, 1
  %.not20 = icmp slt i32 %20, %1
  br i1 %.not20, label %24, label %21

21:                                               ; preds = %15
  %22 = getelementptr inbounds i8, ptr %5, i64 8
  %23 = load i32, ptr %22, align 8, !tbaa !13
  tail call void @_Z5queryiii(i32 noundef %23, i32 noundef %1, i32 noundef %2)
  br label %24

24:                                               ; preds = %21, %15
  %25 = icmp slt i32 %20, %2
  br i1 %25, label %26, label %.loopexit

26:                                               ; preds = %24
  %27 = getelementptr inbounds i8, ptr %5, i64 12
  %28 = load i32, ptr %27, align 4, !tbaa !14
  br label %tailrecurse

.loopexit:                                        ; preds = %24, %10
  ret void
}

attributes #0 = { 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 #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" }

!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, !7, i64 0}
!6 = !{!"_ZTS7segment", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = !{!6, !7, i64 16}
!12 = !{!7, !7, i64 0}
!13 = !{!6, !7, i64 8}
!14 = !{!6, !7, i64 12}


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