; LLVM IR from TheAlgorithms/C-Plus-Plus :: data_structures/binary_search_tree.ll (Z6RemoveP4nodeS0_i)

; 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 nounwind uwtable
declare dso_local noundef i32 @_Z15findMaxInLeftSTP4node(ptr noundef) #1

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

13:                                               ; preds = %3
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.node, ptr %14, i32 0, i32 2
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %36

18:                                               ; preds = %13
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.node, ptr %19, i32 0, i32 1
  %21 = load ptr, ptr %20, align 8, !tbaa !14
  %22 = icmp eq ptr %21, null
  br i1 %22, label %23, label %36

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

29:                                               ; preds = %23
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.node, ptr %30, i32 0, i32 2
  store ptr null, ptr %31, align 8, !tbaa !13
  br label %35

32:                                               ; preds = %23
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.node, ptr %33, i32 0, i32 1
  store ptr null, ptr %34, align 8, !tbaa !14
  br label %35

35:                                               ; preds = %32, %29
  br label %99

36:                                               ; preds = %18, %13
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.node, ptr %37, i32 0, i32 2
  %39 = load ptr, ptr %38, align 8, !tbaa !13
  %40 = icmp eq ptr %39, null
  br i1 %40, label %41, label %60

41:                                               ; preds = %36
  %42 = load i32, ptr %6, align 4, !tbaa !9
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.node, ptr %43, i32 0, i32 0
  %45 = load i32, ptr %44, align 8, !tbaa !11
  %46 = icmp slt i32 %42, %45
  br i1 %46, label %47, label %53

47:                                               ; preds = %41
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.node, ptr %48, i32 0, i32 1
  %50 = load ptr, ptr %49, align 8, !tbaa !14
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.node, ptr %51, i32 0, i32 2
  store ptr %50, ptr %52, align 8, !tbaa !13
  br label %59

53:                                               ; preds = %41
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.node, ptr %54, i32 0, i32 1
  %56 = load ptr, ptr %55, align 8, !tbaa !14
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.node, ptr %57, i32 0, i32 1
  store ptr %56, ptr %58, align 8, !tbaa !14
  br label %59

59:                                               ; preds = %53, %47
  br label %98

60:                                               ; preds = %36
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.node, ptr %61, i32 0, i32 1
  %63 = load ptr, ptr %62, align 8, !tbaa !14
  %64 = icmp eq ptr %63, null
  br i1 %64, label %65, label %84

65:                                               ; preds = %60
  %66 = load i32, ptr %6, align 4, !tbaa !9
  %67 = load ptr, ptr %4, 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 = icmp slt i32 %66, %69
  br i1 %70, label %71, label %77

71:                                               ; preds = %65
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.node, ptr %72, i32 0, i32 2
  %74 = load ptr, ptr %73, align 8, !tbaa !13
  %75 = load ptr, ptr %4, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.node, ptr %75, i32 0, i32 2
  store ptr %74, ptr %76, align 8, !tbaa !13
  br label %83

77:                                               ; preds = %65
  %78 = load ptr, ptr %5, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.node, ptr %78, i32 0, i32 2
  %80 = load ptr, ptr %79, align 8, !tbaa !13
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.node, ptr %81, i32 0, i32 1
  store ptr %80, ptr %82, align 8, !tbaa !14
  br label %83

83:                                               ; preds = %77, %71
  br label %97

84:                                               ; preds = %60
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %85 = load ptr, ptr %5, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.node, ptr %85, i32 0, i32 1
  %87 = load ptr, ptr %86, align 8, !tbaa !14
  %88 = call noundef i32 @_Z15findMaxInLeftSTP4node(ptr noundef %87)
  store i32 %88, ptr %7, align 4, !tbaa !9
  %89 = load i32, ptr %7, align 4, !tbaa !9
  %90 = load ptr, ptr %5, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.node, ptr %90, i32 0, i32 0
  store i32 %89, ptr %91, align 8, !tbaa !11
  %92 = load ptr, ptr %5, align 8, !tbaa !5
  %93 = load ptr, ptr %5, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.node, ptr %93, i32 0, i32 2
  %95 = load ptr, ptr %94, align 8, !tbaa !13
  %96 = load i32, ptr %7, align 4, !tbaa !9
  call void @_Z6RemoveP4nodeS0_i(ptr noundef %92, ptr noundef %95, i32 noundef %96)
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %97

97:                                               ; preds = %84, %83
  br label %98

98:                                               ; preds = %97, %59
  br label %99

99:                                               ; preds = %98, %35
  br label %119

100:                                              ; preds = %3
  %101 = load i32, ptr %6, align 4, !tbaa !9
  %102 = load ptr, ptr %5, align 8, !tbaa !5
  %103 = getelementptr inbounds %struct.node, ptr %102, i32 0, i32 0
  %104 = load i32, ptr %103, align 8, !tbaa !11
  %105 = icmp slt i32 %101, %104
  br i1 %105, label %106, label %112

106:                                              ; preds = %100
  %107 = load ptr, ptr %5, align 8, !tbaa !5
  %108 = load ptr, ptr %5, align 8, !tbaa !5
  %109 = getelementptr inbounds %struct.node, ptr %108, i32 0, i32 1
  %110 = load ptr, ptr %109, align 8, !tbaa !14
  %111 = load i32, ptr %6, align 4, !tbaa !9
  call void @_Z6RemoveP4nodeS0_i(ptr noundef %107, ptr noundef %110, i32 noundef %111)
  br label %118

112:                                              ; preds = %100
  %113 = load ptr, ptr %5, align 8, !tbaa !5
  %114 = load ptr, ptr %5, align 8, !tbaa !5
  %115 = getelementptr inbounds %struct.node, ptr %114, i32 0, i32 2
  %116 = load ptr, ptr %115, align 8, !tbaa !13
  %117 = load i32, ptr %6, align 4, !tbaa !9
  call void @_Z6RemoveP4nodeS0_i(ptr noundef %113, ptr noundef %116, i32 noundef %117)
  br label %118

118:                                              ; preds = %112, %106
  br label %119

119:                                              ; preds = %118, %99
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #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 16}
!14 = !{!12, !6, 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 nounwind uwtable
declare dso_local noundef i32 @_Z15findMaxInLeftSTP4node(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z6RemoveP4nodeS0_i(ptr nocapture noundef %0, ptr nocapture noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  br label %tailrecurse.outer

tailrecurse.outer:                                ; preds = %13, %3
  %.tr.ph = phi ptr [ %.tr38.lcssa, %13 ], [ %0, %3 ]
  %.tr38.ph = phi ptr [ %15, %13 ], [ %1, %3 ]
  %.tr39.ph = phi i32 [ %14, %13 ], [ %2, %3 ]
  %4 = load i32, ptr %.tr38.ph, align 8, !tbaa !5
  %5 = icmp eq i32 %4, %.tr39.ph
  br i1 %5, label %tailrecurse._crit_edge, label %.lr.ph

tailrecurse._crit_edge:                           ; preds = %.lr.ph, %tailrecurse.outer
  %.tr.lcssa = phi ptr [ %.tr.ph, %tailrecurse.outer ], [ %.tr3844, %.lr.ph ]
  %.tr38.lcssa = phi ptr [ %.tr38.ph, %tailrecurse.outer ], [ %.tr38.be, %.lr.ph ]
  %6 = getelementptr inbounds i8, ptr %.tr38.lcssa, i64 16
  %7 = load ptr, ptr %6, align 8, !tbaa !11
  %8 = icmp eq ptr %7, null
  %9 = getelementptr inbounds i8, ptr %.tr38.lcssa, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  br i1 %8, label %21, label %11

11:                                               ; preds = %tailrecurse._crit_edge
  %12 = icmp eq ptr %10, null
  br i1 %12, label %21, label %13

13:                                               ; preds = %11
  %14 = tail call noundef i32 @_Z15findMaxInLeftSTP4node(ptr noundef nonnull %10)
  store i32 %14, ptr %.tr38.lcssa, align 8, !tbaa !5
  %15 = load ptr, ptr %6, align 8, !tbaa !11
  br label %tailrecurse.outer

.lr.ph:                                           ; preds = %tailrecurse.outer, %.lr.ph
  %16 = phi i32 [ %19, %.lr.ph ], [ %4, %tailrecurse.outer ]
  %.tr3844 = phi ptr [ %.tr38.be, %.lr.ph ], [ %.tr38.ph, %tailrecurse.outer ]
  %17 = icmp sgt i32 %16, %.tr39.ph
  %spec.select = select i1 %17, i64 8, i64 16
  %18 = getelementptr inbounds i8, ptr %.tr3844, i64 %spec.select
  %.tr38.be = load ptr, ptr %18, align 8, !tbaa !13
  %19 = load i32, ptr %.tr38.be, align 8, !tbaa !5
  %20 = icmp eq i32 %19, %.tr39.ph
  br i1 %20, label %tailrecurse._crit_edge, label %.lr.ph

21:                                               ; preds = %11, %tailrecurse._crit_edge
  %.sink = phi ptr [ %10, %tailrecurse._crit_edge ], [ %7, %11 ]
  %.pn = load i32, ptr %.tr.lcssa, align 8, !tbaa !5
  %.sink62 = icmp sgt i32 %.pn, %.tr39.ph
  %.61 = select i1 %.sink62, i64 16, i64 8
  %22 = getelementptr inbounds i8, ptr %.tr.lcssa, i64 %.61
  store ptr %.sink, ptr %22, align 8, !tbaa !13
  ret void
}

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 = !{!6, !10, i64 16}
!12 = !{!6, !10, i64 8}
!13 = !{!10, !10, i64 0}


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