; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/ds/avl_tree.ll (Z12balance_treeP4Node)

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

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_Z12balance_treeP4Node(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds %struct.Node, ptr %3, i32 0, i32 3
  %5 = load i32, ptr %4, align 4, !tbaa !9
  %6 = icmp sgt i32 %5, 1
  br i1 %6, label %7, label %24

7:                                                ; preds = %1
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.Node, ptr %8, i32 0, i32 0
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  %11 = getelementptr inbounds %struct.Node, ptr %10, i32 0, i32 3
  %12 = load i32, ptr %11, align 4, !tbaa !9
  %13 = icmp eq i32 %12, 1
  br i1 %13, label %21, label %14

14:                                               ; preds = %7
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.Node, ptr %15, i32 0, i32 0
  %17 = load ptr, ptr %16, align 8, !tbaa !12
  %18 = getelementptr inbounds %struct.Node, ptr %17, i32 0, i32 3
  %19 = load i32, ptr %18, align 4, !tbaa !9
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %24

21:                                               ; preds = %14, %7
  %22 = load ptr, ptr %2, align 8, !tbaa !5
  %23 = call noundef ptr @_Z12right_rotateP4Node(ptr noundef %22)
  store ptr %23, ptr %2, align 8, !tbaa !5
  br label %91

24:                                               ; preds = %14, %1
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.Node, ptr %25, i32 0, i32 3
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = icmp sgt i32 %27, 1
  br i1 %28, label %29, label %45

29:                                               ; preds = %24
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.Node, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !12
  %33 = getelementptr inbounds %struct.Node, ptr %32, i32 0, i32 3
  %34 = load i32, ptr %33, align 4, !tbaa !9
  %35 = icmp eq i32 %34, -1
  br i1 %35, label %36, label %45

36:                                               ; preds = %29
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.Node, ptr %37, i32 0, i32 0
  %39 = load ptr, ptr %38, align 8, !tbaa !12
  %40 = call noundef ptr @_Z11left_rotateP4Node(ptr noundef %39)
  %41 = load ptr, ptr %2, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.Node, ptr %41, i32 0, i32 0
  store ptr %40, ptr %42, align 8, !tbaa !12
  %43 = load ptr, ptr %2, align 8, !tbaa !5
  %44 = call noundef ptr @_Z12right_rotateP4Node(ptr noundef %43)
  store ptr %44, ptr %2, align 8, !tbaa !5
  br label %90

45:                                               ; preds = %29, %24
  %46 = load ptr, ptr %2, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.Node, ptr %46, i32 0, i32 3
  %48 = load i32, ptr %47, align 4, !tbaa !9
  %49 = icmp slt i32 %48, -1
  br i1 %49, label %50, label %67

50:                                               ; preds = %45
  %51 = load ptr, ptr %2, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.Node, ptr %51, i32 0, i32 1
  %53 = load ptr, ptr %52, align 8, !tbaa !13
  %54 = getelementptr inbounds %struct.Node, ptr %53, i32 0, i32 3
  %55 = load i32, ptr %54, align 4, !tbaa !9
  %56 = icmp eq i32 %55, -1
  br i1 %56, label %64, label %57

57:                                               ; preds = %50
  %58 = load ptr, ptr %2, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.Node, ptr %58, i32 0, i32 1
  %60 = load ptr, ptr %59, align 8, !tbaa !13
  %61 = getelementptr inbounds %struct.Node, ptr %60, i32 0, i32 3
  %62 = load i32, ptr %61, align 4, !tbaa !9
  %63 = icmp eq i32 %62, 0
  br i1 %63, label %64, label %67

64:                                               ; preds = %57, %50
  %65 = load ptr, ptr %2, align 8, !tbaa !5
  %66 = call noundef ptr @_Z11left_rotateP4Node(ptr noundef %65)
  store ptr %66, ptr %2, align 8, !tbaa !5
  br label %89

67:                                               ; preds = %57, %45
  %68 = load ptr, ptr %2, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.Node, ptr %68, i32 0, i32 3
  %70 = load i32, ptr %69, align 4, !tbaa !9
  %71 = icmp slt i32 %70, -1
  br i1 %71, label %72, label %88

72:                                               ; preds = %67
  %73 = load ptr, ptr %2, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.Node, ptr %73, i32 0, i32 1
  %75 = load ptr, ptr %74, align 8, !tbaa !13
  %76 = getelementptr inbounds %struct.Node, ptr %75, i32 0, i32 3
  %77 = load i32, ptr %76, align 4, !tbaa !9
  %78 = icmp eq i32 %77, 1
  br i1 %78, label %79, label %88

79:                                               ; preds = %72
  %80 = load ptr, ptr %2, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.Node, ptr %80, i32 0, i32 1
  %82 = load ptr, ptr %81, align 8, !tbaa !13
  %83 = call noundef ptr @_Z12right_rotateP4Node(ptr noundef %82)
  %84 = load ptr, ptr %2, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.Node, ptr %84, i32 0, i32 1
  store ptr %83, ptr %85, align 8, !tbaa !13
  %86 = load ptr, ptr %2, align 8, !tbaa !5
  %87 = call noundef ptr @_Z11left_rotateP4Node(ptr noundef %86)
  store ptr %87, ptr %2, align 8, !tbaa !5
  br label %88

88:                                               ; preds = %79, %72, %67
  br label %89

89:                                               ; preds = %88, %64
  br label %90

90:                                               ; preds = %89, %36
  br label %91

91:                                               ; preds = %90, %21
  %92 = load ptr, ptr %2, align 8, !tbaa !5
  ret ptr %92
}

