; LLVM IR from jrouwe/JoltPhysics :: Jolt/Physics/IslandBuilder.ll (ZNK3JPH13IslandBuilder17GetBodiesInIslandEjRPNS_6BodyIDES3_)

; 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.JPH::AssertLastParam" = type { i8 }
%"class.JPH::IslandBuilder" = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i32, i32, i32, i32, i32 }
%"class.JPH::BodyID" = type { i32 }

@.str.1 = external hidden unnamed_addr constant [83 x i8], align 1
@.str.22 = external hidden unnamed_addr constant [28 x i8], align 1

; 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: inlinehint mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN3JPH23AssertFailedParamHelperEPKcS1_jNS_15AssertLastParamE(ptr noundef, ptr noundef, i32 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZNK3JPH13IslandBuilder17GetBodiesInIslandEjRPNS_6BodyIDES3_(ptr noundef nonnull align 8 dereferenceable(112) %0, i32 noundef %1, ptr noundef nonnull align 8 dereferenceable(8) %2, ptr noundef nonnull align 8 dereferenceable(8) %3) #2 align 2 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca %"struct.JPH::AssertLastParam", align 1
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !5
  %11 = load ptr, ptr %5, align 8
  br label %12

12:                                               ; preds = %4
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = getelementptr inbounds %"class.JPH::IslandBuilder", ptr %11, i32 0, i32 16
  %15 = load i32, ptr %14, align 4, !tbaa !11
  %16 = icmp ult i32 %13, %15
  br i1 %16, label %20, label %17

17:                                               ; preds = %12
  %18 = call noundef zeroext i1 @_ZN3JPH23AssertFailedParamHelperEPKcS1_jNS_15AssertLastParamE(ptr noundef @.str.22, ptr noundef @.str.1, i32 noundef 391)
  br i1 %18, label %19, label %20

19:                                               ; preds = %17
  call void asm sideeffect "int $$0x3", "~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !13
  br label %20

20:                                               ; preds = %19, %17, %12
  br label %21

21:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %22 = getelementptr inbounds %"class.JPH::IslandBuilder", ptr %11, i32 0, i32 9
  %23 = load ptr, ptr %22, align 8, !tbaa !14
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %32

25:                                               ; preds = %21
  %26 = getelementptr inbounds %"class.JPH::IslandBuilder", ptr %11, i32 0, i32 9
  %27 = load ptr, ptr %26, align 8, !tbaa !14
  %28 = load i32, ptr %6, align 4, !tbaa !9
  %29 = zext i32 %28 to i64
  %30 = getelementptr inbounds i32, ptr %27, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !9
  br label %34

32:                                               ; preds = %21
  %33 = load i32, ptr %6, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %32, %25
  %35 = phi i32 [ %31, %25 ], [ %33, %32 ]
  store i32 %35, ptr %10, align 4, !tbaa !9
  %36 = load i32, ptr %10, align 4, !tbaa !9
  %37 = icmp ugt i32 %36, 0
  br i1 %37, label %38, label %50

38:                                               ; preds = %34
  %39 = getelementptr inbounds %"class.JPH::IslandBuilder", ptr %11, i32 0, i32 3
  %40 = load ptr, ptr %39, align 8, !tbaa !15
  %41 = getelementptr inbounds %"class.JPH::IslandBuilder", ptr %11, i32 0, i32 4
  %42 = load ptr, ptr %41, align 8, !tbaa !16
  %43 = load i32, ptr %10, align 4, !tbaa !9
  %44 = sub i32 %43, 1
  %45 = zext i32 %44 to i64
  %46 = getelementptr inbounds i32, ptr %42, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !9
  %48 = zext i32 %47 to i64
  %49 = getelementptr inbounds %"class.JPH::BodyID", ptr %40, i64 %48
  br label %53

50:                                               ; preds = %34
  %51 = getelementptr inbounds %"class.JPH::IslandBuilder", ptr %11, i32 0, i32 3
  %52 = load ptr, ptr %51, align 8, !tbaa !15
  br label %53

53:                                               ; preds = %50, %38
  %54 = phi ptr [ %49, %38 ], [ %52, %50 ]
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %54, ptr %55, align 8, !tbaa !5
  %56 = getelementptr inbounds %"class.JPH::IslandBuilder", ptr %11, i32 0, i32 3
  %57 = load ptr, ptr %56, align 8, !tbaa !15
  %58 = getelementptr inbounds %"class.JPH::IslandBuilder", ptr %11, i32 0, i32 4
  %59 = load ptr, ptr %58, align 8, !tbaa !16
  %60 = load i32, ptr %10, align 4, !tbaa !9
  %61 = zext i32 %60 to i64
  %62 = getelementptr inbounds i32, ptr %59, i64 %61
  %63 = load i32, ptr %62, align 4, !tbaa !9
  %64 = zext i32 %63 to i64
  %65 = getelementptr inbounds %"class.JPH::BodyID", ptr %57, i64 %64
  %66 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %65, ptr %66, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint mustprogress 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 = { mustprogress 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 #3 = { 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 108}
!12 = !{!"_ZTSN3JPH13IslandBuilderE", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !10, i64 88, !10, i64 92, !10, i64 96, !10, i64 100, !10, i64 104, !10, i64 108}
!13 = !{i64 2158304772}
!14 = !{!12, !6, i64 72}
!15 = !{!12, !6, i64 24}
!16 = !{!12, !6, i64 32}


; 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"

%"class.JPH::BodyID" = type { i32 }

@.str.1 = external hidden unnamed_addr constant [83 x i8], align 1
@.str.22 = external hidden unnamed_addr constant [28 x i8], align 1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN3JPH23AssertFailedParamHelperEPKcS1_jNS_15AssertLastParamE(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZNK3JPH13IslandBuilder17GetBodiesInIslandEjRPNS_6BodyIDES3_(ptr nocapture noundef nonnull readonly align 8 dereferenceable(112) %0, i32 noundef %1, ptr nocapture noundef nonnull writeonly align 8 dereferenceable(8) %2, ptr nocapture noundef nonnull writeonly align 8 dereferenceable(8) %3) local_unnamed_addr #1 align 2 {
  %5 = getelementptr inbounds i8, ptr %0, i64 108
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = icmp ugt i32 %6, %1
  br i1 %7, label %11, label %8

8:                                                ; preds = %4
  %9 = tail call noundef zeroext i1 @_ZN3JPH23AssertFailedParamHelperEPKcS1_jNS_15AssertLastParamE(ptr noundef nonnull @.str.22, ptr noundef nonnull @.str.1, i32 noundef 391)
  br i1 %9, label %10, label %11

10:                                               ; preds = %8
  tail call void asm sideeffect "int $$0x3", "~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !11
  br label %11

11:                                               ; preds = %4, %8, %10
  %12 = getelementptr inbounds i8, ptr %0, i64 72
  %13 = load ptr, ptr %12, align 8, !tbaa !12
  %.not = icmp eq ptr %13, null
  br i1 %.not, label %18, label %14

14:                                               ; preds = %11
  %15 = zext i32 %1 to i64
  %16 = getelementptr inbounds i32, ptr %13, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !13
  br label %18

18:                                               ; preds = %11, %14
  %19 = phi i32 [ %17, %14 ], [ %1, %11 ]
  %.not9 = icmp eq i32 %19, 0
  %20 = getelementptr inbounds i8, ptr %0, i64 24
  %21 = load ptr, ptr %20, align 8, !tbaa !14
  br i1 %.not9, label %31, label %22

22:                                               ; preds = %18
  %23 = getelementptr inbounds i8, ptr %0, i64 32
  %24 = load ptr, ptr %23, align 8, !tbaa !15
  %25 = add i32 %19, -1
  %26 = zext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %24, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !13
  %29 = zext i32 %28 to i64
  %30 = getelementptr inbounds %"class.JPH::BodyID", ptr %21, i64 %29
  br label %31

31:                                               ; preds = %18, %22
  %32 = phi ptr [ %30, %22 ], [ %21, %18 ]
  store ptr %32, ptr %2, align 8, !tbaa !16
  %33 = getelementptr inbounds i8, ptr %0, i64 24
  %34 = load ptr, ptr %33, align 8, !tbaa !14
  %35 = getelementptr inbounds i8, ptr %0, i64 32
  %36 = load ptr, ptr %35, align 8, !tbaa !15
  %37 = zext i32 %19 to i64
  %38 = getelementptr inbounds i32, ptr %36, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !13
  %40 = zext i32 %39 to i64
  %41 = getelementptr inbounds %"class.JPH::BodyID", ptr %34, i64 %40
  store ptr %41, ptr %3, align 8, !tbaa !16
  ret void
}

attributes #0 = { inlinehint mustprogress 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 = { mustprogress 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, !10, i64 108}
!6 = !{!"_ZTSN3JPH13IslandBuilderE", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !10, i64 88, !10, i64 92, !10, i64 96, !10, i64 100, !10, i64 104, !10, i64 108}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{i64 2158304772}
!12 = !{!6, !7, i64 72}
!13 = !{!10, !10, i64 0}
!14 = !{!6, !7, i64 24}
!15 = !{!6, !7, i64 32}
!16 = !{!7, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass