; LLVM IR from ncduy0303/Competitive-Programming :: Data Structures/Segment Tree/Advanced example 2.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"

@st = external dso_local global [800004 x i32], 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 i32 @_Z5queryiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #1 {
  %5 = alloca i32, align 4
  %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 %27

15:                                               ; preds = %4
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [800004 x i32], ptr @st, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = load i32, ptr %9, align 4, !tbaa !5
  %21 = icmp sge i32 %19, %20
  br i1 %21, label %22, label %24

22:                                               ; preds = %15
  %23 = load i32, ptr %7, align 4, !tbaa !5
  br label %25

24:                                               ; preds = %15
  br label %25

25:                                               ; preds = %24, %22
  %26 = phi i32 [ %23, %22 ], [ 0, %24 ]
  store i32 %26, ptr %5, align 4
  br label %56

27:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %28 = load i32, ptr %7, align 4, !tbaa !5
  %29 = load i32, ptr %8, align 4, !tbaa !5
  %30 = add nsw i32 %28, %29
  %31 = sdiv i32 %30, 2
  store i32 %31, ptr %10, align 4, !tbaa !5
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = mul nsw i32 2, %32
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [800004 x i32], ptr @st, i64 0, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = load i32, ptr %9, align 4, !tbaa !5
  %38 = icmp sge i32 %36, %37
  br i1 %38, label %39, label %46

39:                                               ; preds = %27
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = mul nsw i32 2, %40
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = load i32, ptr %10, align 4, !tbaa !5
  %44 = load i32, ptr %9, align 4, !tbaa !5
  %45 = call noundef i32 @_Z5queryiiii(i32 noundef %41, i32 noundef %42, i32 noundef %43, i32 noundef %44)
  store i32 %45, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %55

46:                                               ; preds = %27
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = mul nsw i32 2, %47
  %49 = add nsw i32 %48, 1
  %50 = load i32, ptr %10, align 4, !tbaa !5
  %51 = add nsw i32 %50, 1
  %52 = load i32, ptr %8, align 4, !tbaa !5
  %53 = load i32, ptr %9, align 4, !tbaa !5
  %54 = call noundef i32 @_Z5queryiiii(i32 noundef %49, i32 noundef %51, i32 noundef %52, i32 noundef %53)
  store i32 %54, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %55

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

56:                                               ; preds = %55, %25
  %57 = load i32, ptr %5, align 4
  ret i32 %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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


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

@st = external dso_local local_unnamed_addr global [800004 x i32], align 16

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

tailrecurse.outer:                                ; preds = %18, %4
  %.tr.ph = phi i32 [ %19, %18 ], [ %0, %4 ]
  %.tr24.ph = phi i32 [ %20, %18 ], [ %1, %4 ]
  %.tr25.ph = phi i32 [ %.tr25, %18 ], [ %2, %4 ]
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse.outer, %11
  %.tr = phi i32 [ %14, %11 ], [ %.tr.ph, %tailrecurse.outer ]
  %.tr25 = phi i32 [ %13, %11 ], [ %.tr25.ph, %tailrecurse.outer ]
  %5 = icmp eq i32 %.tr24.ph, %.tr25
  br i1 %5, label %6, label %11

6:                                                ; preds = %tailrecurse
  %7 = sext i32 %.tr to i64
  %8 = getelementptr inbounds [800004 x i32], ptr @st, i64 0, i64 %7
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %.not23 = icmp slt i32 %9, %3
  %10 = select i1 %.not23, i32 0, i32 %.tr24.ph
  ret i32 %10

11:                                               ; preds = %tailrecurse
  %12 = add nsw i32 %.tr25, %.tr24.ph
  %13 = sdiv i32 %12, 2
  %14 = shl nsw i32 %.tr, 1
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [800004 x i32], ptr @st, i64 0, i64 %15
  %17 = load i32, ptr %16, align 8, !tbaa !5
  %.not = icmp slt i32 %17, %3
  br i1 %.not, label %18, label %tailrecurse

18:                                               ; preds = %11
  %19 = or disjoint i32 %14, 1
  %20 = add nsw i32 %13, 1
  br label %tailrecurse.outer
}

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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


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