; LLVM IR from 1runeberg/xrlib :: src/thread_pool.ll (ZN5xrlib11CThreadPool18ScaleWorkerThreadsEv)

; 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.std::chrono::duration" = type { i64 }
%"class.xrlib::CThreadPool" = type { %"struct.std::atomic", %"class.std::vector", %"class.std::queue", %"class.std::mutex", %"class.std::condition_variable", %"class.std::map", %"class.std::mutex", %"class.std::condition_variable", %"struct.std::atomic.6", %"struct.std::atomic.6", %"class.std::vector.8", %"class.std::queue.13", %"class.std::mutex", %"class.std::thread" }
%"struct.std::atomic" = type { %"struct.std::__atomic_base" }
%"struct.std::__atomic_base" = type { i8 }
%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<std::thread, std::allocator<std::thread>>::_Vector_impl" }
%"struct.std::_Vector_base<std::thread, std::allocator<std::thread>>::_Vector_impl" = type { %"struct.std::_Vector_base<std::thread, std::allocator<std::thread>>::_Vector_impl_data" }
%"struct.std::_Vector_base<std::thread, std::allocator<std::thread>>::_Vector_impl_data" = type { ptr, ptr, ptr }
%"class.std::queue" = type { %"class.std::deque" }
%"class.std::deque" = type { %"class.std::_Deque_base" }
%"class.std::_Deque_base" = type { %"struct.std::_Deque_base<std::function<void ()>, std::allocator<std::function<void ()>>>::_Deque_impl" }
%"struct.std::_Deque_base<std::function<void ()>, std::allocator<std::function<void ()>>>::_Deque_impl" = type { %"struct.std::_Deque_base<std::function<void ()>, std::allocator<std::function<void ()>>>::_Deque_impl_data" }
%"struct.std::_Deque_base<std::function<void ()>, std::allocator<std::function<void ()>>>::_Deque_impl_data" = type { ptr, i64, %"struct.std::_Deque_iterator", %"struct.std::_Deque_iterator" }
%"struct.std::_Deque_iterator" = type { ptr, ptr, ptr, ptr }
%"class.std::map" = type { %"class.std::_Rb_tree" }
%"class.std::_Rb_tree" = type { %"struct.std::_Rb_tree<xrlib::EThreadType, std::pair<const xrlib::EThreadType, xrlib::SDedicatedThread>, std::_Select1st<std::pair<const xrlib::EThreadType, xrlib::SDedicatedThread>>, std::less<xrlib::EThreadType>>::_Rb_tree_impl" }
%"struct.std::_Rb_tree<xrlib::EThreadType, std::pair<const xrlib::EThreadType, xrlib::SDedicatedThread>, std::_Select1st<std::pair<const xrlib::EThreadType, xrlib::SDedicatedThread>>, std::less<xrlib::EThreadType>>::_Rb_tree_impl" = type { [8 x i8], %"struct.std::_Rb_tree_header" }
%"struct.std::_Rb_tree_header" = type { %"struct.std::_Rb_tree_node_base", i64 }
%"struct.std::_Rb_tree_node_base" = type { i32, ptr, ptr, ptr }
%"class.std::condition_variable" = type { %"class.std::__condvar" }
%"class.std::__condvar" = type { %union.pthread_cond_t }
%union.pthread_cond_t = type { %struct.__pthread_cond_s }
%struct.__pthread_cond_s = type { %union.__atomic_wide_counter, %union.__atomic_wide_counter, [2 x i32], [2 x i32], i32, i32, [2 x i32] }
%union.__atomic_wide_counter = type { i64 }
%"struct.std::atomic.6" = type { %"struct.std::__atomic_base.7" }
%"struct.std::__atomic_base.7" = type { i64 }
%"class.std::vector.8" = type { %"struct.std::_Vector_base.9" }
%"struct.std::_Vector_base.9" = type { %"struct.std::_Vector_base<std::unique_ptr<xrlib::SWorkerThread>, std::allocator<std::unique_ptr<xrlib::SWorkerThread>>>::_Vector_impl" }
%"struct.std::_Vector_base<std::unique_ptr<xrlib::SWorkerThread>, std::allocator<std::unique_ptr<xrlib::SWorkerThread>>>::_Vector_impl" = type { %"struct.std::_Vector_base<std::unique_ptr<xrlib::SWorkerThread>, std::allocator<std::unique_ptr<xrlib::SWorkerThread>>>::_Vector_impl_data" }
%"struct.std::_Vector_base<std::unique_ptr<xrlib::SWorkerThread>, std::allocator<std::unique_ptr<xrlib::SWorkerThread>>>::_Vector_impl_data" = type { ptr, ptr, ptr }
%"class.std::queue.13" = type { %"class.std::deque.14" }
%"class.std::deque.14" = type { %"class.std::_Deque_base.15" }
%"class.std::_Deque_base.15" = type { %"struct.std::_Deque_base<unsigned long, std::allocator<unsigned long>>::_Deque_impl" }
%"struct.std::_Deque_base<unsigned long, std::allocator<unsigned long>>::_Deque_impl" = type { %"struct.std::_Deque_base<unsigned long, std::allocator<unsigned long>>::_Deque_impl_data" }
%"struct.std::_Deque_base<unsigned long, std::allocator<unsigned long>>::_Deque_impl_data" = type { ptr, i64, %"struct.std::_Deque_iterator.19", %"struct.std::_Deque_iterator.19" }
%"struct.std::_Deque_iterator.19" = type { ptr, ptr, ptr, ptr }
%"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" = type { %"class.std::thread::id" }
%"class.std::thread::id" = type { i64 }

@_ZN5xrlib11CThreadPool20SCALE_CHECK_INTERVALE = external dso_local constant i64, align 8

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

; Function Attrs: nounwind
declare noundef i32 @_ZNSt6thread20hardware_concurrencyEv() #1

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNKSt6atomicIbEcvbEv(ptr noundef nonnull align 1 dereferenceable(1)) #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN5xrlib11CThreadPool18ScaleWorkerThreadsEv(ptr noundef nonnull align 8 dereferenceable(504) %0) #3 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca %"class.std::chrono::duration", align 8
  %5 = alloca double, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %7 = call noundef i32 @_ZNSt6thread20hardware_concurrencyEv() #5
  %8 = sub i32 %7, 3
  %9 = zext i32 %8 to i64
  store i64 %9, ptr %3, align 8, !tbaa !9
  br label %10

10:                                               ; preds = %38, %1
  %11 = getelementptr inbounds %"class.xrlib::CThreadPool", ptr %6, i32 0, i32 0
  %12 = call noundef zeroext i1 @_ZNKSt6atomicIbEcvbEv(ptr noundef nonnull align 1 dereferenceable(1) %11) #5
  br i1 %12, label %13, label %39

13:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  call void @_ZNSt6chrono8durationIlSt5ratioILl1ELl1000EEEC2ImvEERKT_(ptr noundef nonnull align 8 dereferenceable(8) %4, ptr noundef nonnull align 8 dereferenceable(8) @_ZN5xrlib11CThreadPool20SCALE_CHECK_INTERVALE)
  call void @_ZNSt11this_thread9sleep_forIlSt5ratioILl1ELl1000EEEEvRKNSt6chrono8durationIT_T0_EE(ptr noundef nonnull align 8 dereferenceable(8) %4)
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %14 = getelementptr inbounds %"class.xrlib::CThreadPool", ptr %6, i32 0, i32 8
  %15 = call noundef i64 @_ZNKSt13__atomic_baseImEcvmEv(ptr noundef nonnull align 8 dereferenceable(8) %14) #5
  %16 = uitofp i64 %15 to double
  %17 = getelementptr inbounds %"class.xrlib::CThreadPool", ptr %6, i32 0, i32 9
  %18 = call noundef i64 @_ZNKSt13__atomic_baseImEcvmEv(ptr noundef nonnull align 8 dereferenceable(8) %17) #5
  %19 = uitofp i64 %18 to double
  %20 = fdiv double %16, %19
  store double %20, ptr %5, align 8, !tbaa !11
  %21 = load double, ptr %5, align 8, !tbaa !11
  %22 = fcmp oge double %21, 7.500000e-01
  br i1 %22, label %23, label %29

23:                                               ; preds = %13
  %24 = getelementptr inbounds %"class.xrlib::CThreadPool", ptr %6, i32 0, i32 9
  %25 = call noundef i64 @_ZNKSt13__atomic_baseImEcvmEv(ptr noundef nonnull align 8 dereferenceable(8) %24) #5
  %26 = load i64, ptr %3, align 8, !tbaa !9
  %27 = icmp ult i64 %25, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %23
  call void @_ZN5xrlib11CThreadPool15AddWorkerThreadEv(ptr noundef nonnull align 8 dereferenceable(504) %6)
  br label %38

29:                                               ; preds = %23, %13
  %30 = load double, ptr %5, align 8, !tbaa !11
  %31 = fcmp ole double %30, 2.500000e-01
  br i1 %31, label %32, label %37

32:                                               ; preds = %29
  %33 = getelementptr inbounds %"class.xrlib::CThreadPool", ptr %6, i32 0, i32 9
  %34 = call noundef i64 @_ZNKSt13__atomic_baseImEcvmEv(ptr noundef nonnull align 8 dereferenceable(8) %33) #5
  %35 = icmp ugt i64 %34, 2
  br i1 %35, label %36, label %37

36:                                               ; preds = %32
  call void @_ZN5xrlib11CThreadPool18RemoveWorkerThreadEv(ptr noundef nonnull align 8 dereferenceable(504) %6)
  br label %37

