; LLVM IR from Tunghohin/Competitive_Coding :: Codeforces/cf510_D.ll (Z5queryiiii)

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

@tr = external dso_local global [3600180 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 uwtable
define dso_local noundef i64 @_Z5queryiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #1 {
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !5
  %12 = load i32, ptr %7, align 4, !tbaa !5
  %13 = load i32, ptr %8, align 4, !tbaa !5
  %14 = icmp eq i32 %12, %13
  br i1 %14, label %15, label %21

15:                                               ; preds = %4
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %17
  %19 = getelementptr inbounds %struct.Node, ptr %18, i32 0, i32 2
  %20 = load i64, ptr %19, align 8, !tbaa !9
  store i64 %20, ptr %5, align 8
  br label %61

21:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %22 = load i32, ptr %7, align 4, !tbaa !5
  %23 = load i32, ptr %8, align 4, !tbaa !5
  %24 = add nsw i32 %22, %23
  %25 = ashr i32 %24, 1
  store i32 %25, ptr %10, align 4, !tbaa !5
  %26 = load i32, ptr %9, align 4, !tbaa !5
  %27 = load i32, ptr %10, align 4, !tbaa !5
  %28 = icmp sle i32 %26, %27
  br i1 %28, label %29, label %39

29:                                               ; preds = %21
  %30 = load i32, ptr %6, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %31
  %33 = getelementptr inbounds %struct.Node, ptr %32, i32 0, i32 0
  %34 = load i32, ptr %33, align 16, !tbaa !12
  %35 = load i32, ptr %7, align 4, !tbaa !5
  %36 = load i32, ptr %10, align 4, !tbaa !5
  %37 = load i32, ptr %9, align 4, !tbaa !5
  %38 = call noundef i64 @_Z5queryiiii(i32 noundef %34, i32 noundef %35, i32 noundef %36, i32 noundef %37)
  store i64 %38, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %60

39:                                               ; preds = %21
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %41
  %43 = getelementptr inbounds %struct.Node, ptr %42, i32 0, i32 0
  %44 = load i32, ptr %43, align 16, !tbaa !12
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %45
  %47 = getelementptr inbounds %struct.Node, ptr %46, i32 0, i32 2
  %48 = load i64, ptr %47, align 8, !tbaa !9
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %50
  %52 = getelementptr inbounds %struct.Node, ptr %51, i32 0, i32 1
  %53 = load i32, ptr %52, align 4, !tbaa !13
  %54 = load i32, ptr %10, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  %56 = load i32, ptr %8, align 4, !tbaa !5
  %57 = load i32, ptr %9, align 4, !tbaa !5
  %58 = call noundef i64 @_Z5queryiiii(i32 noundef %53, i32 noundef %55, i32 noundef %56, i32 noundef %57)
  %59 = add nsw i64 %48, %58
  store i64 %59, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %60

60:                                               ; preds = %39, %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %61

61:                                               ; preds = %60, %15
  %62 = load i64, ptr %5, align 8
  ret i64 %62
}

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


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

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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z5queryiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp eq i32 %1, %2
  br i1 %5, label %tailrecurse.outer._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %tailrecurse.outer
  %.tr24.ph36 = phi i32 [ %.tr2429, %tailrecurse.outer ], [ %2, %4 ]
  %.tr23.ph35 = phi i32 [ %21, %tailrecurse.outer ], [ %1, %4 ]
  %.tr.ph34 = phi i32 [ %20, %tailrecurse.outer ], [ %0, %4 ]
  %accumulator.tr.ph33 = phi i64 [ %22, %tailrecurse.outer ], [ 0, %4 ]
  br label %9

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %tailrecurse, %4
  %accumulator.tr.ph.lcssa = phi i64 [ 0, %4 ], [ %accumulator.tr.ph33, %tailrecurse ], [ %22, %tailrecurse.outer ]
  %.tr.lcssa = phi i32 [ %0, %4 ], [ %14, %tailrecurse ], [ %20, %tailrecurse.outer ]
  %6 = sext i32 %.tr.lcssa to i64
  %7 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %6, i32 2
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %accumulator.ret.tr = add nsw i64 %8, %accumulator.tr.ph.lcssa
  ret i64 %accumulator.ret.tr

9:                                                ; preds = %.lr.ph, %tailrecurse
  %.tr2429 = phi i32 [ %.tr24.ph36, %.lr.ph ], [ %11, %tailrecurse ]
  %.tr28 = phi i32 [ %.tr.ph34, %.lr.ph ], [ %14, %tailrecurse ]
  %10 = add nsw i32 %.tr2429, %.tr23.ph35
  %11 = ashr i32 %10, 1
  %.not = icmp slt i32 %11, %3
  %12 = sext i32 %.tr28 to i64
  %13 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %12
  %14 = load i32, ptr %13, align 16, !tbaa !11
  br i1 %.not, label %tailrecurse.outer, label %tailrecurse

tailrecurse:                                      ; preds = %9
  %15 = icmp eq i32 %.tr23.ph35, %11
  br i1 %15, label %tailrecurse.outer._crit_edge, label %9

tailrecurse.outer:                                ; preds = %9
  %16 = sext i32 %14 to i64
  %17 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %16, i32 2
  %18 = load i64, ptr %17, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %13, i64 4
  %20 = load i32, ptr %19, align 4, !tbaa !12
  %21 = add nsw i32 %11, 1
  %22 = add nsw i64 %18, %accumulator.tr.ph33
  %23 = icmp eq i32 %21, %.tr2429
  br i1 %23, label %tailrecurse.outer._crit_edge, label %.lr.ph
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, 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, !10, i64 8}
!6 = !{!"_ZTS4Node", !7, i64 0, !7, i64 4, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"long long", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!6, !7, i64 4}


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