; LLVM IR from priyankchheda/algorithms :: _other_languages/cpp/BinarySearchTree/inorderSuccessor.ll (Z16inorderSuccessorR3BSTi)

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

; 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 ptr @_Z16inorderSuccessorR3BSTi(ptr noundef nonnull align 8 dereferenceable(8) %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = call noundef ptr @_ZNK3BST7getRootEv(ptr noundef nonnull align 8 dereferenceable(8) %10)
  store ptr %11, ptr %6, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %44, %2
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %15, label %21

15:                                               ; preds = %12
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.Node, ptr %16, i32 0, i32 0
  %18 = load i32, ptr %17, align 8, !tbaa !11
  %19 = load i32, ptr %5, align 4, !tbaa !9
  %20 = icmp ne i32 %18, %19
  br label %21

21:                                               ; preds = %15, %12
  %22 = phi i1 [ false, %12 ], [ %20, %15 ]
  br i1 %22, label %23, label %45

23:                                               ; preds = %21
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.Node, ptr %24, i32 0, i32 0
  %26 = load i32, ptr %25, align 8, !tbaa !11
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = icmp sgt i32 %26, %27
  br i1 %28, label %29, label %33

29:                                               ; preds = %23
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.Node, ptr %30, i32 0, i32 1
  %32 = load ptr, ptr %31, align 8, !tbaa !13
  store ptr %32, ptr %6, align 8, !tbaa !5
  br label %44

33:                                               ; preds = %23
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.Node, ptr %34, i32 0, i32 0
  %36 = load i32, ptr %35, align 8, !tbaa !11
  %37 = load i32, ptr %5, align 4, !tbaa !9
  %38 = icmp slt i32 %36, %37
  br i1 %38, label %39, label %43

39:                                               ; preds = %33
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.Node, ptr %40, i32 0, i32 2
  %42 = load ptr, ptr %41, align 8, !tbaa !14
  store ptr %42, ptr %6, align 8, !tbaa !5
  br label %43

43:                                               ; preds = %39, %33
  br label %44

44:                                               ; preds = %43, %29
  br label %12, !llvm.loop !15

45:                                               ; preds = %21
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = icmp eq ptr %46, null
  br i1 %47, label %48, label %49

48:                                               ; preds = %45
  store ptr null, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %86

49:                                               ; preds = %45
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.Node, ptr %50, i32 0, i32 2
  %52 = load ptr, ptr %51, align 8, !tbaa !14
  %53 = icmp ne ptr %52, null
  br i1 %53, label %54, label %59

54:                                               ; preds = %49
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.Node, ptr %55, i32 0, i32 2
  %57 = load ptr, ptr %56, align 8, !tbaa !14
  %58 = call noundef ptr @_Z11findMinNodeP4Node(ptr noundef %57)
  store ptr %58, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %86

59:                                               ; preds = %49
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  store ptr null, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = call noundef ptr @_ZNK3BST7getRootEv(ptr noundef nonnull align 8 dereferenceable(8) %60)
  store ptr %61, ptr %9, align 8, !tbaa !5
  br label %62

62:                                               ; preds = %83, %59
  %63 = load ptr, ptr %9, align 8, !tbaa !5
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = icmp ne ptr %63, %64
  br i1 %65, label %66, label %84

66:                                               ; preds = %62
  %67 = load ptr, ptr %6, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.Node, ptr %67, i32 0, i32 0
  %69 = load i32, ptr %68, align 8, !tbaa !11
  %70 = load ptr, ptr %9, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.Node, ptr %70, i32 0, i32 0
  %72 = load i32, ptr %71, align 8, !tbaa !11
  %73 = icmp slt i32 %69, %72
  br i1 %73, label %74, label %79

74:                                               ; preds = %66
  %75 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %75, ptr %8, align 8, !tbaa !5
  %76 = load ptr, ptr %9, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.Node, ptr %76, i32 0, i32 1
  %78 = load ptr, ptr %77, align 8, !tbaa !13
  store ptr %78, ptr %9, align 8, !tbaa !5
  br label %83

79:                                               ; preds = %66
  %80 = load ptr, ptr %9, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.Node, ptr %80, i32 0, i32 2
  %82 = load ptr, ptr %81, align 8, !tbaa !14
  store ptr %82, ptr %9, align 8, !tbaa !5
  br label %83

83:                                               ; preds = %79, %74
  br label %62, !llvm.loop !17

84:                                               ; preds = %62
  %85 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %85, ptr %3, align 8
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %86

86:                                               ; preds = %84, %54, %48
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %87 = load ptr, ptr %3, align 8
  ret ptr %87
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_Z11findMinNodeP4Node(ptr noundef) #2

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

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 = { 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 #3 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTS4Node", !10, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!12, !6, i64 8}
!14 = !{!12, !6, i64 16}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !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
define dso_local noundef ptr @_Z16inorderSuccessorR3BSTi(ptr noundef nonnull align 8 dereferenceable(8) %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call noundef ptr @_ZNK3BST7getRootEv(ptr noundef nonnull align 8 dereferenceable(8) %0)
  %cond35 = icmp eq ptr %3, null
  br i1 %cond35, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %2, %11
  %.02436 = phi ptr [ %.125, %11 ], [ %3, %2 ]
  %4 = load i32, ptr %.02436, align 8, !tbaa !5
  %.not30 = icmp eq i32 %4, %1
  br i1 %.not30, label %.critedge, label %5

5:                                                ; preds = %.lr.ph
  %6 = icmp sgt i32 %4, %1
  br i1 %6, label %.sink.split, label %7

7:                                                ; preds = %5
  %8 = icmp slt i32 %4, %1
  br i1 %8, label %.sink.split, label %11

.sink.split:                                      ; preds = %7, %5
  %.sink45 = phi i64 [ 8, %5 ], [ 16, %7 ]
  %9 = getelementptr inbounds i8, ptr %.02436, i64 %.sink45
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  br label %11

11:                                               ; preds = %.sink.split, %7
  %.125 = phi ptr [ %.02436, %7 ], [ %10, %.sink.split ]
  %cond = icmp eq ptr %.125, null
  br i1 %cond, label %.loopexit, label %.lr.ph, !llvm.loop !12

.critedge:                                        ; preds = %.lr.ph
  %12 = getelementptr inbounds i8, ptr %.02436, i64 16
  %13 = load ptr, ptr %12, align 8, !tbaa !14
  %.not31 = icmp eq ptr %13, null
  br i1 %.not31, label %16, label %14

14:                                               ; preds = %.critedge
  %15 = tail call noundef ptr @_Z11findMinNodeP4Node(ptr noundef nonnull %13)
  br label %.loopexit

16:                                               ; preds = %.critedge
  %17 = tail call noundef ptr @_ZNK3BST7getRootEv(ptr noundef nonnull align 8 dereferenceable(8) %0)
  %.not3237 = icmp eq ptr %17, %.02436
  br i1 %.not3237, label %.loopexit, label %.lr.ph40

.lr.ph40:                                         ; preds = %16
  %18 = load i32, ptr %.02436, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %.lr.ph40, %19
  %.039 = phi ptr [ %17, %.lr.ph40 ], [ %.1, %19 ]
  %.02238 = phi ptr [ null, %.lr.ph40 ], [ %.123, %19 ]
  %20 = load i32, ptr %.039, align 8, !tbaa !5
  %21 = icmp slt i32 %18, %20
  %.123 = select i1 %21, ptr %.039, ptr %.02238
  %.1.in.v = select i1 %21, i64 8, i64 16
  %.1.in = getelementptr inbounds i8, ptr %.039, i64 %.1.in.v
  %.1 = load ptr, ptr %.1.in, align 8, !tbaa !11
  %.not32 = icmp eq ptr %.1, %.02436
  br i1 %.not32, label %.loopexit, label %19, !llvm.loop !15

.loopexit:                                        ; preds = %11, %19, %2, %16, %14
  %.026 = phi ptr [ %15, %14 ], [ null, %16 ], [ null, %2 ], [ %.123, %19 ], [ null, %11 ]
  ret ptr %.026
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_Z11findMinNodeP4Node(ptr noundef) local_unnamed_addr #0

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

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 = !{!"_ZTS4Node", !7, i64 0, !10, i64 8, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!10, !10, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!6, !10, i64 16}
!15 = distinct !{!15, !13}


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