; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/ds/ancestors_of_given_number_BST.ll (Z7DisplayP3BSTii)

; 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.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }
%struct.BST = type { i32, i32, i32, ptr, ptr }

@l1 = external dso_local global i32, align 4
@l2 = external dso_local global i32, align 4
@_ZSt4cout = external global %"class.std::basic_ostream", align 8
@.str.3 = external hidden unnamed_addr constant [11 x i8], align 1

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) #0

declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8), i32 noundef) #1

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

12:                                               ; preds = %3
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.BST, ptr %14, i32 0, i32 0
  %16 = load i32, ptr %15, align 8, !tbaa !11
  %17 = icmp sgt i32 %13, %16
  br i1 %17, label %18, label %24

18:                                               ; preds = %12
  %19 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, ptr noundef @.str.3)
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.BST, ptr %20, i32 0, i32 0
  %22 = load i32, ptr %21, align 8, !tbaa !11
  %23 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8) %19, i32 noundef %22)
  br label %92

24:                                               ; preds = %12, %3
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.BST, ptr %25, i32 0, i32 2
  %27 = load i32, ptr %26, align 8, !tbaa !13
  %28 = load i32, ptr @l1, align 4, !tbaa !9
  %29 = sub nsw i32 %28, 1
  %30 = icmp eq i32 %27, %29
  br i1 %30, label %38, label %31

31:                                               ; preds = %24
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.BST, ptr %32, i32 0, i32 2
  %34 = load i32, ptr %33, align 8, !tbaa !13
  %35 = load i32, ptr @l2, align 4, !tbaa !9
  %36 = sub nsw i32 %35, 1
  %37 = icmp eq i32 %34, %36
  br i1 %37, label %38, label %44

38:                                               ; preds = %31, %24
  %39 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, ptr noundef @.str.3)
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.BST, ptr %40, i32 0, i32 0
  %42 = load i32, ptr %41, align 8, !tbaa !11
  %43 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8) %39, i32 noundef %42)
  br label %92

44:                                               ; preds = %31
  br label %45

45:                                               ; preds = %44
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.BST, ptr %46, i32 0, i32 3
  %48 = load ptr, ptr %47, align 8, !tbaa !14
  %49 = icmp ne ptr %48, null
  br i1 %49, label %50, label %68

50:                                               ; preds = %45
  %51 = load i32, ptr %5, align 4, !tbaa !9
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.BST, ptr %52, i32 0, i32 0
  %54 = load i32, ptr %53, align 8, !tbaa !11
  %55 = icmp slt i32 %51, %54
  br i1 %55, label %62, label %56

56:                                               ; preds = %50
  %57 = load i32, ptr %6, align 4, !tbaa !9
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.BST, ptr %58, i32 0, i32 0
  %60 = load i32, ptr %59, align 8, !tbaa !11
  %61 = icmp slt i32 %57, %60
  br i1 %61, label %62, label %68

62:                                               ; preds = %56, %50
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.BST, ptr %63, i32 0, i32 3
  %65 = load ptr, ptr %64, align 8, !tbaa !14
  %66 = load i32, ptr %5, align 4, !tbaa !9
  %67 = load i32, ptr %6, align 4, !tbaa !9
  call void @_Z7DisplayP3BSTii(ptr noundef %65, i32 noundef %66, i32 noundef %67)
  br label %92

68:                                               ; preds = %56, %45
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.BST, ptr %69, i32 0, i32 4
  %71 = load ptr, ptr %70, align 8, !tbaa !15
  %72 = icmp ne ptr %71, null
  br i1 %72, label %73, label %91

73:                                               ; preds = %68
  %74 = load i32, ptr %5, align 4, !tbaa !9
  %75 = load ptr, ptr %4, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.BST, ptr %75, i32 0, i32 0
  %77 = load i32, ptr %76, align 8, !tbaa !11
  %78 = icmp sgt i32 %74, %77
  br i1 %78, label %85, label %79

79:                                               ; preds = %73
  %80 = load i32, ptr %6, align 4, !tbaa !9
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.BST, ptr %81, i32 0, i32 0
  %83 = load i32, ptr %82, align 8, !tbaa !11
  %84 = icmp sgt i32 %80, %83
  br i1 %84, label %85, label %91

85:                                               ; preds = %79, %73
  %86 = load ptr, ptr %4, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.BST, ptr %86, i32 0, i32 4
  %88 = load ptr, ptr %87, align 8, !tbaa !15
  %89 = load i32, ptr %5, align 4, !tbaa !9
  %90 = load i32, ptr %6, align 4, !tbaa !9
  call void @_Z7DisplayP3BSTii(ptr noundef %88, i32 noundef %89, i32 noundef %90)
  br label %91

91:                                               ; preds = %85, %79, %68
  br label %92

92:                                               ; preds = %91, %62, %38, %18
  ret void
}

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


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

%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@l1 = external dso_local local_unnamed_addr global i32, align 4
@l2 = external dso_local local_unnamed_addr global i32, align 4
@_ZSt4cout = external global %"class.std::basic_ostream", align 8
@.str.3 = external hidden unnamed_addr constant [11 x i8], align 1

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) local_unnamed_addr #0

declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8), i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z7DisplayP3BSTii(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #2 {
  %4 = load i32, ptr %0, align 8, !tbaa !5
  %5 = icmp sgt i32 %4, %1
  %6 = icmp slt i32 %4, %2
  %or.cond35 = and i1 %5, %6
  br i1 %or.cond35, label %.loopexit.sink.split, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %7 = load i32, ptr @l1, align 4, !tbaa !11
  %8 = add nsw i32 %7, -1
  %9 = load i32, ptr @l2, align 4
  %10 = add nsw i32 %9, -1
  br label %11

11:                                               ; preds = %.lr.ph, %tailrecurse.backedge
  %12 = phi i1 [ %6, %.lr.ph ], [ %25, %tailrecurse.backedge ]
  %13 = phi i1 [ %5, %.lr.ph ], [ %24, %tailrecurse.backedge ]
  %14 = phi i32 [ %4, %.lr.ph ], [ %23, %tailrecurse.backedge ]
  %.tr36 = phi ptr [ %0, %.lr.ph ], [ %.tr.be, %tailrecurse.backedge ]
  %15 = getelementptr inbounds i8, ptr %.tr36, i64 8
  %16 = load i32, ptr %15, align 8, !tbaa !12
  %17 = icmp eq i32 %16, %8
  %18 = icmp eq i32 %16, %10
  %or.cond37 = select i1 %17, i1 true, i1 %18
  br i1 %or.cond37, label %.loopexit.sink.split, label %19

19:                                               ; preds = %11
  %20 = getelementptr inbounds i8, ptr %.tr36, i64 16
  %21 = load ptr, ptr %20, align 8, !tbaa !13
  %.not = icmp ne ptr %21, null
  %22 = icmp sgt i32 %14, %2
  %or.cond27 = or i1 %13, %22
  %or.cond29 = and i1 %or.cond27, %.not
  br i1 %or.cond29, label %tailrecurse.backedge, label %26

tailrecurse.backedge:                             ; preds = %19, %26
  %.tr.be = phi ptr [ %21, %19 ], [ %28, %26 ]
  %23 = load i32, ptr %.tr.be, align 8, !tbaa !5
  %24 = icmp sgt i32 %23, %1
  %25 = icmp slt i32 %23, %2
  %or.cond = and i1 %24, %25
  br i1 %or.cond, label %.loopexit.sink.split, label %11

26:                                               ; preds = %19
  %27 = getelementptr inbounds i8, ptr %.tr36, i64 24
  %28 = load ptr, ptr %27, align 8, !tbaa !14
  %.not26 = icmp ne ptr %28, null
  %29 = icmp slt i32 %14, %1
  %or.cond28 = or i1 %29, %12
  %or.cond30 = and i1 %or.cond28, %.not26
  br i1 %or.cond30, label %tailrecurse.backedge, label %.loopexit

.loopexit.sink.split:                             ; preds = %11, %tailrecurse.backedge, %3
  %.tr36.lcssa.sink = phi ptr [ %0, %3 ], [ %.tr.be, %tailrecurse.backedge ], [ %.tr36, %11 ]
  %30 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, ptr noundef nonnull @.str.3)
  %31 = load i32, ptr %.tr36.lcssa.sink, align 8, !tbaa !5
  %32 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8) %30, i32 noundef %31)
  br label %.loopexit

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

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


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