; LLVM IR from erincatto/box2d :: src (dynamic_tree)

; 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.b2DynamicTree = type { ptr, i32, i32, i32, i32, i32, ptr, ptr, ptr, ptr, i32 }
%struct.b2TreeNode = type { %struct.b2AABB, i64, %union.anon, %union.anon.0, i16, i16 }
%struct.b2AABB = type { %struct.b2Vec2, %struct.b2Vec2 }
%struct.b2Vec2 = type { float, float }
%union.anon = type { i64 }
%union.anon.0 = type { 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 zeroext i1 @b2IsLeaf(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local float @b2DynamicTree_GetAreaRatio(ptr noundef %0) #2 {
  %2 = alloca float, align 4
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca float, align 4
  %6 = alloca float, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.b2DynamicTree, ptr %10, i32 0, i32 1
  %12 = load i32, ptr %11, align 8, !tbaa !9
  %13 = icmp eq i32 %12, -1
  br i1 %13, label %14, label %15

14:                                               ; preds = %1
  store float 0.000000e+00, ptr %2, align 4
  br label %79

15:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.b2DynamicTree, ptr %16, i32 0, i32 0
  %18 = load ptr, ptr %17, align 8, !tbaa !12
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.b2DynamicTree, ptr %19, i32 0, i32 1
  %21 = load i32, ptr %20, align 8, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds %struct.b2TreeNode, ptr %18, i64 %22
  store ptr %23, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.b2TreeNode, ptr %24, i32 0, i32 0
  %26 = getelementptr inbounds { <2 x float>, <2 x float> }, ptr %25, i32 0, i32 0
  %27 = load <2 x float>, ptr %26, align 8
  %28 = getelementptr inbounds { <2 x float>, <2 x float> }, ptr %25, i32 0, i32 1
  %29 = load <2 x float>, ptr %28, align 8
  %30 = call float @b2Perimeter(<2 x float> %27, <2 x float> %29)
  store float %30, ptr %5, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store float 0.000000e+00, ptr %6, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !15
  br label %31

31:                                               ; preds = %72, %15
  %32 = load i32, ptr %7, align 4, !tbaa !15
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.b2DynamicTree, ptr %33, i32 0, i32 3
  %35 = load i32, ptr %34, align 8, !tbaa !16
  %36 = icmp slt i32 %32, %35
  br i1 %36, label %38, label %37

37:                                               ; preds = %31
  store i32 2, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %75

38:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.b2DynamicTree, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !12
  %42 = load i32, ptr %7, align 4, !tbaa !15
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds %struct.b2TreeNode, ptr %41, i64 %43
  store ptr %44, ptr %9, align 8, !tbaa !5
  %45 = load ptr, ptr %9, align 8, !tbaa !5
  %46 = call zeroext i1 @b2IsAllocated(ptr noundef %45)
  %47 = zext i1 %46 to i32
  %48 = icmp eq i32 %47, 0
  br i1 %48, label %58, label %49

49:                                               ; preds = %38
  %50 = load ptr, ptr %9, align 8, !tbaa !5
  %51 = call zeroext i1 @b2IsLeaf(ptr noundef %50)
  br i1 %51, label %58, label %52

52:                                               ; preds = %49
  %53 = load i32, ptr %7, align 4, !tbaa !15
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.b2DynamicTree, ptr %54, i32 0, i32 1
  %56 = load i32, ptr %55, align 8, !tbaa !9
  %57 = icmp eq i32 %53, %56
  br i1 %57, label %58, label %59

58:                                               ; preds = %52, %49, %38
  store i32 4, ptr %8, align 4
  br label %69

59:                                               ; preds = %52
  %60 = load ptr, ptr %9, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.b2TreeNode, ptr %60, i32 0, i32 0
  %62 = getelementptr inbounds { <2 x float>, <2 x float> }, ptr %61, i32 0, i32 0
  %63 = load <2 x float>, ptr %62, align 8
  %64 = getelementptr inbounds { <2 x float>, <2 x float> }, ptr %61, i32 0, i32 1
  %65 = load <2 x float>, ptr %64, align 8
  %66 = call float @b2Perimeter(<2 x float> %63, <2 x float> %65)
  %67 = load float, ptr %6, align 4, !tbaa !13
  %68 = fadd float %67, %66
  store float %68, ptr %6, align 4, !tbaa !13
  store i32 0, ptr %8, align 4
  br label %69

69:                                               ; preds = %59, %58
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  %70 = load i32, ptr %8, align 4
  switch i32 %70, label %81 [
    i32 0, label %71
    i32 4, label %72
  ]

71:                                               ; preds = %69
  br label %72

72:                                               ; preds = %71, %69
  %73 = load i32, ptr %7, align 4, !tbaa !15
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %7, align 4, !tbaa !15
  br label %31, !llvm.loop !17

75:                                               ; preds = %37
  %76 = load float, ptr %6, align 4, !tbaa !13
  %77 = load float, ptr %5, align 4, !tbaa !13
  %78 = fdiv float %76, %77
  store float %78, ptr %2, align 4
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %79

79:                                               ; preds = %75, %14
  %80 = load float, ptr %2, align 4
  ret float %80

81:                                               ; preds = %69
  unreachable
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden float @b2Perimeter(<2 x float>, <2 x float>) #3

; Function Attrs: nounwind uwtable
declare hidden zeroext i1 @b2IsAllocated(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 uwtable "min-legal-vector-width"="64" "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 = { inlinehint nounwind uwtable "min-legal-vector-width"="64" "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 #4 = { 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, !11, i64 8}
!10 = !{!"b2DynamicTree", !6, i64 0, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !11, i64 64}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"float", !7, i64 0}
!15 = !{!11, !11, i64 0}
!16 = !{!10, !11, i64 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"

%struct.b2TreeNode = type { %struct.b2AABB, i64, %union.anon, %union.anon.0, i16, i16 }
%struct.b2AABB = type { %struct.b2Vec2, %struct.b2Vec2 }
%struct.b2Vec2 = type { float, float }
%union.anon = type { i64 }
%union.anon.0 = type { i32 }

; Function Attrs: nounwind uwtable
declare hidden zeroext i1 @b2IsLeaf(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local float @b2DynamicTree_GetAreaRatio(ptr nocapture noundef readonly %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = icmp eq i32 %3, -1
  br i1 %4, label %36, label %5

5:                                                ; preds = %1
  %6 = load ptr, ptr %0, align 8, !tbaa !11
  %7 = sext i32 %3 to i64
  %8 = getelementptr inbounds %struct.b2TreeNode, ptr %6, i64 %7
  %9 = load <2 x float>, ptr %8, align 8
  %10 = getelementptr inbounds i8, ptr %8, i64 8
  %11 = load <2 x float>, ptr %10, align 8
  %12 = tail call float @b2Perimeter(<2 x float> %9, <2 x float> %11)
  %13 = getelementptr inbounds i8, ptr %0, i64 16
  %14 = load i32, ptr %13, align 8, !tbaa !12
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %32, %5
  %.018.lcssa = phi float [ 0.000000e+00, %5 ], [ %.1, %32 ]
  %16 = fdiv float %.018.lcssa, %12
  br label %36

.lr.ph:                                           ; preds = %5, %32
  %indvars.iv = phi i64 [ %indvars.iv.next, %32 ], [ 0, %5 ]
  %.01820 = phi float [ %.1, %32 ], [ 0.000000e+00, %5 ]
  %17 = load ptr, ptr %0, align 8, !tbaa !11
  %18 = getelementptr inbounds %struct.b2TreeNode, ptr %17, i64 %indvars.iv
  %19 = tail call zeroext i1 @b2IsAllocated(ptr noundef %18)
  br i1 %19, label %20, label %32

20:                                               ; preds = %.lr.ph
  %21 = tail call zeroext i1 @b2IsLeaf(ptr noundef %18)
  br i1 %21, label %32, label %22

22:                                               ; preds = %20
  %23 = load i32, ptr %2, align 8, !tbaa !5
  %24 = zext i32 %23 to i64
  %25 = icmp eq i64 %indvars.iv, %24
  br i1 %25, label %32, label %26

26:                                               ; preds = %22
  %27 = load <2 x float>, ptr %18, align 8
  %28 = getelementptr inbounds i8, ptr %18, i64 8
  %29 = load <2 x float>, ptr %28, align 8
  %30 = tail call float @b2Perimeter(<2 x float> %27, <2 x float> %29)
  %31 = fadd float %.01820, %30
  br label %32

32:                                               ; preds = %.lr.ph, %20, %22, %26
  %.1 = phi float [ %31, %26 ], [ %.01820, %22 ], [ %.01820, %20 ], [ %.01820, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %33 = load i32, ptr %13, align 8, !tbaa !12
  %34 = sext i32 %33 to i64
  %35 = icmp slt i64 %indvars.iv.next, %34
  br i1 %35, label %.lr.ph, label %._crit_edge, !llvm.loop !13

36:                                               ; preds = %1, %._crit_edge
  %.0 = phi float [ %16, %._crit_edge ], [ 0.000000e+00, %1 ]
  ret float %.0
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden float @b2Perimeter(<2 x float>, <2 x float>) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden zeroext i1 @b2IsAllocated(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" }
attributes #1 = { nounwind uwtable "min-legal-vector-width"="64" "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 = { inlinehint nounwind uwtable "min-legal-vector-width"="64" "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 8}
!6 = !{!"b2DynamicTree", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !10, i64 64}
!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 0}
!12 = !{!6, !10, i64 16}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass