; LLVM IR from li-xilin/axe :: src/core (splay)

; 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.ax_splay_node_st = type { ptr, ptr, ptr }
%struct.ax_splay = type { ptr, ptr, i32 }

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

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @ax_splay_insert(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store ptr null, ptr %6, align 8, !tbaa !5
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.ax_splay_node_st, ptr %12, i32 0, i32 1
  store ptr null, ptr %13, align 8, !tbaa !9
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.ax_splay_node_st, ptr %14, i32 0, i32 2
  store ptr null, ptr %15, align 8, !tbaa !11
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.ax_splay, ptr %16, i32 0, i32 0
  %18 = load ptr, ptr %17, align 8, !tbaa !12
  %19 = icmp ne ptr %18, null
  br i1 %19, label %26, label %20

20:                                               ; preds = %2
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.ax_splay, ptr %22, i32 0, i32 0
  store ptr %21, ptr %23, align 8, !tbaa !12
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.ax_splay_node_st, ptr %24, i32 0, i32 0
  store ptr null, ptr %25, align 8, !tbaa !15
  br label %76

26:                                               ; preds = %2
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.ax_splay, ptr %27, i32 0, i32 0
  %29 = load ptr, ptr %28, align 8, !tbaa !12
  store ptr %29, ptr %7, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %60, %26
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = icmp ne ptr %31, null
  br i1 %32, label %33, label %61

33:                                               ; preds = %30
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %34, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.ax_splay, ptr %35, i32 0, i32 1
  %37 = load ptr, ptr %36, align 8, !tbaa !16
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = call i32 %37(ptr noundef %38, ptr noundef %39)
  store i32 %40, ptr %10, align 4, !tbaa !17
  %41 = load i32, ptr %10, align 4, !tbaa !17
  %42 = icmp slt i32 %41, 0
  br i1 %42, label %43, label %47

43:                                               ; preds = %33
  store i32 1, ptr %9, align 4, !tbaa !17
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.ax_splay_node_st, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !9
  store ptr %46, ptr %7, align 8, !tbaa !5
  br label %57

47:                                               ; preds = %33
  %48 = load i32, ptr %10, align 4, !tbaa !17
  %49 = icmp sgt i32 %48, 0
  br i1 %49, label %50, label %54

50:                                               ; preds = %47
  store i32 0, ptr %9, align 4, !tbaa !17
  %51 = load ptr, ptr %7, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.ax_splay_node_st, ptr %51, i32 0, i32 2
  %53 = load ptr, ptr %52, align 8, !tbaa !11
  store ptr %53, ptr %7, align 8, !tbaa !5
  br label %56

54:                                               ; preds = %47
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %55, ptr %6, align 8, !tbaa !5
  store ptr %55, ptr %5, align 8, !tbaa !5
  store i32 2, ptr %11, align 4
  br label %58

56:                                               ; preds = %50
  br label %57

57:                                               ; preds = %56, %43
  store i32 0, ptr %11, align 4
  br label %58

58:                                               ; preds = %57, %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  %59 = load i32, ptr %11, align 4
  switch i32 %59, label %84 [
    i32 0, label %60
    i32 2, label %76
  ]

60:                                               ; preds = %58
  br label %30, !llvm.loop !18

61:                                               ; preds = %30
  %62 = load ptr, ptr %8, align 8, !tbaa !5
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.ax_splay_node_st, ptr %63, i32 0, i32 0
  store ptr %62, ptr %64, align 8, !tbaa !15
  %65 = load i32, ptr %9, align 4, !tbaa !17
  %66 = icmp ne i32 %65, 0
  br i1 %66, label %67, label %71

67:                                               ; preds = %61
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = load ptr, ptr %8, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.ax_splay_node_st, ptr %69, i32 0, i32 1
  store ptr %68, ptr %70, align 8, !tbaa !9
  br label %75

71:                                               ; preds = %61
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  %73 = load ptr, ptr %8, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.ax_splay_node_st, ptr %73, i32 0, i32 2
  store ptr %72, ptr %74, align 8, !tbaa !11
  br label %75

75:                                               ; preds = %71, %67
  br label %76

76:                                               ; preds = %75, %58, %20
  %77 = load ptr, ptr %4, align 8, !tbaa !5
  %78 = load ptr, ptr %5, align 8, !tbaa !5
  call void @ax_splay_splay(ptr noundef %77, ptr noundef %78)
  %79 = load ptr, ptr %4, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.ax_splay, ptr %79, i32 0, i32 2
  %81 = load i32, ptr %80, align 8, !tbaa !20
  %82 = add nsw i32 %81, 1
  store i32 %82, ptr %80, align 8, !tbaa !20
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %83, ptr %3, align 8
  store i32 1, ptr %11, align 4
  br label %84

84:                                               ; preds = %76, %58
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %85 = load ptr, ptr %3, align 8
  ret ptr %85
}

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 }

