; LLVM IR from sbshah97/DS-Algo-Handbook :: Data-Structures/Trees/Binary_Search_Trees (BinarSearchTree)

; 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 }

@flag = external dso_local global i32, align 4
@root = external dso_local global ptr, align 8
@t2 = external dso_local global ptr, align 8
@t1 = external dso_local global ptr, align 8

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @largst(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @searchl(ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @deletel(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.node, ptr %4, i32 0, i32 1
  %6 = load ptr, ptr %5, align 8, !tbaa !9
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %27

8:                                                ; preds = %1
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.node, ptr %9, i32 0, i32 2
  %11 = load ptr, ptr %10, align 8, !tbaa !12
  %12 = icmp eq ptr %11, null
  br i1 %12, label %13, label %27

13:                                               ; preds = %8
  %14 = load ptr, ptr @t1, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.node, ptr %14, i32 0, i32 1
  %16 = load ptr, ptr %15, align 8, !tbaa !9
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = icmp eq ptr %16, %17
  br i1 %18, label %19, label %22

19:                                               ; preds = %13
  %20 = load ptr, ptr @t1, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.node, ptr %20, i32 0, i32 1
  store ptr null, ptr %21, align 8, !tbaa !9
  br label %25

22:                                               ; preds = %13
  %23 = load ptr, ptr @t1, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.node, ptr %23, i32 0, i32 2
  store ptr null, ptr %24, align 8, !tbaa !12
  br label %25

25:                                               ; preds = %22, %19
  store ptr null, ptr %2, align 8, !tbaa !5
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  call void @free(ptr noundef %26) #3
  br label %132

27:                                               ; preds = %8, %1
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.node, ptr %28, i32 0, i32 2
  %30 = load ptr, ptr %29, align 8, !tbaa !12
  %31 = icmp eq ptr %30, null
  br i1 %31, label %32, label %62

32:                                               ; preds = %27
  %33 = load ptr, ptr @t1, align 8, !tbaa !5
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = icmp eq ptr %33, %34
  br i1 %35, label %36, label %41

36:                                               ; preds = %32
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.node, ptr %37, i32 0, i32 1
  %39 = load ptr, ptr %38, align 8, !tbaa !9
  store ptr %39, ptr @root, align 8, !tbaa !5
  %40 = load ptr, ptr @root, align 8, !tbaa !5
  store ptr %40, ptr @t1, align 8, !tbaa !5
  br label %60

41:                                               ; preds = %32
  %42 = load ptr, ptr @t1, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.node, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !9
  %45 = load ptr, ptr %2, align 8, !tbaa !5
  %46 = icmp eq ptr %44, %45
  br i1 %46, label %47, label %53

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

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

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

60:                                               ; preds = %59, %36
  store ptr null, ptr %2, align 8, !tbaa !5
  %61 = load ptr, ptr %2, align 8, !tbaa !5
  call void @free(ptr noundef %61) #3
  br label %131

62:                                               ; preds = %27
  %63 = load ptr, ptr %2, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.node, ptr %63, i32 0, i32 1
  %65 = load ptr, ptr %64, align 8, !tbaa !9
  %66 = icmp eq ptr %65, null
  br i1 %66, label %67, label %97

67:                                               ; preds = %62
  %68 = load ptr, ptr @t1, align 8, !tbaa !5
  %69 = load ptr, ptr %2, align 8, !tbaa !5
  %70 = icmp eq ptr %68, %69
  br i1 %70, label %71, label %76

71:                                               ; preds = %67
  %72 = load ptr, ptr %2, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.node, ptr %72, i32 0, i32 2
  %74 = load ptr, ptr %73, align 8, !tbaa !12
  store ptr %74, ptr @root, align 8, !tbaa !5
  %75 = load ptr, ptr @root, align 8, !tbaa !5
  store ptr %75, ptr @t1, align 8, !tbaa !5
  br label %95

76:                                               ; preds = %67
  %77 = load ptr, ptr @t1, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.node, ptr %77, i32 0, i32 2
  %79 = load ptr, ptr %78, align 8, !tbaa !12
  %80 = load ptr, ptr %2, align 8, !tbaa !5
  %81 = icmp eq ptr %79, %80
  br i1 %81, label %82, label %88

82:                                               ; preds = %76
  %83 = load ptr, ptr %2, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.node, ptr %83, i32 0, i32 2
  %85 = load ptr, ptr %84, align 8, !tbaa !12
  %86 = load ptr, ptr @t1, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.node, ptr %86, i32 0, i32 2
  store ptr %85, ptr %87, align 8, !tbaa !12
  br label %94

88:                                               ; preds = %76
  %89 = load ptr, ptr %2, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.node, ptr %89, i32 0, i32 2
  %91 = load ptr, ptr %90, align 8, !tbaa !12
  %92 = load ptr, ptr @t1, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.node, ptr %92, i32 0, i32 1
  store ptr %91, ptr %93, align 8, !tbaa !9
  br label %94

94:                                               ; preds = %88, %82
  br label %95

95:                                               ; preds = %94, %71
  store ptr null, ptr %2, align 8, !tbaa !5
  %96 = load ptr, ptr %2, align 8, !tbaa !5
  call void @free(ptr noundef %96) #3
  br label %130

97:                                               ; preds = %62
  %98 = load ptr, ptr %2, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.node, ptr %98, i32 0, i32 1
  %100 = load ptr, ptr %99, align 8, !tbaa !9
  %101 = icmp ne ptr %100, null
  br i1 %101, label %102, label %129

102:                                              ; preds = %97
  %103 = load ptr, ptr %2, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.node, ptr %103, i32 0, i32 2
  %105 = load ptr, ptr %104, align 8, !tbaa !12
  %106 = icmp ne ptr %105, null
  br i1 %106, label %107, label %129

107:                                              ; preds = %102
  %108 = load ptr, ptr @root, align 8, !tbaa !5
  store ptr %108, ptr @t2, align 8, !tbaa !5
  %109 = load ptr, ptr %2, align 8, !tbaa !5
  %110 = getelementptr inbounds %struct.node, ptr %109, i32 0, i32 2
  %111 = load ptr, ptr %110, align 8, !tbaa !12
  %112 = icmp ne ptr %111, null
  br i1 %112, label %113, label %118

113:                                              ; preds = %107
  %114 = load ptr, ptr %2, align 8, !tbaa !5
  %115 = getelementptr inbounds %struct.node, ptr %114, i32 0, i32 2
  %116 = load ptr, ptr %115, align 8, !tbaa !12
  %117 = call i32 @smallst(ptr noundef %116)
  store i32 %117, ptr %3, align 4, !tbaa !13
  store i32 1, ptr @flag, align 4, !tbaa !13
  br label %123

118:                                              ; preds = %107
  %119 = load ptr, ptr %2, align 8, !tbaa !5
  %120 = getelementptr inbounds %struct.node, ptr %119, i32 0, i32 1
  %121 = load ptr, ptr %120, align 8, !tbaa !9
  %122 = call i32 @largst(ptr noundef %121)
  store i32 %122, ptr %3, align 4, !tbaa !13
  store i32 2, ptr @flag, align 4, !tbaa !13
  br label %123

123:                                              ; preds = %118, %113
  %124 = load ptr, ptr @root, align 8, !tbaa !5
  %125 = load i32, ptr %3, align 4, !tbaa !13
  call void @searchl(ptr noundef %124, i32 noundef %125)
  %126 = load i32, ptr %3, align 4, !tbaa !13
  %127 = load ptr, ptr %2, align 8, !tbaa !5
  %128 = getelementptr inbounds %struct.node, ptr %127, i32 0, i32 0
  store i32 %126, ptr %128, align 8, !tbaa !14
  br label %129

129:                                              ; preds = %123, %102, %97
  br label %130

130:                                              ; preds = %129, %95
  br label %131

131:                                              ; preds = %130, %60
  br label %132

132:                                              ; preds = %131, %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret void
}

; Function Attrs: nounwind
declare void @free(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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 #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/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"node", !11, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 16}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !11, i64 0}


; 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"

@flag = external dso_local local_unnamed_addr global i32, align 4
@root = external dso_local local_unnamed_addr global ptr, align 8
@t2 = external dso_local local_unnamed_addr global ptr, align 8
@t1 = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local i32 @smallst(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @searchl(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @deletel(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = load ptr, ptr %5, align 8, !tbaa !11
  %7 = icmp eq ptr %6, null
  br i1 %4, label %8, label %.thread

8:                                                ; preds = %1
  %9 = load ptr, ptr @t1, align 8, !tbaa !12
  br i1 %7, label %10, label %28

10:                                               ; preds = %8
  %11 = getelementptr inbounds i8, ptr %9, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %13 = icmp eq ptr %12, %0
  br i1 %13, label %14, label %15

14:                                               ; preds = %10
  store ptr null, ptr %11, align 8, !tbaa !5
  br label %42

15:                                               ; preds = %10
  %16 = getelementptr inbounds i8, ptr %9, i64 16
  store ptr null, ptr %16, align 8, !tbaa !11
  br label %42

.thread:                                          ; preds = %1
  br i1 %7, label %17, label %38

17:                                               ; preds = %.thread
  %18 = load ptr, ptr @t1, align 8, !tbaa !12
  %19 = icmp eq ptr %18, %0
  br i1 %19, label %20, label %21

20:                                               ; preds = %17
  store ptr %3, ptr @root, align 8, !tbaa !12
  store ptr %3, ptr @t1, align 8, !tbaa !12
  br label %42

21:                                               ; preds = %17
  %22 = getelementptr inbounds i8, ptr %18, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !5
  %24 = icmp eq ptr %23, %0
  br i1 %24, label %25, label %26

25:                                               ; preds = %21
  store ptr %3, ptr %22, align 8, !tbaa !5
  br label %42

26:                                               ; preds = %21
  %27 = getelementptr inbounds i8, ptr %18, i64 16
  store ptr %3, ptr %27, align 8, !tbaa !11
  br label %42

28:                                               ; preds = %8
  %29 = icmp eq ptr %9, %0
  br i1 %29, label %30, label %31

30:                                               ; preds = %28
  store ptr %6, ptr @root, align 8, !tbaa !12
  store ptr %6, ptr @t1, align 8, !tbaa !12
  br label %42

31:                                               ; preds = %28
  %32 = getelementptr inbounds i8, ptr %9, i64 16
  %33 = load ptr, ptr %32, align 8, !tbaa !11
  %34 = icmp eq ptr %33, %0
  br i1 %34, label %35, label %36

35:                                               ; preds = %31
  store ptr %6, ptr %32, align 8, !tbaa !11
  br label %42

36:                                               ; preds = %31
  %37 = getelementptr inbounds i8, ptr %9, i64 8
  store ptr %6, ptr %37, align 8, !tbaa !5
  br label %42

38:                                               ; preds = %.thread
  %39 = load ptr, ptr @root, align 8, !tbaa !12
  store ptr %39, ptr @t2, align 8, !tbaa !12
  %40 = tail call i32 @smallst(ptr noundef nonnull %6)
  store i32 1, ptr @flag, align 4, !tbaa !13
  %41 = load ptr, ptr @root, align 8, !tbaa !12
  tail call void @searchl(ptr noundef %41, i32 noundef %40)
  store i32 %40, ptr %0, align 8, !tbaa !14
  br label %42

42:                                               ; preds = %30, %36, %35, %20, %26, %25, %14, %15, %38
  ret void
}

attributes #0 = { 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, !10, i64 8}
!6 = !{!"node", !7, i64 0, !10, i64 8, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 16}
!12 = !{!10, !10, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!6, !7, i64 0}


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