; LLVM IR from gargarchit/Data_Structure_n_Algorithms :: Binary_Tree/implementation.ll (Z6insertRP5Treesi)

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z6insertRP5Treesi(ptr noundef nonnull align 8 dereferenceable(8) %0, i32 noundef %1) #0 personality ptr @__gxx_personality_v0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = load ptr, ptr %7, align 8, !tbaa !5
  %9 = icmp eq ptr %8, null
  br i1 %9, label %10, label %19

10:                                               ; preds = %2
  %11 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 24) #4
  %12 = load i32, ptr %4, align 4, !tbaa !9
  invoke void @_ZN5TreesC2Ei(ptr noundef nonnull align 8 dereferenceable(24) %11, i32 noundef %12)
          to label %13 unwind label %15

13:                                               ; preds = %10
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %11, ptr %14, align 8, !tbaa !5
  br label %81

15:                                               ; preds = %10
  %16 = landingpad { ptr, i32 }
          cleanup
  %17 = extractvalue { ptr, i32 } %16, 0
  store ptr %17, ptr %5, align 8
  %18 = extractvalue { ptr, i32 } %16, 1
  store i32 %18, ptr %6, align 4
  call void @_ZdlPvm(ptr noundef %11, i64 noundef 24) #5
  br label %82

19:                                               ; preds = %2
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = load ptr, ptr %20, align 8, !tbaa !5
  %22 = getelementptr inbounds %class.Trees, ptr %21, i32 0, i32 0
  %23 = load i32, ptr %22, align 8, !tbaa !11
  %24 = load i32, ptr %4, align 4, !tbaa !9
  %25 = icmp sgt i32 %23, %24
  br i1 %25, label %26, label %49

26:                                               ; preds = %19
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = load ptr, ptr %27, align 8, !tbaa !5
  %29 = getelementptr inbounds %class.Trees, ptr %28, i32 0, i32 1
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = icmp ne ptr %30, null
  br i1 %31, label %32, label %37

32:                                               ; preds = %26
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = load ptr, ptr %33, align 8, !tbaa !5
  %35 = getelementptr inbounds %class.Trees, ptr %34, i32 0, i32 1
  %36 = load i32, ptr %4, align 4, !tbaa !9
  call void @_Z6insertRP5Treesi(ptr noundef nonnull align 8 dereferenceable(8) %35, i32 noundef %36)
  br label %48

37:                                               ; preds = %26
  %38 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 24) #4
  %39 = load i32, ptr %4, align 4, !tbaa !9
  invoke void @_ZN5TreesC2Ei(ptr noundef nonnull align 8 dereferenceable(24) %38, i32 noundef %39)
          to label %40 unwind label %44

40:                                               ; preds = %37
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = load ptr, ptr %41, align 8, !tbaa !5
  %43 = getelementptr inbounds %class.Trees, ptr %42, i32 0, i32 1
  store ptr %38, ptr %43, align 8, !tbaa !13
  br label %48

44:                                               ; preds = %37
  %45 = landingpad { ptr, i32 }
          cleanup
  %46 = extractvalue { ptr, i32 } %45, 0
  store ptr %46, ptr %5, align 8
  %47 = extractvalue { ptr, i32 } %45, 1
  store i32 %47, ptr %6, align 4
  call void @_ZdlPvm(ptr noundef %38, i64 noundef 24) #5
  br label %82

48:                                               ; preds = %40, %32
  br label %80

49:                                               ; preds = %19
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = load ptr, ptr %50, align 8, !tbaa !5
  %52 = getelementptr inbounds %class.Trees, ptr %51, i32 0, i32 0
  %53 = load i32, ptr %52, align 8, !tbaa !11
  %54 = load i32, ptr %4, align 4, !tbaa !9
  %55 = icmp slt i32 %53, %54
  br i1 %55, label %56, label %79

56:                                               ; preds = %49
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = load ptr, ptr %57, align 8, !tbaa !5
  %59 = getelementptr inbounds %class.Trees, ptr %58, i32 0, i32 2
  %60 = load ptr, ptr %59, align 8, !tbaa !14
  %61 = icmp ne ptr %60, null
  br i1 %61, label %62, label %67

62:                                               ; preds = %56
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = load ptr, ptr %63, align 8, !tbaa !5
  %65 = getelementptr inbounds %class.Trees, ptr %64, i32 0, i32 2
  %66 = load i32, ptr %4, align 4, !tbaa !9
  call void @_Z6insertRP5Treesi(ptr noundef nonnull align 8 dereferenceable(8) %65, i32 noundef %66)
  br label %78

67:                                               ; preds = %56
  %68 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 24) #4
  %69 = load i32, ptr %4, align 4, !tbaa !9
  invoke void @_ZN5TreesC2Ei(ptr noundef nonnull align 8 dereferenceable(24) %68, i32 noundef %69)
          to label %70 unwind label %74

70:                                               ; preds = %67
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = load ptr, ptr %71, align 8, !tbaa !5
  %73 = getelementptr inbounds %class.Trees, ptr %72, i32 0, i32 2
  store ptr %68, ptr %73, align 8, !tbaa !14
  br label %78

74:                                               ; preds = %67
  %75 = landingpad { ptr, i32 }
          cleanup
  %76 = extractvalue { ptr, i32 } %75, 0
  store ptr %76, ptr %5, align 8
  %77 = extractvalue { ptr, i32 } %75, 1
  store i32 %77, ptr %6, align 4
  call void @_ZdlPvm(ptr noundef %68, i64 noundef 24) #5
  br label %82

78:                                               ; preds = %70, %62
  br label %79

79:                                               ; preds = %78, %49
  br label %80

80:                                               ; preds = %79, %48
  br label %81

81:                                               ; preds = %80, %13
  ret void

82:                                               ; preds = %74, %44, %15
  %83 = load ptr, ptr %5, align 8
  %84 = load i32, ptr %6, align 4
  %85 = insertvalue { ptr, i32 } poison, ptr %83, 0
  %86 = insertvalue { ptr, i32 } %85, i32 %84, 1
  resume { ptr, i32 } %86
}

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

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

declare i32 @__gxx_personality_v0(...)

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

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 = { 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 #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 = { 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 #4 = { builtin allocsize(0) }
attributes #5 = { 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, !10, i64 0}
!12 = !{!"_ZTS5Trees", !10, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!12, !6, i64 8}
!14 = !{!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"

; Function Attrs: mustprogress uwtable
define dso_local void @_Z6insertRP5Treesi(ptr nocapture noundef nonnull align 8 dereferenceable(8) %0, i32 noundef %1) local_unnamed_addr #0 personality ptr @__gxx_personality_v0 {
  %3 = load ptr, ptr %0, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  br i1 %4, label %tailrecurse._crit_edge, label %.lr.ph

tailrecurse._crit_edge:                           ; preds = %2
  %5 = tail call noalias noundef nonnull dereferenceable(24) ptr @_Znwm(i64 noundef 24) #3
  tail call void @_ZN5TreesC2Ei(ptr noundef nonnull align 8 dereferenceable(24) %5, i32 noundef %1)
  store ptr %5, ptr %0, align 8, !tbaa !5
  br label %.loopexit

.lr.ph:                                           ; preds = %2, %.lr.ph.backedge
  %6 = phi ptr [ %.be, %.lr.ph.backedge ], [ %3, %2 ]
  %.tr23 = phi ptr [ %.tr23.be, %.lr.ph.backedge ], [ %0, %2 ]
  %7 = load i32, ptr %6, align 8, !tbaa !9
  %8 = icmp sgt i32 %7, %1
  br i1 %8, label %9, label %16

9:                                                ; preds = %.lr.ph
  %10 = getelementptr inbounds i8, ptr %6, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !5
  %.not18 = icmp eq ptr %11, null
  br i1 %.not18, label %12, label %.lr.ph.backedge

.lr.ph.backedge:                                  ; preds = %9, %18
  %.be = phi ptr [ %11, %9 ], [ %20, %18 ]
  %.tr23.be = phi ptr [ %10, %9 ], [ %19, %18 ]
  br label %.lr.ph

12:                                               ; preds = %9
  %13 = tail call noalias noundef nonnull dereferenceable(24) ptr @_Znwm(i64 noundef 24) #3
  tail call void @_ZN5TreesC2Ei(ptr noundef nonnull align 8 dereferenceable(24) %13, i32 noundef %1)
  %14 = load ptr, ptr %.tr23, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %14, i64 8
  store ptr %13, ptr %15, align 8, !tbaa !12
  br label %.loopexit

16:                                               ; preds = %.lr.ph
  %17 = icmp slt i32 %7, %1
  br i1 %17, label %18, label %.loopexit

18:                                               ; preds = %16
  %19 = getelementptr inbounds i8, ptr %6, i64 16
  %20 = load ptr, ptr %19, align 8, !tbaa !5
  %.not = icmp eq ptr %20, null
  br i1 %.not, label %21, label %.lr.ph.backedge

21:                                               ; preds = %18
  %22 = tail call noalias noundef nonnull dereferenceable(24) ptr @_Znwm(i64 noundef 24) #3
  tail call void @_ZN5TreesC2Ei(ptr noundef nonnull align 8 dereferenceable(24) %22, i32 noundef %1)
  %23 = load ptr, ptr %.tr23, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %23, i64 16
  store ptr %22, ptr %24, align 8, !tbaa !13
  br label %.loopexit

.loopexit:                                        ; preds = %16, %12, %21, %tailrecurse._crit_edge
  ret void
}

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN5TreesC2Ei(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef) unnamed_addr #2 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 = { 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 #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 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"_ZTS5Trees", !11, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{!10, !6, i64 16}


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