; LLVM IR from Leet-Us-Code/DSA-A-Z-Practice :: Sid's Levels/Level - 1/BinarySearchTree/deleteANodeInBST.ll (Z10deleteNodeP4Nodei)

; 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: nobuiltin nounwind
declare void @_ZdlPvm(ptr noundef, i64 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z10deleteNodeP4Nodei(ptr noundef %0, i32 noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %12

11:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  br label %93

12:                                               ; preds = %2
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.Node, 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 %26

18:                                               ; preds = %12
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.Node, ptr %19, i32 0, i32 2
  %21 = load ptr, ptr %20, align 8, !tbaa !13
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = call noundef ptr @_Z10deleteNodeP4Nodei(ptr noundef %21, i32 noundef %22)
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.Node, ptr %24, i32 0, i32 2
  store ptr %23, ptr %25, align 8, !tbaa !13
  br label %91

26:                                               ; preds = %12
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.Node, ptr %28, i32 0, i32 0
  %30 = load i32, ptr %29, align 8, !tbaa !11
  %31 = icmp slt i32 %27, %30
  br i1 %31, label %32, label %40

32:                                               ; preds = %26
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.Node, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !14
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = call noundef ptr @_Z10deleteNodeP4Nodei(ptr noundef %35, i32 noundef %36)
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.Node, ptr %38, i32 0, i32 1
  store ptr %37, ptr %39, align 8, !tbaa !14
  br label %90

40:                                               ; preds = %26
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.Node, ptr %41, i32 0, i32 2
  %43 = load ptr, ptr %42, align 8, !tbaa !13
  %44 = icmp eq ptr %43, null
  br i1 %44, label %45, label %54

45:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.Node, ptr %46, i32 0, i32 1
  %48 = load ptr, ptr %47, align 8, !tbaa !14
  store ptr %48, ptr %6, align 8, !tbaa !5
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = icmp eq ptr %49, null
  br i1 %50, label %52, label %51

51:                                               ; preds = %45
  call void @_ZdlPvm(ptr noundef %49, i64 noundef 24) #5
  br label %52

52:                                               ; preds = %51, %45
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %53, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %93

54:                                               ; preds = %40
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.Node, ptr %55, i32 0, i32 1
  %57 = load ptr, ptr %56, align 8, !tbaa !14
  %58 = icmp eq ptr %57, null
  br i1 %58, label %59, label %68

59:                                               ; preds = %54
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.Node, ptr %60, i32 0, i32 2
  %62 = load ptr, ptr %61, align 8, !tbaa !13
  store ptr %62, ptr %7, align 8, !tbaa !5
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = icmp eq ptr %63, null
  br i1 %64, label %66, label %65

65:                                               ; preds = %59
  call void @_ZdlPvm(ptr noundef %63, i64 noundef 24) #5
  br label %66

66:                                               ; preds = %65, %59
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %67, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %93

68:                                               ; preds = %54
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.Node, ptr %69, i32 0, i32 2
  %71 = load ptr, ptr %70, align 8, !tbaa !13
  %72 = call noundef ptr @_Z7minNodeP4Node(ptr noundef %71)
  store ptr %72, ptr %8, align 8, !tbaa !5
  %73 = load ptr, ptr %8, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.Node, ptr %73, i32 0, i32 0
  %75 = load i32, ptr %74, align 8, !tbaa !11
  %76 = load ptr, ptr %4, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.Node, ptr %76, i32 0, i32 0
  store i32 %75, ptr %77, align 8, !tbaa !11
  %78 = load ptr, ptr %4, 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 %8, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.Node, ptr %81, i32 0, i32 0
  %83 = load i32, ptr %82, align 8, !tbaa !11
  %84 = call noundef ptr @_Z10deleteNodeP4Nodei(ptr noundef %80, i32 noundef %83)
  %85 = load ptr, ptr %4, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.Node, ptr %85, i32 0, i32 2
  store ptr %84, ptr %86, align 8, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %87

87:                                               ; preds = %68
  br label %88

88:                                               ; preds = %87
  %89 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %89, ptr %3, align 8
  br label %93

90:                                               ; preds = %32
  br label %91

91:                                               ; preds = %90, %18
  br label %92

92:                                               ; preds = %91
  unreachable

93:                                               ; preds = %88, %66, %52, %11
  %94 = load ptr, ptr %3, align 8
  ret ptr %94
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_Z7minNodeP4Node(ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { 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 = { nounwind }
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 = !{!"_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: nobuiltin nounwind
declare void @_ZdlPvm(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_Z10deleteNodeP4Nodei(ptr noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %common.ret28, label %4

4:                                                ; preds = %2
  %5 = load i32, ptr %0, align 8, !tbaa !5
  %6 = icmp slt i32 %5, %1
  br i1 %6, label %7, label %11

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = tail call noundef ptr @_Z10deleteNodeP4Nodei(ptr noundef %9, i32 noundef %1)
  unreachable

11:                                               ; preds = %4
  %12 = icmp sgt i32 %5, %1
  br i1 %12, label %13, label %17

13:                                               ; preds = %11
  %14 = getelementptr inbounds i8, ptr %0, i64 8
  %15 = load ptr, ptr %14, align 8, !tbaa !12
  %16 = tail call noundef ptr @_Z10deleteNodeP4Nodei(ptr noundef %15, i32 noundef %1)
  unreachable

17:                                               ; preds = %11
  %18 = getelementptr inbounds i8, ptr %0, i64 16
  %19 = load ptr, ptr %18, align 8, !tbaa !11
  %20 = icmp eq ptr %19, null
  %21 = getelementptr inbounds i8, ptr %0, i64 8
  %22 = load ptr, ptr %21, align 8, !tbaa !12
  br i1 %20, label %23, label %24

23:                                               ; preds = %17
  tail call void @_ZdlPvm(ptr noundef nonnull %0, i64 noundef 24) #2
  br label %common.ret28

24:                                               ; preds = %17
  %25 = icmp eq ptr %22, null
  br i1 %25, label %26, label %27

26:                                               ; preds = %24
  tail call void @_ZdlPvm(ptr noundef nonnull %0, i64 noundef 24) #2
  br label %common.ret28

common.ret28:                                     ; preds = %2, %26, %23, %27
  %common.ret28.op = phi ptr [ %0, %27 ], [ %22, %23 ], [ %19, %26 ], [ null, %2 ]
  ret ptr %common.ret28.op

27:                                               ; preds = %24
  %28 = tail call noundef ptr @_Z7minNodeP4Node(ptr noundef nonnull %19)
  %29 = load i32, ptr %28, align 8, !tbaa !5
  store i32 %29, ptr %0, align 8, !tbaa !5
  %30 = load ptr, ptr %18, align 8, !tbaa !11
  %31 = tail call noundef ptr @_Z10deleteNodeP4Nodei(ptr noundef %30, i32 noundef %29)
  store ptr %31, ptr %18, align 8, !tbaa !11
  br label %common.ret28
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_Z7minNodeP4Node(ptr noundef) local_unnamed_addr #1

attributes #0 = { 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 #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 = { 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, !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}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass