; LLVM IR from PetarV-/Algorithms :: Data Structures/Binary Search Tree.ll (ZN3BST6searchEc)

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

%class.BST = type { ptr }
%class.Node = type { i8, i32, ptr, ptr, ptr, ptr }

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZN3BST7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(8)) #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN3BST6searchEc(ptr noundef nonnull align 8 dereferenceable(8) %0, i8 noundef signext %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i8 %1, ptr %5, align 1, !tbaa !9
  %6 = load ptr, ptr %4, align 8
  %7 = call noundef zeroext i1 @_ZN3BST7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(8) %6)
  br i1 %7, label %8, label %9

8:                                                ; preds = %2
  store ptr null, ptr %3, align 8
  br label %44

9:                                                ; preds = %2
  %10 = getelementptr inbounds %class.BST, ptr %6, i32 0, i32 0
  %11 = load ptr, ptr %10, align 8, !tbaa !10
  %12 = getelementptr inbounds %class.Node, ptr %11, i32 0, i32 0
  %13 = load i8, ptr %12, align 8, !tbaa !12
  %14 = sext i8 %13 to i32
  %15 = load i8, ptr %5, align 1, !tbaa !9
  %16 = sext i8 %15 to i32
  %17 = icmp sgt i32 %14, %16
  br i1 %17, label %18, label %25

18:                                               ; preds = %9
  %19 = getelementptr inbounds %class.BST, ptr %6, i32 0, i32 0
  %20 = load ptr, ptr %19, align 8, !tbaa !10
  %21 = getelementptr inbounds %class.Node, ptr %20, i32 0, i32 2
  %22 = load ptr, ptr %21, align 8, !tbaa !15
  %23 = load i8, ptr %5, align 1, !tbaa !9
  %24 = call noundef ptr @_ZN3BST6searchEc(ptr noundef nonnull align 8 dereferenceable(8) %22, i8 noundef signext %23)
  store ptr %24, ptr %3, align 8
  br label %44

25:                                               ; preds = %9
  %26 = getelementptr inbounds %class.BST, ptr %6, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !10
  %28 = getelementptr inbounds %class.Node, ptr %27, i32 0, i32 0
  %29 = load i8, ptr %28, align 8, !tbaa !12
  %30 = sext i8 %29 to i32
  %31 = load i8, ptr %5, align 1, !tbaa !9
  %32 = sext i8 %31 to i32
  %33 = icmp slt i32 %30, %32
  br i1 %33, label %34, label %41

34:                                               ; preds = %25
  %35 = getelementptr inbounds %class.BST, ptr %6, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !10
  %37 = getelementptr inbounds %class.Node, ptr %36, i32 0, i32 3
  %38 = load ptr, ptr %37, align 8, !tbaa !16
  %39 = load i8, ptr %5, align 1, !tbaa !9
  %40 = call noundef ptr @_ZN3BST6searchEc(ptr noundef nonnull align 8 dereferenceable(8) %38, i8 noundef signext %39)
  store ptr %40, ptr %3, align 8
  br label %44

41:                                               ; preds = %25
  %42 = getelementptr inbounds %class.BST, ptr %6, i32 0, i32 0
  %43 = load ptr, ptr %42, align 8, !tbaa !10
  store ptr %43, ptr %3, align 8
  br label %44

44:                                               ; preds = %41, %34, %18, %8
  %45 = load ptr, ptr %3, align 8
  ret ptr %45
}

attributes #0 = { mustprogress nounwind 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 #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" }

!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 = !{!7, !7, i64 0}
!10 = !{!11, !6, i64 0}
!11 = !{!"_ZTS3BST", !6, i64 0}
!12 = !{!13, !7, i64 0}
!13 = !{!"_ZTS4Node", !7, i64 0, !14, i64 4, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!14 = !{!"int", !7, i64 0}
!15 = !{!13, !6, i64 8}
!16 = !{!13, !6, i64 16}


; 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 nounwind uwtable
declare dso_local noundef zeroext i1 @_ZN3BST7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_ZN3BST6searchEc(ptr noundef nonnull align 8 dereferenceable(8) %0, i8 noundef signext %1) local_unnamed_addr #0 align 2 {
  %3 = tail call noundef zeroext i1 @_ZN3BST7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(8) %0)
  br i1 %3, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2, %tailrecurse.backedge
  %.tr8 = phi ptr [ %.tr.be, %tailrecurse.backedge ], [ %0, %2 ]
  %4 = load ptr, ptr %.tr8, align 8, !tbaa !5
  %5 = load i8, ptr %4, align 8, !tbaa !10
  %6 = icmp sgt i8 %5, %1
  br i1 %6, label %tailrecurse.backedge, label %9

tailrecurse.backedge:                             ; preds = %.lr.ph, %9
  %.sink = phi i64 [ 16, %9 ], [ 8, %.lr.ph ]
  %7 = getelementptr inbounds i8, ptr %4, i64 %.sink
  %.tr.be = load ptr, ptr %7, align 8, !tbaa !13
  %8 = tail call noundef zeroext i1 @_ZN3BST7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(8) %.tr.be)
  br i1 %8, label %._crit_edge, label %.lr.ph

9:                                                ; preds = %.lr.ph
  %10 = icmp slt i8 %5, %1
  br i1 %10, label %tailrecurse.backedge, label %._crit_edge

._crit_edge:                                      ; preds = %tailrecurse.backedge, %9, %2
  %.0 = phi ptr [ null, %2 ], [ %4, %9 ], [ null, %tailrecurse.backedge ]
  ret ptr %.0
}

attributes #0 = { mustprogress nounwind 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 = !{!"_ZTS3BST", !7, i64 0}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!11, !8, i64 0}
!11 = !{!"_ZTS4Node", !8, i64 0, !12, i64 4, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32}
!12 = !{!"int", !8, i64 0}
!13 = !{!7, !7, i64 0}


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