; LLVM IR from mtsamis/box2d-optimized :: src/dynamics/b2_world.ll (ZN12b2TOIMinHeap11HeapifyDownEi)

; 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"

%class.b2TOIMinHeap = type { i32, i32, ptr }
%struct.b2HeapNode = type { float, i32, ptr }
%class.b2Contact = type { ptr, ptr, ptr, ptr, ptr, %struct.b2Manifold, i32, i32, float, float, float, float }
%struct.b2Manifold = type { [2 x %struct.b2ManifoldPoint], %struct.b2Vec2, %struct.b2Vec2, i32, i32 }
%struct.b2ManifoldPoint = type { %struct.b2Vec2, float, float, %union.b2ContactID }
%union.b2ContactID = type { i32 }
%struct.b2Vec2 = type { float, float }

; 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: mustprogress uwtable
define dso_local void @_ZN12b2TOIMinHeap11HeapifyDownEi(ptr noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %9 = load ptr, ptr %3, align 8
  br label %10

10:                                               ; preds = %99, %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %11 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %11, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %12 = load i32, ptr %4, align 4, !tbaa !9
  %13 = mul nsw i32 2, %12
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %15 = load i32, ptr %4, align 4, !tbaa !9
  %16 = mul nsw i32 2, %15
  %17 = add nsw i32 %16, 2
  store i32 %17, ptr %7, align 4, !tbaa !9
  %18 = load i32, ptr %6, align 4, !tbaa !9
  %19 = getelementptr inbounds %class.b2TOIMinHeap, ptr %9, i32 0, i32 0
  %20 = load i32, ptr %19, align 8, !tbaa !11
  %21 = icmp slt i32 %18, %20
  br i1 %21, label %22, label %40

22:                                               ; preds = %10
  %23 = getelementptr inbounds %class.b2TOIMinHeap, ptr %9, i32 0, i32 2
  %24 = load ptr, ptr %23, align 8, !tbaa !13
  %25 = load i32, ptr %6, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds %struct.b2HeapNode, ptr %24, i64 %26
  %28 = getelementptr inbounds %struct.b2HeapNode, ptr %27, i32 0, i32 0
  %29 = load float, ptr %28, align 8, !tbaa !14
  %30 = getelementptr inbounds %class.b2TOIMinHeap, ptr %9, i32 0, i32 2
  %31 = load ptr, ptr %30, align 8, !tbaa !13
  %32 = load i32, ptr %5, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds %struct.b2HeapNode, ptr %31, i64 %33
  %35 = getelementptr inbounds %struct.b2HeapNode, ptr %34, i32 0, i32 0
  %36 = load float, ptr %35, align 8, !tbaa !14
  %37 = fcmp olt float %29, %36
  br i1 %37, label %38, label %40

38:                                               ; preds = %22
  %39 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %39, ptr %5, align 4, !tbaa !9
  br label %40

40:                                               ; preds = %38, %22, %10
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = getelementptr inbounds %class.b2TOIMinHeap, ptr %9, i32 0, i32 0
  %43 = load i32, ptr %42, align 8, !tbaa !11
  %44 = icmp slt i32 %41, %43
  br i1 %44, label %45, label %63

45:                                               ; preds = %40
  %46 = getelementptr inbounds %class.b2TOIMinHeap, ptr %9, i32 0, i32 2
  %47 = load ptr, ptr %46, align 8, !tbaa !13
  %48 = load i32, ptr %7, align 4, !tbaa !9
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds %struct.b2HeapNode, ptr %47, i64 %49
  %51 = getelementptr inbounds %struct.b2HeapNode, ptr %50, i32 0, i32 0
  %52 = load float, ptr %51, align 8, !tbaa !14
  %53 = getelementptr inbounds %class.b2TOIMinHeap, ptr %9, i32 0, i32 2
  %54 = load ptr, ptr %53, align 8, !tbaa !13
  %55 = load i32, ptr %5, align 4, !tbaa !9
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds %struct.b2HeapNode, ptr %54, i64 %56
  %58 = getelementptr inbounds %struct.b2HeapNode, ptr %57, i32 0, i32 0
  %59 = load float, ptr %58, align 8, !tbaa !14
  %60 = fcmp olt float %52, %59
  br i1 %60, label %61, label %63

61:                                               ; preds = %45
  %62 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %62, ptr %5, align 4, !tbaa !9
  br label %63

63:                                               ; preds = %61, %45, %40
  %64 = load i32, ptr %5, align 4, !tbaa !9
  %65 = load i32, ptr %4, align 4, !tbaa !9
  %66 = icmp ne i32 %64, %65
  br i1 %66, label %67, label %95

67:                                               ; preds = %63
  %68 = getelementptr inbounds %class.b2TOIMinHeap, ptr %9, i32 0, i32 2
  %69 = load ptr, ptr %68, align 8, !tbaa !13
  %70 = load i32, ptr %4, align 4, !tbaa !9
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds %struct.b2HeapNode, ptr %69, i64 %71
  %73 = getelementptr inbounds %struct.b2HeapNode, ptr %72, i32 0, i32 2
  %74 = load ptr, ptr %73, align 8, !tbaa !17
  %75 = getelementptr inbounds %class.b2Contact, ptr %74, i32 0, i32 7
  %76 = getelementptr inbounds %class.b2TOIMinHeap, ptr %9, i32 0, i32 2
  %77 = load ptr, ptr %76, align 8, !tbaa !13
  %78 = load i32, ptr %5, align 4, !tbaa !9
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds %struct.b2HeapNode, ptr %77, i64 %79
  %81 = getelementptr inbounds %struct.b2HeapNode, ptr %80, i32 0, i32 2
  %82 = load ptr, ptr %81, align 8, !tbaa !17
  %83 = getelementptr inbounds %class.b2Contact, ptr %82, i32 0, i32 7
  call void @_Z6b2SwapIiEvRT_S1_(ptr noundef nonnull align 4 dereferenceable(4) %75, ptr noundef nonnull align 4 dereferenceable(4) %83)
  %84 = getelementptr inbounds %class.b2TOIMinHeap, ptr %9, i32 0, i32 2
  %85 = load ptr, ptr %84, align 8, !tbaa !13
  %86 = load i32, ptr %4, align 4, !tbaa !9
  %87 = sext i32 %86 to i64
  %88 = getelementptr inbounds %struct.b2HeapNode, ptr %85, i64 %87
  %89 = getelementptr inbounds %class.b2TOIMinHeap, ptr %9, i32 0, i32 2
  %90 = load ptr, ptr %89, align 8, !tbaa !13
  %91 = load i32, ptr %5, align 4, !tbaa !9
  %92 = sext i32 %91 to i64
  %93 = getelementptr inbounds %struct.b2HeapNode, ptr %90, i64 %92
  call void @_Z6b2SwapI10b2HeapNodeEvRT_S2_(ptr noundef nonnull align 8 dereferenceable(16) %88, ptr noundef nonnull align 8 dereferenceable(16) %93)
  %94 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %94, ptr %4, align 4, !tbaa !9
  br label %96

95:                                               ; preds = %63
  store i32 3, ptr %8, align 4
  br label %97

96:                                               ; preds = %67
  store i32 0, ptr %8, align 4
  br label %97

97:                                               ; preds = %96, %95
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  %98 = load i32, ptr %8, align 4
  switch i32 %98, label %101 [
    i32 0, label %99
    i32 3, label %100
  ]

99:                                               ; preds = %97
  br label %10, !llvm.loop !18

100:                                              ; preds = %97
  ret void

101:                                              ; preds = %97
  unreachable
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z6b2SwapIiEvRT_S1_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z6b2SwapI10b2HeapNodeEvRT_S2_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { inlinehint 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" }
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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTS12b2TOIMinHeap", !10, i64 0, !10, i64 4, !6, i64 8}
!13 = !{!12, !6, i64 8}
!14 = !{!15, !16, i64 0}
!15 = !{!"_ZTS10b2HeapNode", !16, i64 0, !10, i64 4, !6, i64 8}
!16 = !{!"float", !7, i64 0}
!17 = !{!15, !6, i64 8}
!18 = distinct !{!18, !19}
!19 = !{!"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.b2HeapNode = type { float, i32, ptr }

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN12b2TOIMinHeap11HeapifyDownEi(ptr nocapture noundef nonnull readonly align 8 dereferenceable(16) %0, i32 noundef %1) local_unnamed_addr #0 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  br label %4

4:                                                ; preds = %33, %2
  %.019 = phi i32 [ %1, %2 ], [ %.121, %33 ]
  %5 = shl nsw i32 %.019, 1
  %6 = or disjoint i32 %5, 1
  %7 = add nsw i32 %5, 2
  %8 = load i32, ptr %0, align 8, !tbaa !5
  %9 = icmp slt i32 %6, %8
  br i1 %9, label %10, label %20

10:                                               ; preds = %4
  %11 = load ptr, ptr %3, align 8, !tbaa !11
  %12 = sext i32 %6 to i64
  %13 = getelementptr inbounds %struct.b2HeapNode, ptr %11, i64 %12
  %14 = load float, ptr %13, align 8, !tbaa !12
  %15 = sext i32 %.019 to i64
  %16 = getelementptr inbounds %struct.b2HeapNode, ptr %11, i64 %15
  %17 = load float, ptr %16, align 8, !tbaa !12
  %18 = fcmp olt float %14, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %10
  br label %20

20:                                               ; preds = %19, %10, %4
  %.020 = phi i32 [ %6, %19 ], [ %.019, %10 ], [ %.019, %4 ]
  %21 = icmp slt i32 %7, %8
  br i1 %21, label %22, label %32

22:                                               ; preds = %20
  %23 = load ptr, ptr %3, align 8, !tbaa !11
  %24 = sext i32 %7 to i64
  %25 = getelementptr inbounds %struct.b2HeapNode, ptr %23, i64 %24
  %26 = load float, ptr %25, align 8, !tbaa !12
  %27 = sext i32 %.020 to i64
  %28 = getelementptr inbounds %struct.b2HeapNode, ptr %23, i64 %27
  %29 = load float, ptr %28, align 8, !tbaa !12
  %30 = fcmp olt float %26, %29
  br i1 %30, label %31, label %32

31:                                               ; preds = %22
  br label %32

32:                                               ; preds = %31, %22, %20
  %.121 = phi i32 [ %7, %31 ], [ %.020, %22 ], [ %.020, %20 ]
  %.not = icmp eq i32 %.121, %.019
  br i1 %.not, label %46, label %33

33:                                               ; preds = %32
  %34 = load ptr, ptr %3, align 8, !tbaa !11
  %35 = sext i32 %.019 to i64
  %36 = getelementptr inbounds %struct.b2HeapNode, ptr %34, i64 %35, i32 2
  %37 = load ptr, ptr %36, align 8, !tbaa !15
  %38 = getelementptr inbounds i8, ptr %37, i64 108
  %39 = sext i32 %.121 to i64
  %40 = getelementptr inbounds %struct.b2HeapNode, ptr %34, i64 %39, i32 2
  %41 = load ptr, ptr %40, align 8, !tbaa !15
  %42 = getelementptr inbounds i8, ptr %41, i64 108
  tail call void @_Z6b2SwapIiEvRT_S1_(ptr noundef nonnull align 4 dereferenceable(4) %38, ptr noundef nonnull align 4 dereferenceable(4) %42)
  %43 = load ptr, ptr %3, align 8, !tbaa !11
  %44 = getelementptr inbounds %struct.b2HeapNode, ptr %43, i64 %35
  %45 = getelementptr inbounds %struct.b2HeapNode, ptr %43, i64 %39
  tail call void @_Z6b2SwapI10b2HeapNodeEvRT_S2_(ptr noundef nonnull align 8 dereferenceable(16) %44, ptr noundef nonnull align 8 dereferenceable(16) %45)
  br label %4

46:                                               ; preds = %32
  ret void
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z6b2SwapIiEvRT_S1_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z6b2SwapI10b2HeapNodeEvRT_S2_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #1

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" }
attributes #1 = { inlinehint 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, !7, i64 0}
!6 = !{!"_ZTS12b2TOIMinHeap", !7, i64 0, !7, i64 4, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!13, !14, i64 0}
!13 = !{!"_ZTS10b2HeapNode", !14, i64 0, !7, i64 4, !10, i64 8}
!14 = !{!"float", !8, i64 0}
!15 = !{!13, !10, i64 8}


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