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

; 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::internal::tbb_thread_v3::id" = type { i64 }
%"class.tbb::interface5::reader_writer_lock" = type { %"struct.tbb::atomic", %"struct.tbb::atomic.0", %"struct.tbb::atomic.0", %"class.tbb::internal::tbb_thread_v3::id", %"struct.tbb::atomic.4" }
%"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 }
%"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 { ptr }
%"struct.tbb::atomic.4" = type { %"struct.tbb::internal::atomic_impl_with_arithmetic.5" }
%"struct.tbb::internal::atomic_impl_with_arithmetic.5" = type { %"struct.tbb::internal::atomic_impl.6" }
%"struct.tbb::internal::atomic_impl.6" = type { %"struct.tbb::internal::aligned_storage.7" }
%"struct.tbb::internal::aligned_storage.7" = type { i64 }
%"class.tbb::interface5::reader_writer_lock::scoped_lock" = type <{ ptr, ptr, %"struct.tbb::atomic.8", [4 x i8] }>
%"struct.tbb::atomic.8" = type { %"struct.tbb::internal::atomic_impl.9" }
%"struct.tbb::internal::atomic_impl.9" = type { %"struct.tbb::internal::aligned_storage.10" }
%"struct.tbb::internal::aligned_storage.10" = type { i32 }

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZN3tbb6atomicIPNS_10interface518reader_writer_lock11scoped_lockEEaSES4_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) #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: mustprogress nounwind uwtable
declare dso_local void @_ZN3tbb8internal13tbb_thread_v32idC2Ev(ptr noundef nonnull align 8 dereferenceable(8)) unnamed_addr #2 align 2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN3tbb6atomicINS_10interface518reader_writer_lock8status_tEEaSES3_(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) #0 align 2

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

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

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

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN3tbb10interface518reader_writer_lock9end_writeEPNS1_11scoped_lockE(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %1) #0 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca %"class.tbb::internal::tbb_thread_v3::id", align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  call void @_ZN3tbb8internal13tbb_thread_v32idC2Ev(ptr noundef nonnull align 8 dereferenceable(8) %5) #5
  %7 = getelementptr inbounds %"class.tbb::interface5::reader_writer_lock", ptr %6, i32 0, i32 3
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %7, ptr align 8 %5, i64 8, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = getelementptr inbounds %"class.tbb::interface5::reader_writer_lock::scoped_lock", ptr %8, i32 0, i32 1
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %22

12:                                               ; preds = %2
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %"class.tbb::interface5::reader_writer_lock::scoped_lock", ptr %13, i32 0, i32 1
  %15 = load ptr, ptr %14, align 8, !tbaa !12
  %16 = getelementptr inbounds %"class.tbb::interface5::reader_writer_lock", ptr %6, i32 0, i32 1
  %17 = call noundef ptr @_ZN3tbb6atomicIPNS_10interface518reader_writer_lock11scoped_lockEEaSES4_(ptr noundef nonnull align 8 dereferenceable(8) %16, ptr noundef %15)
  %18 = getelementptr inbounds %"class.tbb::interface5::reader_writer_lock", ptr %6, i32 0, i32 1
  %19 = call noundef ptr @_ZNK3tbb6atomicIPNS_10interface518reader_writer_lock11scoped_lockEEptEv(ptr noundef nonnull align 8 dereferenceable(8) %18)
  %20 = getelementptr inbounds %"class.tbb::interface5::reader_writer_lock::scoped_lock", ptr %19, i32 0, i32 2
  %21 = call noundef i32 @_ZN3tbb6atomicINS_10interface518reader_writer_lock8status_tEEaSES3_(ptr noundef nonnull align 4 dereferenceable(4) %20, i32 noundef 2)
  br label %43

22:                                               ; preds = %2
  %23 = getelementptr inbounds %"class.tbb::interface5::reader_writer_lock", ptr %6, i32 0, i32 4
  %24 = call noundef i64 @_ZN3tbb10interface513fetch_and_andERNS_6atomicImEEm(ptr noundef nonnull align 8 dereferenceable(8) %23, i64 noundef -4)
  %25 = and i64 %24, 4
  %26 = icmp ne i64 %25, 0
  br i1 %26, label %27, label %28

27:                                               ; preds = %22
  call void @_ZN3tbb10interface518reader_writer_lock15unblock_readersEv(ptr noundef nonnull align 8 dereferenceable(40) %6)
  br label %28

28:                                               ; preds = %27, %22
  %29 = getelementptr inbounds %"class.tbb::interface5::reader_writer_lock", ptr %6, i32 0, i32 1
  %30 = call noundef ptr @_ZN3tbb8internal11atomic_implIPNS_10interface518reader_writer_lock11scoped_lockEE15fetch_and_storeES5_(ptr noundef nonnull align 8 dereferenceable(8) %29, ptr noundef null)
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %"class.tbb::interface5::reader_writer_lock", ptr %6, i32 0, i32 2
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = call noundef ptr @_ZN3tbb8internal11atomic_implIPNS_10interface518reader_writer_lock11scoped_lockEE16compare_and_swapES5_S5_(ptr noundef nonnull align 8 dereferenceable(8) %32, ptr noundef null, ptr noundef %33)
  %35 = icmp ne ptr %31, %34
  br i1 %35, label %36, label %42

36:                                               ; preds = %28
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %"class.tbb::interface5::reader_writer_lock::scoped_lock", ptr %37, i32 0, i32 1
  call void @_ZN3tbb8internal18spin_wait_while_eqIPNS_10interface518reader_writer_lock11scoped_lockES5_EEvRVKT_T0_(ptr noundef nonnull align 8 dereferenceable(8) %38, ptr noundef null)
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %"class.tbb::interface5::reader_writer_lock::scoped_lock", ptr %39, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !12
  call void @_ZN3tbb10interface518reader_writer_lock15set_next_writerEPNS1_11scoped_lockE(ptr noundef nonnull align 8 dereferenceable(40) %6, ptr noundef %41)
  br label %42

42:                                               ; preds = %36, %28
  br label %43

43:                                               ; preds = %42, %12
  ret void
}

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i64 @_ZN3tbb10interface513fetch_and_andERNS_6atomicImEEm(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef) #4

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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { 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 = !{i64 0, i64 8, !10}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !6, i64 8}
!13 = !{!"_ZTSN3tbb10interface518reader_writer_lock11scoped_lockE", !6, i64 0, !6, i64 8, !14, i64 16}
!14 = !{!"_ZTSN3tbb6atomicINS_10interface518reader_writer_lock8status_tEEE", !15, i64 0}
!15 = !{!"_ZTSN3tbb8internal11atomic_implINS_10interface518reader_writer_lock8status_tEEE", !16, i64 0}
!16 = !{!"_ZTSN3tbb8internal15aligned_storageINS_10interface518reader_writer_lock8status_tELm4EEE", !17, i64 0}
!17 = !{!"_ZTSN3tbb10interface518reader_writer_lock8status_tE", !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"

%"class.tbb::internal::tbb_thread_v3::id" = type { i64 }

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN3tbb8internal13tbb_thread_v32idC2Ev(ptr noundef nonnull align 8 dereferenceable(8)) unnamed_addr #2 align 2

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

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

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

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

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

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN3tbb10interface518reader_writer_lock9end_writeEPNS1_11scoped_lockE(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %1) local_unnamed_addr #0 align 2 {
  %3 = alloca %"class.tbb::internal::tbb_thread_v3::id", align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #4
  call void @_ZN3tbb8internal13tbb_thread_v32idC2Ev(ptr noundef nonnull align 8 dereferenceable(8) %3) #4
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %5, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #4
  %6 = getelementptr inbounds i8, ptr %1, i64 8
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  %.not = icmp eq ptr %7, null
  br i1 %.not, label %14, label %8

8:                                                ; preds = %2
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = call noundef ptr @_ZN3tbb6atomicIPNS_10interface518reader_writer_lock11scoped_lockEEaSES4_(ptr noundef nonnull align 8 dereferenceable(8) %9, ptr noundef nonnull %7)
  %11 = call noundef ptr @_ZNK3tbb6atomicIPNS_10interface518reader_writer_lock11scoped_lockEEptEv(ptr noundef nonnull align 8 dereferenceable(8) %9)
  %12 = getelementptr inbounds i8, ptr %11, i64 16
  %13 = call noundef i32 @_ZN3tbb6atomicINS_10interface518reader_writer_lock8status_tEEaSES3_(ptr noundef nonnull align 4 dereferenceable(4) %12, i32 noundef 2)
  br label %26

14:                                               ; preds = %2
  %15 = getelementptr inbounds i8, ptr %0, i64 32
  %16 = call noundef i64 @_ZN3tbb10interface513fetch_and_andERNS_6atomicImEEm(ptr noundef nonnull align 8 dereferenceable(8) %15, i64 noundef -4)
  %17 = and i64 %16, 4
  %.not7 = icmp eq i64 %17, 0
  br i1 %.not7, label %19, label %18

18:                                               ; preds = %14
  call void @_ZN3tbb10interface518reader_writer_lock15unblock_readersEv(ptr noundef nonnull align 8 dereferenceable(40) %0)
  br label %19

19:                                               ; preds = %18, %14
  %20 = getelementptr inbounds i8, ptr %0, i64 8
  %21 = call noundef ptr @_ZN3tbb8internal11atomic_implIPNS_10interface518reader_writer_lock11scoped_lockEE15fetch_and_storeES5_(ptr noundef nonnull align 8 dereferenceable(8) %20, ptr noundef null)
  %22 = getelementptr inbounds i8, ptr %0, i64 16
  %23 = call noundef ptr @_ZN3tbb8internal11atomic_implIPNS_10interface518reader_writer_lock11scoped_lockEE16compare_and_swapES5_S5_(ptr noundef nonnull align 8 dereferenceable(8) %22, ptr noundef null, ptr noundef nonnull %1)
  %.not8 = icmp eq ptr %23, %1
  br i1 %.not8, label %26, label %24

24:                                               ; preds = %19
  call void @_ZN3tbb8internal18spin_wait_while_eqIPNS_10interface518reader_writer_lock11scoped_lockES5_EEvRVKT_T0_(ptr noundef nonnull align 8 dereferenceable(8) %6, ptr noundef null)
  %25 = load ptr, ptr %6, align 8, !tbaa !9
  call void @_ZN3tbb10interface518reader_writer_lock15set_next_writerEPNS1_11scoped_lockE(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %25)
  br label %26

26:                                               ; preds = %19, %24, %8
  ret void
}

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

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

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !11, i64 8}
!10 = !{!"_ZTSN3tbb10interface518reader_writer_lock11scoped_lockE", !11, i64 0, !11, i64 8, !12, i64 16}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"_ZTSN3tbb6atomicINS_10interface518reader_writer_lock8status_tEEE", !13, i64 0}
!13 = !{!"_ZTSN3tbb8internal11atomic_implINS_10interface518reader_writer_lock8status_tEEE", !14, i64 0}
!14 = !{!"_ZTSN3tbb8internal15aligned_storageINS_10interface518reader_writer_lock8status_tELm4EEE", !15, i64 0}
!15 = !{!"_ZTSN3tbb10interface518reader_writer_lock8status_tE", !7, i64 0}


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