; LLVM IR from AmplitudeAudio/sdk :: src/Utils/SmMalloc/smmalloc.ll (ZN2sm9Allocator18DestroyThreadCacheEv)

; 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.sm::Allocator" = type { i64, i64, ptr, %"struct.std::array", [56 x i8], %"struct.std::array.0", %"class.std::unique_ptr", ptr, [40 x i8] }
%"struct.std::array" = type { [62 x ptr] }
%"struct.std::array.0" = type { [62 x %"struct.sm::Allocator::PoolBucket"] }
%"struct.sm::Allocator::PoolBucket" = type <{ %"struct.std::atomic", ptr, ptr, %"struct.std::atomic.1", [36 x i8] }>
%"struct.std::atomic" = type { %"struct.std::__atomic_base" }
%"struct.std::__atomic_base" = type { i64 }
%"struct.std::atomic.1" = type { %"struct.std::__atomic_base.2" }
%"struct.std::__atomic_base.2" = type { i32 }
%"class.std::unique_ptr" = type { %"struct.std::__uniq_ptr_data" }
%"struct.std::__uniq_ptr_data" = type { %"class.std::__uniq_ptr_impl" }
%"class.std::__uniq_ptr_impl" = type { %"class.std::tuple" }
%"class.std::tuple" = type { %"struct.std::_Tuple_impl" }
%"struct.std::_Tuple_impl" = type { %"struct.std::_Tuple_impl.3", %"struct.std::_Head_base.4" }
%"struct.std::_Tuple_impl.3" = type { %"struct.std::_Head_base" }
%"struct.std::_Head_base" = type { %"struct.sm::GenericAllocator::Deleter" }
%"struct.sm::GenericAllocator::Deleter" = type { ptr }
%"struct.std::_Head_base.4" = type { ptr }

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

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZN2sm8internal13TlsPoolBucket7DestroyEv(ptr noundef nonnull align 8 dereferenceable(64)) #1 align 2

declare noundef ptr @_ZN2sm12GetTlsBucketEm(i64 noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN2sm9Allocator18DestroyThreadCacheEv(ptr noundef nonnull align 64 dereferenceable(4568) %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %5 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store i64 0, ptr %3, align 8, !tbaa !9
  br label %6

6:                                                ; preds = %17, %1
  %7 = load i64, ptr %3, align 8, !tbaa !9
  %8 = icmp ult i64 %7, 62
  br i1 %8, label %10, label %9

9:                                                ; preds = %6
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %20

10:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %11 = load i64, ptr %3, align 8, !tbaa !9
  %12 = call noundef ptr @_ZN2sm12GetTlsBucketEm(i64 noundef %11)
  %13 = call noundef ptr @_ZN2sm8internal13TlsPoolBucket7DestroyEv(ptr noundef nonnull align 8 dereferenceable(64) %12)
  store ptr %13, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %"class.sm::Allocator", ptr %5, i32 0, i32 7
  %15 = load ptr, ptr %14, align 16, !tbaa !11
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  call void @_ZN2sm16GenericAllocator4FreeEPvS1_(ptr noundef %15, ptr noundef %16)
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %17

17:                                               ; preds = %10
  %18 = load i64, ptr %3, align 8, !tbaa !9
  %19 = add i64 %18, 1
  store i64 %19, ptr %3, align 8, !tbaa !9
  br label %6, !llvm.loop !24

20:                                               ; preds = %9
  ret void
}

declare void @_ZN2sm16GenericAllocator4FreeEPvS1_(ptr noundef, ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { "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 = { 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, !6, i64 4560}
!12 = !{!"_ZTSN2sm9AllocatorE", !10, i64 0, !10, i64 8, !6, i64 16, !13, i64 24, !14, i64 576, !15, i64 4544, !6, i64 4560}
!13 = !{!"_ZTSSt5arrayIPhLm62EE", !7, i64 0}
!14 = !{!"_ZTSSt5arrayIN2sm9Allocator10PoolBucketELm62EE", !7, i64 0}
!15 = !{!"_ZTSSt10unique_ptrIhN2sm16GenericAllocator7DeleterEE", !16, i64 0}
!16 = !{!"_ZTSSt15__uniq_ptr_dataIhN2sm16GenericAllocator7DeleterELb1ELb1EE", !17, i64 0}
!17 = !{!"_ZTSSt15__uniq_ptr_implIhN2sm16GenericAllocator7DeleterEE", !18, i64 0}
!18 = !{!"_ZTSSt5tupleIJPhN2sm16GenericAllocator7DeleterEEE", !19, i64 0}
!19 = !{!"_ZTSSt11_Tuple_implILm0EJPhN2sm16GenericAllocator7DeleterEEE", !20, i64 0, !23, i64 8}
!20 = !{!"_ZTSSt11_Tuple_implILm1EJN2sm16GenericAllocator7DeleterEEE", !21, i64 0}
!21 = !{!"_ZTSSt10_Head_baseILm1EN2sm16GenericAllocator7DeleterELb0EE", !22, i64 0}
!22 = !{!"_ZTSN2sm16GenericAllocator7DeleterE", !6, i64 0}
!23 = !{!"_ZTSSt10_Head_baseILm0EPhLb0EE", !6, i64 0}
!24 = distinct !{!24, !25}
!25 = !{!"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"

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

declare noundef ptr @_ZN2sm12GetTlsBucketEm(i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN2sm9Allocator18DestroyThreadCacheEv(ptr nocapture noundef nonnull readonly align 64 dereferenceable(4568) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 4560
  br label %4

3:                                                ; preds = %4
  ret void

4:                                                ; preds = %1, %4
  %.04 = phi i64 [ 0, %1 ], [ %8, %4 ]
  %5 = tail call noundef ptr @_ZN2sm12GetTlsBucketEm(i64 noundef %.04)
  %6 = tail call noundef ptr @_ZN2sm8internal13TlsPoolBucket7DestroyEv(ptr noundef nonnull align 8 dereferenceable(64) %5)
  %7 = load ptr, ptr %2, align 16, !tbaa !5
  tail call void @_ZN2sm16GenericAllocator4FreeEPvS1_(ptr noundef %7, ptr noundef %6)
  %8 = add nuw nsw i64 %.04, 1
  %exitcond.not = icmp eq i64 %8, 62
  br i1 %exitcond.not, label %3, label %4, !llvm.loop !22
}

declare void @_ZN2sm16GenericAllocator4FreeEPvS1_(ptr noundef, ptr noundef) local_unnamed_addr #1

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 = { "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, !10, i64 4560}
!6 = !{!"_ZTSN2sm9AllocatorE", !7, i64 0, !7, i64 8, !10, i64 16, !11, i64 24, !12, i64 576, !13, i64 4544, !10, i64 4560}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"_ZTSSt5arrayIPhLm62EE", !8, i64 0}
!12 = !{!"_ZTSSt5arrayIN2sm9Allocator10PoolBucketELm62EE", !8, i64 0}
!13 = !{!"_ZTSSt10unique_ptrIhN2sm16GenericAllocator7DeleterEE", !14, i64 0}
!14 = !{!"_ZTSSt15__uniq_ptr_dataIhN2sm16GenericAllocator7DeleterELb1ELb1EE", !15, i64 0}
!15 = !{!"_ZTSSt15__uniq_ptr_implIhN2sm16GenericAllocator7DeleterEE", !16, i64 0}
!16 = !{!"_ZTSSt5tupleIJPhN2sm16GenericAllocator7DeleterEEE", !17, i64 0}
!17 = !{!"_ZTSSt11_Tuple_implILm0EJPhN2sm16GenericAllocator7DeleterEEE", !18, i64 0, !21, i64 8}
!18 = !{!"_ZTSSt11_Tuple_implILm1EJN2sm16GenericAllocator7DeleterEEE", !19, i64 0}
!19 = !{!"_ZTSSt10_Head_baseILm1EN2sm16GenericAllocator7DeleterELb0EE", !20, i64 0}
!20 = !{!"_ZTSN2sm16GenericAllocator7DeleterE", !10, i64 0}
!21 = !{!"_ZTSSt10_Head_baseILm0EPhLb0EE", !10, i64 0}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass