; LLVM IR from Tunghohin/Competitive_Coding :: Luogu/p2894.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.Node = type { i32, i32, i32, i32, i32, i32, i32 }

@tr = external dso_local global [2000040 x %struct.Node], 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
declare dso_local void @_Z9push_downi(i32 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z5queryii(i32 noundef %0, i32 noundef %1) #2 {
  %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
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %8 = load i32, ptr %4, align 4, !tbaa !5
  call void @_Z9push_downi(i32 noundef %8)
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [2000040 x %struct.Node], ptr @tr, i64 0, i64 %10
  %12 = getelementptr inbounds %struct.Node, ptr %11, i32 0, i32 0
  %13 = load i32, ptr %12, align 4, !tbaa !9
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [2000040 x %struct.Node], ptr @tr, i64 0, i64 %15
  %17 = getelementptr inbounds %struct.Node, ptr %16, i32 0, i32 1
  %18 = load i32, ptr %17, align 4, !tbaa !11
  %19 = icmp eq i32 %13, %18
  br i1 %19, label %20, label %26

20:                                               ; preds = %2
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [2000040 x %struct.Node], ptr @tr, i64 0, i64 %22
  %24 = getelementptr inbounds %struct.Node, ptr %23, i32 0, i32 0
  %25 = load i32, ptr %24, align 4, !tbaa !9
  store i32 %25, ptr %3, align 4
  br label %86

26:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [2000040 x %struct.Node], ptr @tr, i64 0, i64 %28
  %30 = getelementptr inbounds %struct.Node, ptr %29, i32 0, i32 0
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [2000040 x %struct.Node], ptr @tr, i64 0, i64 %33
  %35 = getelementptr inbounds %struct.Node, ptr %34, i32 0, i32 1
  %36 = load i32, ptr %35, align 4, !tbaa !11
  %37 = add nsw i32 %31, %36
  %38 = ashr i32 %37, 1
  store i32 %38, ptr %6, align 4, !tbaa !5
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = shl i32 %39, 1
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [2000040 x %struct.Node], ptr @tr, i64 0, i64 %41
  %43 = getelementptr inbounds %struct.Node, ptr %42, i32 0, i32 3
  %44 = load i32, ptr %43, align 4, !tbaa !12
  %45 = load i32, ptr %5, align 4, !tbaa !5
  %46 = icmp sge i32 %44, %45
  br i1 %46, label %47, label %52

47:                                               ; preds = %26
  %48 = load i32, ptr %4, align 4, !tbaa !5
  %49 = shl i32 %48, 1
  %50 = load i32, ptr %5, align 4, !tbaa !5
  %51 = call noundef i32 @_Z5queryii(i32 noundef %49, i32 noundef %50)
  store i32 %51, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %85

52:                                               ; preds = %26
  %53 = load i32, ptr %4, align 4, !tbaa !5
  %54 = shl i32 %53, 1
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [2000040 x %struct.Node], ptr @tr, i64 0, i64 %55
  %57 = getelementptr inbounds %struct.Node, ptr %56, i32 0, i32 5
  %58 = load i32, ptr %57, align 4, !tbaa !13
  %59 = load i32, ptr %4, align 4, !tbaa !5
  %60 = shl i32 %59, 1
  %61 = or i32 %60, 1
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [2000040 x %struct.Node], ptr @tr, i64 0, i64 %62
  %64 = getelementptr inbounds %struct.Node, ptr %63, i32 0, i32 4
  %65 = load i32, ptr %64, align 4, !tbaa !14
  %66 = add nsw i32 %58, %65
  %67 = load i32, ptr %5, align 4, !tbaa !5
  %68 = icmp sge i32 %66, %67
  br i1 %68, label %69, label %79

69:                                               ; preds = %52
  %70 = load i32, ptr %6, align 4, !tbaa !5
  %71 = load i32, ptr %4, align 4, !tbaa !5
  %72 = shl i32 %71, 1
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds [2000040 x %struct.Node], ptr @tr, i64 0, i64 %73
  %75 = getelementptr inbounds %struct.Node, ptr %74, i32 0, i32 5
  %76 = load i32, ptr %75, align 4, !tbaa !13
  %77 = sub nsw i32 %70, %76
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %85

79:                                               ; preds = %52
  %80 = load i32, ptr %4, align 4, !tbaa !5
  %81 = shl i32 %80, 1
  %82 = or i32 %81, 1
  %83 = load i32, ptr %5, align 4, !tbaa !5
  %84 = call noundef i32 @_Z5queryii(i32 noundef %82, i32 noundef %83)
  store i32 %84, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %85

85:                                               ; preds = %79, %69, %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %86

86:                                               ; preds = %85, %20
  %87 = load i32, ptr %3, align 4
  ret i32 %87
}

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, !6, i64 0}
!10 = !{!"_ZTS4Node", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20, !6, i64 24}
!11 = !{!10, !6, i64 4}
!12 = !{!10, !6, i64 12}
!13 = !{!10, !6, i64 20}
!14 = !{!10, !6, i64 16}


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

@tr = external dso_local local_unnamed_addr global [2000040 x %struct.Node], align 16

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z5queryii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  tail call void @_Z9push_downi(i32 noundef %0)
  %3 = sext i32 %0 to i64
  %4 = getelementptr inbounds [2000040 x %struct.Node], ptr @tr, i64 0, i64 %3
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %4, i64 4
  %7 = load i32, ptr %6, align 4, !tbaa !10
  %8 = icmp eq i32 %5, %7
  br i1 %8, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %2, %tailrecurse.backedge
  %9 = phi i32 [ %19, %tailrecurse.backedge ], [ %7, %2 ]
  %10 = phi i32 [ %17, %tailrecurse.backedge ], [ %5, %2 ]
  %.tr31 = phi i32 [ %.tr.be, %tailrecurse.backedge ], [ %0, %2 ]
  %11 = shl i32 %.tr31, 1
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [2000040 x %struct.Node], ptr @tr, i64 0, i64 %12
  %14 = getelementptr inbounds i8, ptr %13, i64 12
  %15 = load i32, ptr %14, align 4, !tbaa !11
  %.not = icmp slt i32 %15, %1
  br i1 %.not, label %21, label %tailrecurse.backedge

tailrecurse.backedge:                             ; preds = %.lr.ph, %21
  %.pre-phi = phi i64 [ %12, %.lr.ph ], [ %25, %21 ]
  %.tr.be = phi i32 [ %11, %.lr.ph ], [ %24, %21 ]
  tail call void @_Z9push_downi(i32 noundef %.tr.be)
  %16 = getelementptr inbounds [2000040 x %struct.Node], ptr @tr, i64 0, i64 %.pre-phi
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %16, i64 4
  %19 = load i32, ptr %18, align 4, !tbaa !10
  %20 = icmp eq i32 %17, %19
  br i1 %20, label %.loopexit, label %.lr.ph

21:                                               ; preds = %.lr.ph
  %22 = getelementptr inbounds i8, ptr %13, i64 20
  %23 = load i32, ptr %22, align 4, !tbaa !12
  %24 = or disjoint i32 %11, 1
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [2000040 x %struct.Node], ptr @tr, i64 0, i64 %25, i32 4
  %27 = load i32, ptr %26, align 4, !tbaa !13
  %28 = add nsw i32 %27, %23
  %.not23 = icmp slt i32 %28, %1
  br i1 %.not23, label %tailrecurse.backedge, label %29

29:                                               ; preds = %21
  %30 = add nsw i32 %9, %10
  %31 = ashr i32 %30, 1
  %32 = add nsw i32 %31, 1
  %33 = sub i32 %32, %23
  br label %.loopexit

.loopexit:                                        ; preds = %tailrecurse.backedge, %2, %29
  %.1 = phi i32 [ %33, %29 ], [ %5, %2 ], [ %17, %tailrecurse.backedge ]
  ret i32 %.1
}

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


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