; LLVM IR from Lakhankumawat/LearnCPP :: T-Tree/B-BinaryTree/LargestBSTinBT.ll (Z13constructTreeRSt6vectorIiSaIiEE)

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

@idx = external dso_local global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z13constructTreeRSt6vectorIiSaIiEE(ptr noundef nonnull align 8 dereferenceable(24) %0) #0 personality ptr @__gxx_personality_v0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %7 = load i32, ptr @idx, align 4, !tbaa !9
  %8 = sext i32 %7 to i64
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) %9) #5
  %11 = icmp eq i64 %8, %10
  br i1 %11, label %19, label %12

12:                                               ; preds = %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = load i32, ptr @idx, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %13, i64 noundef %15) #5
  %17 = load i32, ptr %16, align 4, !tbaa !9
  %18 = icmp eq i32 %17, -1
  br i1 %18, label %19, label %22

19:                                               ; preds = %12, %1
  %20 = load i32, ptr @idx, align 4, !tbaa !9
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr @idx, align 4, !tbaa !9
  store ptr null, ptr %2, align 8
  br label %44

22:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  %23 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 24) #6
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = load i32, ptr @idx, align 4, !tbaa !9
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr @idx, align 4, !tbaa !9
  %27 = sext i32 %25 to i64
  %28 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %24, i64 noundef %27) #5
  %29 = load i32, ptr %28, align 4, !tbaa !9
  invoke void @_ZN4NodeC2Ei(ptr noundef nonnull align 8 dereferenceable(24) %23, i32 noundef %29)
          to label %30 unwind label %40

30:                                               ; preds = %22
  store ptr %23, ptr %4, align 8, !tbaa !5
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = call noundef ptr @_Z13constructTreeRSt6vectorIiSaIiEE(ptr noundef nonnull align 8 dereferenceable(24) %31)
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %class.Node, ptr %33, i32 0, i32 1
  store ptr %32, ptr %34, align 8, !tbaa !11
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = call noundef ptr @_Z13constructTreeRSt6vectorIiSaIiEE(ptr noundef nonnull align 8 dereferenceable(24) %35)
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %class.Node, ptr %37, i32 0, i32 2
  store ptr %36, ptr %38, align 8, !tbaa !13
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %39, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  br label %44

40:                                               ; preds = %22
  %41 = landingpad { ptr, i32 }
          cleanup
  %42 = extractvalue { ptr, i32 } %41, 0
  store ptr %42, ptr %5, align 8
  %43 = extractvalue { ptr, i32 } %41, 1
  store i32 %43, ptr %6, align 4
  call void @_ZdlPvm(ptr noundef %23, i64 noundef 24) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  br label %46

44:                                               ; preds = %30, %19
  %45 = load ptr, ptr %2, align 8
  ret ptr %45

46:                                               ; preds = %40
  %47 = load ptr, ptr %5, align 8
  %48 = load i32, ptr %6, align 4
  %49 = insertvalue { ptr, i32 } poison, ptr %47, 0
  %50 = insertvalue { ptr, i32 } %49, i32 %48, 1
  resume { ptr, i32 } %50
}

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) #1 align 2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znwm(i64 noundef) #3

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN4NodeC2Ei(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef) unnamed_addr #1 align 2

declare i32 @__gxx_personality_v0(...)

; Function Attrs: nobuiltin nounwind
declare void @_ZdlPvm(ptr noundef, i64 noundef) #4

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

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" }
attributes #1 = { 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nobuiltin allocsize(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 #4 = { nobuiltin nounwind "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 #5 = { nounwind }
attributes #6 = { builtin allocsize(0) }
attributes #7 = { builtin 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, !6, i64 8}
!12 = !{!"_ZTS4Node", !10, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!12, !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"

@idx = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z13constructTreeRSt6vectorIiSaIiEE(ptr noundef nonnull align 8 dereferenceable(24) %0) local_unnamed_addr #0 personality ptr @__gxx_personality_v0 {
  %2 = load i32, ptr @idx, align 4, !tbaa !5
  %3 = sext i32 %2 to i64
  %4 = tail call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) %0) #3
  %5 = icmp eq i64 %4, %3
  br i1 %5, label %common.ret, label %6

6:                                                ; preds = %1
  %7 = load i32, ptr @idx, align 4, !tbaa !5
  %8 = sext i32 %7 to i64
  %9 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %8) #3
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = icmp eq i32 %10, -1
  br i1 %11, label %common.ret, label %14

common.ret:                                       ; preds = %6, %1
  %12 = load i32, ptr @idx, align 4, !tbaa !5
  %13 = add nsw i32 %12, 1
  store i32 %13, ptr @idx, align 4, !tbaa !5
  br label %common.ret9

common.ret9:                                      ; preds = %14, %common.ret
  %common.ret9.op = phi ptr [ null, %common.ret ], [ %15, %14 ]
  ret ptr %common.ret9.op

14:                                               ; preds = %6
  %15 = tail call noalias noundef nonnull dereferenceable(24) ptr @_Znwm(i64 noundef 24) #4
  %16 = load i32, ptr @idx, align 4, !tbaa !5
  %17 = add nsw i32 %16, 1
  store i32 %17, ptr @idx, align 4, !tbaa !5
  %18 = sext i32 %16 to i64
  %19 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %18) #3
  %20 = load i32, ptr %19, align 4, !tbaa !5
  tail call void @_ZN4NodeC2Ei(ptr noundef nonnull align 8 dereferenceable(24) %15, i32 noundef %20)
  %21 = tail call noundef ptr @_Z13constructTreeRSt6vectorIiSaIiEE(ptr noundef nonnull align 8 dereferenceable(24) %0)
  %22 = getelementptr inbounds i8, ptr %15, i64 8
  store ptr %21, ptr %22, align 8, !tbaa !9
  %23 = tail call noundef ptr @_Z13constructTreeRSt6vectorIiSaIiEE(ptr noundef nonnull align 8 dereferenceable(24) %0)
  %24 = getelementptr inbounds i8, ptr %15, i64 16
  store ptr %23, ptr %24, align 8, !tbaa !12
  br label %common.ret9
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) local_unnamed_addr #1 align 2

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znwm(i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN4NodeC2Ei(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef) unnamed_addr #1 align 2

declare i32 @__gxx_personality_v0(...)

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" }
attributes #1 = { 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 #2 = { nobuiltin allocsize(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 }
attributes #4 = { builtin allocsize(0) }

!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, !11, i64 8}
!10 = !{!"_ZTS4Node", !6, i64 0, !11, i64 8, !11, i64 16}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !11, i64 16}


; Effective O3 passes: GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass