; LLVM IR from ncduy0303/Competitive-Programming :: Data Structures/Segment Tree/Segment Tree (Persistent).ll (Z5queryP5tdataiiii)

; 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.tdata = type { ptr, ptr, i64 }

; 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 @_Z5queryP5tdataiiii(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #1 {
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !9
  store i32 %2, ptr %9, align 4, !tbaa !9
  store i32 %3, ptr %10, align 4, !tbaa !9
  store i32 %4, ptr %11, align 4, !tbaa !9
  %13 = load i32, ptr %11, align 4, !tbaa !9
  %14 = load i32, ptr %8, align 4, !tbaa !9
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %20, label %16

16:                                               ; preds = %5
  %17 = load i32, ptr %9, align 4, !tbaa !9
  %18 = load i32, ptr %10, align 4, !tbaa !9
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %20, label %21

20:                                               ; preds = %16, %5
  store i64 0, ptr %6, align 8
  br label %56

21:                                               ; preds = %16
  %22 = load i32, ptr %10, align 4, !tbaa !9
  %23 = load i32, ptr %8, align 4, !tbaa !9
  %24 = icmp sle i32 %22, %23
  br i1 %24, label %25, label %33

25:                                               ; preds = %21
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = load i32, ptr %11, align 4, !tbaa !9
  %28 = icmp sle i32 %26, %27
  br i1 %28, label %29, label %33

29:                                               ; preds = %25
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.tdata, ptr %30, i32 0, i32 2
  %32 = load i64, ptr %31, align 8, !tbaa !11
  store i64 %32, ptr %6, align 8
  br label %56

33:                                               ; preds = %25, %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %34 = load i32, ptr %8, align 4, !tbaa !9
  %35 = load i32, ptr %9, align 4, !tbaa !9
  %36 = add nsw i32 %34, %35
  %37 = sdiv i32 %36, 2
  store i32 %37, ptr %12, align 4, !tbaa !9
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.tdata, ptr %38, i32 0, i32 0
  %40 = load ptr, ptr %39, align 8, !tbaa !14
  %41 = load i32, ptr %8, align 4, !tbaa !9
  %42 = load i32, ptr %12, align 4, !tbaa !9
  %43 = load i32, ptr %10, align 4, !tbaa !9
  %44 = load i32, ptr %11, align 4, !tbaa !9
  %45 = call noundef i64 @_Z5queryP5tdataiiii(ptr noundef %40, i32 noundef %41, i32 noundef %42, i32 noundef %43, i32 noundef %44)
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.tdata, ptr %46, i32 0, i32 1
  %48 = load ptr, ptr %47, align 8, !tbaa !15
  %49 = load i32, ptr %12, align 4, !tbaa !9
  %50 = add nsw i32 %49, 1
  %51 = load i32, ptr %9, align 4, !tbaa !9
  %52 = load i32, ptr %10, align 4, !tbaa !9
  %53 = load i32, ptr %11, align 4, !tbaa !9
  %54 = call noundef i64 @_Z5queryP5tdataiiii(ptr noundef %48, i32 noundef %50, i32 noundef %51, i32 noundef %52, i32 noundef %53)
  %55 = add nsw i64 %45, %54
  store i64 %55, ptr %6, align 8
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %56

56:                                               ; preds = %33, %29, %20
  %57 = load i64, ptr %6, align 8
  ret i64 %57
}

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


; 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"

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z5queryP5tdataiiii(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = icmp slt i32 %2, %3
  %7 = icmp slt i32 %4, %1
  %or.cond34 = or i1 %6, %7
  br i1 %or.cond34, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %5
  %.not25 = icmp sgt i32 %2, %4
  br i1 %.not25, label %tailrecurse.us, label %.lr.ph.split.split

tailrecurse.us:                                   ; preds = %.lr.ph, %tailrecurse.us
  %.tr2737.us = phi i32 [ %14, %tailrecurse.us ], [ %1, %.lr.ph ]
  %.tr36.us = phi ptr [ %13, %tailrecurse.us ], [ %0, %.lr.ph ]
  %accumulator.tr35.us = phi i64 [ %15, %tailrecurse.us ], [ 0, %.lr.ph ]
  %8 = add nsw i32 %.tr2737.us, %2
  %9 = sdiv i32 %8, 2
  %10 = load ptr, ptr %.tr36.us, align 8, !tbaa !5
  %11 = tail call noundef i64 @_Z5queryP5tdataiiii(ptr noundef %10, i32 noundef %.tr2737.us, i32 noundef %9, i32 noundef %3, i32 noundef %4)
  %12 = getelementptr inbounds i8, ptr %.tr36.us, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !11
  %14 = add nsw i32 %9, 1
  %15 = add nsw i64 %11, %accumulator.tr35.us
  %.not49 = icmp slt i32 %9, %4
  br i1 %.not49, label %tailrecurse.us, label %.loopexit

.lr.ph.split.split:                               ; preds = %.lr.ph, %tailrecurse
  %.tr2737 = phi i32 [ %24, %tailrecurse ], [ %1, %.lr.ph ]
  %.tr36 = phi ptr [ %23, %tailrecurse ], [ %0, %.lr.ph ]
  %accumulator.tr35 = phi i64 [ %25, %tailrecurse ], [ 0, %.lr.ph ]
  %.not = icmp slt i32 %.tr2737, %3
  br i1 %.not, label %tailrecurse, label %.split.us

.split.us:                                        ; preds = %.lr.ph.split.split
  %16 = getelementptr inbounds i8, ptr %.tr36, i64 16
  %17 = load i64, ptr %16, align 8, !tbaa !12
  br label %.loopexit

tailrecurse:                                      ; preds = %.lr.ph.split.split
  %18 = add nsw i32 %.tr2737, %2
  %19 = sdiv i32 %18, 2
  %20 = load ptr, ptr %.tr36, align 8, !tbaa !5
  %21 = tail call noundef i64 @_Z5queryP5tdataiiii(ptr noundef %20, i32 noundef %.tr2737, i32 noundef %19, i32 noundef %3, i32 noundef %4)
  %22 = getelementptr inbounds i8, ptr %.tr36, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !11
  %24 = add nsw i32 %19, 1
  %25 = add nsw i64 %21, %accumulator.tr35
  %.not48 = icmp slt i32 %19, %4
  br i1 %.not48, label %.lr.ph.split.split, label %.loopexit

.loopexit:                                        ; preds = %tailrecurse, %tailrecurse.us, %5, %.split.us
  %accumulator.tr33 = phi i64 [ %accumulator.tr35, %.split.us ], [ 0, %5 ], [ %15, %tailrecurse.us ], [ %25, %tailrecurse ]
  %.0 = phi i64 [ %17, %.split.us ], [ 0, %5 ], [ 0, %tailrecurse.us ], [ 0, %tailrecurse ]
  %accumulator.ret.tr = add nsw i64 %.0, %accumulator.tr33
  ret i64 %accumulator.ret.tr
}

attributes #0 = { mustprogress nofree nosync nounwind willreturn memory(read, 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, !7, i64 0}
!6 = !{!"_ZTS5tdata", !7, i64 0, !7, i64 8, !10, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"long long", !8, i64 0}
!11 = !{!6, !7, i64 8}
!12 = !{!6, !10, i64 16}


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