; 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.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 }

@pool_local = external hidden thread_local global [16 x %struct.pool_local_t], align 16
@.str = external hidden unnamed_addr constant [19 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [73 x i8], align 1
@__func__.ponyint_pool_free = external hidden unnamed_addr constant [18 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [31 x i8], align 1
@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
define dso_local void @ponyint_pool_free(i64 noundef %0, ptr noundef %1) #2 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  %8 = load i64, ptr %3, align 8, !tbaa !5
  %9 = icmp ult i64 %8, 16
  br i1 %9, label %10, label %11

10:                                               ; preds = %2
  br label %12

11:                                               ; preds = %2
  call void @ponyint_assert_fail(ptr noundef @.str, ptr noundef @.str.1, i64 noundef 873, ptr noundef @__func__.ponyint_pool_free)
  br label %12

12:                                               ; preds = %11, %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %13 = call align 16 ptr @llvm.threadlocal.address.p0(ptr align 16 @pool_local)
  %14 = load i64, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds [16 x %struct.pool_local_t], ptr %13, i64 0, i64 %14
  store ptr %15, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %16 = load i64, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds [16 x %struct.pool_global_t], ptr @pool_global, i64 0, i64 %16
  store ptr %17, ptr %6, align 8, !tbaa !9
  %18 = load ptr, ptr %5, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.pool_local_t, ptr %18, i32 0, i32 1
  %20 = load i64, ptr %19, align 8, !tbaa !11
  %21 = load ptr, ptr %6, align 8, !tbaa !9
  %22 = getelementptr inbounds %struct.pool_global_t, ptr %21, i32 0, i32 1
  %23 = load i64, ptr %22, align 8, !tbaa !13
  %24 = icmp eq i64 %20, %23
  br i1 %24, label %25, label %28

25:                                               ; preds = %12
  %26 = load ptr, ptr %5, align 8, !tbaa !9
  %27 = load ptr, ptr %6, align 8, !tbaa !9
  call void @pool_push(ptr noundef %26, ptr noundef %27)
  br label %28

28:                                               ; preds = %25, %12
  %29 = load ptr, ptr %5, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.pool_local_t, ptr %29, i32 0, i32 1
  %31 = load i64, ptr %30, align 8, !tbaa !11
  %32 = load ptr, ptr %6, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.pool_global_t, ptr %32, i32 0, i32 1
  %34 = load i64, ptr %33, align 8, !tbaa !13
  %35 = icmp ult i64 %31, %34
  br i1 %35, label %36, label %37

36:                                               ; preds = %28
  br label %38

37:                                               ; preds = %28
  call void @ponyint_assert_fail(ptr noundef @.str.2, ptr noundef @.str.1, i64 noundef 887, ptr noundef @__func__.ponyint_pool_free)
  br label %38

38:                                               ; preds = %37, %36
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %39 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %39, ptr %7, align 8, !tbaa !9
  %40 = load ptr, ptr %5, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.pool_local_t, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !15
  %43 = load ptr, ptr %7, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.pool_item_t, ptr %43, i32 0, i32 0
  store ptr %42, ptr %44, align 8, !tbaa !16
  %45 = load ptr, ptr %7, align 8, !tbaa !9
  %46 = load ptr, ptr %5, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.pool_local_t, ptr %46, i32 0, i32 0
  store ptr %45, ptr %47, align 8, !tbaa !15
  %48 = load ptr, ptr %5, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.pool_local_t, ptr %48, i32 0, i32 1
  %50 = load i64, ptr %49, align 8, !tbaa !11
  %51 = add i64 %50, 1
  store i64 %51, ptr %49, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret void
}

declare void @ponyint_assert_fail(ptr noundef, ptr noundef, i64 noundef, ptr noundef) #3

; Function Attrs: nounwind uwtable
declare hidden void @pool_push(ptr noundef, 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 = { "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 = !{!"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, !6, i64 8}
!12 = !{!"pool_local_t", !10, i64 0, !6, i64 8, !10, i64 16, !10, i64 24}
!13 = !{!14, !6, i64 8}
!14 = !{!"pool_global_t", !6, i64 0, !6, i64 8, !7, i64 16}
!15 = !{!12, !10, i64 0}
!16 = !{!17, !10, i64 0}
!17 = !{!"pool_item_t", !10, 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"

%struct.pool_local_t = type { ptr, i64, ptr, ptr }
%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
@.str = external hidden unnamed_addr constant [19 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [73 x i8], align 1
@__func__.ponyint_pool_free = external hidden unnamed_addr constant [18 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [31 x i8], align 1
@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
define dso_local void @ponyint_pool_free(i64 noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = icmp ult i64 %0, 16
  br i1 %3, label %5, label %4

4:                                                ; preds = %2
  tail call void @ponyint_assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i64 noundef 873, ptr noundef nonnull @__func__.ponyint_pool_free) #3
  br label %5

5:                                                ; preds = %2, %4
  %6 = tail call align 16 ptr @llvm.threadlocal.address.p0(ptr align 16 @pool_local)
  %7 = getelementptr inbounds [16 x %struct.pool_local_t], ptr %6, i64 0, i64 %0
  %8 = getelementptr inbounds [16 x %struct.pool_global_t], ptr @pool_global, i64 0, i64 %0
  %9 = getelementptr inbounds i8, ptr %7, i64 8
  %10 = load i64, ptr %9, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %8, i64 8
  %12 = load i64, ptr %11, align 8, !tbaa !11
  %13 = icmp eq i64 %10, %12
  br i1 %13, label %14, label %15

14:                                               ; preds = %5
  tail call void @pool_push(ptr noundef nonnull %7, ptr noundef nonnull %8)
  %.pre = load i64, ptr %9, align 8, !tbaa !5
  %.pre14 = load i64, ptr %11, align 8, !tbaa !11
  br label %15

15:                                               ; preds = %14, %5
  %16 = phi i64 [ %.pre14, %14 ], [ %12, %5 ]
  %17 = phi i64 [ %.pre, %14 ], [ %10, %5 ]
  %18 = icmp ult i64 %17, %16
  br i1 %18, label %20, label %19

19:                                               ; preds = %15
  tail call void @ponyint_assert_fail(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.1, i64 noundef 887, ptr noundef nonnull @__func__.ponyint_pool_free) #3
  %.pre15 = load i64, ptr %9, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %15, %19
  %21 = phi i64 [ %17, %15 ], [ %.pre15, %19 ]
  %22 = load ptr, ptr %7, align 16, !tbaa !13
  store ptr %22, ptr %1, align 8, !tbaa !14
  store ptr %1, ptr %7, align 16, !tbaa !13
  %23 = add i64 %21, 1
  store i64 %23, ptr %9, align 8, !tbaa !5
  ret void
}

declare void @ponyint_assert_fail(ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

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

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 = { "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, !10, i64 8}
!6 = !{!"pool_local_t", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"pool_global_t", !10, i64 0, !10, i64 8, !8, i64 16}
!13 = !{!6, !7, i64 0}
!14 = !{!15, !7, i64 0}
!15 = !{!"pool_item_t", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass