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

; 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.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 }

; 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 nonnull align 8 dereferenceable(64) ptr @_ZN3JPH10MutexArrayINS_11SharedMutexEE15GetMutexByIndexEj(ptr noundef nonnull align 8 dereferenceable(12), i32 noundef) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZNK3JPH11BodyManager10UnlockReadEm(ptr noundef nonnull align 8 dereferenceable(297) %0, i64 noundef %1) #2 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  %7 = load ptr, ptr %3, align 8
  call void @_ZN3JPH11PhysicsLock12sCheckUnlockEPKNS_11BodyManagerENS_17EPhysicsLockTypesE(ptr noundef %7, i32 noundef 2)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %8 = load i64, ptr %4, align 8, !tbaa !9
  store i64 %8, ptr %6, align 8, !tbaa !9
  br label %9

9:                                                ; preds = %22, %2
  %10 = load i64, ptr %6, align 8, !tbaa !9
  %11 = icmp ne i64 %10, 0
  br i1 %11, label %13, label %12

12:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %27

13:                                               ; preds = %9
  %14 = load i64, ptr %6, align 8, !tbaa !9
  %15 = and i64 %14, 1
  %16 = icmp ne i64 %15, 0
  br i1 %16, label %17, label %21

17:                                               ; preds = %13
  %18 = getelementptr inbounds %"class.JPH::BodyManager", ptr %7, i32 0, i32 5
  %19 = load i32, ptr %5, align 4, !tbaa !11
  %20 = call noundef nonnull align 8 dereferenceable(64) ptr @_ZN3JPH10MutexArrayINS_11SharedMutexEE15GetMutexByIndexEj(ptr noundef nonnull align 8 dereferenceable(12) %18, i32 noundef %19)
  call void @_ZNSt12shared_mutex13unlock_sharedEv(ptr noundef nonnull align 8 dereferenceable(56) %20)
  br label %21

21:                                               ; preds = %17, %13
  br label %22

22:                                               ; preds = %21
  %23 = load i64, ptr %6, align 8, !tbaa !9
  %24 = lshr i64 %23, 1
  store i64 %24, ptr %6, align 8, !tbaa !9
  %25 = load i32, ptr %5, align 4, !tbaa !11
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr %5, align 4, !tbaa !11
  br label %9, !llvm.loop !13

27:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZN3JPH11PhysicsLock12sCheckUnlockEPKNS_11BodyManagerENS_17EPhysicsLockTypesE(ptr noundef, i32 noundef) #3 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNSt12shared_mutex13unlock_sharedEv(ptr noundef nonnull align 8 dereferenceable(56)) #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 = { 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 = { 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 #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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(64) ptr @_ZN3JPH10MutexArrayINS_11SharedMutexEE15GetMutexByIndexEj(ptr noundef nonnull align 8 dereferenceable(12), i32 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZNK3JPH11BodyManager10UnlockReadEm(ptr noundef nonnull align 8 dereferenceable(297) %0, i64 noundef %1) local_unnamed_addr #1 align 2 {
  tail call void @_ZN3JPH11PhysicsLock12sCheckUnlockEPKNS_11BodyManagerENS_17EPhysicsLockTypesE(ptr noundef nonnull %0, i32 noundef 2)
  %.not8 = icmp eq i64 %1, 0
  br i1 %.not8, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %3 = getelementptr inbounds i8, ptr %0, i64 88
  br label %4

._crit_edge:                                      ; preds = %8, %2
  ret void

4:                                                ; preds = %.lr.ph, %8
  %.010 = phi i64 [ %1, %.lr.ph ], [ %9, %8 ]
  %.069 = phi i32 [ 0, %.lr.ph ], [ %10, %8 ]
  %5 = and i64 %.010, 1
  %.not7 = icmp eq i64 %5, 0
  br i1 %.not7, label %8, label %6

6:                                                ; preds = %4
  %7 = tail call noundef nonnull align 8 dereferenceable(64) ptr @_ZN3JPH10MutexArrayINS_11SharedMutexEE15GetMutexByIndexEj(ptr noundef nonnull align 8 dereferenceable(12) %3, i32 noundef %.069)
  tail call void @_ZNSt12shared_mutex13unlock_sharedEv(ptr noundef nonnull align 8 dereferenceable(56) %7)
  br label %8

8:                                                ; preds = %4, %6
  %9 = lshr i64 %.010, 1
  %10 = add nuw nsw i32 %.069, 1
  %.not = icmp ult i64 %.010, 2
  br i1 %.not, label %._crit_edge, label %4, !llvm.loop !5
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZN3JPH11PhysicsLock12sCheckUnlockEPKNS_11BodyManagerENS_17EPhysicsLockTypesE(ptr noundef, i32 noundef) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNSt12shared_mutex13unlock_sharedEv(ptr noundef nonnull align 8 dereferenceable(56)) 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 = { 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 = { 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" }

!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.mustprogress"}


; Effective O3 passes: GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass