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

; 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.b2ContactSolver = type <{ ptr, ptr, ptr, ptr, ptr, i32, [4 x i8] }>
%struct.b2ContactVelocityConstraint = type { [2 x %struct.b2VelocityConstraintPoint], %struct.b2Vec2, %struct.b2Mat22, %struct.b2Mat22, i32, i32, float, float, float, float, float, float, float, float, i32, ptr }
%struct.b2VelocityConstraintPoint = type { %struct.b2Vec2, %struct.b2Vec2, float, float, float, float, float }
%struct.b2Vec2 = type { float, float }
%struct.b2Mat22 = type { %struct.b2Vec2, %struct.b2Vec2 }
%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 }

; 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 nounwind uwtable
define dso_local void @_ZN15b2ContactSolver13StoreImpulsesEv(ptr noundef nonnull align 8 dereferenceable(44) %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %8 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %62, %1
  %10 = load i32, ptr %3, align 4, !tbaa !9
  %11 = getelementptr inbounds %class.b2ContactSolver, ptr %8, i32 0, i32 5
  %12 = load i32, ptr %11, align 8, !tbaa !11
  %13 = icmp slt i32 %10, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %9
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %65

15:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %16 = getelementptr inbounds %class.b2ContactSolver, ptr %8, i32 0, i32 4
  %17 = load ptr, ptr %16, align 8, !tbaa !13
  %18 = load i32, ptr %3, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds %struct.b2ContactVelocityConstraint, ptr %17, i64 %19
  store ptr %20, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.b2ContactVelocityConstraint, ptr %21, i32 0, i32 15
  %23 = load ptr, ptr %22, align 8, !tbaa !14
  store ptr %23, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %58, %15
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.b2ContactVelocityConstraint, ptr %26, i32 0, i32 14
  %28 = load i32, ptr %27, align 8, !tbaa !19
  %29 = icmp slt i32 %25, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %24
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %61

31:                                               ; preds = %24
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.b2ContactVelocityConstraint, ptr %32, i32 0, i32 0
  %34 = load i32, ptr %7, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [2 x %struct.b2VelocityConstraintPoint], ptr %33, i64 0, i64 %35
  %37 = getelementptr inbounds %struct.b2VelocityConstraintPoint, ptr %36, i32 0, i32 2
  %38 = load float, ptr %37, align 4, !tbaa !20
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.b2Manifold, ptr %39, i32 0, i32 0
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [2 x %struct.b2ManifoldPoint], ptr %40, i64 0, i64 %42
  %44 = getelementptr inbounds %struct.b2ManifoldPoint, ptr %43, i32 0, i32 1
  store float %38, ptr %44, align 4, !tbaa !22
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.b2ContactVelocityConstraint, ptr %45, i32 0, i32 0
  %47 = load i32, ptr %7, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [2 x %struct.b2VelocityConstraintPoint], ptr %46, i64 0, i64 %48
  %50 = getelementptr inbounds %struct.b2VelocityConstraintPoint, ptr %49, i32 0, i32 4
  %51 = load float, ptr %50, align 4, !tbaa !24
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.b2Manifold, ptr %52, i32 0, i32 0
  %54 = load i32, ptr %7, align 4, !tbaa !9
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [2 x %struct.b2ManifoldPoint], ptr %53, i64 0, i64 %55
  %57 = getelementptr inbounds %struct.b2ManifoldPoint, ptr %56, i32 0, i32 2
  store float %51, ptr %57, align 4, !tbaa !25
  br label %58

58:                                               ; preds = %31
  %59 = load i32, ptr %7, align 4, !tbaa !9
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %7, align 4, !tbaa !9
  br label %24, !llvm.loop !26

61:                                               ; preds = %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %3, align 4, !tbaa !9
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %3, align 4, !tbaa !9
  br label %9, !llvm.loop !28

65:                                               ; preds = %14
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 40}
!12 = !{!"_ZTS15b2ContactSolver", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !10, i64 40}
!13 = !{!12, !6, i64 32}
!14 = !{!15, !6, i64 160}
!15 = !{!"_ZTS27b2ContactVelocityConstraint", !7, i64 0, !16, i64 72, !18, i64 80, !18, i64 96, !10, i64 112, !10, i64 116, !17, i64 120, !17, i64 124, !17, i64 128, !17, i64 132, !17, i64 136, !17, i64 140, !17, i64 144, !17, i64 148, !10, i64 152, !6, i64 160}
!16 = !{!"_ZTS6b2Vec2", !17, i64 0, !17, i64 4}
!17 = !{!"float", !7, i64 0}
!18 = !{!"_ZTS7b2Mat22", !16, i64 0, !16, i64 8}
!19 = !{!15, !10, i64 152}
!20 = !{!21, !17, i64 16}
!21 = !{!"_ZTS25b2VelocityConstraintPoint", !16, i64 0, !16, i64 8, !17, i64 16, !17, i64 20, !17, i64 24, !17, i64 28, !17, i64 32}
!22 = !{!23, !17, i64 8}
!23 = !{!"_ZTS15b2ManifoldPoint", !16, i64 0, !17, i64 8, !17, i64 12, !7, i64 16}
!24 = !{!21, !17, i64 24}
!25 = !{!23, !17, i64 12}
!26 = distinct !{!26, !27}
!27 = !{!"llvm.loop.mustprogress"}
!28 = distinct !{!28, !27}


; 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.b2ContactVelocityConstraint = type { [2 x %struct.b2VelocityConstraintPoint], %struct.b2Vec2, %struct.b2Mat22, %struct.b2Mat22, i32, i32, float, float, float, float, float, float, float, float, i32, ptr }
%struct.b2VelocityConstraintPoint = type { %struct.b2Vec2, %struct.b2Vec2, float, float, float, float, float }
%struct.b2Vec2 = type { float, float }
%struct.b2Mat22 = type { %struct.b2Vec2, %struct.b2Vec2 }
%struct.b2ManifoldPoint = type { %struct.b2Vec2, float, float, %union.b2ContactID }
%union.b2ContactID = type { i32 }

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_ZN15b2ContactSolver13StoreImpulsesEv(ptr nocapture noundef nonnull readonly align 8 dereferenceable(44) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 40
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph19, label %._crit_edge20

.lr.ph19:                                         ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 32
  %6 = load ptr, ptr %5, align 8, !tbaa !11
  %wide.trip.count25 = zext nneg i32 %3 to i64
  br label %7

._crit_edge20:                                    ; preds = %._crit_edge, %1
  ret void

7:                                                ; preds = %.lr.ph19, %._crit_edge
  %indvars.iv22 = phi i64 [ 0, %.lr.ph19 ], [ %indvars.iv.next23, %._crit_edge ]
  %8 = getelementptr inbounds %struct.b2ContactVelocityConstraint, ptr %6, i64 %indvars.iv22
  %9 = getelementptr inbounds i8, ptr %8, i64 160
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  %11 = getelementptr inbounds i8, ptr %8, i64 152
  %12 = load i32, ptr %11, align 8, !tbaa !17
  %13 = icmp sgt i32 %12, 0
  br i1 %13, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %7
  %wide.trip.count = zext nneg i32 %12 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %14 = icmp eq i32 %12, 1
  br i1 %14, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %.lr.ph

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

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %15 = getelementptr inbounds [2 x %struct.b2VelocityConstraintPoint], ptr %8, i64 0, i64 %indvars.iv.unr
  %16 = getelementptr inbounds i8, ptr %15, i64 16
  %17 = load float, ptr %16, align 4, !tbaa !18
  %18 = getelementptr inbounds [2 x %struct.b2ManifoldPoint], ptr %10, i64 0, i64 %indvars.iv.unr
  %19 = getelementptr inbounds i8, ptr %18, i64 8
  store float %17, ptr %19, align 4, !tbaa !20
  %20 = getelementptr inbounds i8, ptr %15, i64 24
  %21 = load float, ptr %20, align 4, !tbaa !22
  %22 = getelementptr inbounds i8, ptr %18, i64 12
  store float %21, ptr %22, align 4, !tbaa !23
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %7
  %indvars.iv.next23 = add nuw nsw i64 %indvars.iv22, 1
  %exitcond26.not = icmp eq i64 %indvars.iv.next23, %wide.trip.count25
  br i1 %exitcond26.not, label %._crit_edge20, label %7, !llvm.loop !24

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %23 = getelementptr inbounds [2 x %struct.b2VelocityConstraintPoint], ptr %8, i64 0, i64 %indvars.iv
  %24 = getelementptr inbounds i8, ptr %23, i64 16
  %25 = load float, ptr %24, align 4, !tbaa !18
  %26 = getelementptr inbounds [2 x %struct.b2ManifoldPoint], ptr %10, i64 0, i64 %indvars.iv
  %27 = getelementptr inbounds i8, ptr %26, i64 8
  store float %25, ptr %27, align 4, !tbaa !20
  %28 = getelementptr inbounds i8, ptr %23, i64 24
  %29 = load float, ptr %28, align 4, !tbaa !22
  %30 = getelementptr inbounds i8, ptr %26, i64 12
  store float %29, ptr %30, align 4, !tbaa !23
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %31 = getelementptr inbounds [2 x %struct.b2VelocityConstraintPoint], ptr %8, i64 0, i64 %indvars.iv.next
  %32 = getelementptr inbounds i8, ptr %31, i64 16
  %33 = load float, ptr %32, align 4, !tbaa !18
  %34 = getelementptr inbounds [2 x %struct.b2ManifoldPoint], ptr %10, i64 0, i64 %indvars.iv.next
  %35 = getelementptr inbounds i8, ptr %34, i64 8
  store float %33, ptr %35, align 4, !tbaa !20
  %36 = getelementptr inbounds i8, ptr %31, i64 24
  %37 = load float, ptr %36, align 4, !tbaa !22
  %38 = getelementptr inbounds i8, ptr %34, i64 12
  store float %37, ptr %38, align 4, !tbaa !23
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !26
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 40}
!6 = !{!"_ZTS15b2ContactSolver", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !10, i64 40}
!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 32}
!12 = !{!13, !7, i64 160}
!13 = !{!"_ZTS27b2ContactVelocityConstraint", !8, i64 0, !14, i64 72, !16, i64 80, !16, i64 96, !10, i64 112, !10, i64 116, !15, i64 120, !15, i64 124, !15, i64 128, !15, i64 132, !15, i64 136, !15, i64 140, !15, i64 144, !15, i64 148, !10, i64 152, !7, i64 160}
!14 = !{!"_ZTS6b2Vec2", !15, i64 0, !15, i64 4}
!15 = !{!"float", !8, i64 0}
!16 = !{!"_ZTS7b2Mat22", !14, i64 0, !14, i64 8}
!17 = !{!13, !10, i64 152}
!18 = !{!19, !15, i64 16}
!19 = !{!"_ZTS25b2VelocityConstraintPoint", !14, i64 0, !14, i64 8, !15, i64 16, !15, i64 20, !15, i64 24, !15, i64 28, !15, i64 32}
!20 = !{!21, !15, i64 8}
!21 = !{!"_ZTS15b2ManifoldPoint", !14, i64 0, !15, i64 8, !15, i64 12, !8, i64 16}
!22 = !{!19, !15, i64 24}
!23 = !{!21, !15, i64 12}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = distinct !{!26, !25}


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