; LLVM IR from hutusi/rethink-c :: src (avltree)

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

; 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 hidden void @avl_tree_balance_tree(ptr noundef, ptr noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local ptr @avl_tree_remove_node(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
  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) #2
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct._AVLTreeNode, ptr %7, i32 0, i32 2
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  store ptr %9, ptr %5, align 8, !tbaa !5
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct._AVLTreeNode, ptr %10, i32 0, i32 3
  %12 = load ptr, ptr %11, align 8, !tbaa !12
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %23

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

19:                                               ; preds = %14
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = call ptr @avl_tree_replace_node(ptr noundef %20, ptr noundef %21, ptr noundef null)
  br label %100

23:                                               ; preds = %14, %2
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct._AVLTreeNode, ptr %24, i32 0, i32 3
  %26 = load ptr, ptr %25, align 8, !tbaa !12
  %27 = icmp eq ptr %26, null
  br i1 %27, label %28, label %38

28:                                               ; preds = %23
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct._AVLTreeNode, ptr %29, i32 0, i32 4
  %31 = load ptr, ptr %30, align 8, !tbaa !13
  store ptr %31, ptr %5, align 8, !tbaa !5
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct._AVLTreeNode, ptr %34, i32 0, i32 4
  %36 = load ptr, ptr %35, align 8, !tbaa !13
  %37 = call ptr @avl_tree_replace_node(ptr noundef %32, ptr noundef %33, ptr noundef %36)
  br label %99

38:                                               ; preds = %23
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct._AVLTreeNode, ptr %39, i32 0, i32 4
  %41 = load ptr, ptr %40, align 8, !tbaa !13
  %42 = icmp eq ptr %41, null
  br i1 %42, label %43, label %53

43:                                               ; preds = %38
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct._AVLTreeNode, ptr %44, i32 0, i32 3
  %46 = load ptr, ptr %45, align 8, !tbaa !12
  store ptr %46, ptr %5, align 8, !tbaa !5
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct._AVLTreeNode, ptr %49, i32 0, i32 3
  %51 = load ptr, ptr %50, align 8, !tbaa !12
  %52 = call ptr @avl_tree_replace_node(ptr noundef %47, ptr noundef %48, ptr noundef %51)
  br label %98

53:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct._AVLTreeNode, ptr %54, i32 0, i32 4
  %56 = load ptr, ptr %55, align 8, !tbaa !13
  %57 = call ptr @avl_tree_leftmost_node(ptr noundef %56)
  store ptr %57, ptr %6, align 8, !tbaa !5
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct._AVLTreeNode, ptr %59, i32 0, i32 4
  %61 = load ptr, ptr %60, align 8, !tbaa !13
  %62 = icmp ne ptr %58, %61
  br i1 %62, label %63, label %83

63:                                               ; preds = %53
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct._AVLTreeNode, ptr %64, i32 0, i32 2
  %66 = load ptr, ptr %65, align 8, !tbaa !9
  store ptr %66, ptr %5, align 8, !tbaa !5
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = load ptr, ptr %6, align 8, !tbaa !5
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct._AVLTreeNode, ptr %69, i32 0, i32 4
  %71 = load ptr, ptr %70, align 8, !tbaa !13
  %72 = call ptr @avl_tree_replace_node(ptr noundef %67, ptr noundef %68, ptr noundef %71)
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct._AVLTreeNode, ptr %73, i32 0, i32 4
  %75 = load ptr, ptr %74, align 8, !tbaa !13
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct._AVLTreeNode, ptr %76, i32 0, i32 4
  store ptr %75, ptr %77, align 8, !tbaa !13
  %78 = load ptr, ptr %6, align 8, !tbaa !5
  %79 = load ptr, ptr %4, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct._AVLTreeNode, ptr %79, i32 0, i32 4
  %81 = load ptr, ptr %80, align 8, !tbaa !13
  %82 = getelementptr inbounds %struct._AVLTreeNode, ptr %81, i32 0, i32 2
  store ptr %78, ptr %82, align 8, !tbaa !9
  br label %83

83:                                               ; preds = %63, %53
  %84 = load ptr, ptr %3, align 8, !tbaa !5
  %85 = load ptr, ptr %4, align 8, !tbaa !5
  %86 = load ptr, ptr %6, align 8, !tbaa !5
  %87 = call ptr @avl_tree_replace_node(ptr noundef %84, ptr noundef %85, ptr noundef %86)
  %88 = load ptr, ptr %4, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct._AVLTreeNode, ptr %88, i32 0, i32 3
  %90 = load ptr, ptr %89, align 8, !tbaa !12
  %91 = load ptr, ptr %6, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct._AVLTreeNode, ptr %91, i32 0, i32 3
  store ptr %90, ptr %92, align 8, !tbaa !12
  %93 = load ptr, ptr %6, align 8, !tbaa !5
  %94 = load ptr, ptr %4, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct._AVLTreeNode, ptr %94, i32 0, i32 3
  %96 = load ptr, ptr %95, align 8, !tbaa !12
  %97 = getelementptr inbounds %struct._AVLTreeNode, ptr %96, i32 0, i32 2
  store ptr %93, ptr %97, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %98

98:                                               ; preds = %83, %43
  br label %99

99:                                               ; preds = %98, %28
  br label %100

100:                                              ; preds = %99, %19
  %101 = load ptr, ptr %3, align 8, !tbaa !5
  %102 = load ptr, ptr %5, align 8, !tbaa !5
  call void @avl_tree_balance_tree(ptr noundef %101, ptr noundef %102)
  %103 = load ptr, ptr %3, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct._AVLTree, ptr %103, i32 0, i32 4
  %105 = load i32, ptr %104, align 8, !tbaa !14
  %106 = add i32 %105, -1
  store i32 %106, ptr %104, align 8, !tbaa !14
  %107 = load ptr, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret ptr %107
}

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

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 }

!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 16}
!10 = !{!"_AVLTreeNode", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !11, i64 40}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 24}
!13 = !{!10, !6, i64 32}
!14 = !{!15, !11, i64 32}
!15 = !{!"_AVLTree", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32}


; 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 hidden void @avl_tree_balance_tree(ptr noundef, ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @avl_tree_remove_node(ptr noundef %0, ptr noundef returned %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %1, i64 16
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %1, i64 24
  %6 = load ptr, ptr %5, align 8, !tbaa !11
  %7 = icmp eq ptr %6, null
  %8 = getelementptr inbounds i8, ptr %1, i64 32
  %9 = load ptr, ptr %8, align 8, !tbaa !12
  %10 = icmp eq ptr %9, null
  br i1 %7, label %11, label %16

11:                                               ; preds = %2
  br i1 %10, label %12, label %14

12:                                               ; preds = %11
  %13 = tail call ptr @avl_tree_replace_node(ptr noundef %0, ptr noundef nonnull %1, ptr noundef null)
  br label %35

14:                                               ; preds = %11
  %15 = tail call ptr @avl_tree_replace_node(ptr noundef %0, ptr noundef nonnull %1, ptr noundef nonnull %9)
  br label %35

16:                                               ; preds = %2
  br i1 %10, label %17, label %19

17:                                               ; preds = %16
  %18 = tail call ptr @avl_tree_replace_node(ptr noundef %0, ptr noundef nonnull %1, ptr noundef nonnull %6)
  br label %35

19:                                               ; preds = %16
  %20 = tail call ptr @avl_tree_leftmost_node(ptr noundef nonnull %9)
  %21 = load ptr, ptr %8, align 8, !tbaa !12
  %.not = icmp eq ptr %20, %21
  br i1 %.not, label %30, label %22

22:                                               ; preds = %19
  %23 = getelementptr inbounds i8, ptr %20, i64 16
  %24 = load ptr, ptr %23, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %20, i64 32
  %26 = load ptr, ptr %25, align 8, !tbaa !12
  %27 = tail call ptr @avl_tree_replace_node(ptr noundef %0, ptr noundef %20, ptr noundef %26)
  %28 = load ptr, ptr %8, align 8, !tbaa !12
  store ptr %28, ptr %25, align 8, !tbaa !12
  %29 = getelementptr inbounds i8, ptr %28, i64 16
  store ptr %20, ptr %29, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %22, %19
  %.0 = phi ptr [ %24, %22 ], [ %4, %19 ]
  %31 = tail call ptr @avl_tree_replace_node(ptr noundef %0, ptr noundef nonnull %1, ptr noundef %20)
  %32 = load ptr, ptr %5, align 8, !tbaa !11
  %33 = getelementptr inbounds i8, ptr %20, i64 24
  store ptr %32, ptr %33, align 8, !tbaa !11
  %34 = getelementptr inbounds i8, ptr %32, i64 16
  store ptr %20, ptr %34, align 8, !tbaa !5
  br label %35

35:                                               ; preds = %14, %30, %17, %12
  %.1 = phi ptr [ %4, %12 ], [ %9, %14 ], [ %6, %17 ], [ %.0, %30 ]
  tail call void @avl_tree_balance_tree(ptr noundef %0, ptr noundef %.1)
  %36 = getelementptr inbounds i8, ptr %0, i64 32
  %37 = load i32, ptr %36, align 8, !tbaa !13
  %38 = add i32 %37, -1
  store i32 %38, ptr %36, align 8, !tbaa !13
  ret ptr %1
}

; Function Attrs: nounwind uwtable
declare hidden ptr @avl_tree_replace_node(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

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


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