; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 15. Binary Search Tree/BST_Traversals_MaxMin_Nodes.ll (Z6searchP3BST)

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

@temp = external dso_local global ptr, align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_Z6searchP3BST(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %3 = load ptr, ptr @temp, align 8, !tbaa !5
  %4 = getelementptr inbounds %struct.BST, ptr %3, i32 0, i32 0
  %5 = load i32, ptr %4, align 8, !tbaa !9
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.BST, ptr %6, i32 0, i32 0
  %8 = load i32, ptr %7, align 8, !tbaa !9
  %9 = icmp slt i32 %5, %8
  br i1 %9, label %10, label %22

10:                                               ; preds = %1
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.BST, ptr %11, i32 0, i32 1
  %13 = load ptr, ptr %12, align 8, !tbaa !12
  %14 = icmp eq ptr %13, null
  br i1 %14, label %15, label %22

15:                                               ; preds = %10
  %16 = load ptr, ptr @temp, align 8, !tbaa !5
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.BST, ptr %17, i32 0, i32 1
  store ptr %16, ptr %18, align 8, !tbaa !12
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = load ptr, ptr @temp, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.BST, ptr %20, i32 0, i32 3
  store ptr %19, ptr %21, align 8, !tbaa !13
  br label %79

22:                                               ; preds = %10, %1
  %23 = load ptr, ptr @temp, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.BST, ptr %23, i32 0, i32 0
  %25 = load i32, ptr %24, align 8, !tbaa !9
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.BST, ptr %26, i32 0, i32 0
  %28 = load i32, ptr %27, align 8, !tbaa !9
  %29 = icmp slt i32 %25, %28
  br i1 %29, label %30, label %39

30:                                               ; preds = %22
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.BST, ptr %31, i32 0, i32 1
  %33 = load ptr, ptr %32, align 8, !tbaa !12
  %34 = icmp ne ptr %33, null
  br i1 %34, label %35, label %39

35:                                               ; preds = %30
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.BST, ptr %36, i32 0, i32 1
  %38 = load ptr, ptr %37, align 8, !tbaa !12
  call void @_Z6searchP3BST(ptr noundef %38)
  br label %78

39:                                               ; preds = %30, %22
  %40 = load ptr, ptr @temp, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.BST, ptr %40, i32 0, i32 0
  %42 = load i32, ptr %41, align 8, !tbaa !9
  %43 = load ptr, ptr %2, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.BST, ptr %43, i32 0, i32 0
  %45 = load i32, ptr %44, align 8, !tbaa !9
  %46 = icmp sgt i32 %42, %45
  br i1 %46, label %47, label %59

47:                                               ; preds = %39
  %48 = load ptr, ptr %2, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.BST, ptr %48, i32 0, i32 2
  %50 = load ptr, ptr %49, align 8, !tbaa !14
  %51 = icmp eq ptr %50, null
  br i1 %51, label %52, label %59

52:                                               ; preds = %47
  %53 = load ptr, ptr @temp, align 8, !tbaa !5
  %54 = load ptr, ptr %2, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.BST, ptr %54, i32 0, i32 2
  store ptr %53, ptr %55, align 8, !tbaa !14
  %56 = load ptr, ptr %2, align 8, !tbaa !5
  %57 = load ptr, ptr @temp, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.BST, ptr %57, i32 0, i32 3
  store ptr %56, ptr %58, align 8, !tbaa !13
  br label %77

59:                                               ; preds = %47, %39
  %60 = load ptr, ptr @temp, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.BST, ptr %60, i32 0, i32 0
  %62 = load i32, ptr %61, align 8, !tbaa !9
  %63 = load ptr, ptr %2, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.BST, ptr %63, i32 0, i32 0
  %65 = load i32, ptr %64, align 8, !tbaa !9
  %66 = icmp sgt i32 %62, %65
  br i1 %66, label %67, label %76

67:                                               ; preds = %59
  %68 = load ptr, ptr %2, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.BST, ptr %68, i32 0, i32 2
  %70 = load ptr, ptr %69, align 8, !tbaa !14
  %71 = icmp ne ptr %70, null
  br i1 %71, label %72, label %76

72:                                               ; preds = %67
  %73 = load ptr, ptr %2, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.BST, ptr %73, i32 0, i32 2
  %75 = load ptr, ptr %74, align 8, !tbaa !14
  call void @_Z6searchP3BST(ptr noundef %75)
  br label %76

76:                                               ; preds = %72, %67, %59
  br label %77

77:                                               ; preds = %76, %52
  br label %78

78:                                               ; preds = %77, %35
  br label %79

79:                                               ; preds = %78, %15
  ret void
}

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

@temp = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_Z6searchP3BST(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @temp, align 8, !tbaa !5
  %3 = load i32, ptr %2, align 8, !tbaa !9
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse.backedge, %1
  %.tr = phi ptr [ %0, %1 ], [ %.tr.be, %tailrecurse.backedge ]
  %4 = load i32, ptr %.tr, align 8, !tbaa !9
  %5 = icmp slt i32 %3, %4
  br i1 %5, label %6, label %10

6:                                                ; preds = %tailrecurse
  %7 = getelementptr inbounds i8, ptr %.tr, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !12
  %9 = icmp eq ptr %8, null
  br i1 %9, label %.loopexit.sink.split, label %tailrecurse.backedge

10:                                               ; preds = %tailrecurse
  %11 = icmp sgt i32 %3, %4
  br i1 %11, label %12, label %.loopexit

12:                                               ; preds = %10
  %13 = getelementptr inbounds i8, ptr %.tr, i64 16
  %14 = load ptr, ptr %13, align 8, !tbaa !13
  %15 = icmp eq ptr %14, null
  br i1 %15, label %.loopexit.sink.split, label %tailrecurse.backedge

tailrecurse.backedge:                             ; preds = %12, %6
  %.tr.be = phi ptr [ %8, %6 ], [ %14, %12 ]
  br label %tailrecurse

.loopexit.sink.split:                             ; preds = %12, %6
  %.sink34 = phi i64 [ 8, %6 ], [ 16, %12 ]
  %16 = getelementptr inbounds i8, ptr %.tr, i64 %.sink34
  store ptr %2, ptr %16, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %2, i64 24
  store ptr %.tr, ptr %17, align 8, !tbaa !14
  br label %.loopexit

.loopexit:                                        ; preds = %10, %.loopexit.sink.split
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"_ZTS3BST", !11, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{!10, !6, i64 16}
!14 = !{!10, !6, i64 24}


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