; LLVM IR from jdeokkim/ferox :: src (broad_phase)

; 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.frSpatialHash_ = type { ptr, float, float, %struct.anon, ptr }
%struct.anon = type { ptr, i64, i64 }
%struct.frSpatialHashEntry_ = type { %struct.frVector2i_, %struct.anon.0 }
%struct.frVector2i_ = type { i32, i32 }
%struct.anon.0 = type { ptr, i64, i64 }
%struct.stbds_array_header = type { i64, i64, ptr, 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: nounwind uwtable
define dso_local void @frClearSpatialHash(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %5 = icmp eq ptr %4, null
  br i1 %5, label %6, label %7

6:                                                ; preds = %1
  br label %41

7:                                                ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %8

8:                                                ; preds = %38, %7
  %9 = load i32, ptr %3, align 4, !tbaa !9
  %10 = sext i32 %9 to i64
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.frSpatialHash_, ptr %11, i32 0, i32 0
  %13 = load ptr, ptr %12, align 8, !tbaa !11
  %14 = icmp ne ptr %13, null
  br i1 %14, label %15, label %24

15:                                               ; preds = %8
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.frSpatialHash_, ptr %16, i32 0, i32 0
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  %19 = getelementptr inbounds %struct.frSpatialHashEntry_, ptr %18, i64 -1
  %20 = getelementptr inbounds %struct.stbds_array_header, ptr %19, i64 -1
  %21 = getelementptr inbounds %struct.stbds_array_header, ptr %20, i32 0, i32 0
  %22 = load i64, ptr %21, align 8, !tbaa !16
  %23 = sub nsw i64 %22, 1
  br label %25

24:                                               ; preds = %8
  br label %25

25:                                               ; preds = %24, %15
  %26 = phi i64 [ %23, %15 ], [ 0, %24 ]
  %27 = icmp slt i64 %10, %26
  br i1 %27, label %29, label %28

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

29:                                               ; preds = %25
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.frSpatialHash_, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !11
  %33 = load i32, ptr %3, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds %struct.frSpatialHashEntry_, ptr %32, i64 %34
  %36 = getelementptr inbounds %struct.frSpatialHashEntry_, ptr %35, i32 0, i32 1
  %37 = getelementptr inbounds %struct.anon.0, ptr %36, i32 0, i32 1
  store i64 0, ptr %37, align 8, !tbaa !18
  br label %38

38:                                               ; preds = %29
  %39 = load i32, ptr %3, align 4, !tbaa !9
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %3, align 4, !tbaa !9
  br label %8, !llvm.loop !21

41:                                               ; preds = %28, %6
  ret void
}

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 }

!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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"frSpatialHash_", !6, i64 0, !13, i64 8, !13, i64 12, !14, i64 16, !6, i64 40}
!13 = !{!"float", !7, i64 0}
!14 = !{!"", !6, i64 0, !15, i64 8, !15, i64 16}
!15 = !{!"long", !7, i64 0}
!16 = !{!17, !15, i64 0}
!17 = !{!"", !15, i64 0, !15, i64 8, !6, i64 16, !15, i64 24}
!18 = !{!19, !15, i64 16}
!19 = !{!"frSpatialHashEntry_", !20, i64 0, !14, i64 8}
!20 = !{!"frVector2i_", !10, i64 0, !10, i64 4}
!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"

%struct.frSpatialHashEntry_ = type { %struct.frVector2i_, %struct.anon.0 }
%struct.frVector2i_ = type { i32, i32 }
%struct.anon.0 = type { ptr, i64, i64 }

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @frClearSpatialHash(ptr noundef readonly %0) local_unnamed_addr #0 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %.thread, label %.preheader

.preheader:                                       ; preds = %1
  %3 = load ptr, ptr %0, align 8, !tbaa !5
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %4 = getelementptr inbounds i8, ptr %3, i64 -64
  %5 = load i64, ptr %4, align 8, !tbaa !13
  %6 = icmp sgt i64 %5, 1
  br i1 %6, label %.lr.ph12.preheader, label %.thread

.lr.ph12.preheader:                               ; preds = %.lr.ph
  %7 = add nsw i64 %5, -2
  %8 = add nsw i64 %5, -1
  %xtraiter = and i64 %8, 3
  %9 = icmp ult i64 %7, 3
  br i1 %9, label %.thread.loopexit.unr-lcssa, label %.lr.ph12.preheader.new

.lr.ph12.preheader.new:                           ; preds = %.lr.ph12.preheader
  %unroll_iter = and i64 %8, -4
  br label %.lr.ph12

.lr.ph12:                                         ; preds = %.lr.ph12, %.lr.ph12.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph12.preheader.new ], [ %indvars.iv.next.3, %.lr.ph12 ]
  %niter = phi i64 [ 0, %.lr.ph12.preheader.new ], [ %niter.next.3, %.lr.ph12 ]
  %10 = getelementptr inbounds %struct.frSpatialHashEntry_, ptr %3, i64 %indvars.iv, i32 1, i32 1
  store i64 0, ptr %10, align 8, !tbaa !15
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %11 = getelementptr inbounds %struct.frSpatialHashEntry_, ptr %3, i64 %indvars.iv.next, i32 1, i32 1
  store i64 0, ptr %11, align 8, !tbaa !15
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %12 = getelementptr inbounds %struct.frSpatialHashEntry_, ptr %3, i64 %indvars.iv.next.1, i32 1, i32 1
  store i64 0, ptr %12, align 8, !tbaa !15
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %13 = getelementptr inbounds %struct.frSpatialHashEntry_, ptr %3, i64 %indvars.iv.next.2, i32 1, i32 1
  store i64 0, ptr %13, align 8, !tbaa !15
  %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 %.thread.loopexit.unr-lcssa, label %.lr.ph12

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

.lr.ph12.epil:                                    ; preds = %.thread.loopexit.unr-lcssa, %.lr.ph12.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph12.epil ], [ %indvars.iv.unr, %.thread.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph12.epil ], [ 0, %.thread.loopexit.unr-lcssa ]
  %14 = getelementptr inbounds %struct.frSpatialHashEntry_, ptr %3, i64 %indvars.iv.epil, i32 1, i32 1
  store i64 0, ptr %14, align 8, !tbaa !15
  %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 %.thread, label %.lr.ph12.epil, !llvm.loop !19

.thread:                                          ; preds = %.thread.loopexit.unr-lcssa, %.lr.ph12.epil, %.preheader, %.lr.ph, %1
  ret void
}

attributes #0 = { 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, !7, i64 0}
!6 = !{!"frSpatialHash_", !7, i64 0, !10, i64 8, !10, i64 12, !11, i64 16, !7, i64 40}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"float", !8, i64 0}
!11 = !{!"", !7, i64 0, !12, i64 8, !12, i64 16}
!12 = !{!"long", !8, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"", !12, i64 0, !12, i64 8, !7, i64 16, !12, i64 24}
!15 = !{!16, !12, i64 16}
!16 = !{!"frSpatialHashEntry_", !17, i64 0, !11, i64 8}
!17 = !{!"frVector2i_", !18, i64 0, !18, i64 4}
!18 = !{!"int", !8, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.unroll.disable"}


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