; LLVM IR from ponylang/ponyc :: src/libponyrt/mem (pool)

; 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.pool_local_t = type { ptr, i64, ptr, ptr }
%struct.pool_block_header_t = type { ptr, i64, i64 }
%struct.anon = type { ptr, i64 }
%struct.pool_global_t = type { i64, i64, %union.aba_protected_pool_central_t }
%union.aba_protected_pool_central_t = type { i128 }
%struct.pool_item_t = type { ptr }
%struct.pool_block_t = type { ptr, %union.anon, i64, i8 }
%union.anon = type { ptr }

@pool_local = external hidden thread_local global [16 x %struct.pool_local_t], align 16
@pool_block_header = external hidden thread_local global %struct.pool_block_header_t, align 8
@pool_global = external hidden global [16 x { i64, i64, { %struct.anon } }], align 16

; 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 speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #1

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

; Function Attrs: nounwind uwtable
declare hidden void @pool_push(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @ponyint_pool_thread_cleanup() #2 {
  %1 = alloca i64, align 8
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  store i64 0, ptr %1, align 8, !tbaa !5
  br label %7

7:                                                ; preds = %68, %0
  %8 = load i64, ptr %1, align 8, !tbaa !5
  %9 = icmp ult i64 %8, 16
  br i1 %9, label %11, label %10

10:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  br label %71

11:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %12 = call align 16 ptr @llvm.threadlocal.address.p0(ptr align 16 @pool_local)
  %13 = load i64, ptr %1, align 8, !tbaa !5
  %14 = getelementptr inbounds [16 x %struct.pool_local_t], ptr %12, i64 0, i64 %13
  store ptr %14, ptr %2, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %15 = load i64, ptr %1, align 8, !tbaa !5
  %16 = getelementptr inbounds [16 x %struct.pool_global_t], ptr @pool_global, i64 0, i64 %15
  store ptr %16, ptr %3, align 8, !tbaa !9
  br label %17

17:                                               ; preds = %36, %11
  %18 = load ptr, ptr %2, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.pool_local_t, ptr %18, i32 0, i32 2
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  %21 = load ptr, ptr %2, align 8, !tbaa !9
  %22 = getelementptr inbounds %struct.pool_local_t, ptr %21, i32 0, i32 3
  %23 = load ptr, ptr %22, align 8, !tbaa !13
  %24 = icmp ult ptr %20, %23
  br i1 %24, label %25, label %59

25:                                               ; preds = %17
  %26 = load ptr, ptr %2, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.pool_local_t, ptr %26, i32 0, i32 1
  %28 = load i64, ptr %27, align 8, !tbaa !14
  %29 = load ptr, ptr %3, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.pool_global_t, ptr %29, i32 0, i32 1
  %31 = load i64, ptr %30, align 8, !tbaa !15
  %32 = icmp eq i64 %28, %31
  br i1 %32, label %33, label %36

33:                                               ; preds = %25
  %34 = load ptr, ptr %2, align 8, !tbaa !9
  %35 = load ptr, ptr %3, align 8, !tbaa !9
  call void @pool_push(ptr noundef %34, ptr noundef %35)
  br label %36

36:                                               ; preds = %33, %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %37 = load ptr, ptr %2, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.pool_local_t, ptr %37, i32 0, i32 2
  %39 = load ptr, ptr %38, align 8, !tbaa !11
  store ptr %39, ptr %4, align 8, !tbaa !9
  %40 = load ptr, ptr %3, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.pool_global_t, ptr %40, i32 0, i32 0
  %42 = load i64, ptr %41, align 16, !tbaa !17
  %43 = load ptr, ptr %2, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.pool_local_t, ptr %43, i32 0, i32 2
  %45 = load ptr, ptr %44, align 8, !tbaa !11
  %46 = getelementptr inbounds i8, ptr %45, i64 %42
  store ptr %46, ptr %44, align 8, !tbaa !11
  %47 = load ptr, ptr %2, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.pool_local_t, ptr %47, i32 0, i32 0
  %49 = load ptr, ptr %48, align 8, !tbaa !18
  %50 = load ptr, ptr %4, align 8, !tbaa !9
  %51 = getelementptr inbounds %struct.pool_item_t, ptr %50, i32 0, i32 0
  store ptr %49, ptr %51, align 8, !tbaa !19
  %52 = load ptr, ptr %4, align 8, !tbaa !9
  %53 = load ptr, ptr %2, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.pool_local_t, ptr %53, i32 0, i32 0
  store ptr %52, ptr %54, align 8, !tbaa !18
  %55 = load ptr, ptr %2, align 8, !tbaa !9
  %56 = getelementptr inbounds %struct.pool_local_t, ptr %55, i32 0, i32 1
  %57 = load i64, ptr %56, align 8, !tbaa !14
  %58 = add i64 %57, 1
  store i64 %58, ptr %56, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %17, !llvm.loop !21

59:                                               ; preds = %17
  %60 = load ptr, ptr %2, align 8, !tbaa !9
  %61 = getelementptr inbounds %struct.pool_local_t, ptr %60, i32 0, i32 1
  %62 = load i64, ptr %61, align 8, !tbaa !14
  %63 = icmp ugt i64 %62, 0
  br i1 %63, label %64, label %67

64:                                               ; preds = %59
  %65 = load ptr, ptr %2, align 8, !tbaa !9
  %66 = load ptr, ptr %3, align 8, !tbaa !9
  call void @pool_push(ptr noundef %65, ptr noundef %66)
  br label %67

67:                                               ; preds = %64, %59
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  br label %68

68:                                               ; preds = %67
  %69 = load i64, ptr %1, align 8, !tbaa !5
  %70 = add i64 %69, 1
  store i64 %70, ptr %1, align 8, !tbaa !5
  br label %7, !llvm.loop !23

71:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %72 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @pool_block_header)
  %73 = getelementptr inbounds %struct.pool_block_header_t, ptr %72, i32 0, i32 0
  %74 = load ptr, ptr %73, align 8, !tbaa !24
  store ptr %74, ptr %5, align 8, !tbaa !9
  br label %75

75:                                               ; preds = %78, %71
  %76 = load ptr, ptr %5, align 8, !tbaa !9
  %77 = icmp ne ptr %76, null
  br i1 %77, label %78, label %85

78:                                               ; preds = %75
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %79 = load ptr, ptr %5, align 8, !tbaa !9
  %80 = getelementptr inbounds %struct.pool_block_t, ptr %79, i32 0, i32 1
  %81 = load ptr, ptr %80, align 8, !tbaa !26
  store ptr %81, ptr %6, align 8, !tbaa !9
  %82 = load ptr, ptr %5, align 8, !tbaa !9
  call void @pool_block_remove(ptr noundef %82)
  %83 = load ptr, ptr %5, align 8, !tbaa !9
  call void @pool_block_push(ptr noundef %83)
  %84 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %84, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %75, !llvm.loop !27

85:                                               ; preds = %75
  %86 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @pool_block_header)
  %87 = getelementptr inbounds %struct.pool_block_header_t, ptr %86, i32 0, i32 1
  store i64 0, ptr %87, align 8, !tbaa !28
  %88 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @pool_block_header)
  %89 = getelementptr inbounds %struct.pool_block_header_t, ptr %88, i32 0, i32 2
  store i64 0, ptr %89, align 8, !tbaa !29
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @pool_block_remove(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @pool_block_push(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { 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 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"pool_local_t", !10, i64 0, !6, i64 8, !10, i64 16, !10, i64 24}
!13 = !{!12, !10, i64 24}
!14 = !{!12, !6, i64 8}
!15 = !{!16, !6, i64 8}
!16 = !{!"pool_global_t", !6, i64 0, !6, i64 8, !7, i64 16}
!17 = !{!16, !6, i64 0}
!18 = !{!12, !10, i64 0}
!19 = !{!20, !10, i64 0}
!20 = !{!"pool_item_t", !10, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = distinct !{!23, !22}
!24 = !{!25, !10, i64 0}
!25 = !{!"pool_block_header_t", !10, i64 0, !6, i64 8, !6, i64 16}
!26 = !{!7, !7, i64 0}
!27 = distinct !{!27, !22}
!28 = !{!25, !6, i64 8}
!29 = !{!25, !6, i64 16}


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

%struct.pool_local_t = type { ptr, i64, ptr, ptr }
%struct.pool_block_header_t = type { ptr, i64, i64 }
%struct.anon = type { ptr, i64 }
%struct.pool_global_t = type { i64, i64, %union.aba_protected_pool_central_t }
%union.aba_protected_pool_central_t = type { i128 }

@pool_local = external hidden thread_local global [16 x %struct.pool_local_t], align 16
@pool_block_header = external hidden thread_local local_unnamed_addr global %struct.pool_block_header_t, align 8
@pool_global = external hidden global [16 x { i64, i64, { %struct.anon } }], align 16

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #0

; Function Attrs: nounwind uwtable
declare hidden void @pool_push(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @ponyint_pool_thread_cleanup() local_unnamed_addr #1 {
  %1 = tail call align 16 ptr @llvm.threadlocal.address.p0(ptr align 16 @pool_local)
  br label %5

2:                                                ; preds = %33
  %3 = tail call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @pool_block_header)
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %.not28 = icmp eq ptr %4, null
  br i1 %.not28, label %._crit_edge32, label %.lr.ph31

5:                                                ; preds = %0, %33
  %.027 = phi i64 [ 0, %0 ], [ %34, %33 ]
  %6 = getelementptr inbounds [16 x %struct.pool_local_t], ptr %1, i64 0, i64 %.027
  %7 = getelementptr inbounds [16 x %struct.pool_global_t], ptr @pool_global, i64 0, i64 %.027
  %8 = getelementptr inbounds i8, ptr %6, i64 16
  %9 = getelementptr inbounds i8, ptr %6, i64 24
  %10 = load ptr, ptr %8, align 16, !tbaa !11
  %11 = load ptr, ptr %9, align 8, !tbaa !13
  %12 = icmp ult ptr %10, %11
  %13 = getelementptr inbounds i8, ptr %6, i64 8
  br i1 %12, label %.lr.ph, label %.._crit_edge_crit_edge

.._crit_edge_crit_edge:                           ; preds = %5
  %.pre36 = load i64, ptr %13, align 8, !tbaa !14
  br label %._crit_edge

.lr.ph:                                           ; preds = %5
  %14 = getelementptr inbounds i8, ptr %7, i64 8
  %.pre = load i64, ptr %13, align 8, !tbaa !14
  br label %15

15:                                               ; preds = %.lr.ph, %22
  %16 = phi ptr [ %11, %.lr.ph ], [ %23, %22 ]
  %17 = phi ptr [ %10, %.lr.ph ], [ %27, %22 ]
  %18 = phi i64 [ %.pre, %.lr.ph ], [ %29, %22 ]
  %19 = load i64, ptr %14, align 8, !tbaa !15
  %20 = icmp eq i64 %18, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %15
  tail call void @pool_push(ptr noundef nonnull %6, ptr noundef nonnull %7)
  %.pre33 = load ptr, ptr %8, align 16, !tbaa !11
  %.pre34 = load i64, ptr %13, align 8, !tbaa !14
  %.pre35 = load ptr, ptr %9, align 8, !tbaa !13
  br label %22

22:                                               ; preds = %21, %15
  %23 = phi ptr [ %.pre35, %21 ], [ %16, %15 ]
  %24 = phi i64 [ %.pre34, %21 ], [ %18, %15 ]
  %25 = phi ptr [ %.pre33, %21 ], [ %17, %15 ]
  %26 = load i64, ptr %7, align 16, !tbaa !17
  %27 = getelementptr inbounds i8, ptr %25, i64 %26
  store ptr %27, ptr %8, align 16, !tbaa !11
  %28 = load ptr, ptr %6, align 16, !tbaa !18
  store ptr %28, ptr %25, align 8, !tbaa !19
  store ptr %25, ptr %6, align 16, !tbaa !18
  %29 = add i64 %24, 1
  store i64 %29, ptr %13, align 8, !tbaa !14
  %30 = icmp ult ptr %27, %23
  br i1 %30, label %15, label %._crit_edge, !llvm.loop !21

._crit_edge:                                      ; preds = %22, %.._crit_edge_crit_edge
  %31 = phi i64 [ %.pre36, %.._crit_edge_crit_edge ], [ %29, %22 ]
  %.not26 = icmp eq i64 %31, 0
  br i1 %.not26, label %33, label %32

32:                                               ; preds = %._crit_edge
  tail call void @pool_push(ptr noundef nonnull %6, ptr noundef nonnull %7)
  br label %33

33:                                               ; preds = %32, %._crit_edge
  %34 = add nuw nsw i64 %.027, 1
  %exitcond.not = icmp eq i64 %34, 16
  br i1 %exitcond.not, label %2, label %5, !llvm.loop !23

.lr.ph31:                                         ; preds = %2, %.lr.ph31
  %.02529 = phi ptr [ %36, %.lr.ph31 ], [ %4, %2 ]
  %35 = getelementptr inbounds i8, ptr %.02529, i64 8
  %36 = load ptr, ptr %35, align 8, !tbaa !24
  tail call void @pool_block_remove(ptr noundef nonnull %.02529)
  tail call void @pool_block_push(ptr noundef nonnull %.02529)
  %.not = icmp eq ptr %36, null
  br i1 %.not, label %._crit_edge32, label %.lr.ph31, !llvm.loop !25

._crit_edge32:                                    ; preds = %.lr.ph31, %2
  %37 = getelementptr inbounds i8, ptr %3, i64 8
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %37, i8 0, i64 16, i1 false)
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @pool_block_remove(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @pool_block_push(ptr noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!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 = !{!"pool_block_header_t", !7, i64 0, !10, i64 8, !10, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!12, !7, i64 16}
!12 = !{!"pool_local_t", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 24}
!13 = !{!12, !7, i64 24}
!14 = !{!12, !10, i64 8}
!15 = !{!16, !10, i64 8}
!16 = !{!"pool_global_t", !10, i64 0, !10, i64 8, !8, i64 16}
!17 = !{!16, !10, i64 0}
!18 = !{!12, !7, i64 0}
!19 = !{!20, !7, i64 0}
!20 = !{!"pool_item_t", !7, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = distinct !{!23, !22}
!24 = !{!8, !8, i64 0}
!25 = distinct !{!25, !22}


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