; LLVM IR from Tunghohin/Competitive_Coding :: Codeforces/cf1691_D.ll (Z5queryxxx)

; 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 { i64, i64, i64, i64, i64, i64 }

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z7push_upR4NodeS0_S0_(ptr noundef nonnull align 8 dereferenceable(48), ptr noundef nonnull align 8 dereferenceable(48), ptr noundef nonnull align 8 dereferenceable(48)) #0

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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5queryxxx(ptr dead_on_unwind noalias writable sret(%struct.Node) align 8 %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca %struct.Node, align 8
  %11 = alloca %struct.Node, align 8
  store i64 %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !5
  store i64 %3, ptr %7, align 8, !tbaa !5
  %12 = load i64, ptr %5, align 8, !tbaa !5
  %13 = getelementptr inbounds [800040 x %struct.Node], ptr @tr, i64 0, i64 %12
  %14 = getelementptr inbounds %struct.Node, ptr %13, i32 0, i32 0
  %15 = load i64, ptr %14, align 16, !tbaa !9
  %16 = load i64, ptr %6, align 8, !tbaa !5
  %17 = icmp sge i64 %15, %16
  br i1 %17, label %18, label %28

18:                                               ; preds = %4
  %19 = load i64, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds [800040 x %struct.Node], ptr @tr, i64 0, i64 %19
  %21 = getelementptr inbounds %struct.Node, ptr %20, i32 0, i32 1
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = load i64, ptr %7, align 8, !tbaa !5
  %24 = icmp sle i64 %22, %23
  br i1 %24, label %25, label %28

25:                                               ; preds = %18
  %26 = load i64, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds [800040 x %struct.Node], ptr @tr, i64 0, i64 %26
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %0, ptr align 16 %27, i64 48, i1 false), !tbaa.struct !12
  br label %68

28:                                               ; preds = %18, %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %29 = load i64, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds [800040 x %struct.Node], ptr @tr, i64 0, i64 %29
  %31 = getelementptr inbounds %struct.Node, ptr %30, i32 0, i32 0
  %32 = load i64, ptr %31, align 16, !tbaa !9
  %33 = load i64, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds [800040 x %struct.Node], ptr @tr, i64 0, i64 %33
  %35 = getelementptr inbounds %struct.Node, ptr %34, i32 0, i32 1
  %36 = load i64, ptr %35, align 8, !tbaa !11
  %37 = add nsw i64 %32, %36
  %38 = ashr i64 %37, 1
  store i64 %38, ptr %8, align 8, !tbaa !5
  %39 = load i64, ptr %7, align 8, !tbaa !5
  %40 = load i64, ptr %8, align 8, !tbaa !5
  %41 = icmp sle i64 %39, %40
  br i1 %41, label %42, label %47

42:                                               ; preds = %28
  %43 = load i64, ptr %5, align 8, !tbaa !5
  %44 = shl i64 %43, 1
  %45 = load i64, ptr %6, align 8, !tbaa !5
  %46 = load i64, ptr %7, align 8, !tbaa !5
  call void @_Z5queryxxx(ptr dead_on_unwind writable sret(%struct.Node) align 8 %0, i64 noundef %44, i64 noundef %45, i64 noundef %46)
  store i32 1, ptr %9, align 4
  br label %67

47:                                               ; preds = %28
  %48 = load i64, ptr %6, align 8, !tbaa !5
  %49 = load i64, ptr %8, align 8, !tbaa !5
  %50 = icmp sgt i64 %48, %49
  br i1 %50, label %51, label %57

51:                                               ; preds = %47
  %52 = load i64, ptr %5, align 8, !tbaa !5
  %53 = shl i64 %52, 1
  %54 = or i64 %53, 1
  %55 = load i64, ptr %6, align 8, !tbaa !5
  %56 = load i64, ptr %7, align 8, !tbaa !5
  call void @_Z5queryxxx(ptr dead_on_unwind writable sret(%struct.Node) align 8 %0, i64 noundef %54, i64 noundef %55, i64 noundef %56)
  store i32 1, ptr %9, align 4
  br label %67

57:                                               ; preds = %47
  call void @llvm.lifetime.start.p0(i64 48, ptr %10) #3
  %58 = load i64, ptr %5, align 8, !tbaa !5
  %59 = shl i64 %58, 1
  %60 = load i64, ptr %6, align 8, !tbaa !5
  %61 = load i64, ptr %7, align 8, !tbaa !5
  call void @_Z5queryxxx(ptr dead_on_unwind writable sret(%struct.Node) align 8 %10, i64 noundef %59, i64 noundef %60, i64 noundef %61)
  call void @llvm.lifetime.start.p0(i64 48, ptr %11) #3
  %62 = load i64, ptr %5, align 8, !tbaa !5
  %63 = shl i64 %62, 1
  %64 = or i64 %63, 1
  %65 = load i64, ptr %6, align 8, !tbaa !5
  %66 = load i64, ptr %7, align 8, !tbaa !5
  call void @_Z5queryxxx(ptr dead_on_unwind writable sret(%struct.Node) align 8 %11, i64 noundef %64, i64 noundef %65, i64 noundef %66)
  call void @_Z7push_upR4NodeS0_S0_(ptr noundef nonnull align 8 dereferenceable(48) %0, ptr noundef nonnull align 8 dereferenceable(48) %10, ptr noundef nonnull align 8 dereferenceable(48) %11)
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 48, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 48, ptr %10) #3
  br label %67

67:                                               ; preds = %57, %51, %42
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %68

68:                                               ; preds = %67, %25
  ret void
}

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 0}
!10 = !{!"_ZTS4Node", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!11 = !{!10, !6, i64 8}
!12 = !{i64 0, i64 8, !5, i64 8, i64 8, !5, i64 16, i64 8, !5, i64 24, i64 8, !5, i64 32, i64 8, !5, i64 40, i64 8, !5}


; 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 { i64, i64, i64, i64, i64, i64 }

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z7push_upR4NodeS0_S0_(ptr noundef nonnull align 8 dereferenceable(48), ptr noundef nonnull align 8 dereferenceable(48), ptr noundef nonnull align 8 dereferenceable(48)) local_unnamed_addr #0

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

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5queryxxx(ptr dead_on_unwind noalias writable sret(%struct.Node) align 8 %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = alloca %struct.Node, align 8
  %6 = alloca %struct.Node, align 8
  %7 = getelementptr inbounds [800040 x %struct.Node], ptr @tr, i64 0, i64 %1
  %8 = load i64, ptr %7, align 16, !tbaa !5
  %.not39 = icmp slt i64 %8, %2
  %.phi.trans.insert40 = getelementptr inbounds i8, ptr %7, i64 8
  %.pre41 = load i64, ptr %.phi.trans.insert40, align 8, !tbaa !10
  %.not2642 = icmp sgt i64 %.pre41, %3
  %or.cond43 = select i1 %.not39, i1 true, i1 %.not2642
  br i1 %or.cond43, label %tailrecurse._crit_edge, label %tailrecurse._crit_edge46

tailrecurse._crit_edge46:                         ; preds = %tailrecurse.backedge, %4
  %.lcssa = phi ptr [ %7, %4 ], [ %14, %tailrecurse.backedge ]
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(48) %0, ptr noundef nonnull align 16 dereferenceable(48) %.lcssa, i64 48, i1 false), !tbaa.struct !11
  br label %24

tailrecurse._crit_edge:                           ; preds = %4, %tailrecurse.backedge
  %.pre45 = phi i64 [ %.pre, %tailrecurse.backedge ], [ %.pre41, %4 ]
  %9 = phi i64 [ %15, %tailrecurse.backedge ], [ %8, %4 ]
  %.tr2844 = phi i64 [ %.tr28.be, %tailrecurse.backedge ], [ %1, %4 ]
  %10 = add nsw i64 %.pre45, %9
  %11 = ashr i64 %10, 1
  %.not27 = icmp slt i64 %11, %3
  br i1 %.not27, label %16, label %12

12:                                               ; preds = %tailrecurse._crit_edge
  %13 = shl i64 %.tr2844, 1
  br label %tailrecurse.backedge

tailrecurse.backedge:                             ; preds = %12, %18
  %.tr28.be = phi i64 [ %13, %12 ], [ %20, %18 ]
  %14 = getelementptr inbounds [800040 x %struct.Node], ptr @tr, i64 0, i64 %.tr28.be
  %15 = load i64, ptr %14, align 16, !tbaa !5
  %.not = icmp slt i64 %15, %2
  %.phi.trans.insert = getelementptr inbounds i8, ptr %14, i64 8
  %.pre = load i64, ptr %.phi.trans.insert, align 8, !tbaa !10
  %.not26 = icmp sgt i64 %.pre, %3
  %or.cond = select i1 %.not, i1 true, i1 %.not26
  br i1 %or.cond, label %tailrecurse._crit_edge, label %tailrecurse._crit_edge46

16:                                               ; preds = %tailrecurse._crit_edge
  %17 = icmp slt i64 %11, %2
  br i1 %17, label %18, label %21

18:                                               ; preds = %16
  %19 = shl i64 %.tr2844, 1
  %20 = or disjoint i64 %19, 1
  br label %tailrecurse.backedge

21:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 48, ptr nonnull %5) #3
  %22 = shl i64 %.tr2844, 1
  call void @_Z5queryxxx(ptr dead_on_unwind nonnull writable sret(%struct.Node) align 8 %5, i64 noundef %22, i64 noundef %2, i64 noundef %3)
  call void @llvm.lifetime.start.p0(i64 48, ptr nonnull %6) #3
  %23 = or disjoint i64 %22, 1
  call void @_Z5queryxxx(ptr dead_on_unwind nonnull writable sret(%struct.Node) align 8 %6, i64 noundef %23, i64 noundef %2, i64 noundef %3)
  call void @_Z7push_upR4NodeS0_S0_(ptr noundef nonnull align 8 dereferenceable(48) %0, ptr noundef nonnull align 8 dereferenceable(48) %5, ptr noundef nonnull align 8 dereferenceable(48) %6)
  call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %5) #3
  br label %24

24:                                               ; preds = %21, %tailrecurse._crit_edge46
  ret void
}

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !7, i64 0}
!6 = !{!"_ZTS4Node", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40}
!7 = !{!"long long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{i64 0, i64 8, !12, i64 8, i64 8, !12, i64 16, i64 8, !12, i64 24, i64 8, !12, i64 32, i64 8, !12, i64 40, i64 8, !12}
!12 = !{!7, !7, i64 0}


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