; LLVM IR from nonstriater/Learn-Algorithms :: 4 Tree/2-二叉查找树/BiSearchTree (bisearchtree)

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

@.str = external hidden unnamed_addr constant [16 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [23 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [55 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [30 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @bisearch_tree_insert(ptr noundef %0, i32 noundef %1) #0 {
  %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
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %10, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store ptr null, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %11 = call noalias ptr @malloc(i64 noundef 24) #5
  store ptr %11, ptr %8, align 8, !tbaa !5
  %12 = load ptr, ptr %8, align 8, !tbaa !5
  %13 = icmp eq ptr null, %12
  br i1 %13, label %14, label %17

14:                                               ; preds = %2
  %15 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %16, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %93

17:                                               ; preds = %2
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.BiSearchTree, ptr %19, i32 0, i32 0
  store i32 %18, ptr %20, align 8, !tbaa !11
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.BiSearchTree, ptr %21, i32 0, i32 1
  store ptr null, ptr %22, align 8, !tbaa !13
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.BiSearchTree, ptr %23, i32 0, i32 2
  store ptr null, ptr %24, align 8, !tbaa !14
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = icmp eq ptr null, %25
  br i1 %26, label %27, label %32

27:                                               ; preds = %17
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %28, ptr %4, align 8, !tbaa !5
  %29 = load i32, ptr %5, align 4, !tbaa !9
  %30 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %29)
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %31, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %93

32:                                               ; preds = %17
  br label %33

33:                                               ; preds = %62, %32
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = icmp ne ptr %34, null
  br i1 %35, label %36, label %63

36:                                               ; preds = %33
  %37 = load i32, ptr %5, align 4, !tbaa !9
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.BiSearchTree, ptr %38, i32 0, i32 0
  %40 = load i32, ptr %39, align 8, !tbaa !11
  %41 = icmp slt i32 %37, %40
  br i1 %41, label %42, label %47

42:                                               ; preds = %36
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %43, ptr %7, align 8, !tbaa !5
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.BiSearchTree, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !13
  store ptr %46, ptr %6, align 8, !tbaa !5
  br label %62

47:                                               ; preds = %36
  %48 = load i32, ptr %5, align 4, !tbaa !9
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.BiSearchTree, ptr %49, i32 0, i32 0
  %51 = load i32, ptr %50, align 8, !tbaa !11
  %52 = icmp eq i32 %48, %51
  br i1 %52, label %53, label %56

53:                                               ; preds = %47
  %54 = load i32, ptr %5, align 4, !tbaa !9
  %55 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %54)
  br label %63

56:                                               ; preds = %47
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %57, ptr %7, align 8, !tbaa !5
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.BiSearchTree, ptr %58, i32 0, i32 2
  %60 = load ptr, ptr %59, align 8, !tbaa !14
  store ptr %60, ptr %6, align 8, !tbaa !5
  br label %61

61:                                               ; preds = %56
  br label %62

62:                                               ; preds = %61, %42
  br label %33, !llvm.loop !15

63:                                               ; preds = %53, %33
  %64 = load ptr, ptr %7, align 8, !tbaa !5
  %65 = icmp ne ptr %64, null
  br i1 %65, label %66, label %91

66:                                               ; preds = %63
  %67 = load i32, ptr %5, align 4, !tbaa !9
  %68 = load ptr, ptr %7, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.BiSearchTree, ptr %68, i32 0, i32 0
  %70 = load i32, ptr %69, align 8, !tbaa !11
  %71 = icmp sgt i32 %67, %70
  br i1 %71, label %72, label %81

72:                                               ; preds = %66
  %73 = load i32, ptr %5, align 4, !tbaa !9
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.BiSearchTree, ptr %74, i32 0, i32 0
  %76 = load i32, ptr %75, align 8, !tbaa !11
  %77 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i32 noundef %73, i32 noundef %76)
  %78 = load ptr, ptr %8, align 8, !tbaa !5
  %79 = load ptr, ptr %7, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.BiSearchTree, ptr %79, i32 0, i32 2
  store ptr %78, ptr %80, align 8, !tbaa !14
  br label %90

81:                                               ; preds = %66
  %82 = load i32, ptr %5, align 4, !tbaa !9
  %83 = load ptr, ptr %7, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.BiSearchTree, ptr %83, i32 0, i32 0
  %85 = load i32, ptr %84, align 8, !tbaa !11
  %86 = call i32 (ptr, ...) @printf(ptr noundef @.str.4, i32 noundef %82, i32 noundef %85)
  %87 = load ptr, ptr %8, align 8, !tbaa !5
  %88 = load ptr, ptr %7, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.BiSearchTree, ptr %88, i32 0, i32 1
  store ptr %87, ptr %89, align 8, !tbaa !13
  br label %90

90:                                               ; preds = %81, %72
  br label %91

91:                                               ; preds = %90, %63
  %92 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %92, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %93

93:                                               ; preds = %91, %27, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %94 = load ptr, ptr %3, align 8
  ret ptr %94
}

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

declare i32 @printf(ptr noundef, ...) #3

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

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


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

@.str = external hidden unnamed_addr constant [16 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [23 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [55 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [30 x i8], align 1

; Function Attrs: nofree nounwind uwtable
define dso_local noundef ptr @bisearch_tree_insert(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call noalias dereferenceable_or_null(24) ptr @malloc(i64 noundef 24) #4
  %4 = icmp eq ptr %3, null
  br i1 %4, label %5, label %7

5:                                                ; preds = %2
  %6 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  br label %27

7:                                                ; preds = %2
  store i32 %1, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %3, i64 8
  %9 = icmp eq ptr %0, null
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %8, i8 0, i64 16, i1 false)
  br i1 %9, label %10, label %.preheader

10:                                               ; preds = %7
  %11 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %1)
  br label %27

.preheader:                                       ; preds = %7, %16
  %.03345 = phi ptr [ %.03444, %16 ], [ null, %7 ]
  %.03444 = phi ptr [ %.135, %16 ], [ %0, %7 ]
  %12 = load i32, ptr %.03444, align 8, !tbaa !5
  %13 = icmp sgt i32 %12, %1
  br i1 %13, label %16, label %14

14:                                               ; preds = %.preheader
  %15 = icmp eq i32 %12, %1
  br i1 %15, label %.loopexit, label %16

16:                                               ; preds = %14, %.preheader
  %.sink = phi i64 [ 8, %.preheader ], [ 16, %14 ]
  %17 = getelementptr inbounds i8, ptr %.03444, i64 %.sink
  %.135 = load ptr, ptr %17, align 8, !tbaa !11
  %.not = icmp eq ptr %.135, null
  br i1 %.not, label %.loopexit.thread, label %.preheader, !llvm.loop !12

.loopexit:                                        ; preds = %14
  %18 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %1)
  %.not41 = icmp eq ptr %.03345, null
  br i1 %.not41, label %27, label %.loopexit.thread

.loopexit.thread:                                 ; preds = %16, %.loopexit
  %.0334350 = phi ptr [ %.03345, %.loopexit ], [ %.03444, %16 ]
  %19 = load i32, ptr %.0334350, align 8, !tbaa !5
  %20 = icmp slt i32 %19, %1
  br i1 %20, label %21, label %24

21:                                               ; preds = %.loopexit.thread
  %22 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef %1, i32 noundef %19)
  %23 = getelementptr inbounds i8, ptr %.0334350, i64 16
  store ptr %3, ptr %23, align 8, !tbaa !14
  br label %27

24:                                               ; preds = %.loopexit.thread
  %25 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, i32 noundef %1, i32 noundef %19)
  %26 = getelementptr inbounds i8, ptr %.0334350, i64 8
  store ptr %3, ptr %26, align 8, !tbaa !15
  br label %27

27:                                               ; preds = %.loopexit, %24, %21, %10, %5
  %.0 = phi ptr [ %0, %5 ], [ %3, %10 ], [ %0, %21 ], [ %0, %24 ], [ %0, %.loopexit ]
  ret ptr %.0
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

attributes #0 = { nofree 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 #1 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { nofree 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { nounwind 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 = !{!"BiSearchTree", !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 = !{!10, !10, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!6, !10, i64 16}
!15 = !{!6, !10, i64 8}


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