; LLVM IR from Dev-XYS/Algorithms :: Binary-Search-Tree.ll (Z5queryii)

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

@T = external dso_local global [100000 x %struct.node], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z5queryii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %6 = load i32, ptr %4, align 4, !tbaa !5
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %7
  %9 = getelementptr inbounds %struct.node, ptr %8, i32 0, i32 2
  %10 = load i32, ptr %9, align 8, !tbaa !9
  %11 = load i32, ptr %5, align 4, !tbaa !5
  %12 = icmp eq i32 %10, %11
  br i1 %12, label %13, label %19

13:                                               ; preds = %2
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %15
  %17 = getelementptr inbounds %struct.node, ptr %16, i32 0, i32 3
  %18 = load i32, ptr %17, align 4, !tbaa !11
  store i32 %18, ptr %3, align 4
  br label %45

19:                                               ; preds = %2
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %22
  %24 = getelementptr inbounds %struct.node, ptr %23, i32 0, i32 2
  %25 = load i32, ptr %24, align 8, !tbaa !9
  %26 = icmp slt i32 %20, %25
  br i1 %26, label %27, label %35

27:                                               ; preds = %19
  %28 = load i32, ptr %4, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %29
  %31 = getelementptr inbounds %struct.node, ptr %30, i32 0, i32 0
  %32 = load i32, ptr %31, align 16, !tbaa !12
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = call noundef i32 @_Z5queryii(i32 noundef %32, i32 noundef %33)
  br label %43

35:                                               ; preds = %19
  %36 = load i32, ptr %4, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %37
  %39 = getelementptr inbounds %struct.node, ptr %38, i32 0, i32 1
  %40 = load i32, ptr %39, align 4, !tbaa !13
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = call noundef i32 @_Z5queryii(i32 noundef %40, i32 noundef %41)
  br label %43

43:                                               ; preds = %35, %27
  %44 = phi i32 [ %34, %27 ], [ %42, %35 ]
  store i32 %44, ptr %3, align 4
  br label %45

45:                                               ; preds = %43, %13
  %46 = load i32, ptr %3, align 4
  ret i32 %46
}

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 8}
!10 = !{!"_ZTS4node", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12}
!11 = !{!10, !6, i64 12}
!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, i32, i32 }

@T = external dso_local local_unnamed_addr global [100000 x %struct.node], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5queryii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = sext i32 %0 to i64
  %4 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %3, i32 2
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = icmp eq i32 %5, %1
  br i1 %6, label %tailrecurse._crit_edge, label %.lr.ph

tailrecurse._crit_edge:                           ; preds = %.lr.ph, %2
  %7 = phi i64 [ %3, %2 ], [ %14, %.lr.ph ]
  %8 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %7, i32 3
  %9 = load i32, ptr %8, align 4, !tbaa !10
  ret i32 %9

.lr.ph:                                           ; preds = %2, %.lr.ph
  %10 = phi i32 [ %16, %.lr.ph ], [ %5, %2 ]
  %11 = phi i64 [ %14, %.lr.ph ], [ %3, %2 ]
  %12 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %11
  %13 = icmp sgt i32 %10, %1
  %spec.select.idx = select i1 %13, i64 0, i64 4
  %spec.select = getelementptr inbounds i8, ptr %12, i64 %spec.select.idx
  %.tr.be = load i32, ptr %spec.select, align 4, !tbaa !11
  %14 = sext i32 %.tr.be to i64
  %15 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %14, i32 2
  %16 = load i32, ptr %15, align 8, !tbaa !5
  %17 = icmp eq i32 %16, %1
  br i1 %17, label %tailrecurse._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, !7, i64 8}
!6 = !{!"_ZTS4node", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 12}
!11 = !{!7, !7, i64 0}


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