; LLVM IR from mtsamis/box2d-optimized :: src/particle/b2_particle_system.ll (ZN16b2ParticleSystem22InsideBoundsEnumerator7GetNextEv)

; 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.b2ParticleSystem::InsideBoundsEnumerator" = type { i32, i32, i32, i32, ptr, ptr }
%"struct.b2ParticleSystem::Proxy" = type { i32, i32 }

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_ZN16b2ParticleSystem22InsideBoundsEnumerator7GetNextEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #0 align 2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8
  br label %7

7:                                                ; preds = %40, %1
  %8 = getelementptr inbounds %"class.b2ParticleSystem::InsideBoundsEnumerator", ptr %6, i32 0, i32 4
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = getelementptr inbounds %"class.b2ParticleSystem::InsideBoundsEnumerator", ptr %6, i32 0, i32 5
  %11 = load ptr, ptr %10, align 8, !tbaa !12
  %12 = icmp ult ptr %9, %11
  br i1 %12, label %13, label %41

13:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %14 = getelementptr inbounds %"class.b2ParticleSystem::InsideBoundsEnumerator", ptr %6, i32 0, i32 4
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  %16 = getelementptr inbounds %"struct.b2ParticleSystem::Proxy", ptr %15, i32 0, i32 1
  %17 = load i32, ptr %16, align 4, !tbaa !13
  %18 = and i32 %17, 1048575
  store i32 %18, ptr %4, align 4, !tbaa !15
  %19 = load i32, ptr %4, align 4, !tbaa !15
  %20 = getelementptr inbounds %"class.b2ParticleSystem::InsideBoundsEnumerator", ptr %6, i32 0, i32 0
  %21 = load i32, ptr %20, align 8, !tbaa !16
  %22 = icmp uge i32 %19, %21
  br i1 %22, label %23, label %34

23:                                               ; preds = %13
  %24 = load i32, ptr %4, align 4, !tbaa !15
  %25 = getelementptr inbounds %"class.b2ParticleSystem::InsideBoundsEnumerator", ptr %6, i32 0, i32 1
  %26 = load i32, ptr %25, align 4, !tbaa !17
  %27 = icmp ule i32 %24, %26
  br i1 %27, label %28, label %34

28:                                               ; preds = %23
  %29 = getelementptr inbounds %"class.b2ParticleSystem::InsideBoundsEnumerator", ptr %6, i32 0, i32 4
  %30 = load ptr, ptr %29, align 8, !tbaa !9
  %31 = getelementptr inbounds %"struct.b2ParticleSystem::Proxy", ptr %30, i32 1
  store ptr %31, ptr %29, align 8, !tbaa !9
  %32 = getelementptr inbounds %"struct.b2ParticleSystem::Proxy", ptr %30, i32 0, i32 0
  %33 = load i32, ptr %32, align 4, !tbaa !18
  store i32 %33, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %38

34:                                               ; preds = %23, %13
  %35 = getelementptr inbounds %"class.b2ParticleSystem::InsideBoundsEnumerator", ptr %6, i32 0, i32 4
  %36 = load ptr, ptr %35, align 8, !tbaa !9
  %37 = getelementptr inbounds %"struct.b2ParticleSystem::Proxy", ptr %36, i32 1
  store ptr %37, ptr %35, align 8, !tbaa !9
  store i32 0, ptr %5, align 4
  br label %38

38:                                               ; preds = %34, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %39 = load i32, ptr %5, align 4
  switch i32 %39, label %44 [
    i32 0, label %40
    i32 1, label %42
  ]

40:                                               ; preds = %38
  br label %7, !llvm.loop !19

41:                                               ; preds = %7
  store i32 -1, ptr %2, align 4
  br label %42

42:                                               ; preds = %41, %38
  %43 = load i32, ptr %2, align 4
  ret i32 %43

44:                                               ; preds = %38
  unreachable
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 16}
!10 = !{!"_ZTSN16b2ParticleSystem22InsideBoundsEnumeratorE", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !6, i64 16, !6, i64 24}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 24}
!13 = !{!14, !11, i64 4}
!14 = !{!"_ZTSN16b2ParticleSystem5ProxyE", !11, i64 0, !11, i64 4}
!15 = !{!11, !11, i64 0}
!16 = !{!10, !11, i64 0}
!17 = !{!10, !11, i64 4}
!18 = !{!14, !11, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"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 memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_ZN16b2ParticleSystem22InsideBoundsEnumerator7GetNextEv(ptr nocapture noundef nonnull align 8 dereferenceable(32) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 16
  %3 = getelementptr inbounds i8, ptr %0, i64 24
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %.promoted = load ptr, ptr %2, align 8, !tbaa !11
  %5 = icmp ult ptr %.promoted, %4
  br i1 %5, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %1
  %6 = load i32, ptr %0, align 8, !tbaa !12
  %7 = getelementptr inbounds i8, ptr %0, i64 4
  %8 = load i32, ptr %7, align 4
  br label %9

9:                                                ; preds = %.lr.ph, %16
  %10 = phi ptr [ %.promoted, %.lr.ph ], [ %14, %16 ]
  %11 = getelementptr inbounds i8, ptr %10, i64 4
  %12 = load i32, ptr %11, align 4, !tbaa !13
  %13 = and i32 %12, 1048575
  %.not = icmp ult i32 %13, %6
  %.not7 = icmp ugt i32 %13, %8
  %or.cond = select i1 %.not, i1 true, i1 %.not7
  %14 = getelementptr inbounds i8, ptr %10, i64 8
  br i1 %or.cond, label %16, label %.thread

.thread:                                          ; preds = %9
  store ptr %14, ptr %2, align 8, !tbaa !11
  %15 = load i32, ptr %10, align 4, !tbaa !15
  br label %.loopexit

16:                                               ; preds = %9
  %17 = icmp ult ptr %14, %4
  br i1 %17, label %9, label %.loopexit.loopexit

.loopexit.loopexit:                               ; preds = %16
  store ptr %14, ptr %2, align 8, !tbaa !11
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit, %1, %.thread
  %.2 = phi i32 [ %15, %.thread ], [ -1, %1 ], [ -1, %.loopexit.loopexit ]
  ret i32 %.2
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(read, argmem: 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 24}
!6 = !{!"_ZTSN16b2ParticleSystem22InsideBoundsEnumeratorE", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !10, i64 16, !10, i64 24}
!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 16}
!12 = !{!6, !7, i64 0}
!13 = !{!14, !7, i64 4}
!14 = !{!"_ZTSN16b2ParticleSystem5ProxyE", !7, i64 0, !7, i64 4}
!15 = !{!14, !7, i64 0}


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