attributes #0 = { mustprogress 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++ TBAA"}
!9 = !{!10, !11, i64 20}
!10 = !{!"_ZTS4Node", !6, i64 0, !6, i64 8, !11, i64 16, !11, i64 20, !11, i64 24}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 0}
!13 = !{!10, !6, i64 8}


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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_Z12balance_treeP4Node(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 20
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 1
  br i1 %4, label %5, label %16

5:                                                ; preds = %1
  %6 = load ptr, ptr %0, align 8, !tbaa !11
  %7 = getelementptr inbounds i8, ptr %6, i64 20
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %switch = icmp ult i32 %8, 2
  br i1 %switch, label %9, label %11

9:                                                ; preds = %5
  %10 = tail call noundef ptr @_Z12right_rotateP4Node(ptr noundef nonnull %0)
  br label %.thread23

11:                                               ; preds = %5
  %12 = icmp eq i32 %8, -1
  br i1 %12, label %13, label %.thread23

13:                                               ; preds = %11
  %14 = tail call noundef ptr @_Z11left_rotateP4Node(ptr noundef nonnull %6)
  store ptr %14, ptr %0, align 8, !tbaa !11
  %15 = tail call noundef ptr @_Z12right_rotateP4Node(ptr noundef nonnull %0)
  br label %.thread23

16:                                               ; preds = %1
  %17 = icmp slt i32 %3, -1
  br i1 %17, label %18, label %.thread23

18:                                               ; preds = %16
  %19 = getelementptr inbounds i8, ptr %0, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !12
  %21 = getelementptr inbounds i8, ptr %20, i64 20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  switch i32 %22, label %.thread23 [
    i32 -1, label %23
    i32 0, label %23
    i32 1, label %25
  ]

23:                                               ; preds = %18, %18
  %24 = tail call noundef ptr @_Z11left_rotateP4Node(ptr noundef nonnull %0)
  br label %.thread23

25:                                               ; preds = %18
  %26 = tail call noundef ptr @_Z12right_rotateP4Node(ptr noundef nonnull %20)
  store ptr %26, ptr %19, align 8, !tbaa !12
  %27 = tail call noundef ptr @_Z11left_rotateP4Node(ptr noundef nonnull %0)
  br label %.thread23

.thread23:                                        ; preds = %18, %16, %11, %13, %25, %23, %9
  %.0 = phi ptr [ %10, %9 ], [ %15, %13 ], [ %24, %23 ], [ %27, %25 ], [ %0, %11 ], [ %0, %16 ], [ %0, %18 ]
  ret ptr %.0
}

attributes #0 = { mustprogress 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, !10, i64 20}
!6 = !{!"_ZTS4Node", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 20, !10, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!6, !7, i64 8}


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