; 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: nounwind uwtable
define dso_local void @iwavl_rebalance_after_insert(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  %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 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #3
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.iwavl_node, ptr %9, i32 0, i32 0
  store ptr null, ptr %10, align 8, !tbaa !9
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.iwavl_node, ptr %11, i32 0, i32 1
  store ptr null, ptr %12, align 8, !tbaa !12
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %13, ptr %5, align 8, !tbaa !5
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = call ptr @iwavl_get_parent(ptr noundef %14)
  store ptr %15, ptr %6, align 8, !tbaa !5
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %19, label %18

18:                                               ; preds = %2
  store i32 1, ptr %8, align 4
  br label %66

19:                                               ; preds = %2
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.iwavl_node, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !9
  %24 = icmp eq ptr %20, %23
  br i1 %24, label %25, label %27

25:                                               ; preds = %19
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  call void @avl_adjust_balance_factor(ptr noundef %26, i32 noundef -1)
  br label %29

27:                                               ; preds = %19
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  call void @avl_adjust_balance_factor(ptr noundef %28, i32 noundef 1)
  br label %29

29:                                               ; preds = %27, %25
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = call i32 @avl_get_balance_factor(ptr noundef %30)
  %32 = icmp eq i32 %31, 0
  br i1 %32, label %33, label %34

33:                                               ; preds = %29
  store i32 1, ptr %8, align 4
  br label %66

34:                                               ; preds = %29
  br label %35

35:                                               ; preds = %61, %34
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %36, ptr %5, align 8, !tbaa !5
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = call ptr @iwavl_get_parent(ptr noundef %37)
  store ptr %38, ptr %6, align 8, !tbaa !5
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = icmp ne ptr %39, null
  br i1 %40, label %42, label %41

41:                                               ; preds = %35
  store i32 1, ptr %8, align 4
  br label %66

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

48:                                               ; preds = %42
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = call zeroext i1 @avl_handle_subtree_growth(ptr noundef %49, ptr noundef %50, ptr noundef %51, i32 noundef -1)
  %53 = zext i1 %52 to i8
  store i8 %53, ptr %7, align 1, !tbaa !13
  br label %60

54:                                               ; preds = %42
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = call zeroext i1 @avl_handle_subtree_growth(ptr noundef %55, ptr noundef %56, ptr noundef %57, i32 noundef 1)
  %59 = zext i1 %58 to i8
  store i8 %59, ptr %7, align 1, !tbaa !13
  br label %60

60:                                               ; preds = %54, %48
  br label %61

61:                                               ; preds = %60
  %62 = load i8, ptr %7, align 1, !tbaa !13, !range !15, !noundef !16
  %63 = trunc i8 %62 to i1
  %64 = xor i1 %63, true
  br i1 %64, label %35, label %65, !llvm.loop !17

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

66:                                               ; preds = %65, %41, %33, %18
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  %67 = load i32, ptr %8, align 4
  switch i32 %67, label %69 [
    i32 0, label %68
    i32 1, label %68
  ]

68:                                               ; preds = %66, %66
  ret void

69:                                               ; preds = %66
  unreachable
}

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @avl_adjust_balance_factor(ptr noundef, i32 noundef) #2

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @avl_get_balance_factor(ptr noundef) #2

; Function Attrs: alwaysinline nounwind uwtable
declare hidden zeroext i1 @avl_handle_subtree_growth(ptr noundef, ptr noundef, ptr noundef, i32 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 = { 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 #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 0}
!10 = !{!"iwavl_node", !6, i64 0, !6, i64 8, !11, i64 16}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{!14, !14, i64 0}
!14 = !{!"_Bool", !7, i64 0}
!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: nounwind uwtable
define dso_local void @iwavl_rebalance_after_insert(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %1, i8 0, i64 16, i1 false)
  %3 = tail call ptr @iwavl_get_parent(ptr noundef nonnull %1)
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %.loopexit, label %4

4:                                                ; preds = %2
  %5 = load ptr, ptr %3, align 8, !tbaa !5
  %6 = icmp eq ptr %5, %1
  %. = select i1 %6, i32 -1, i32 1
  tail call void @avl_adjust_balance_factor(ptr noundef nonnull %3, i32 noundef %.)
  %7 = tail call i32 @avl_get_balance_factor(ptr noundef nonnull %3)
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %4
  %9 = tail call ptr @iwavl_get_parent(ptr noundef nonnull %3)
  %.not2425 = icmp eq ptr %9, null
  br i1 %.not2425, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.backedge
  %10 = phi ptr [ %17, %.backedge ], [ %9, %.preheader ]
  %.02126 = phi ptr [ %10, %.backedge ], [ %3, %.preheader ]
  %11 = load ptr, ptr %10, align 8, !tbaa !5
  %12 = icmp eq ptr %.02126, %11
  br i1 %12, label %13, label %15

13:                                               ; preds = %.lr.ph
  %14 = tail call zeroext i1 @avl_handle_subtree_growth(ptr noundef %0, ptr noundef nonnull %.02126, ptr noundef nonnull %10, i32 noundef -1)
  br i1 %14, label %.loopexit, label %.backedge

15:                                               ; preds = %.lr.ph
  %16 = tail call zeroext i1 @avl_handle_subtree_growth(ptr noundef %0, ptr noundef nonnull %.02126, ptr noundef nonnull %10, i32 noundef 1)
  br i1 %16, label %.loopexit, label %.backedge

.backedge:                                        ; preds = %15, %13
  %17 = tail call ptr @iwavl_get_parent(ptr noundef nonnull %10)
  %.not24 = icmp eq ptr %17, null
  br i1 %.not24, label %.loopexit, label %.lr.ph, !llvm.loop !11

.loopexit:                                        ; preds = %.backedge, %15, %13, %.preheader, %4, %2
  ret void
}

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

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

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

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

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

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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!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 = !{!"iwavl_node", !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 = !{!"long", !8, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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