!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 = !{!"ax_splay_node_st", !6, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!10, !6, i64 16}
!12 = !{!13, !6, i64 0}
!13 = !{!"ax_splay", !6, i64 0, !6, i64 8, !14, i64 16}
!14 = !{!"int", !7, i64 0}
!15 = !{!10, !6, i64 0}
!16 = !{!13, !6, i64 8}
!17 = !{!14, !14, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!13, !14, i64 16}


; 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: nounwind uwtable
declare dso_local void @ax_splay_splay(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @ax_splay_insert(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %1, i64 8
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %3, i8 0, i64 16, i1 false)
  %4 = load ptr, ptr %0, align 8, !tbaa !5
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %6, label %.preheader

.preheader:                                       ; preds = %2
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  br label %.backedge

6:                                                ; preds = %2
  store ptr %1, ptr %0, align 8, !tbaa !5
  store ptr null, ptr %1, align 8, !tbaa !11
  br label %.thread

.backedge:                                        ; preds = %.backedge.backedge, %.preheader
  %.03256 = phi ptr [ %4, %.preheader ], [ %.03256.be, %.backedge.backedge ]
  %7 = load ptr, ptr %5, align 8, !tbaa !13
  %8 = tail call i32 %7(ptr noundef %1, ptr noundef nonnull %.03256) #2
  %9 = icmp sgt i32 %8, -1
  br i1 %9, label %10, label %.thread59

10:                                               ; preds = %.backedge
  %.not45 = icmp eq i32 %8, 0
  br i1 %.not45, label %.thread, label %11

11:                                               ; preds = %10
  %12 = getelementptr inbounds i8, ptr %.03256, i64 16
  %.133 = load ptr, ptr %12, align 8, !tbaa !14
  %.not43 = icmp eq ptr %.133, null
  br i1 %.not43, label %16, label %.backedge.backedge

.backedge.backedge:                               ; preds = %11, %.thread59
  %.03256.be = phi ptr [ %.133, %11 ], [ %.13361, %.thread59 ]
  br label %.backedge

.thread59:                                        ; preds = %.backedge
  %13 = getelementptr inbounds i8, ptr %.03256, i64 8
  %.13361 = load ptr, ptr %13, align 8, !tbaa !14
  %.not4362 = icmp eq ptr %.13361, null
  br i1 %.not4362, label %14, label %.backedge.backedge

14:                                               ; preds = %.thread59
  store ptr %.03256, ptr %1, align 8, !tbaa !11
  %15 = getelementptr inbounds i8, ptr %.03256, i64 8
  store ptr %1, ptr %15, align 8, !tbaa !15
  br label %.thread

16:                                               ; preds = %11
  store ptr %.03256, ptr %1, align 8, !tbaa !11
  %17 = getelementptr inbounds i8, ptr %.03256, i64 16
  store ptr %1, ptr %17, align 8, !tbaa !16
  br label %.thread

.thread:                                          ; preds = %10, %14, %16, %6
  %.240 = phi ptr [ %1, %14 ], [ %1, %16 ], [ %1, %6 ], [ %.03256, %10 ]
  %.237 = phi ptr [ null, %14 ], [ null, %16 ], [ null, %6 ], [ %.03256, %10 ]
  tail call void @ax_splay_splay(ptr noundef nonnull %0, ptr noundef nonnull %.240)
  %18 = getelementptr inbounds i8, ptr %0, i64 16
  %19 = load i32, ptr %18, align 8, !tbaa !17
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr %18, align 8, !tbaa !17
  ret ptr %.237
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 = !{!"ax_splay", !7, i64 0, !7, i64 8, !10, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !7, i64 0}
!12 = !{!"ax_splay_node_st", !7, i64 0, !7, i64 8, !7, i64 16}
!13 = !{!6, !7, i64 8}
!14 = !{!7, !7, i64 0}
!15 = !{!12, !7, i64 8}
!16 = !{!12, !7, i64 16}
!17 = !{!6, !10, i64 16}


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