; LLVM IR from stan-dev/math :: lib/tbb_2020.3/src/old/spin_rw_mutex_v2.ll (ZN3tbb13spin_rw_mutex16internal_upgradeEPS0_)

; 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" = type { i64 }

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

; Function Attrs: inlinehint mustprogress uwtable
declare hidden noundef zeroext i1 @_ZN3tbbL3CASERVmmm(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: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN3tbb13spin_rw_mutex16internal_upgradeEPS0_(ptr noundef %0) #0 align 2 {
  %2 = alloca i1, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca %"class.tbb::internal::atomic_backoff", align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %"class.tbb::spin_rw_mutex", ptr %7, i32 0, i32 0
  %9 = load volatile i64, ptr %8, align 8, !tbaa !9
  store i64 %9, ptr %4, align 8, !tbaa !12
  br label %10

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

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

19:                                               ; preds = %14, %10
  %20 = phi i1 [ true, %10 ], [ %18, %14 ]
  br i1 %20, label %21, label %44

21:                                               ; preds = %19
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %"class.tbb::spin_rw_mutex", ptr %22, i32 0, i32 0
  %24 = load i64, ptr %4, align 8, !tbaa !12
  %25 = or i64 %24, 2
  %26 = load i64, ptr %4, align 8, !tbaa !12
  %27 = call noundef zeroext i1 @_ZN3tbbL3CASERVmmm(ptr noundef nonnull align 8 dereferenceable(8) %23, i64 noundef %25, i64 noundef %26)
  br i1 %27, label %28, label %39

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

29:                                               ; preds = %35, %28
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %"class.tbb::spin_rw_mutex", ptr %30, i32 0, i32 0
  %32 = load volatile i64, ptr %31, align 8, !tbaa !9
  %33 = and i64 %32, -4
  %34 = icmp ne i64 %33, 4
  br i1 %34, label %35, label %36

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

36:                                               ; preds = %29
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %"class.tbb::spin_rw_mutex", ptr %37, i32 0, i32 0
  store volatile i64 1, ptr %38, align 8, !tbaa !9
  store i1 true, ptr %2, align 1
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %48

39:                                               ; preds = %21
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %"class.tbb::spin_rw_mutex", ptr %40, i32 0, i32 0
  %42 = load volatile i64, ptr %41, align 8, !tbaa !9
  store i64 %42, ptr %4, align 8, !tbaa !12
  br label %43

43:                                               ; preds = %39
  br label %10, !llvm.loop !15

44:                                               ; preds = %19
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  call void @_ZN3tbb13spin_rw_mutex23internal_release_readerEPS0_(ptr noundef %45)
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = call noundef zeroext i1 @_ZN3tbb13spin_rw_mutex23internal_acquire_writerEPS0_(ptr noundef %46)
  store i1 %47, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %48

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN3tbb13spin_rw_mutex23internal_release_readerEPS0_(ptr noundef) #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 = { 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 = !{!"_ZTSN3tbb13spin_rw_mutexE", !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 @_ZN3tbb13spin_rw_mutex23internal_acquire_writerEPS0_(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 @_ZN3tbb8internal14atomic_backoffC2Ev(ptr noundef nonnull align 4 dereferenceable(4)) unnamed_addr #2 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare hidden noundef zeroext i1 @_ZN3tbbL3CASERVmmm(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: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN3tbb13spin_rw_mutex16internal_upgradeEPS0_(ptr noundef %0) local_unnamed_addr #0 align 2 {
  %2 = alloca %"class.tbb::internal::atomic_backoff", align 4
  br label %3

3:                                                ; preds = %8, %1
  %.0 = load volatile i64, ptr %0, align 8, !tbaa !5
  %4 = and i64 %.0, -4
  %5 = icmp eq i64 %4, 4
  %6 = and i64 %.0, 2
  %.not = icmp eq i64 %6, 0
  %7 = or i1 %5, %.not
  br i1 %7, label %8, label %16

8:                                                ; preds = %3
  %9 = or i64 %.0, 2
  %10 = tail call noundef zeroext i1 @_ZN3tbbL3CASERVmmm(ptr noundef nonnull align 8 dereferenceable(8) %0, i64 noundef %9, i64 noundef %.0)
  br i1 %10, label %11, label %3, !llvm.loop !10

11:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #4
  call void @_ZN3tbb8internal14atomic_backoffC2Ev(ptr noundef nonnull align 4 dereferenceable(4) %2)
  %12 = load volatile i64, ptr %0, align 8, !tbaa !5
  %13 = and i64 %12, -4
  %.not1213 = icmp eq i64 %13, 4
  br i1 %.not1213, 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 volatile i64, ptr %0, align 8, !tbaa !5
  %15 = and i64 %14, -4
  %.not12 = icmp eq i64 %15, 4
  br i1 %.not12, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph, %11
  store volatile i64 1, ptr %0, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #4
  br label %18

16:                                               ; preds = %3
  tail call void @_ZN3tbb13spin_rw_mutex23internal_release_readerEPS0_(ptr noundef nonnull %0)
  %17 = tail call noundef zeroext i1 @_ZN3tbb13spin_rw_mutex23internal_acquire_writerEPS0_(ptr noundef nonnull %0)
  br label %18

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN3tbb13spin_rw_mutex23internal_release_readerEPS0_(ptr noundef) 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 = { 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 = !{!"_ZTSN3tbb13spin_rw_mutexE", !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,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass