; LLVM IR from SuprDewd/CompetitiveProgramming :: code/data-structures/persistent_segment_tree.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 }

@segs = external dso_local global [2000000 x %struct.segment], align 16

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

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

23:                                               ; preds = %15, %3
  store i32 0, ptr %4, align 4
  br label %64

24:                                               ; preds = %15
  %25 = load i32, ptr %6, align 4, !tbaa !5
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %27
  %29 = getelementptr inbounds %struct.segment, ptr %28, i32 0, i32 0
  %30 = load i32, ptr %29, align 4, !tbaa !9
  %31 = icmp sle i32 %25, %30
  br i1 %31, label %32, label %46

32:                                               ; preds = %24
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %34
  %36 = getelementptr inbounds %struct.segment, ptr %35, i32 0, i32 1
  %37 = load i32, ptr %36, align 4, !tbaa !11
  %38 = load i32, ptr %7, align 4, !tbaa !5
  %39 = icmp sle i32 %37, %38
  br i1 %39, label %40, label %46

40:                                               ; preds = %32
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %42
  %44 = getelementptr inbounds %struct.segment, ptr %43, i32 0, i32 4
  %45 = load i32, ptr %44, align 4, !tbaa !12
  store i32 %45, ptr %4, align 4
  br label %64

46:                                               ; preds = %32, %24
  %47 = load i32, ptr %5, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %48
  %50 = getelementptr inbounds %struct.segment, ptr %49, i32 0, i32 2
  %51 = load i32, ptr %50, align 4, !tbaa !13
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = load i32, ptr %7, align 4, !tbaa !5
  %54 = call noundef i32 @_Z5queryiii(i32 noundef %51, i32 noundef %52, i32 noundef %53)
  %55 = load i32, ptr %5, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %56
  %58 = getelementptr inbounds %struct.segment, ptr %57, i32 0, i32 3
  %59 = load i32, ptr %58, align 4, !tbaa !14
  %60 = load i32, ptr %6, align 4, !tbaa !5
  %61 = load i32, ptr %7, align 4, !tbaa !5
  %62 = call noundef i32 @_Z5queryiii(i32 noundef %59, i32 noundef %60, i32 noundef %61)
  %63 = add nsw i32 %54, %62
  store i32 %63, ptr %4, align 4
  br label %64

64:                                               ; preds = %46, %40, %23
  %65 = load i32, ptr %4, align 4
  ret i32 %65
}

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

!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}
!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 }

@segs = external dso_local local_unnamed_addr global [2000000 x %struct.segment], align 16

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5queryiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = sext i32 %0 to i64
  %5 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %4
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = icmp sgt i32 %6, %2
  br i1 %7, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %3, %tailrecurse
  %8 = phi i32 [ %25, %tailrecurse ], [ %6, %3 ]
  %9 = phi ptr [ %24, %tailrecurse ], [ %5, %3 ]
  %accumulator.tr26 = phi i32 [ %22, %tailrecurse ], [ 0, %3 ]
  %10 = getelementptr inbounds i8, ptr %9, i64 4
  %11 = load i32, ptr %10, align 4, !tbaa !10
  %12 = icmp slt i32 %11, %1
  br i1 %12, label %.loopexit, label %13

13:                                               ; preds = %.lr.ph
  %.not = icmp slt i32 %8, %1
  %.not20 = icmp sgt i32 %11, %2
  %or.cond = or i1 %.not, %.not20
  br i1 %or.cond, label %tailrecurse, label %14

14:                                               ; preds = %13
  %15 = getelementptr inbounds i8, ptr %9, i64 16
  %16 = load i32, ptr %15, align 4, !tbaa !11
  br label %.loopexit

tailrecurse:                                      ; preds = %13
  %17 = getelementptr inbounds i8, ptr %9, i64 8
  %18 = load i32, ptr %17, align 4, !tbaa !12
  %19 = tail call noundef i32 @_Z5queryiii(i32 noundef %18, i32 noundef %1, i32 noundef %2)
  %20 = getelementptr inbounds i8, ptr %9, i64 12
  %21 = load i32, ptr %20, align 4, !tbaa !13
  %22 = add nsw i32 %19, %accumulator.tr26
  %23 = sext i32 %21 to i64
  %24 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = icmp sgt i32 %25, %2
  br i1 %26, label %.loopexit, label %.lr.ph

.loopexit:                                        ; preds = %.lr.ph, %tailrecurse, %3, %14
  %accumulator.tr25 = phi i32 [ %accumulator.tr26, %14 ], [ 0, %3 ], [ %accumulator.tr26, %.lr.ph ], [ %22, %tailrecurse ]
  %.0 = phi i32 [ %16, %14 ], [ 0, %3 ], [ 0, %tailrecurse ], [ 0, %.lr.ph ]
  %accumulator.ret.tr = add nsw i32 %.0, %accumulator.tr25
  ret i32 %accumulator.ret.tr
}

attributes #0 = { mustprogress nofree 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, !7, i64 0}
!6 = !{!"_ZTS7segment", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16}
!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 = !{!6, !7, i64 8}
!13 = !{!6, !7, i64 12}


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