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

; 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
declare dso_local void @_ZN12b2TOIMinHeap11HeapifyDownEi(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN12b2TOIMinHeap5BuildEv(ptr noundef nonnull align 8 dereferenceable(16) %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %5 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %6 = getelementptr inbounds %class.b2TOIMinHeap, ptr %5, i32 0, i32 0
  %7 = load i32, ptr %6, align 8, !tbaa !9
  %8 = sdiv i32 %7, 2
  %9 = sub nsw i32 %8, 1
  store i32 %9, ptr %3, align 4, !tbaa !12
  br label %10

10:                                               ; preds = %16, %1
  %11 = load i32, ptr %3, align 4, !tbaa !12
  %12 = icmp sge i32 %11, 0
  br i1 %12, label %14, label %13

13:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %19

14:                                               ; preds = %10
  %15 = load i32, ptr %3, align 4, !tbaa !12
  call void @_ZN12b2TOIMinHeap11HeapifyDownEi(ptr noundef nonnull align 8 dereferenceable(16) %5, i32 noundef %15)
  br label %16

16:                                               ; preds = %14
  %17 = load i32, ptr %3, align 4, !tbaa !12
  %18 = add nsw i32 %17, -1
  store i32 %18, ptr %3, align 4, !tbaa !12
  br label %10, !llvm.loop !13

19:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !12
  br label %20

20:                                               ; preds = %36, %19
  %21 = load i32, ptr %4, align 4, !tbaa !12
  %22 = getelementptr inbounds %class.b2TOIMinHeap, ptr %5, i32 0, i32 0
  %23 = load i32, ptr %22, align 8, !tbaa !9
  %24 = icmp slt i32 %21, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %39

26:                                               ; preds = %20
  %27 = load i32, ptr %4, align 4, !tbaa !12
  %28 = getelementptr inbounds %class.b2TOIMinHeap, ptr %5, i32 0, i32 2
  %29 = load ptr, ptr %28, align 8, !tbaa !15
  %30 = load i32, ptr %4, align 4, !tbaa !12
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds %struct.b2HeapNode, ptr %29, i64 %31
  %33 = getelementptr inbounds %struct.b2HeapNode, ptr %32, i32 0, i32 2
  %34 = load ptr, ptr %33, align 8, !tbaa !16
  %35 = getelementptr inbounds %class.b2Contact, ptr %34, i32 0, i32 7
  store i32 %27, ptr %35, align 4, !tbaa !19
  br label %36

36:                                               ; preds = %26
  %37 = load i32, ptr %4, align 4, !tbaa !12
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %4, align 4, !tbaa !12
  br label %20, !llvm.loop !24

39:                                               ; preds = %25
  ret void
}

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 = { 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, !11, i64 0}
!10 = !{!"_ZTS12b2TOIMinHeap", !11, i64 0, !11, i64 4, !6, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!10, !6, i64 8}
!16 = !{!17, !6, i64 8}
!17 = !{!"_ZTS10b2HeapNode", !18, i64 0, !11, i64 4, !6, i64 8}
!18 = !{!"float", !7, i64 0}
!19 = !{!20, !11, i64 108}
!20 = !{!"_ZTS9b2Contact", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !21, i64 40, !11, i64 104, !11, i64 108, !18, i64 112, !18, i64 116, !18, i64 120, !18, i64 124}
!21 = !{!"_ZTS10b2Manifold", !7, i64 0, !22, i64 40, !22, i64 48, !23, i64 56, !11, i64 60}
!22 = !{!"_ZTS6b2Vec2", !18, i64 0, !18, i64 4}
!23 = !{!"_ZTSN10b2Manifold4TypeE", !7, i64 0}
!24 = distinct !{!24, !14}


; 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 uwtable
declare dso_local void @_ZN12b2TOIMinHeap11HeapifyDownEi(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN12b2TOIMinHeap5BuildEv(ptr noundef nonnull align 8 dereferenceable(16) %0) local_unnamed_addr #0 align 2 {
  %2 = load i32, ptr %0, align 8, !tbaa !5
  %3 = icmp sgt i32 %2, 1
  br i1 %3, label %.lr.ph.preheader, label %.preheader

.lr.ph.preheader:                                 ; preds = %1
  %4 = lshr i32 %2, 1
  br label %.lr.ph

.preheader.loopexit:                              ; preds = %.lr.ph
  %.pre = load i32, ptr %0, align 8, !tbaa !5
  br label %.preheader

.preheader:                                       ; preds = %.preheader.loopexit, %1
  %5 = phi i32 [ %.pre, %.preheader.loopexit ], [ %2, %1 ]
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph10, label %._crit_edge

.lr.ph10:                                         ; preds = %.preheader
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !11
  %wide.trip.count = zext nneg i32 %5 to i64
  %xtraiter = and i64 %wide.trip.count, 3
  %9 = icmp ult i32 %5, 4
  br i1 %9, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph10.new

.lr.ph10.new:                                     ; preds = %.lr.ph10
  %unroll_iter = and i64 %wide.trip.count, 2147483644
  br label %15

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.07.in8 = phi i32 [ %.07, %.lr.ph ], [ %4, %.lr.ph.preheader ]
  %.07 = add nsw i32 %.07.in8, -1
  tail call void @_ZN12b2TOIMinHeap11HeapifyDownEi(ptr noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %.07)
  %10 = icmp ugt i32 %.07.in8, 1
  br i1 %10, label %.lr.ph, label %.preheader.loopexit, !llvm.loop !12

._crit_edge.loopexit.unr-lcssa:                   ; preds = %15, %.lr.ph10
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph10 ], [ %indvars.iv.next.3, %15 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.loopexit.unr-lcssa, %.epil.preheader
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.epil.preheader ], [ %indvars.iv.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %11 = getelementptr inbounds %struct.b2HeapNode, ptr %8, i64 %indvars.iv.epil, i32 2
  %12 = load ptr, ptr %11, align 8, !tbaa !14
  %13 = getelementptr inbounds i8, ptr %12, i64 108
  %14 = trunc nuw nsw i64 %indvars.iv.epil to i32
  store i32 %14, ptr %13, align 4, !tbaa !17
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.epil.preheader, !llvm.loop !22

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.epil.preheader, %.preheader
  ret void

15:                                               ; preds = %15, %.lr.ph10.new
  %indvars.iv = phi i64 [ 0, %.lr.ph10.new ], [ %indvars.iv.next.3, %15 ]
  %niter = phi i64 [ 0, %.lr.ph10.new ], [ %niter.next.3, %15 ]
  %16 = getelementptr inbounds %struct.b2HeapNode, ptr %8, i64 %indvars.iv, i32 2
  %17 = load ptr, ptr %16, align 8, !tbaa !14
  %18 = getelementptr inbounds i8, ptr %17, i64 108
  %19 = trunc nuw nsw i64 %indvars.iv to i32
  store i32 %19, ptr %18, align 4, !tbaa !17
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %20 = getelementptr inbounds %struct.b2HeapNode, ptr %8, i64 %indvars.iv.next, i32 2
  %21 = load ptr, ptr %20, align 8, !tbaa !14
  %22 = getelementptr inbounds i8, ptr %21, i64 108
  %23 = trunc nuw nsw i64 %indvars.iv.next to i32
  store i32 %23, ptr %22, align 4, !tbaa !17
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %24 = getelementptr inbounds %struct.b2HeapNode, ptr %8, i64 %indvars.iv.next.1, i32 2
  %25 = load ptr, ptr %24, align 8, !tbaa !14
  %26 = getelementptr inbounds i8, ptr %25, i64 108
  %27 = trunc nuw nsw i64 %indvars.iv.next.1 to i32
  store i32 %27, ptr %26, align 4, !tbaa !17
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %28 = getelementptr inbounds %struct.b2HeapNode, ptr %8, i64 %indvars.iv.next.2, i32 2
  %29 = load ptr, ptr %28, align 8, !tbaa !14
  %30 = getelementptr inbounds i8, ptr %29, i64 108
  %31 = trunc nuw nsw i64 %indvars.iv.next.2 to i32
  store i32 %31, ptr %30, align 4, !tbaa !17
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %15, !llvm.loop !24
}

attributes #0 = { 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" }

!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 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !10, i64 8}
!15 = !{!"_ZTS10b2HeapNode", !16, i64 0, !7, i64 4, !10, i64 8}
!16 = !{!"float", !8, i64 0}
!17 = !{!18, !7, i64 108}
!18 = !{!"_ZTS9b2Contact", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !19, i64 40, !7, i64 104, !7, i64 108, !16, i64 112, !16, i64 116, !16, i64 120, !16, i64 124}
!19 = !{!"_ZTS10b2Manifold", !8, i64 0, !20, i64 40, !20, i64 48, !21, i64 56, !7, i64 60}
!20 = !{!"_ZTS6b2Vec2", !16, i64 0, !16, i64 4}
!21 = !{!"_ZTSN10b2Manifold4TypeE", !8, i64 0}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.unroll.disable"}
!24 = distinct !{!24, !13}


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