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

; 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"

%"struct.sm::internal::TlsPoolBucket" = type { ptr, ptr, ptr, %"struct.std::array.5", i32, i32, i8 }
%"struct.std::array.5" = type { [7 x i32] }

; 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
define dso_local noundef ptr @_ZN2sm8internal13TlsPoolBucket7DestroyEv(ptr noundef nonnull align 8 dereferenceable(64) %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %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 4, ptr %3) #4
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %6

6:                                                ; preds = %28, %1
  %7 = load i32, ptr %3, align 4, !tbaa !9
  %8 = getelementptr inbounds %"struct.sm::internal::TlsPoolBucket", ptr %5, i32 0, i32 6
  %9 = load i8, ptr %8, align 4, !tbaa !11
  %10 = zext i8 %9 to i32
  %11 = icmp ult i32 %7, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %31

13:                                               ; preds = %6
  %14 = getelementptr inbounds %"struct.sm::internal::TlsPoolBucket", ptr %5, i32 0, i32 3
  %15 = load i32, ptr %3, align 4, !tbaa !9
  %16 = zext i32 %15 to i64
  %17 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIjLm7EEixEm(ptr noundef nonnull align 4 dereferenceable(28) %14, i64 noundef %16) #4
  %18 = load i32, ptr %17, align 4, !tbaa !9
  %19 = getelementptr inbounds %"struct.sm::internal::TlsPoolBucket", ptr %5, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !14
  %21 = getelementptr inbounds %"struct.sm::internal::TlsPoolBucket", ptr %5, i32 0, i32 5
  %22 = load i32, ptr %21, align 8, !tbaa !15
  %23 = zext i32 %22 to i64
  %24 = getelementptr inbounds i32, ptr %20, i64 %23
  store i32 %18, ptr %24, align 4, !tbaa !9
  %25 = getelementptr inbounds %"struct.sm::internal::TlsPoolBucket", ptr %5, i32 0, i32 5
  %26 = load i32, ptr %25, align 8, !tbaa !15
  %27 = add i32 %26, 1
  store i32 %27, ptr %25, align 8, !tbaa !15
  br label %28

28:                                               ; preds = %13
  %29 = load i32, ptr %3, align 4, !tbaa !9
  %30 = add i32 %29, 1
  store i32 %30, ptr %3, align 4, !tbaa !9
  br label %6, !llvm.loop !16

31:                                               ; preds = %12
  %32 = getelementptr inbounds %"struct.sm::internal::TlsPoolBucket", ptr %5, i32 0, i32 5
  %33 = load i32, ptr %32, align 8, !tbaa !15
  %34 = icmp ugt i32 %33, 0
  br i1 %34, label %35, label %38

35:                                               ; preds = %31
  %36 = getelementptr inbounds %"struct.sm::internal::TlsPoolBucket", ptr %5, i32 0, i32 5
  %37 = load i32, ptr %36, align 8, !tbaa !15
  call void @_ZN2sm8internal13TlsPoolBucket21ReturnL1CacheToMasterEj(ptr noundef nonnull align 8 dereferenceable(64) %5, i32 noundef %37)
  br label %38

38:                                               ; preds = %35, %31
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %39 = getelementptr inbounds %"struct.sm::internal::TlsPoolBucket", ptr %5, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !14
  store ptr %40, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %"struct.sm::internal::TlsPoolBucket", ptr %5, i32 0, i32 1
  store ptr null, ptr %41, align 8, !tbaa !14
  %42 = getelementptr inbounds %"struct.sm::internal::TlsPoolBucket", ptr %5, i32 0, i32 6
  store i8 0, ptr %42, align 4, !tbaa !11
  %43 = getelementptr inbounds %"struct.sm::internal::TlsPoolBucket", ptr %5, i32 0, i32 5
  store i32 0, ptr %43, align 8, !tbaa !15
  %44 = getelementptr inbounds %"struct.sm::internal::TlsPoolBucket", ptr %5, i32 0, i32 4
  store i32 0, ptr %44, align 4, !tbaa !18
  %45 = getelementptr inbounds %"struct.sm::internal::TlsPoolBucket", ptr %5, i32 0, i32 2
  store ptr null, ptr %45, align 8, !tbaa !19
  %46 = getelementptr inbounds %"struct.sm::internal::TlsPoolBucket", ptr %5, i32 0, i32 0
  store ptr null, ptr %46, align 8, !tbaa !20
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  ret ptr %47
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIjLm7EEixEm(ptr noundef nonnull align 4 dereferenceable(28), i64 noundef) #2 align 2

; Function Attrs: alwaysinline mustprogress uwtable
declare dso_local void @_ZN2sm8internal13TlsPoolBucket21ReturnL1CacheToMasterEj(ptr noundef nonnull align 8 dereferenceable(64), i32 noundef) #3 align 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 = { 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 = { alwaysinline 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !7, i64 60}
!12 = !{!"_ZTSN2sm8internal13TlsPoolBucketE", !6, i64 0, !6, i64 8, !6, i64 16, !13, i64 24, !10, i64 52, !10, i64 56, !7, i64 60}
!13 = !{!"_ZTSSt5arrayIjLm7EE", !7, i64 0}
!14 = !{!12, !6, i64 8}
!15 = !{!12, !10, i64 56}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!12, !10, i64 52}
!19 = !{!12, !6, i64 16}
!20 = !{!12, !6, 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"

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN2sm8internal13TlsPoolBucket7DestroyEv(ptr noundef nonnull align 8 dereferenceable(64) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 60
  %3 = load i8, ptr %2, align 4, !tbaa !5
  %.not6 = icmp eq i8 %3, 0
  br i1 %.not6, label %.._crit_edge_crit_edge, label %.lr.ph

.._crit_edge_crit_edge:                           ; preds = %1
  %.phi.trans.insert = getelementptr inbounds i8, ptr %0, i64 56
  %.pre = load i32, ptr %.phi.trans.insert, align 8, !tbaa !12
  br label %._crit_edge

.lr.ph:                                           ; preds = %1
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = getelementptr inbounds i8, ptr %0, i64 56
  br label %8

._crit_edge:                                      ; preds = %8, %.._crit_edge_crit_edge
  %7 = phi i32 [ %.pre, %.._crit_edge_crit_edge ], [ %16, %8 ]
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %21, label %20

8:                                                ; preds = %.lr.ph, %8
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %8 ]
  %9 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIjLm7EEixEm(ptr noundef nonnull align 4 dereferenceable(28) %4, i64 noundef %indvars.iv) #4
  %10 = load i32, ptr %9, align 4, !tbaa !13
  %11 = load ptr, ptr %5, align 8, !tbaa !14
  %12 = load i32, ptr %6, align 8, !tbaa !12
  %13 = zext i32 %12 to i64
  %14 = getelementptr inbounds i32, ptr %11, i64 %13
  store i32 %10, ptr %14, align 4, !tbaa !13
  %15 = load i32, ptr %6, align 8, !tbaa !12
  %16 = add i32 %15, 1
  store i32 %16, ptr %6, align 8, !tbaa !12
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %17 = load i8, ptr %2, align 4, !tbaa !5
  %18 = zext i8 %17 to i64
  %19 = icmp ult i64 %indvars.iv.next, %18
  br i1 %19, label %8, label %._crit_edge, !llvm.loop !15

20:                                               ; preds = %._crit_edge
  tail call void @_ZN2sm8internal13TlsPoolBucket21ReturnL1CacheToMasterEj(ptr noundef nonnull align 8 dereferenceable(64) %0, i32 noundef %7)
  br label %21

21:                                               ; preds = %20, %._crit_edge
  %22 = getelementptr inbounds i8, ptr %0, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !14
  %24 = getelementptr inbounds i8, ptr %0, i64 52
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %0, i8 0, i64 24, i1 false)
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(9) %24, i8 0, i64 9, i1 false)
  ret ptr %23
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIjLm7EEixEm(ptr noundef nonnull align 4 dereferenceable(28), i64 noundef) local_unnamed_addr #1 align 2

; Function Attrs: alwaysinline mustprogress uwtable
declare dso_local void @_ZN2sm8internal13TlsPoolBucket21ReturnL1CacheToMasterEj(ptr noundef nonnull align 8 dereferenceable(64), i32 noundef) local_unnamed_addr #2 align 2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 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 #2 = { alwaysinline 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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, !8, i64 60}
!6 = !{!"_ZTSN2sm8internal13TlsPoolBucketE", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24, !11, i64 52, !11, i64 56, !8, i64 60}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"_ZTSSt5arrayIjLm7EE", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!6, !11, i64 56}
!13 = !{!11, !11, i64 0}
!14 = !{!6, !7, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass