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

; 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.HGrid = type { ptr, i32, float, i32 }
%union.Vec2 = type { %struct.anon.1 }
%struct.anon.1 = type { float, float }
%struct.Grid = type { %union.Vec2, float, i32, ptr }

; 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 @_Z15initialize_gridP4Gridjjf4Vec2(ptr noundef, i32 noundef, i32 noundef, float noundef, <2 x float>) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z12hgrid_createjf(ptr dead_on_unwind noalias writable sret(%struct.HGrid) align 8 %0, i32 noundef %1, float noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca float, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca %union.Vec2, align 4
  store i32 %1, ptr %4, align 4, !tbaa !5
  store float %2, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #6
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #6
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %22, %3
  %11 = load i32, ptr %7, align 4, !tbaa !5
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = icmp ult i32 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #6
  br label %25

15:                                               ; preds = %10
  %16 = load i32, ptr %7, align 4, !tbaa !5
  %17 = uitofp i32 %16 to float
  %18 = call float @powf(float noundef 4.000000e+00, float noundef %17) #6
  %19 = fptosi float %18 to i32
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = add i32 %20, %19
  store i32 %21, ptr %6, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %15
  %23 = load i32, ptr %7, align 4, !tbaa !5
  %24 = add i32 %23, 1
  store i32 %24, ptr %7, align 4, !tbaa !5
  br label %10, !llvm.loop !11

25:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  %26 = load i32, ptr %6, align 4, !tbaa !5
  %27 = zext i32 %26 to i64
  %28 = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %27, i64 24)
  %29 = extractvalue { i64, i1 } %28, 1
  %30 = extractvalue { i64, i1 } %28, 0
  %31 = select i1 %29, i64 -1, i64 %30
  %32 = call noalias noundef nonnull ptr @_Znam(i64 noundef %31) #7
  store ptr %32, ptr %8, align 8, !tbaa !13
  %33 = load ptr, ptr %8, align 8, !tbaa !13
  %34 = getelementptr inbounds %struct.Grid, ptr %33, i64 0
  %35 = load i32, ptr %6, align 4, !tbaa !5
  %36 = load float, ptr %5, align 4, !tbaa !9
  %37 = load float, ptr %5, align 4, !tbaa !9
  %38 = fneg float %37
  %39 = fdiv float %38, 2.000000e+00
  %40 = call <2 x float> @_Z4vec2f(float noundef %39)
  %41 = getelementptr inbounds %union.Vec2, ptr %9, i32 0, i32 0
  store <2 x float> %40, ptr %41, align 4
  %42 = getelementptr inbounds %union.Vec2, ptr %9, i32 0, i32 0
  %43 = load <2 x float>, ptr %42, align 4
  call void @_Z15initialize_gridP4Gridjjf4Vec2(ptr noundef %34, i32 noundef 0, i32 noundef %35, float noundef %36, <2 x float> %43)
  %44 = getelementptr inbounds %struct.HGrid, ptr %0, i32 0, i32 0
  %45 = load ptr, ptr %8, align 8, !tbaa !13
  store ptr %45, ptr %44, align 8, !tbaa !15
  %46 = getelementptr inbounds %struct.HGrid, ptr %0, i32 0, i32 1
  %47 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %47, ptr %46, align 8, !tbaa !17
  %48 = getelementptr inbounds %struct.HGrid, ptr %0, i32 0, i32 2
  %49 = load float, ptr %5, align 4, !tbaa !9
  store float %49, ptr %48, align 4, !tbaa !18
  %50 = getelementptr inbounds %struct.HGrid, ptr %0, i32 0, i32 3
  %51 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %51, ptr %50, align 8, !tbaa !19
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #6
  ret void
}

; Function Attrs: nounwind
declare float @powf(float noundef, float noundef) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare { i64, i1 } @llvm.umul.with.overflow.i64(i64, i64) #3

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) #4

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local <2 x float> @_Z4vec2f(float noundef) #5

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress uwtable "min-legal-vector-width"="64" "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 "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nobuiltin allocsize(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 #5 = { inlinehint mustprogress nounwind uwtable "min-legal-vector-width"="64" "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 #6 = { nounwind }
attributes #7 = { builtin allocsize(0) }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"float", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !14, i64 0}
!14 = !{!"any pointer", !7, i64 0}
!15 = !{!16, !14, i64 0}
!16 = !{!"_ZTS5HGrid", !14, i64 0, !6, i64 8, !10, i64 12, !6, i64 16}
!17 = !{!16, !6, i64 8}
!18 = !{!16, !10, i64 12}
!19 = !{!16, !6, i64 16}


; 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.HGrid = type { ptr, i32, float, i32 }

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z15initialize_gridP4Gridjjf4Vec2(ptr noundef, i32 noundef, i32 noundef, float noundef, <2 x float>) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z12hgrid_createjf(ptr dead_on_unwind noalias nocapture writable writeonly sret(%struct.HGrid) align 8 %0, i32 noundef %1, float noundef %2) local_unnamed_addr #0 {
  %.not = icmp eq i32 %1, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %xtraiter = and i32 %1, 3
  %4 = icmp ult i32 %1, 4
  br i1 %4, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i32 %1, -4
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %31, %.lr.ph ]
  %.016.unr = phi i32 [ 0, %.lr.ph.preheader ], [ %31, %.lr.ph ]
  %.01315.unr = phi i32 [ 0, %.lr.ph.preheader ], [ %32, %.lr.ph ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil
  %.016.epil = phi i32 [ %7, %.lr.ph.epil ], [ %.016.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.01315.epil = phi i32 [ %8, %.lr.ph.epil ], [ %.01315.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i32 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %5 = uitofp i32 %.01315.epil to float
  %mul.epil = fmul float %5, 2.000000e+00
  %exp2f.epil = tail call float @exp2f(float %mul.epil)
  %6 = fptosi float %exp2f.epil to i32
  %7 = add i32 %.016.epil, %6
  %8 = add nuw i32 %.01315.epil, 1
  %epil.iter.next = add i32 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i32 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph.epil, !llvm.loop !5

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil, %3
  %.0.lcssa = phi i32 [ 0, %3 ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %7, %.lr.ph.epil ]
  %9 = zext i32 %.0.lcssa to i64
  %10 = mul nuw nsw i64 %9, 24
  %11 = tail call noalias noundef nonnull ptr @_Znam(i64 noundef %10) #3
  %12 = fmul float %2, -5.000000e-01
  %13 = tail call <2 x float> @_Z4vec2f(float noundef %12)
  tail call void @_Z15initialize_gridP4Gridjjf4Vec2(ptr noundef nonnull %11, i32 noundef 0, i32 noundef %.0.lcssa, float noundef %2, <2 x float> %13)
  store ptr %11, ptr %0, align 8, !tbaa !7
  %14 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 %.0.lcssa, ptr %14, align 8, !tbaa !14
  %15 = getelementptr inbounds i8, ptr %0, i64 12
  store float %2, ptr %15, align 4, !tbaa !15
  %16 = getelementptr inbounds i8, ptr %0, i64 16
  store i32 %1, ptr %16, align 8, !tbaa !16
  ret void

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.016 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %31, %.lr.ph ]
  %.01315 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %32, %.lr.ph ]
  %niter = phi i32 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %17 = uitofp i32 %.01315 to float
  %mul = fmul float %17, 2.000000e+00
  %exp2f = tail call float @exp2f(float %mul)
  %18 = fptosi float %exp2f to i32
  %19 = add i32 %.016, %18
  %20 = or disjoint i32 %.01315, 1
  %21 = uitofp i32 %20 to float
  %mul.1 = fmul float %21, 2.000000e+00
  %exp2f.1 = tail call float @exp2f(float %mul.1)
  %22 = fptosi float %exp2f.1 to i32
  %23 = add i32 %19, %22
  %24 = or disjoint i32 %.01315, 2
  %25 = uitofp i32 %24 to float
  %mul.2 = fmul float %25, 2.000000e+00
  %exp2f.2 = tail call float @exp2f(float %mul.2)
  %26 = fptosi float %exp2f.2 to i32
  %27 = add i32 %23, %26
  %28 = or disjoint i32 %.01315, 3
  %29 = uitofp i32 %28 to float
  %mul.3 = fmul float %29, 2.000000e+00
  %exp2f.3 = tail call float @exp2f(float %mul.3)
  %30 = fptosi float %exp2f.3 to i32
  %31 = add i32 %27, %30
  %32 = add nuw i32 %.01315, 4
  %niter.next.3 = add i32 %niter, 4
  %niter.ncmp.3 = icmp eq i32 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !17
}

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local <2 x float> @_Z4vec2f(float noundef) local_unnamed_addr #2

declare float @exp2f(float) local_unnamed_addr

attributes #0 = { mustprogress uwtable "min-legal-vector-width"="64" "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 = { nobuiltin allocsize(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"="64" "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 = { builtin allocsize(0) }

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.unroll.disable"}
!7 = !{!8, !9, i64 0}
!8 = !{!"_ZTS5HGrid", !9, i64 0, !12, i64 8, !13, i64 12, !12, i64 16}
!9 = !{!"any pointer", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!"int", !10, i64 0}
!13 = !{!"float", !10, i64 0}
!14 = !{!8, !12, i64 8}
!15 = !{!8, !13, i64 12}
!16 = !{!8, !12, i64 16}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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