; LLVM IR from Softmotions/iowow :: src/utils (iwavl)

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

; 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: alwaysinline nounwind uwtable
declare hidden ptr @iwavl_get_parent(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @iwavl_remove(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #3
  store i8 0, ptr %6, align 1, !tbaa !9
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.iwavl_node, ptr %9, i32 0, i32 0
  %11 = load ptr, ptr %10, align 8, !tbaa !11
  %12 = icmp ne ptr %11, null
  br i1 %12, label %13, label %22

13:                                               ; preds = %2
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.iwavl_node, ptr %14, i32 0, i32 1
  %16 = load ptr, ptr %15, align 8, !tbaa !14
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %22

18:                                               ; preds = %13
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = call ptr @avl_tree_swap_with_successor(ptr noundef %19, ptr noundef %20, ptr noundef %6)
  store ptr %21, ptr %5, align 8, !tbaa !5
  br label %75

22:                                               ; preds = %13, %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.iwavl_node, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !11
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %31

27:                                               ; preds = %22
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.iwavl_node, ptr %28, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !11
  br label %35

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

35:                                               ; preds = %31, %27
  %36 = phi ptr [ %30, %27 ], [ %34, %31 ]
  store ptr %36, ptr %7, align 8, !tbaa !5
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = call ptr @iwavl_get_parent(ptr noundef %37)
  store ptr %38, ptr %5, align 8, !tbaa !5
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = icmp ne ptr %39, null
  br i1 %40, label %41, label %62

41:                                               ; preds = %35
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.iwavl_node, ptr %43, i32 0, i32 0
  %45 = load ptr, ptr %44, align 8, !tbaa !11
  %46 = icmp eq ptr %42, %45
  br i1 %46, label %47, label %51

47:                                               ; preds = %41
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.iwavl_node, ptr %49, i32 0, i32 0
  store ptr %48, ptr %50, align 8, !tbaa !11
  store i8 1, ptr %6, align 1, !tbaa !9
  br label %55

51:                                               ; preds = %41
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.iwavl_node, ptr %53, i32 0, i32 1
  store ptr %52, ptr %54, align 8, !tbaa !14
  store i8 0, ptr %6, align 1, !tbaa !9
  br label %55

55:                                               ; preds = %51, %47
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = icmp ne ptr %56, null
  br i1 %57, label %58, label %61

58:                                               ; preds = %55
  %59 = load ptr, ptr %7, align 8, !tbaa !5
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  call void @avl_set_parent(ptr noundef %59, ptr noundef %60)
  br label %61

61:                                               ; preds = %58, %55
  br label %71

62:                                               ; preds = %35
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = icmp ne ptr %63, null
  br i1 %64, label %65, label %68

65:                                               ; preds = %62
  %66 = load ptr, ptr %7, align 8, !tbaa !5
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  call void @avl_set_parent(ptr noundef %66, ptr noundef %67)
  br label %68

68:                                               ; preds = %65, %62
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %69, ptr %70, align 8, !tbaa !5
  store i32 1, ptr %8, align 4
  br label %72

71:                                               ; preds = %61
  store i32 0, ptr %8, align 4
  br label %72

72:                                               ; preds = %71, %68
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  %73 = load i32, ptr %8, align 4
  switch i32 %73, label %92 [
    i32 0, label %74
  ]

74:                                               ; preds = %72
  br label %75

75:                                               ; preds = %74, %18
  br label %76

76:                                               ; preds = %88, %75
  %77 = load i8, ptr %6, align 1, !tbaa !9, !range !15, !noundef !16
  %78 = trunc i8 %77 to i1
  br i1 %78, label %79, label %83

79:                                               ; preds = %76
  %80 = load ptr, ptr %3, align 8, !tbaa !5
  %81 = load ptr, ptr %5, align 8, !tbaa !5
  %82 = call ptr @avl_handle_subtree_shrink(ptr noundef %80, ptr noundef %81, i32 noundef 1, ptr noundef %6)
  store ptr %82, ptr %5, align 8, !tbaa !5
  br label %87

83:                                               ; preds = %76
  %84 = load ptr, ptr %3, align 8, !tbaa !5
  %85 = load ptr, ptr %5, align 8, !tbaa !5
  %86 = call ptr @avl_handle_subtree_shrink(ptr noundef %84, ptr noundef %85, i32 noundef -1, ptr noundef %6)
  store ptr %86, ptr %5, align 8, !tbaa !5
  br label %87

87:                                               ; preds = %83, %79
  br label %88

88:                                               ; preds = %87
  %89 = load ptr, ptr %5, align 8, !tbaa !5
  %90 = icmp ne ptr %89, null
  br i1 %90, label %76, label %91, !llvm.loop !17

91:                                               ; preds = %88
  store i32 0, ptr %8, align 4
  br label %92

92:                                               ; preds = %91, %72
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  %93 = load i32, ptr %8, align 4
  switch i32 %93, label %95 [
    i32 0, label %94
    i32 1, label %94
  ]

94:                                               ; preds = %92, %92
  ret void

95:                                               ; preds = %92
  unreachable
}

; Function Attrs: alwaysinline nounwind uwtable
declare hidden ptr @avl_tree_swap_with_successor(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @avl_set_parent(ptr noundef, ptr noundef) #1

; Function Attrs: alwaysinline nounwind uwtable
declare hidden ptr @avl_handle_subtree_shrink(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { alwaysinline 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 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"_Bool", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"iwavl_node", !6, i64 0, !6, i64 8, !13, i64 16}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !6, i64 8}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = distinct !{!17, !18}
!18 = !{!"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"

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

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden ptr @iwavl_get_parent(ptr noundef) local_unnamed_addr #1

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

7:                                                ; preds = %2
  %.not34 = icmp eq ptr %6, null
  br i1 %.not34, label %10, label %8

8:                                                ; preds = %7
  %9 = call ptr @avl_tree_swap_with_successor(ptr noundef %0, ptr noundef nonnull %1, ptr noundef nonnull %3)
  br label %.thread40.preheader

.thread40.preheader:                              ; preds = %13, %16, %8
  %.1.ph = phi ptr [ %12, %13 ], [ %12, %16 ], [ %9, %8 ]
  br label %.thread40

10:                                               ; preds = %2, %7
  %11 = phi ptr [ %4, %7 ], [ %6, %2 ]
  %12 = tail call ptr @iwavl_get_parent(ptr noundef nonnull %1)
  %.not36.not = icmp eq ptr %12, null
  br i1 %.not36.not, label %17, label %13

13:                                               ; preds = %10
  %14 = load ptr, ptr %12, align 8, !tbaa !9
  %15 = icmp eq ptr %14, %1
  %.sink.idx = select i1 %15, i64 0, i64 8
  %.sink = getelementptr inbounds i8, ptr %12, i64 %.sink.idx
  %storemerge = zext i1 %15 to i8
  store ptr %11, ptr %.sink, align 8, !tbaa !14
  store i8 %storemerge, ptr %3, align 1, !tbaa !5
  %.not38 = icmp eq ptr %11, null
  br i1 %.not38, label %.thread40.preheader, label %16

16:                                               ; preds = %13
  tail call void @avl_set_parent(ptr noundef nonnull %11, ptr noundef nonnull %12)
  br label %.thread40.preheader

17:                                               ; preds = %10
  %.not37 = icmp eq ptr %11, null
  br i1 %.not37, label %19, label %18

18:                                               ; preds = %17
  tail call void @avl_set_parent(ptr noundef nonnull %11, ptr noundef null)
  br label %19

19:                                               ; preds = %17, %18
  store ptr %11, ptr %0, align 8, !tbaa !14
  br label %.loopexit

.thread40:                                        ; preds = %.thread40.preheader, %.thread40
  %.1 = phi ptr [ %22, %.thread40 ], [ %.1.ph, %.thread40.preheader ]
  %20 = load i8, ptr %3, align 1, !tbaa !5, !range !15, !noundef !16
  %21 = trunc nuw i8 %20 to i1
  %. = select i1 %21, i32 1, i32 -1
  %22 = call ptr @avl_handle_subtree_shrink(ptr noundef %0, ptr noundef %.1, i32 noundef %., ptr noundef nonnull %3)
  %.not39 = icmp eq ptr %22, null
  br i1 %.not39, label %.loopexit, label %.thread40, !llvm.loop !17

.loopexit:                                        ; preds = %.thread40, %19
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %3) #3
  ret void
}

; Function Attrs: alwaysinline nounwind uwtable
declare hidden ptr @avl_tree_swap_with_successor(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @avl_set_parent(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: alwaysinline nounwind uwtable
declare hidden ptr @avl_handle_subtree_shrink(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { alwaysinline 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 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 = !{!"_Bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"iwavl_node", !11, i64 0, !11, i64 8, !12, i64 16}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!10, !11, i64 8}
!14 = !{!11, !11, i64 0}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass