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

; 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::atomic_backoff" = type { i32 }
%"class.tbb::spin_rw_mutex_v3" = type { i64 }

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN3tbb16spin_rw_mutex_v323internal_acquire_writerEv(ptr noundef nonnull align 8 dereferenceable(8)) #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 @_ZN3tbb8internal14atomic_backoffC2Ev(ptr noundef nonnull align 4 dereferenceable(4)) unnamed_addr #2 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare hidden noundef i64 @_ZN3tbbL3CASIlEET_RVS1_S1_S1_(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef, i64 noundef) #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 void @_ZN3tbb8internal14atomic_backoff5pauseEv(ptr noundef nonnull align 4 dereferenceable(4)) #0 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i64 @_ZL23__TBB_machine_fetchadd8PVvl(ptr noundef, i64 noundef) #4

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN3tbb16spin_rw_mutex_v316internal_upgradeEv(ptr noundef nonnull align 8 dereferenceable(8) %0) #0 align 2 {
  %2 = alloca i1, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca %"class.tbb::internal::atomic_backoff", align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %8 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  %9 = getelementptr inbounds %"class.tbb::spin_rw_mutex_v3", ptr %8, i32 0, i32 0
  %10 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %10, ptr %4, align 8, !tbaa !12
  br label %11

11:                                               ; preds = %45, %1
  %12 = load i64, ptr %4, align 8, !tbaa !12
  %13 = and i64 %12, -4
  %14 = icmp eq i64 %13, 4
  br i1 %14, label %20, label %15

15:                                               ; preds = %11
  %16 = load i64, ptr %4, align 8, !tbaa !12
  %17 = and i64 %16, 2
  %18 = icmp ne i64 %17, 0
  %19 = xor i1 %18, true
  br label %20

20:                                               ; preds = %15, %11
  %21 = phi i1 [ true, %11 ], [ %19, %15 ]
  br i1 %21, label %22, label %46

22:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %23 = load i64, ptr %4, align 8, !tbaa !12
  store i64 %23, ptr %5, align 8, !tbaa !12
  %24 = getelementptr inbounds %"class.tbb::spin_rw_mutex_v3", ptr %8, i32 0, i32 0
  %25 = load i64, ptr %4, align 8, !tbaa !12
  %26 = or i64 %25, 1
  %27 = or i64 %26, 2
  %28 = load i64, ptr %4, align 8, !tbaa !12
  %29 = call noundef i64 @_ZN3tbbL3CASIlEET_RVS1_S1_S1_(ptr noundef nonnull align 8 dereferenceable(8) %24, i64 noundef %27, i64 noundef %28)
  store i64 %29, ptr %4, align 8, !tbaa !12
  %30 = load i64, ptr %5, align 8, !tbaa !12
  %31 = icmp eq i64 %29, %30
  br i1 %31, label %32, label %42

32:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  call void @_ZN3tbb8internal14atomic_backoffC2Ev(ptr noundef nonnull align 4 dereferenceable(4) %6)
  br label %33

33:                                               ; preds = %38, %32
  %34 = getelementptr inbounds %"class.tbb::spin_rw_mutex_v3", ptr %8, i32 0, i32 0
  %35 = load i64, ptr %34, align 8, !tbaa !9
  %36 = and i64 %35, -4
  %37 = icmp ne i64 %36, 4
  br i1 %37, label %38, label %39

38:                                               ; preds = %33
  call void @_ZN3tbb8internal14atomic_backoff5pauseEv(ptr noundef nonnull align 4 dereferenceable(4) %6)
  br label %33, !llvm.loop !13

39:                                               ; preds = %33
  %40 = getelementptr inbounds %"class.tbb::spin_rw_mutex_v3", ptr %8, i32 0, i32 0
  %41 = call noundef i64 @_ZL23__TBB_machine_fetchadd8PVvl(ptr noundef %40, i64 noundef -6)
  store i1 true, ptr %2, align 1
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  br label %43

42:                                               ; preds = %22
  store i32 0, ptr %7, align 4
  br label %43

43:                                               ; preds = %42, %39
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  %44 = load i32, ptr %7, align 4
  switch i32 %44, label %48 [
    i32 0, label %45
  ]

45:                                               ; preds = %43
  br label %11, !llvm.loop !15

46:                                               ; preds = %20
  call void @_ZN3tbb16spin_rw_mutex_v323internal_release_readerEv(ptr noundef nonnull align 8 dereferenceable(8) %8)
  %47 = call noundef zeroext i1 @_ZN3tbb16spin_rw_mutex_v323internal_acquire_writerEv(ptr noundef nonnull align 8 dereferenceable(8) %8)
  store i1 %47, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %48

48:                                               ; preds = %46, %43
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  %49 = load i1, ptr %2, align 1
  ret i1 %49
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN3tbb16spin_rw_mutex_v323internal_release_readerEv(ptr noundef nonnull align 8 dereferenceable(8)) #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 = { 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 = { 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 #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 = !{!10, !11, i64 0}
!10 = !{!"_ZTSN3tbb16spin_rw_mutex_v3E", !11, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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::atomic_backoff" = type { i32 }

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN3tbb16spin_rw_mutex_v323internal_acquire_writerEv(ptr noundef nonnull align 8 dereferenceable(8)) 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 @_ZN3tbb8internal14atomic_backoffC2Ev(ptr noundef nonnull align 4 dereferenceable(4)) unnamed_addr #2 align 2

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

; 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 void @_ZN3tbb8internal14atomic_backoff5pauseEv(ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #0 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i64 @_ZL23__TBB_machine_fetchadd8PVvl(ptr noundef, i64 noundef) local_unnamed_addr #4

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN3tbb16spin_rw_mutex_v316internal_upgradeEv(ptr noundef nonnull align 8 dereferenceable(8) %0) local_unnamed_addr #0 align 2 {
  %2 = alloca %"class.tbb::internal::atomic_backoff", align 4
  %3 = load i64, ptr %0, align 8, !tbaa !5
  br label %.critedge

.critedge:                                        ; preds = %8, %1
  %.09 = phi i64 [ %3, %1 ], [ %10, %8 ]
  %4 = and i64 %.09, -4
  %5 = icmp eq i64 %4, 4
  %6 = and i64 %.09, 2
  %.not = icmp eq i64 %6, 0
  %7 = or i1 %5, %.not
  br i1 %7, label %8, label %17

8:                                                ; preds = %.critedge
  %9 = or i64 %.09, 3
  %10 = tail call noundef i64 @_ZN3tbbL3CASIlEET_RVS1_S1_S1_(ptr noundef nonnull align 8 dereferenceable(8) %0, i64 noundef %9, i64 noundef %.09)
  %.not11 = icmp eq i64 %10, %.09
  br i1 %.not11, label %11, label %.critedge

11:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #5
  call void @_ZN3tbb8internal14atomic_backoffC2Ev(ptr noundef nonnull align 4 dereferenceable(4) %2)
  %12 = load i64, ptr %0, align 8, !tbaa !5
  %13 = and i64 %12, -4
  %.not1012 = icmp eq i64 %13, 4
  br i1 %.not1012, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %11, %.lr.ph
  call void @_ZN3tbb8internal14atomic_backoff5pauseEv(ptr noundef nonnull align 4 dereferenceable(4) %2)
  %14 = load i64, ptr %0, align 8, !tbaa !5
  %15 = and i64 %14, -4
  %.not10 = icmp eq i64 %15, 4
  br i1 %.not10, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph, %11
  %16 = call noundef i64 @_ZL23__TBB_machine_fetchadd8PVvl(ptr noundef nonnull %0, i64 noundef -6)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #5
  br label %19, !llvm.loop !12

17:                                               ; preds = %.critedge
  tail call void @_ZN3tbb16spin_rw_mutex_v323internal_release_readerEv(ptr noundef nonnull align 8 dereferenceable(8) %0)
  %18 = tail call noundef zeroext i1 @_ZN3tbb16spin_rw_mutex_v323internal_acquire_writerEv(ptr noundef nonnull align 8 dereferenceable(8) %0)
  br label %19

19:                                               ; preds = %._crit_edge, %17
  %.2 = phi i1 [ true, %._crit_edge ], [ %18, %17 ]
  ret i1 %.2
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN3tbb16spin_rw_mutex_v323internal_release_readerEv(ptr noundef nonnull align 8 dereferenceable(8)) 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 = { 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 = { 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 #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, !7, i64 0}
!6 = !{!"_ZTSN3tbb16spin_rw_mutex_v3E", !7, i64 0}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


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