; LLVM IR from priyankchheda/algorithms :: _other_languages/cpp/RedBlackTree/insert.ll (ZN12RedBlackTree6insertEi)

; 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.RedBlackTree = type <{ ptr, i32, [4 x i8] }>
%struct.Node = type { i32, i32, ptr, ptr, ptr }

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN12RedBlackTree6insertEi(ptr noundef nonnull align 8 dereferenceable(12) %0, i32 noundef %1) #0 align 2 personality ptr @__gxx_personality_v0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %10 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  store ptr null, ptr %5, align 8, !tbaa !5
  %11 = getelementptr inbounds %class.RedBlackTree, ptr %10, i32 0, i32 0
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %25

14:                                               ; preds = %2
  %15 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 32) #6
  %16 = load i32, ptr %4, align 4, !tbaa !9
  invoke void @_ZN4NodeC2Eii(ptr noundef nonnull align 8 dereferenceable(32) %15, i32 noundef %16, i32 noundef 0)
          to label %17 unwind label %21

17:                                               ; preds = %14
  %18 = getelementptr inbounds %class.RedBlackTree, ptr %10, i32 0, i32 0
  store ptr %15, ptr %18, align 8, !tbaa !11
  %19 = getelementptr inbounds %class.RedBlackTree, ptr %10, i32 0, i32 0
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  store ptr %20, ptr %5, align 8, !tbaa !5
  br label %101

21:                                               ; preds = %14
  %22 = landingpad { ptr, i32 }
          cleanup
  %23 = extractvalue { ptr, i32 } %22, 0
  store ptr %23, ptr %6, align 8
  %24 = extractvalue { ptr, i32 } %22, 1
  store i32 %24, ptr %7, align 4
  call void @_ZdlPvm(ptr noundef %15, i64 noundef 32) #7
  br label %109

25:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %26 = getelementptr inbounds %class.RedBlackTree, ptr %10, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !11
  store ptr %27, ptr %8, align 8, !tbaa !5
  br label %28

28:                                               ; preds = %92, %25
  br label %29

29:                                               ; preds = %28
  %30 = load i32, ptr %4, align 4, !tbaa !9
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.Node, ptr %31, i32 0, i32 0
  %33 = load i32, ptr %32, align 8, !tbaa !13
  %34 = icmp slt i32 %30, %33
  br i1 %34, label %35, label %58

35:                                               ; preds = %29
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.Node, ptr %36, i32 0, i32 2
  %38 = load ptr, ptr %37, align 8, !tbaa !15
  %39 = icmp eq ptr %38, null
  br i1 %39, label %40, label %53

40:                                               ; preds = %35
  %41 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 32) #6
  %42 = load i32, ptr %4, align 4, !tbaa !9
  invoke void @_ZN4NodeC2Eii(ptr noundef nonnull align 8 dereferenceable(32) %41, i32 noundef %42, i32 noundef 0)
          to label %43 unwind label %49

43:                                               ; preds = %40
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.Node, ptr %44, i32 0, i32 2
  store ptr %41, ptr %45, align 8, !tbaa !15
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.Node, ptr %46, i32 0, i32 2
  %48 = load ptr, ptr %47, align 8, !tbaa !15
  store ptr %48, ptr %5, align 8, !tbaa !5
  br label %93

49:                                               ; preds = %40
  %50 = landingpad { ptr, i32 }
          cleanup
  %51 = extractvalue { ptr, i32 } %50, 0
  store ptr %51, ptr %6, align 8
  %52 = extractvalue { ptr, i32 } %50, 1
  store i32 %52, ptr %7, align 4
  call void @_ZdlPvm(ptr noundef %41, i64 noundef 32) #7
  br label %100

53:                                               ; preds = %35
  %54 = load ptr, ptr %8, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.Node, ptr %54, i32 0, i32 2
  %56 = load ptr, ptr %55, align 8, !tbaa !15
  store ptr %56, ptr %8, align 8, !tbaa !5
  br label %57

57:                                               ; preds = %53
  br label %92

58:                                               ; preds = %29
  %59 = load i32, ptr %4, align 4, !tbaa !9
  %60 = load ptr, ptr %8, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.Node, ptr %60, i32 0, i32 0
  %62 = load i32, ptr %61, align 8, !tbaa !13
  %63 = icmp sgt i32 %59, %62
  br i1 %63, label %64, label %87

64:                                               ; preds = %58
  %65 = load ptr, ptr %8, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.Node, ptr %65, i32 0, i32 3
  %67 = load ptr, ptr %66, align 8, !tbaa !16
  %68 = icmp eq ptr %67, null
  br i1 %68, label %69, label %82

69:                                               ; preds = %64
  %70 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 32) #6
  %71 = load i32, ptr %4, align 4, !tbaa !9
  invoke void @_ZN4NodeC2Eii(ptr noundef nonnull align 8 dereferenceable(32) %70, i32 noundef %71, i32 noundef 0)
          to label %72 unwind label %78

72:                                               ; preds = %69
  %73 = load ptr, ptr %8, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.Node, ptr %73, i32 0, i32 3
  store ptr %70, ptr %74, align 8, !tbaa !16
  %75 = load ptr, ptr %8, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.Node, ptr %75, i32 0, i32 3
  %77 = load ptr, ptr %76, align 8, !tbaa !16
  store ptr %77, ptr %5, align 8, !tbaa !5
  br label %93

78:                                               ; preds = %69
  %79 = landingpad { ptr, i32 }
          cleanup
  %80 = extractvalue { ptr, i32 } %79, 0
  store ptr %80, ptr %6, align 8
  %81 = extractvalue { ptr, i32 } %79, 1
  store i32 %81, ptr %7, align 4
  call void @_ZdlPvm(ptr noundef %70, i64 noundef 32) #7
  br label %100

82:                                               ; preds = %64
  %83 = load ptr, ptr %8, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.Node, ptr %83, i32 0, i32 3
  %85 = load ptr, ptr %84, align 8, !tbaa !16
  store ptr %85, ptr %8, align 8, !tbaa !5
  br label %86

86:                                               ; preds = %82
  br label %91

87:                                               ; preds = %58
  %88 = load i32, ptr %4, align 4, !tbaa !9
  %89 = load ptr, ptr %8, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.Node, ptr %89, i32 0, i32 0
  store i32 %88, ptr %90, align 8, !tbaa !13
  store i32 1, ptr %9, align 4
  br label %97

91:                                               ; preds = %86
  br label %92

92:                                               ; preds = %91, %57
  br label %28, !llvm.loop !17

93:                                               ; preds = %72, %43
  %94 = load ptr, ptr %8, align 8, !tbaa !5
  %95 = load ptr, ptr %5, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.Node, ptr %95, i32 0, i32 4
  store ptr %94, ptr %96, align 8, !tbaa !19
  store i32 0, ptr %9, align 4
  br label %97

97:                                               ; preds = %93, %87
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %98 = load i32, ptr %9, align 4
  switch i32 %98, label %106 [
    i32 0, label %99
  ]

99:                                               ; preds = %97
  br label %101

100:                                              ; preds = %78, %49
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  br label %109

101:                                              ; preds = %99, %17
  %102 = load ptr, ptr %5, align 8, !tbaa !5
  call void @_ZN12RedBlackTree11insertCase1EP4Node(ptr noundef nonnull align 8 dereferenceable(12) %10, ptr noundef %102)
  %103 = getelementptr inbounds %class.RedBlackTree, ptr %10, i32 0, i32 1
  %104 = load i32, ptr %103, align 8, !tbaa !20
  %105 = add nsw i32 %104, 1
  store i32 %105, ptr %103, align 8, !tbaa !20
  store i32 0, ptr %9, align 4
  br label %106

106:                                              ; preds = %101, %97
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  %107 = load i32, ptr %9, align 4
  switch i32 %107, label %115 [
    i32 0, label %108
    i32 1, label %108
  ]

108:                                              ; preds = %106, %106
  ret void

109:                                              ; preds = %100, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  br label %110

110:                                              ; preds = %109
  %111 = load ptr, ptr %6, align 8
  %112 = load i32, ptr %7, align 4
  %113 = insertvalue { ptr, i32 } poison, ptr %111, 0
  %114 = insertvalue { ptr, i32 } %113, i32 %112, 1
  resume { ptr, i32 } %114

115:                                              ; preds = %106
  unreachable
}

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN4NodeC2Eii(ptr noundef nonnull align 8 dereferenceable(32), i32 noundef, i32 noundef) unnamed_addr #3 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) #1

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN12RedBlackTree11insertCase1EP4Node(ptr noundef nonnull align 8 dereferenceable(12), ptr noundef) #0 align 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #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 0}
!12 = !{!"_ZTS12RedBlackTree", !6, i64 0, !10, i64 8}
!13 = !{!14, !10, i64 0}
!14 = !{!"_ZTS4Node", !10, i64 0, !10, i64 4, !6, i64 8, !6, i64 16, !6, i64 24}
!15 = !{!14, !6, i64 8}
!16 = !{!14, !6, i64 16}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!14, !6, i64 24}
!20 = !{!12, !10, i64 8}


; 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 @_ZN12RedBlackTree6insertEi(ptr noundef nonnull align 8 dereferenceable(12) %0, i32 noundef %1) local_unnamed_addr #0 align 2 personality ptr @__gxx_personality_v0 {
  %3 = load ptr, ptr %0, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  br i1 %4, label %5, label %.preheader

5:                                                ; preds = %2
  %6 = tail call noalias noundef nonnull dereferenceable(32) ptr @_Znwm(i64 noundef 32) #3
  tail call void @_ZN4NodeC2Eii(ptr noundef nonnull align 8 dereferenceable(32) %6, i32 noundef %1, i32 noundef 0)
  store ptr %6, ptr %0, align 8, !tbaa !5
  br label %23

.preheader:                                       ; preds = %2, %.preheader.backedge
  %.021 = phi ptr [ %.021.be, %.preheader.backedge ], [ %3, %2 ]
  %7 = load i32, ptr %.021, align 8, !tbaa !11
  %8 = icmp sgt i32 %7, %1
  br i1 %8, label %9, label %13

9:                                                ; preds = %.preheader
  %10 = getelementptr inbounds i8, ptr %.021, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !13
  %12 = icmp eq ptr %11, null
  br i1 %12, label %.thread, label %.preheader.backedge

13:                                               ; preds = %.preheader
  %14 = icmp slt i32 %7, %1
  br i1 %14, label %15, label %22

15:                                               ; preds = %13
  %16 = getelementptr inbounds i8, ptr %.021, i64 16
  %17 = load ptr, ptr %16, align 8, !tbaa !14
  %18 = icmp eq ptr %17, null
  br i1 %18, label %.thread, label %.preheader.backedge

.preheader.backedge:                              ; preds = %15, %9
  %.021.be = phi ptr [ %11, %9 ], [ %17, %15 ]
  br label %.preheader, !llvm.loop !15

.thread:                                          ; preds = %15, %9
  %.sink = phi i64 [ 8, %9 ], [ 16, %15 ]
  %19 = getelementptr inbounds i8, ptr %.021, i64 %.sink
  %20 = tail call noalias noundef nonnull dereferenceable(32) ptr @_Znwm(i64 noundef 32) #3
  tail call void @_ZN4NodeC2Eii(ptr noundef nonnull align 8 dereferenceable(32) %20, i32 noundef %1, i32 noundef 0)
  store ptr %20, ptr %19, align 8, !tbaa !17
  %21 = getelementptr inbounds i8, ptr %20, i64 24
  store ptr %.021, ptr %21, align 8, !tbaa !18
  br label %23

22:                                               ; preds = %13
  store i32 %1, ptr %.021, align 8, !tbaa !11
  br label %27

23:                                               ; preds = %.thread, %5
  %.2 = phi ptr [ %6, %5 ], [ %20, %.thread ]
  tail call void @_ZN12RedBlackTree11insertCase1EP4Node(ptr noundef nonnull align 8 dereferenceable(12) %0, ptr noundef nonnull %.2)
  %24 = getelementptr inbounds i8, ptr %0, i64 8
  %25 = load i32, ptr %24, align 8, !tbaa !19
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr %24, align 8, !tbaa !19
  br label %27

27:                                               ; preds = %22, %23
  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 @_ZN4NodeC2Eii(ptr noundef nonnull align 8 dereferenceable(32), i32 noundef, i32 noundef) unnamed_addr #2 align 2

declare i32 @__gxx_personality_v0(...)

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN12RedBlackTree11insertCase1EP4Node(ptr noundef nonnull align 8 dereferenceable(12), ptr noundef) local_unnamed_addr #0 align 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 = { 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, !7, i64 0}
!6 = !{!"_ZTS12RedBlackTree", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTS4Node", !10, i64 0, !10, i64 4, !7, i64 8, !7, i64 16, !7, i64 24}
!13 = !{!12, !7, i64 8}
!14 = !{!12, !7, i64 16}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!7, !7, i64 0}
!18 = !{!12, !7, i64 24}
!19 = !{!6, !10, i64 8}


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