; LLVM IR from MostafaTwfiq/C-DataStructures-And-Algorithms :: DataStructure/Trees/Sources (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.AVLTree = type { ptr, i32, ptr, ptr }
%struct.AVLTreeNode = type { ptr, i32, ptr, ptr }

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @avlTreeIsRequiredRotation(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @avlTreeGetRotationType(ptr noundef) #0

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

; Function Attrs: nounwind uwtable
define dso_local ptr @avlTreeInsertR(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  %8 = load ptr, ptr %6, align 8, !tbaa !5
  %9 = icmp eq ptr %8, null
  br i1 %9, label %10, label %17

10:                                               ; preds = %3
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.AVLTree, ptr %11, i32 0, i32 1
  %13 = load i32, ptr %12, align 8, !tbaa !9
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr %12, align 8, !tbaa !9
  %15 = load ptr, ptr %7, align 8, !tbaa !5
  %16 = call ptr @createAVLTreeNode(ptr noundef %15)
  store ptr %16, ptr %4, align 8
  br label %67

17:                                               ; preds = %3
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.AVLTree, ptr %18, i32 0, i32 3
  %20 = load ptr, ptr %19, align 8, !tbaa !12
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.AVLTreeNode, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !13
  %25 = call i32 %20(ptr noundef %21, ptr noundef %24)
  %26 = icmp sgt i32 %25, 0
  br i1 %26, label %37, label %27

27:                                               ; preds = %17
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.AVLTree, ptr %28, i32 0, i32 3
  %30 = load ptr, ptr %29, align 8, !tbaa !12
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.AVLTreeNode, ptr %32, i32 0, i32 0
  %34 = load ptr, ptr %33, align 8, !tbaa !13
  %35 = call i32 %30(ptr noundef %31, ptr noundef %34)
  %36 = icmp eq i32 %35, 0
  br i1 %36, label %37, label %46

37:                                               ; preds = %27, %17
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.AVLTreeNode, ptr %39, i32 0, i32 3
  %41 = load ptr, ptr %40, align 8, !tbaa !15
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = call ptr @avlTreeInsertR(ptr noundef %38, ptr noundef %41, ptr noundef %42)
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.AVLTreeNode, ptr %44, i32 0, i32 3
  store ptr %43, ptr %45, align 8, !tbaa !15
  br label %55

46:                                               ; preds = %27
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.AVLTreeNode, ptr %48, i32 0, i32 2
  %50 = load ptr, ptr %49, align 8, !tbaa !16
  %51 = load ptr, ptr %7, align 8, !tbaa !5
  %52 = call ptr @avlTreeInsertR(ptr noundef %47, ptr noundef %50, ptr noundef %51)
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.AVLTreeNode, ptr %53, i32 0, i32 2
  store ptr %52, ptr %54, align 8, !tbaa !16
  br label %55

55:                                               ; preds = %46, %37
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  call void @avlTreeCalculateNodeHeight(ptr noundef %56)
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = call i32 @avlTreeIsRequiredRotation(ptr noundef %57)
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %60, label %65

60:                                               ; preds = %55
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = call i32 @avlTreeGetRotationType(ptr noundef %62)
  %64 = call ptr @avlTreePerformRotation(ptr noundef %61, i32 noundef %63)
  store ptr %64, ptr %4, align 8
  br label %67

65:                                               ; preds = %55
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %66, ptr %4, align 8
  br label %67

67:                                               ; preds = %65, %60, %10
  %68 = load ptr, ptr %4, align 8
  ret ptr %68
}

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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 8}
!10 = !{!"AVLTree", !6, i64 0, !11, i64 8, !6, i64 16, !6, i64 24}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 24}
!13 = !{!14, !6, i64 0}
!14 = !{!"AVLTreeNode", !6, i64 0, !11, i64 8, !6, i64 16, !6, i64 24}
!15 = !{!14, !6, i64 24}
!16 = !{!14, !6, 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 ptr @createAVLTreeNode(ptr noundef) local_unnamed_addr #0

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @avlTreeInsertR(ptr nocapture noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq ptr %1, null
  br i1 %4, label %5, label %10

5:                                                ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load i32, ptr %6, align 8, !tbaa !5
  %8 = add nsw i32 %7, 1
  store i32 %8, ptr %6, align 8, !tbaa !5
  %9 = tail call ptr @createAVLTreeNode(ptr noundef %2)
  br label %29

10:                                               ; preds = %3
  %11 = getelementptr inbounds i8, ptr %0, i64 24
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %13 = load ptr, ptr %1, align 8, !tbaa !12
  %14 = tail call i32 %12(ptr noundef %2, ptr noundef %13) #1
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %21, label %16

16:                                               ; preds = %10
  %17 = load ptr, ptr %11, align 8, !tbaa !11
  %18 = load ptr, ptr %1, align 8, !tbaa !12
  %19 = tail call i32 %17(ptr noundef %2, ptr noundef %18) #1
  %20 = icmp eq i32 %19, 0
  %spec.select = select i1 %20, i64 24, i64 16
  br label %21

21:                                               ; preds = %16, %10
  %.sink26 = phi i64 [ 24, %10 ], [ %spec.select, %16 ]
  %22 = getelementptr inbounds i8, ptr %1, i64 %.sink26
  %23 = load ptr, ptr %22, align 8, !tbaa !14
  %24 = tail call ptr @avlTreeInsertR(ptr noundef nonnull %0, ptr noundef %23, ptr noundef %2)
  store ptr %24, ptr %22, align 8, !tbaa !14
  tail call void @avlTreeCalculateNodeHeight(ptr noundef nonnull %1)
  %25 = tail call i32 @avlTreeIsRequiredRotation(ptr noundef nonnull %1)
  %.not = icmp eq i32 %25, 0
  br i1 %.not, label %29, label %26

26:                                               ; preds = %21
  %27 = tail call i32 @avlTreeGetRotationType(ptr noundef nonnull %1)
  %28 = tail call ptr @avlTreePerformRotation(ptr noundef nonnull %1, i32 noundef %27)
  br label %29

29:                                               ; preds = %21, %26, %5
  %.0 = phi ptr [ %9, %5 ], [ %28, %26 ], [ %1, %21 ]
  ret ptr %.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" }
attributes #1 = { 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, !10, i64 8}
!6 = !{!"AVLTree", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 24}
!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 = !{!13, !7, i64 0}
!13 = !{!"AVLTreeNode", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 24}
!14 = !{!7, !7, i64 0}


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