37:                                               ; preds = %36, %32, %29
  br label %38

38:                                               ; preds = %37, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  br label %10, !llvm.loop !13

39:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  ret void
}

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

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN5xrlib11CThreadPool15AddWorkerThreadEv(ptr noundef nonnull align 8 dereferenceable(504)) #3 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN5xrlib11CThreadPool18RemoveWorkerThreadEv(ptr noundef nonnull align 8 dereferenceable(504)) #3 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind "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 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 = { 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 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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !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"

%"class.std::chrono::duration" = type { i64 }

@_ZN5xrlib11CThreadPool20SCALE_CHECK_INTERVALE = external dso_local constant i64, align 8

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

; Function Attrs: nounwind
declare noundef i32 @_ZNSt6thread20hardware_concurrencyEv() local_unnamed_addr #1

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNKSt6atomicIbEcvbEv(ptr noundef nonnull align 1 dereferenceable(1)) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN5xrlib11CThreadPool18ScaleWorkerThreadsEv(ptr noundef nonnull align 8 dereferenceable(504) %0) local_unnamed_addr #3 align 2 {
  %2 = alloca %"class.std::chrono::duration", align 8
  %3 = tail call noundef i32 @_ZNSt6thread20hardware_concurrencyEv() #5
  %4 = add i32 %3, -3
  %5 = zext i32 %4 to i64
  %6 = tail call noundef zeroext i1 @_ZNKSt6atomicIbEcvbEv(ptr noundef nonnull align 1 dereferenceable(1) %0) #5
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1
  %7 = getelementptr inbounds i8, ptr %0, i64 336
  %8 = getelementptr inbounds i8, ptr %0, i64 344
  br label %9

9:                                                ; preds = %.lr.ph, %26
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #5
  call void @_ZNSt6chrono8durationIlSt5ratioILl1ELl1000EEEC2ImvEERKT_(ptr noundef nonnull align 8 dereferenceable(8) %2, ptr noundef nonnull align 8 dereferenceable(8) @_ZN5xrlib11CThreadPool20SCALE_CHECK_INTERVALE)
  call void @_ZNSt11this_thread9sleep_forIlSt5ratioILl1ELl1000EEEEvRKNSt6chrono8durationIT_T0_EE(ptr noundef nonnull align 8 dereferenceable(8) %2)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #5
  %10 = call noundef i64 @_ZNKSt13__atomic_baseImEcvmEv(ptr noundef nonnull align 8 dereferenceable(8) %7) #5
  %11 = uitofp i64 %10 to double
  %12 = call noundef i64 @_ZNKSt13__atomic_baseImEcvmEv(ptr noundef nonnull align 8 dereferenceable(8) %8) #5
  %13 = uitofp i64 %12 to double
  %14 = fdiv double %11, %13
  %15 = fcmp ult double %14, 7.500000e-01
  br i1 %15, label %20, label %16

16:                                               ; preds = %9
  %17 = call noundef i64 @_ZNKSt13__atomic_baseImEcvmEv(ptr noundef nonnull align 8 dereferenceable(8) %8) #5
  %18 = icmp ult i64 %17, %5
  br i1 %18, label %19, label %20

19:                                               ; preds = %16
  call void @_ZN5xrlib11CThreadPool15AddWorkerThreadEv(ptr noundef nonnull align 8 dereferenceable(504) %0)
  br label %26

20:                                               ; preds = %16, %9
  %21 = fcmp ugt double %14, 2.500000e-01
  br i1 %21, label %26, label %22

22:                                               ; preds = %20
  %23 = call noundef i64 @_ZNKSt13__atomic_baseImEcvmEv(ptr noundef nonnull align 8 dereferenceable(8) %8) #5
  %24 = icmp ugt i64 %23, 2
  br i1 %24, label %25, label %26

25:                                               ; preds = %22
  call void @_ZN5xrlib11CThreadPool18RemoveWorkerThreadEv(ptr noundef nonnull align 8 dereferenceable(504) %0)
  br label %26

26:                                               ; preds = %20, %22, %25, %19
  %27 = call noundef zeroext i1 @_ZNKSt6atomicIbEcvbEv(ptr noundef nonnull align 1 dereferenceable(1) %0) #5
  br i1 %27, label %9, label %._crit_edge, !llvm.loop !5

._crit_edge:                                      ; preds = %26, %1
  ret void
}

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

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN5xrlib11CThreadPool15AddWorkerThreadEv(ptr noundef nonnull align 8 dereferenceable(504)) local_unnamed_addr #3 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN5xrlib11CThreadPool18RemoveWorkerThreadEv(ptr noundef nonnull align 8 dereferenceable(504)) local_unnamed_addr #3 align 2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind "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 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 = { 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 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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