; LLVM IR from ashishzero/karma :: src/broad_phase.ll (Z20hgrid_test_collisionP5HGridP10Rigid_BodyS2_)

; 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.Rigid_Body = type { i16, i16, float, float, float, float, %union.Vec2, %union.Vec2, %struct.Transform, i32, ptr, %struct.Mm_Rect, %struct.Entity_Id, ptr, i32, i32 }
%union.Vec2 = type { %struct.anon.1 }
%struct.anon.1 = type { float, float }
%struct.Transform = type { %union.Vec2, %union.Mat2 }
%union.Mat2 = type { [2 x %union.Vec2] }
%struct.Mm_Rect = type { %union.Vec2, %union.Vec2 }
%struct.Entity_Id = type { i64 }

; 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 noundef zeroext i1 @_Z20hgrid_test_collisionP5HGridP10Rigid_BodyS2_(ptr noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  store ptr null, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.Rigid_Body, ptr %15, i32 0, i32 15
  %17 = load i32, ptr %16, align 4, !tbaa !9
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.Rigid_Body, ptr %18, i32 0, i32 15
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = icmp eq i32 %17, %20
  br i1 %21, label %22, label %32

22:                                               ; preds = %3
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.Rigid_Body, ptr %23, i32 0, i32 14
  %25 = load i32, ptr %24, align 8, !tbaa !19
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.Rigid_Body, ptr %26, i32 0, i32 14
  %28 = load i32, ptr %27, align 8, !tbaa !19
  %29 = icmp ne i32 %25, %28
  br i1 %29, label %30, label %31

30:                                               ; preds = %22
  store i1 false, ptr %4, align 1
  store i32 1, ptr %13, align 4
  br label %100

31:                                               ; preds = %22
  store i1 true, ptr %4, align 1
  store i32 1, ptr %13, align 4
  br label %100

32:                                               ; preds = %3
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.Rigid_Body, ptr %33, i32 0, i32 15
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.Rigid_Body, ptr %36, i32 0, i32 15
  %38 = load i32, ptr %37, align 4, !tbaa !9
  %39 = icmp ult i32 %35, %38
  br i1 %39, label %40, label %51

40:                                               ; preds = %32
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.Rigid_Body, ptr %41, i32 0, i32 15
  %43 = load i32, ptr %42, align 4, !tbaa !9
  store i32 %43, ptr %8, align 4, !tbaa !20
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %44, ptr %10, align 8, !tbaa !5
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.Rigid_Body, ptr %45, i32 0, i32 14
  %47 = load i32, ptr %46, align 8, !tbaa !19
  store i32 %47, ptr %11, align 4, !tbaa !20
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.Rigid_Body, ptr %48, i32 0, i32 15
  %50 = load i32, ptr %49, align 4, !tbaa !9
  store i32 %50, ptr %9, align 4, !tbaa !20
  br label %62

51:                                               ; preds = %32
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.Rigid_Body, ptr %52, i32 0, i32 15
  %54 = load i32, ptr %53, align 4, !tbaa !9
  store i32 %54, ptr %8, align 4, !tbaa !20
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %55, ptr %10, align 8, !tbaa !5
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.Rigid_Body, ptr %56, i32 0, i32 14
  %58 = load i32, ptr %57, align 8, !tbaa !19
  store i32 %58, ptr %11, align 4, !tbaa !20
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.Rigid_Body, ptr %59, i32 0, i32 15
  %61 = load i32, ptr %60, align 4, !tbaa !9
  store i32 %61, ptr %9, align 4, !tbaa !20
  br label %62

62:                                               ; preds = %51, %40
  %63 = load i32, ptr %11, align 4, !tbaa !20
  store i32 %63, ptr %12, align 4, !tbaa !20
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #2
  %64 = load i32, ptr %8, align 4, !tbaa !20
  store i32 %64, ptr %14, align 4, !tbaa !20
  br label %65

65:                                               ; preds = %94, %62
  %66 = load i32, ptr %14, align 4, !tbaa !20
  %67 = load i32, ptr %9, align 4, !tbaa !20
  %68 = icmp ule i32 %66, %67
  br i1 %68, label %70, label %69

69:                                               ; preds = %65
  store i32 2, ptr %13, align 4
  br label %97

70:                                               ; preds = %65
  %71 = load ptr, ptr %10, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.Rigid_Body, ptr %71, i32 0, i32 14
  %73 = load i32, ptr %72, align 8, !tbaa !19
  %74 = load i32, ptr %11, align 4, !tbaa !20
  %75 = mul i32 4, %74
  %76 = add i32 %75, 1
  %77 = icmp uge i32 %73, %76
  br i1 %77, label %78, label %87

78:                                               ; preds = %70
  %79 = load ptr, ptr %10, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.Rigid_Body, ptr %79, i32 0, i32 14
  %81 = load i32, ptr %80, align 8, !tbaa !19
  %82 = load i32, ptr %12, align 4, !tbaa !20
  %83 = mul i32 4, %82
  %84 = add i32 %83, 4
  %85 = icmp ule i32 %81, %84
  br i1 %85, label %86, label %87

86:                                               ; preds = %78
  store i1 true, ptr %4, align 1
  store i32 1, ptr %13, align 4
  br label %97

87:                                               ; preds = %78, %70
  %88 = load i32, ptr %11, align 4, !tbaa !20
  %89 = mul i32 4, %88
  %90 = add i32 %89, 1
  store i32 %90, ptr %11, align 4, !tbaa !20
  %91 = load i32, ptr %12, align 4, !tbaa !20
  %92 = mul i32 4, %91
  %93 = add i32 %92, 4
  store i32 %93, ptr %12, align 4, !tbaa !20
  br label %94

94:                                               ; preds = %87
  %95 = load i32, ptr %14, align 4, !tbaa !20
  %96 = add i32 %95, 1
  store i32 %96, ptr %14, align 4, !tbaa !20
  br label %65, !llvm.loop !21

97:                                               ; preds = %86, %69
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  %98 = load i32, ptr %13, align 4
  switch i32 %98, label %100 [
    i32 2, label %99
  ]

99:                                               ; preds = %97
  store i1 false, ptr %4, align 1
  store i32 1, ptr %13, align 4
  br label %100

100:                                              ; preds = %99, %97, %31, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %101 = load i1, ptr %4, align 1
  ret i1 %101
}

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, !15, i64 108}
!10 = !{!"_ZTS10Rigid_Body", !11, i64 0, !12, i64 2, !13, i64 4, !13, i64 8, !13, i64 12, !13, i64 16, !7, i64 20, !7, i64 28, !14, i64 36, !15, i64 60, !6, i64 64, !16, i64 72, !17, i64 88, !6, i64 96, !15, i64 104, !15, i64 108}
!11 = !{!"_ZTS15Rigid_Body_Type", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!"float", !7, i64 0}
!14 = !{!"_ZTS9Transform", !7, i64 0, !7, i64 8}
!15 = !{!"int", !7, i64 0}
!16 = !{!"_ZTS7Mm_Rect", !7, i64 0, !7, i64 8}
!17 = !{!"_ZTS9Entity_Id", !18, i64 0}
!18 = !{!"long", !7, i64 0}
!19 = !{!10, !15, i64 104}
!20 = !{!15, !15, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_Z20hgrid_test_collisionP5HGridP10Rigid_BodyS2_(ptr nocapture noundef readnone %0, ptr nocapture noundef readonly %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %1, i64 108
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %2, i64 108
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = icmp eq i32 %5, %7
  br i1 %8, label %9, label %14

9:                                                ; preds = %3
  %10 = getelementptr inbounds i8, ptr %1, i64 104
  %11 = load i32, ptr %10, align 8, !tbaa !18
  %12 = getelementptr inbounds i8, ptr %2, i64 104
  %13 = load i32, ptr %12, align 8, !tbaa !18
  %.not39 = icmp eq i32 %11, %13
  br label %.loopexit

14:                                               ; preds = %3
  %15 = icmp ult i32 %5, %7
  %.40 = tail call i32 @llvm.umin.i32(i32 %5, i32 %7)
  %.41 = tail call i32 @llvm.umax.i32(i32 %5, i32 %7)
  %.42 = select i1 %15, ptr %2, ptr %1
  %.43 = select i1 %15, ptr %1, ptr %2
  %.028.in = getelementptr inbounds i8, ptr %.43, i64 104
  %.028 = load i32, ptr %.028.in, align 8, !tbaa !18
  %16 = getelementptr inbounds i8, ptr %.42, i64 104
  %17 = load i32, ptr %16, align 8, !tbaa !18
  br label %18

18:                                               ; preds = %14, %._crit_edge
  %.046 = phi i32 [ %.40, %14 ], [ %23, %._crit_edge ]
  %.02745 = phi i32 [ %.028, %14 ], [ %21, %._crit_edge ]
  %.144 = phi i32 [ %.028, %14 ], [ %22, %._crit_edge ]
  %19 = shl i32 %.144, 2
  %.not37.not = icmp ugt i32 %17, %19
  %20 = shl i32 %.02745, 2
  %21 = add i32 %20, 4
  %.not38 = icmp ule i32 %17, %21
  %or.cond.not = select i1 %.not37.not, i1 %.not38, i1 false
  br i1 %or.cond.not, label %.loopexit, label %._crit_edge

._crit_edge:                                      ; preds = %18
  %22 = or disjoint i32 %19, 1
  %23 = add i32 %.046, 1
  %.not.not = icmp ugt i32 %23, %.41
  br i1 %.not.not, label %.loopexit, label %18, !llvm.loop !19

.loopexit:                                        ; preds = %._crit_edge, %18, %9
  %.133 = phi i1 [ %.not39, %9 ], [ %or.cond.not, %18 ], [ %or.cond.not, %._crit_edge ]
  ret i1 %.133
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umax.i32(i32, i32) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !13, i64 108}
!6 = !{!"_ZTS10Rigid_Body", !7, i64 0, !10, i64 2, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !8, i64 20, !8, i64 28, !12, i64 36, !13, i64 60, !14, i64 64, !15, i64 72, !16, i64 88, !14, i64 96, !13, i64 104, !13, i64 108}
!7 = !{!"_ZTS15Rigid_Body_Type", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!"float", !8, i64 0}
!12 = !{!"_ZTS9Transform", !8, i64 0, !8, i64 8}
!13 = !{!"int", !8, i64 0}
!14 = !{!"any pointer", !8, i64 0}
!15 = !{!"_ZTS7Mm_Rect", !8, i64 0, !8, i64 8}
!16 = !{!"_ZTS9Entity_Id", !17, i64 0}
!17 = !{!"long", !8, i64 0}
!18 = !{!6, !13, i64 104}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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