; LLVM IR from jrouwe/JoltPhysics :: Jolt/Physics/Body/BodyManager.ll (ZNK3JPH11BodyManager12GetMutexMaskEPKNS_6BodyIDEi)

; 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::BodyManager" = type <{ %"class.JPH::Array", i32, [4 x i8], i64, %"class.JPH::Mutex", %"class.JPH::MutexArray", %"class.JPH::Array.0", %"class.JPH::Mutex", [2 x ptr], [2 x %"struct.std::atomic"], i32, [4 x i8], %"class.JPH::Mutex", %"class.JPH::Array.3", ptr, ptr, i8, [7 x i8] }>
%"class.JPH::Array" = type { i64, i64, ptr }
%"class.JPH::MutexArray" = type <{ ptr, i32, [4 x i8] }>
%"class.JPH::Array.0" = type { i64, i64, ptr }
%"struct.std::atomic" = type { %"struct.std::__atomic_base" }
%"struct.std::__atomic_base" = type { i32 }
%"class.JPH::Mutex" = type { %"class.std::mutex", %"class.std::thread::id" }
%"class.std::mutex" = type { %"class.std::__mutex_base" }
%"class.std::__mutex_base" = type { %union.pthread_mutex_t }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%"class.std::thread::id" = type { i64 }
%"class.JPH::Array.3" = type { i64, i64, ptr }
%"class.JPH::BodyID" = type { i32 }

@.str.1 = external hidden unnamed_addr constant [86 x i8], align 1
@.str.17 = external hidden unnamed_addr constant [54 x i8], align 1
@.str.18 = external hidden unnamed_addr constant [32 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 nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK3JPH6BodyID9IsInvalidEv(ptr noundef nonnull align 4 dereferenceable(4)) #1 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN3JPH23AssertFailedParamHelperEPKcS1_jS1_NS_15AssertLastParamE(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK3JPH6BodyID8GetIndexEv(ptr noundef nonnull align 4 dereferenceable(4)) #1 align 2

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

14:                                               ; preds = %3
  %15 = getelementptr inbounds %"class.JPH::BodyManager", ptr %13, i32 0, i32 5
  %16 = call noundef i32 @_ZNK3JPH10MutexArrayINS_11SharedMutexEE13GetNumMutexesEv(ptr noundef nonnull align 8 dereferenceable(12) %15)
  %17 = zext i32 %16 to i64
  %18 = icmp uge i64 64, %17
  br i1 %18, label %22, label %19

19:                                               ; preds = %14
  %20 = call noundef zeroext i1 @_ZN3JPH23AssertFailedParamHelperEPKcS1_jS1_NS_15AssertLastParamE(ptr noundef @.str.17, ptr noundef @.str.1, i32 noundef 678, ptr noundef @.str.18)
  br i1 %20, label %21, label %22

21:                                               ; preds = %19
  call void asm sideeffect "int $$0x3", "~{dirflag},~{fpsr},~{flags}"() #4, !srcloc !11
  br label %22

22:                                               ; preds = %21, %19, %14
  br label %23

23:                                               ; preds = %22
  %24 = load i32, ptr %7, align 4, !tbaa !9
  %25 = getelementptr inbounds %"class.JPH::BodyManager", ptr %13, i32 0, i32 5
  %26 = call noundef i32 @_ZNK3JPH10MutexArrayINS_11SharedMutexEE13GetNumMutexesEv(ptr noundef nonnull align 8 dereferenceable(12) %25)
  %27 = icmp sge i32 %24, %26
  br i1 %27, label %28, label %30

28:                                               ; preds = %23
  %29 = call noundef i64 @_ZNK3JPH11BodyManager21GetAllBodiesMutexMaskEv(ptr noundef nonnull align 8 dereferenceable(297) %13)
  store i64 %29, ptr %4, align 8
  br label %60

30:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  store i64 0, ptr %9, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %31, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds %"class.JPH::BodyID", ptr %32, i64 %34
  store ptr %35, ptr %11, align 8, !tbaa !5
  br label %36

36:                                               ; preds = %55, %30
  %37 = load ptr, ptr %10, align 8, !tbaa !5
  %38 = load ptr, ptr %11, align 8, !tbaa !5
  %39 = icmp ult ptr %37, %38
  br i1 %39, label %41, label %40

40:                                               ; preds = %36
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %58

41:                                               ; preds = %36
  %42 = load ptr, ptr %10, align 8, !tbaa !5
  %43 = call noundef zeroext i1 @_ZNK3JPH6BodyID9IsInvalidEv(ptr noundef nonnull align 4 dereferenceable(4) %42)
  br i1 %43, label %54, label %44

44:                                               ; preds = %41
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %45 = getelementptr inbounds %"class.JPH::BodyManager", ptr %13, i32 0, i32 5
  %46 = load ptr, ptr %10, align 8, !tbaa !5
  %47 = call noundef i32 @_ZNK3JPH6BodyID8GetIndexEv(ptr noundef nonnull align 4 dereferenceable(4) %46)
  %48 = call noundef i32 @_ZNK3JPH10MutexArrayINS_11SharedMutexEE13GetMutexIndexEj(ptr noundef nonnull align 8 dereferenceable(12) %45, i32 noundef %47)
  store i32 %48, ptr %12, align 4, !tbaa !9
  %49 = load i32, ptr %12, align 4, !tbaa !9
  %50 = zext i32 %49 to i64
  %51 = shl i64 1, %50
  %52 = load i64, ptr %9, align 8, !tbaa !12
  %53 = or i64 %52, %51
  store i64 %53, ptr %9, align 8, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  br label %54

54:                                               ; preds = %44, %41
  br label %55

55:                                               ; preds = %54
  %56 = load ptr, ptr %10, align 8, !tbaa !5
  %57 = getelementptr inbounds %"class.JPH::BodyID", ptr %56, i32 1
  store ptr %57, ptr %10, align 8, !tbaa !5
  br label %36, !llvm.loop !14

58:                                               ; preds = %40
  %59 = load i64, ptr %9, align 8, !tbaa !12
  store i64 %59, ptr %4, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  br label %60

60:                                               ; preds = %58, %28
  %61 = load i64, ptr %4, align 8
  ret i64 %61
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK3JPH10MutexArrayINS_11SharedMutexEE13GetNumMutexesEv(ptr noundef nonnull align 8 dereferenceable(12)) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_ZNK3JPH11BodyManager21GetAllBodiesMutexMaskEv(ptr noundef nonnull align 8 dereferenceable(297)) #3 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i32 @_ZNK3JPH10MutexArrayINS_11SharedMutexEE13GetMutexIndexEj(ptr noundef nonnull align 8 dereferenceable(12), i32 noundef) #2 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 = { 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 #3 = { 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 #4 = { 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 = !{i64 2158739144}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

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

@.str.1 = external hidden unnamed_addr constant [86 x i8], align 1
@.str.17 = external hidden unnamed_addr constant [54 x i8], align 1
@.str.18 = external hidden unnamed_addr constant [32 x i8], align 1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK3JPH6BodyID9IsInvalidEv(ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #0 align 2

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK3JPH6BodyID8GetIndexEv(ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZNK3JPH11BodyManager12GetMutexMaskEPKNS_6BodyIDEi(ptr noundef nonnull align 8 dereferenceable(297) %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #2 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 88
  %5 = tail call noundef i32 @_ZNK3JPH10MutexArrayINS_11SharedMutexEE13GetNumMutexesEv(ptr noundef nonnull align 8 dereferenceable(12) %4)
  %6 = icmp ult i32 %5, 65
  br i1 %6, label %10, label %7

7:                                                ; preds = %3
  %8 = tail call noundef zeroext i1 @_ZN3JPH23AssertFailedParamHelperEPKcS1_jS1_NS_15AssertLastParamE(ptr noundef nonnull @.str.17, ptr noundef nonnull @.str.1, i32 noundef 678, ptr noundef nonnull @.str.18)
  br i1 %8, label %9, label %10

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

10:                                               ; preds = %3, %7, %9
  %11 = tail call noundef i32 @_ZNK3JPH10MutexArrayINS_11SharedMutexEE13GetNumMutexesEv(ptr noundef nonnull align 8 dereferenceable(12) %4)
  %.not = icmp sgt i32 %11, %2
  br i1 %.not, label %14, label %12

12:                                               ; preds = %10
  %13 = tail call noundef i64 @_ZNK3JPH11BodyManager21GetAllBodiesMutexMaskEv(ptr noundef nonnull align 8 dereferenceable(297) %0)
  br label %.loopexit

14:                                               ; preds = %10
  %15 = sext i32 %2 to i64
  %16 = getelementptr inbounds %"class.JPH::BodyID", ptr %1, i64 %15
  %17 = icmp sgt i32 %2, 0
  br i1 %17, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %14, %25
  %.01317 = phi i64 [ %.1, %25 ], [ 0, %14 ]
  %.01416 = phi ptr [ %26, %25 ], [ %1, %14 ]
  %18 = tail call noundef zeroext i1 @_ZNK3JPH6BodyID9IsInvalidEv(ptr noundef nonnull align 4 dereferenceable(4) %.01416)
  br i1 %18, label %25, label %19

19:                                               ; preds = %.lr.ph
  %20 = tail call noundef i32 @_ZNK3JPH6BodyID8GetIndexEv(ptr noundef nonnull align 4 dereferenceable(4) %.01416)
  %21 = tail call noundef i32 @_ZNK3JPH10MutexArrayINS_11SharedMutexEE13GetMutexIndexEj(ptr noundef nonnull align 8 dereferenceable(12) %4, i32 noundef %20)
  %22 = zext nneg i32 %21 to i64
  %23 = shl nuw i64 1, %22
  %24 = or i64 %23, %.01317
  br label %25

25:                                               ; preds = %.lr.ph, %19
  %.1 = phi i64 [ %.01317, %.lr.ph ], [ %24, %19 ]
  %26 = getelementptr inbounds i8, ptr %.01416, i64 4
  %27 = icmp ult ptr %26, %16
  br i1 %27, label %.lr.ph, label %.loopexit, !llvm.loop !6

.loopexit:                                        ; preds = %25, %14, %12
  %.0 = phi i64 [ %13, %12 ], [ 0, %14 ], [ %.1, %25 ]
  ret i64 %.0
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK3JPH10MutexArrayINS_11SharedMutexEE13GetNumMutexesEv(ptr noundef nonnull align 8 dereferenceable(12)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_ZNK3JPH11BodyManager21GetAllBodiesMutexMaskEv(ptr noundef nonnull align 8 dereferenceable(297)) local_unnamed_addr #2 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i32 @_ZNK3JPH10MutexArrayINS_11SharedMutexEE13GetMutexIndexEj(ptr noundef nonnull align 8 dereferenceable(12), i32 noundef) local_unnamed_addr #1 align 2

attributes #0 = { inlinehint 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 = { 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 = !{i64 2158739144}
!6 = distinct !{!6, !7}
!7 = !{!"llvm.loop.mustprogress"}


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