; LLVM IR from stan-dev/math :: lib/tbb_2020.3/src/tbb (queuing_rw_mutex)

; 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.tbb::queuing_rw_mutex::scoped_lock" = type <{ ptr, ptr, ptr, %"struct.tbb::atomic.0", i8, i8, [5 x i8] }>
%"struct.tbb::atomic.0" = type { %"struct.tbb::internal::atomic_impl_with_arithmetic.1" }
%"struct.tbb::internal::atomic_impl_with_arithmetic.1" = type { %"struct.tbb::internal::atomic_impl.2" }
%"struct.tbb::internal::atomic_impl.2" = type { %"struct.tbb::internal::aligned_storage.3" }
%"struct.tbb::internal::aligned_storage.3" = type { i8 }
%"class.tbb::queuing_rw_mutex" = type { %"struct.tbb::atomic" }
%"struct.tbb::atomic" = type { %"struct.tbb::internal::atomic_impl_with_arithmetic" }
%"struct.tbb::internal::atomic_impl_with_arithmetic" = type { %"struct.tbb::internal::atomic_impl" }
%"struct.tbb::internal::atomic_impl" = type { %"struct.tbb::internal::aligned_storage" }
%"struct.tbb::internal::aligned_storage" = type { ptr }

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i8 @_ZN3tbb6atomicIhEaSEh(ptr noundef nonnull align 1 dereferenceable(1), i8 noundef zeroext) #0 align 2

; 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

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i8 @_ZNVK3tbb8internal11atomic_implIhEcvhEv(ptr noundef nonnull align 1 dereferenceable(1)) #0 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZN3tbb8internal23__TBB_load_with_acquireIPNS_16queuing_rw_mutex11scoped_lockEEET_RVKS5_(ptr noundef nonnull align 8 dereferenceable(8)) #3

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZN3tbb8internal18__TBB_load_relaxedIPNS_16queuing_rw_mutex11scoped_lockEEET_RVKS5_(ptr noundef nonnull align 8 dereferenceable(8)) #3

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN3tbb8internal18spin_wait_while_eqIPNS_16queuing_rw_mutex11scoped_lockEPvEEvRVKT_T0_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN3tbb16queuing_rw_mutex11scoped_lock19downgrade_to_readerEv(ptr noundef nonnull align 8 dereferenceable(27) %0) #0 align 2 {
  %2 = alloca i1, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i16, align 2
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8
  %8 = getelementptr inbounds %"class.tbb::queuing_rw_mutex::scoped_lock", ptr %7, i32 0, i32 3
  %9 = call noundef zeroext i8 @_ZNVK3tbb8internal11atomic_implIhEcvhEv(ptr noundef nonnull align 1 dereferenceable(1) %8)
  %10 = zext i8 %9 to i32
  %11 = icmp eq i32 %10, 8
  br i1 %11, label %12, label %13

12:                                               ; preds = %1
  store i1 true, ptr %2, align 1
  br label %65

13:                                               ; preds = %1
  %14 = getelementptr inbounds %"class.tbb::queuing_rw_mutex::scoped_lock", ptr %7, i32 0, i32 3
  %15 = call noundef zeroext i8 @_ZN3tbb6atomicIhEaSEh(ptr noundef nonnull align 1 dereferenceable(1) %14, i8 noundef zeroext 2)
  %16 = getelementptr inbounds %"class.tbb::queuing_rw_mutex::scoped_lock", ptr %7, i32 0, i32 2
  %17 = call noundef ptr @_ZN3tbb8internal18__TBB_load_relaxedIPNS_16queuing_rw_mutex11scoped_lockEEET_RVKS5_(ptr noundef nonnull align 8 dereferenceable(8) %16)
  %18 = icmp ne ptr %17, null
  br i1 %18, label %39, label %19

19:                                               ; preds = %13
  %20 = getelementptr inbounds %"class.tbb::queuing_rw_mutex::scoped_lock", ptr %7, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !9
  %22 = getelementptr inbounds %"class.tbb::queuing_rw_mutex", ptr %21, i32 0, i32 0
  %23 = call noundef ptr @_ZNK3tbb8internal11atomic_implIPNS_16queuing_rw_mutex11scoped_lockEE4loadILNS_16memory_semanticsE0EEES4_v(ptr noundef nonnull align 8 dereferenceable(8) %22)
  %24 = icmp eq ptr %7, %23
  br i1 %24, label %25, label %37

25:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #4
  %26 = getelementptr inbounds %"class.tbb::queuing_rw_mutex::scoped_lock", ptr %7, i32 0, i32 3
  %27 = call noundef zeroext i8 @_ZN3tbb8internal11atomic_implIhE16compare_and_swapILNS_16memory_semanticsE2EEEhhh(ptr noundef nonnull align 1 dereferenceable(1) %26, i8 noundef zeroext 8, i8 noundef zeroext 2)
  %28 = zext i8 %27 to i16
  store i16 %28, ptr %4, align 2, !tbaa !15
  %29 = load i16, ptr %4, align 2, !tbaa !15
  %30 = zext i16 %29 to i32
  %31 = icmp eq i32 %30, 2
  br i1 %31, label %32, label %33

32:                                               ; preds = %25
  store i1 true, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %34

33:                                               ; preds = %25
  store i32 0, ptr %5, align 4
  br label %34

34:                                               ; preds = %33, %32
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #4
  %35 = load i32, ptr %5, align 4
  switch i32 %35, label %67 [
    i32 0, label %36
    i32 1, label %65
  ]

36:                                               ; preds = %34
  br label %37

37:                                               ; preds = %36, %19
  %38 = getelementptr inbounds %"class.tbb::queuing_rw_mutex::scoped_lock", ptr %7, i32 0, i32 2
  call void @_ZN3tbb8internal18spin_wait_while_eqIPNS_16queuing_rw_mutex11scoped_lockEPvEEvRVKT_T0_(ptr noundef nonnull align 8 dereferenceable(8) %38, ptr noundef null)
  br label %39

39:                                               ; preds = %37, %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %40 = getelementptr inbounds %"class.tbb::queuing_rw_mutex::scoped_lock", ptr %7, i32 0, i32 2
  %41 = call noundef ptr @_ZN3tbb8internal23__TBB_load_with_acquireIPNS_16queuing_rw_mutex11scoped_lockEEET_RVKS5_(ptr noundef nonnull align 8 dereferenceable(8) %40)
  store ptr %41, ptr %6, align 8, !tbaa !5
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = getelementptr inbounds %"class.tbb::queuing_rw_mutex::scoped_lock", ptr %42, i32 0, i32 3
  %44 = call noundef zeroext i8 @_ZNVK3tbb8internal11atomic_implIhEcvhEv(ptr noundef nonnull align 1 dereferenceable(1) %43)
  %45 = zext i8 %44 to i32
  %46 = and i32 %45, 6
  %47 = icmp ne i32 %46, 0
  br i1 %47, label %48, label %51

48:                                               ; preds = %39
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = getelementptr inbounds %"class.tbb::queuing_rw_mutex::scoped_lock", ptr %49, i32 0, i32 4
  call void @_ZN3tbb8internal24__TBB_store_with_releaseIhiEEvRVT_T0_(ptr noundef nonnull align 1 dereferenceable(1) %50, i32 noundef 1)
  br label %62

51:                                               ; preds = %39
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = getelementptr inbounds %"class.tbb::queuing_rw_mutex::scoped_lock", ptr %52, i32 0, i32 3
  %54 = call noundef zeroext i8 @_ZNVK3tbb8internal11atomic_implIhEcvhEv(ptr noundef nonnull align 1 dereferenceable(1) %53)
  %55 = zext i8 %54 to i32
  %56 = icmp eq i32 %55, 32
  br i1 %56, label %57, label %61

57:                                               ; preds = %51
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = getelementptr inbounds %"class.tbb::queuing_rw_mutex::scoped_lock", ptr %58, i32 0, i32 3
  %60 = call noundef zeroext i8 @_ZN3tbb6atomicIhEaSEh(ptr noundef nonnull align 1 dereferenceable(1) %59, i8 noundef zeroext 64)
  br label %61

61:                                               ; preds = %57, %51
  br label %62

62:                                               ; preds = %61, %48
  %63 = getelementptr inbounds %"class.tbb::queuing_rw_mutex::scoped_lock", ptr %7, i32 0, i32 3
  %64 = call noundef zeroext i8 @_ZN3tbb6atomicIhEaSEh(ptr noundef nonnull align 1 dereferenceable(1) %63, i8 noundef zeroext 8)
  store i1 true, ptr %2, align 1
  store i32 1, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %65

65:                                               ; preds = %62, %34, %12
  %66 = load i1, ptr %2, align 1
  ret i1 %66

67:                                               ; preds = %34
  unreachable
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZNK3tbb8internal11atomic_implIPNS_16queuing_rw_mutex11scoped_lockEE4loadILNS_16memory_semanticsE0EEES4_v(ptr noundef nonnull align 8 dereferenceable(8)) #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i8 @_ZN3tbb8internal11atomic_implIhE16compare_and_swapILNS_16memory_semanticsE2EEEhhh(ptr noundef nonnull align 1 dereferenceable(1), i8 noundef zeroext, i8 noundef zeroext) #0 align 2

attributes #0 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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, !6, i64 0}
!10 = !{!"_ZTSN3tbb16queuing_rw_mutex11scoped_lockE", !6, i64 0, !6, i64 8, !6, i64 16, !11, i64 24, !7, i64 25, !7, i64 26}
!11 = !{!"_ZTSN3tbb6atomicIhEE", !12, i64 0}
!12 = !{!"_ZTSN3tbb8internal27atomic_impl_with_arithmeticIhhcEE", !13, i64 0}
!13 = !{!"_ZTSN3tbb8internal11atomic_implIhEE", !14, i64 0}
!14 = !{!"_ZTSN3tbb8internal15aligned_storageIhLm1EEE", !7, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"short", !7, i64 0}


; 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 uwtable
declare dso_local noundef zeroext i8 @_ZN3tbb6atomicIhEaSEh(ptr noundef nonnull align 1 dereferenceable(1), i8 noundef zeroext) local_unnamed_addr #0 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZN3tbb8internal24__TBB_store_with_releaseIhiEEvRVT_T0_(ptr noundef nonnull align 1 dereferenceable(1), i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i8 @_ZNVK3tbb8internal11atomic_implIhEcvhEv(ptr noundef nonnull align 1 dereferenceable(1)) local_unnamed_addr #0 align 2

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN3tbb8internal18spin_wait_while_eqIPNS_16queuing_rw_mutex11scoped_lockEPvEEvRVKT_T0_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN3tbb16queuing_rw_mutex11scoped_lock19downgrade_to_readerEv(ptr noundef nonnull align 8 dereferenceable(27) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 24
  %3 = tail call noundef zeroext i8 @_ZNVK3tbb8internal11atomic_implIhEcvhEv(ptr noundef nonnull align 1 dereferenceable(1) %2)
  %4 = icmp eq i8 %3, 8
  br i1 %4, label %30, label %5

5:                                                ; preds = %1
  %6 = tail call noundef zeroext i8 @_ZN3tbb6atomicIhEaSEh(ptr noundef nonnull align 1 dereferenceable(1) %2, i8 noundef zeroext 2)
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %8 = tail call noundef ptr @_ZN3tbb8internal18__TBB_load_relaxedIPNS_16queuing_rw_mutex11scoped_lockEEET_RVKS5_(ptr noundef nonnull align 8 dereferenceable(8) %7)
  %.not = icmp eq ptr %8, null
  br i1 %.not, label %9, label %16

9:                                                ; preds = %5
  %10 = load ptr, ptr %0, align 8, !tbaa !5
  %11 = tail call noundef ptr @_ZNK3tbb8internal11atomic_implIPNS_16queuing_rw_mutex11scoped_lockEE4loadILNS_16memory_semanticsE0EEES4_v(ptr noundef nonnull align 8 dereferenceable(8) %10)
  %12 = icmp eq ptr %11, %0
  br i1 %12, label %13, label %15

13:                                               ; preds = %9
  %14 = tail call noundef zeroext i8 @_ZN3tbb8internal11atomic_implIhE16compare_and_swapILNS_16memory_semanticsE2EEEhhh(ptr noundef nonnull align 1 dereferenceable(1) %2, i8 noundef zeroext 8, i8 noundef zeroext 2)
  %.not9 = icmp eq i8 %14, 2
  br i1 %.not9, label %30, label %15

15:                                               ; preds = %13, %9
  tail call void @_ZN3tbb8internal18spin_wait_while_eqIPNS_16queuing_rw_mutex11scoped_lockEPvEEvRVKT_T0_(ptr noundef nonnull align 8 dereferenceable(8) %7, ptr noundef null)
  br label %16

16:                                               ; preds = %15, %5
  %17 = tail call noundef ptr @_ZN3tbb8internal23__TBB_load_with_acquireIPNS_16queuing_rw_mutex11scoped_lockEEET_RVKS5_(ptr noundef nonnull align 8 dereferenceable(8) %7)
  %18 = getelementptr inbounds i8, ptr %17, i64 24
  %19 = tail call noundef zeroext i8 @_ZNVK3tbb8internal11atomic_implIhEcvhEv(ptr noundef nonnull align 1 dereferenceable(1) %18)
  %20 = and i8 %19, 6
  %.not8 = icmp eq i8 %20, 0
  br i1 %.not8, label %23, label %21

21:                                               ; preds = %16
  %22 = getelementptr inbounds i8, ptr %17, i64 25
  tail call void @_ZN3tbb8internal24__TBB_store_with_releaseIhiEEvRVT_T0_(ptr noundef nonnull align 1 dereferenceable(1) %22, i32 noundef 1)
  br label %28

23:                                               ; preds = %16
  %24 = tail call noundef zeroext i8 @_ZNVK3tbb8internal11atomic_implIhEcvhEv(ptr noundef nonnull align 1 dereferenceable(1) %18)
  %25 = icmp eq i8 %24, 32
  br i1 %25, label %26, label %28

26:                                               ; preds = %23
  %27 = tail call noundef zeroext i8 @_ZN3tbb6atomicIhEaSEh(ptr noundef nonnull align 1 dereferenceable(1) %18, i8 noundef zeroext 64)
  br label %28

28:                                               ; preds = %23, %26, %21
  %29 = tail call noundef zeroext i8 @_ZN3tbb6atomicIhEaSEh(ptr noundef nonnull align 1 dereferenceable(1) %2, i8 noundef zeroext 8)
  br label %30

30:                                               ; preds = %13, %1, %28
  ret i1 true
}

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i8 @_ZN3tbb8internal11atomic_implIhE16compare_and_swapILNS_16memory_semanticsE2EEEhhh(ptr noundef nonnull align 1 dereferenceable(1), i8 noundef zeroext, i8 noundef zeroext) local_unnamed_addr #0 align 2

attributes #0 = { 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 = { 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" }

!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, !7, i64 0}
!6 = !{!"_ZTSN3tbb16queuing_rw_mutex11scoped_lockE", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24, !8, i64 25, !8, i64 26}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"_ZTSN3tbb6atomicIhEE", !11, i64 0}
!11 = !{!"_ZTSN3tbb8internal27atomic_impl_with_arithmeticIhhcEE", !12, i64 0}
!12 = !{!"_ZTSN3tbb8internal11atomic_implIhEE", !13, i64 0}
!13 = !{!"_ZTSN3tbb8internal15aligned_storageIhLm1EEE", !8, i64 0}


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