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

; 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.heap_t = type { [5 x ptr], [5 x ptr], ptr, [4 x ptr], i64, i64 }

; Function Attrs: nounwind uwtable
define dso_local void @ponyint_heap_destroy(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.heap_t, ptr %5, i32 0, i32 2
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  call void @chunk_list(ptr noundef @destroy_large, ptr noundef %7, i32 noundef 0)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !12
  br label %8

8:                                                ; preds = %30, %1
  %9 = load i32, ptr %3, align 4, !tbaa !12
  %10 = icmp slt i32 %9, 4
  br i1 %10, label %12, label %11

11:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %33

12:                                               ; preds = %8
  %13 = load i32, ptr %3, align 4, !tbaa !12
  %14 = icmp eq i32 0, %13
  br i1 %14, label %15, label %22

15:                                               ; preds = %12
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.heap_t, ptr %16, i32 0, i32 3
  %18 = load i32, ptr %3, align 4, !tbaa !12
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [4 x ptr], ptr %17, i64 0, i64 %19
  %21 = load ptr, ptr %20, align 8, !tbaa !5
  call void @chunk_list(ptr noundef @destroy_mixed, ptr noundef %21, i32 noundef 0)
  br label %29

22:                                               ; preds = %12
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.heap_t, ptr %23, i32 0, i32 3
  %25 = load i32, ptr %3, align 4, !tbaa !12
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [4 x ptr], ptr %24, i64 0, i64 %26
  %28 = load ptr, ptr %27, align 8, !tbaa !5
  call void @chunk_list(ptr noundef @destroy_large, ptr noundef %28, i32 noundef 0)
  br label %29

29:                                               ; preds = %22, %15
  br label %30

30:                                               ; preds = %29
  %31 = load i32, ptr %3, align 4, !tbaa !12
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %3, align 4, !tbaa !12
  br label %8, !llvm.loop !14

33:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !12
  br label %34

34:                                               ; preds = %51, %33
  %35 = load i32, ptr %4, align 4, !tbaa !12
  %36 = icmp slt i32 %35, 5
  br i1 %36, label %38, label %37

37:                                               ; preds = %34
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %54

38:                                               ; preds = %34
  %39 = load ptr, ptr %2, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.heap_t, ptr %39, i32 0, i32 0
  %41 = load i32, ptr %4, align 4, !tbaa !12
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [5 x ptr], ptr %40, i64 0, i64 %42
  %44 = load ptr, ptr %43, align 8, !tbaa !5
  call void @chunk_list(ptr noundef @destroy_small, ptr noundef %44, i32 noundef 0)
  %45 = load ptr, ptr %2, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.heap_t, ptr %45, i32 0, i32 1
  %47 = load i32, ptr %4, align 4, !tbaa !12
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [5 x ptr], ptr %46, i64 0, i64 %48
  %50 = load ptr, ptr %49, align 8, !tbaa !5
  call void @chunk_list(ptr noundef @destroy_small, ptr noundef %50, i32 noundef 0)
  br label %51

51:                                               ; preds = %38
  %52 = load i32, ptr %4, align 4, !tbaa !12
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %4, align 4, !tbaa !12
  br label %34, !llvm.loop !16

54:                                               ; preds = %37
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @chunk_list(ptr noundef, ptr noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @destroy_large(ptr noundef, i32 noundef) #0

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

; Function Attrs: nounwind uwtable
declare hidden void @destroy_mixed(ptr noundef, i32 noundef) #0

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

; Function Attrs: nounwind uwtable
declare hidden void @destroy_small(ptr noundef, i32 noundef) #0

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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/C++ TBAA"}
!9 = !{!10, !6, i64 80}
!10 = !{!"heap_t", !7, i64 0, !7, i64 40, !6, i64 80, !7, i64 88, !11, i64 120, !11, i64 128}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; 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: nounwind uwtable
define dso_local void @ponyint_heap_destroy(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
.preheader:
  %1 = getelementptr inbounds i8, ptr %0, i64 80
  %2 = load ptr, ptr %1, align 8, !tbaa !5
  tail call void @chunk_list(ptr noundef nonnull @destroy_large, ptr noundef %2, i32 noundef 0)
  %3 = getelementptr inbounds i8, ptr %0, i64 88
  %4 = load ptr, ptr %3, align 8, !tbaa !11
  tail call void @chunk_list(ptr noundef nonnull @destroy_mixed, ptr noundef %4, i32 noundef 0)
  %5 = getelementptr inbounds i8, ptr %0, i64 96
  %6 = load ptr, ptr %5, align 8, !tbaa !11
  tail call void @chunk_list(ptr noundef nonnull @destroy_large, ptr noundef %6, i32 noundef 0)
  %7 = getelementptr inbounds i8, ptr %0, i64 104
  %8 = load ptr, ptr %7, align 8, !tbaa !11
  tail call void @chunk_list(ptr noundef nonnull @destroy_large, ptr noundef %8, i32 noundef 0)
  %9 = getelementptr inbounds i8, ptr %0, i64 112
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  tail call void @chunk_list(ptr noundef nonnull @destroy_large, ptr noundef %10, i32 noundef 0)
  %.pre = load ptr, ptr %0, align 8, !tbaa !11
  %11 = getelementptr inbounds i8, ptr %0, i64 40
  tail call void @chunk_list(ptr noundef nonnull @destroy_small, ptr noundef %.pre, i32 noundef 0)
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  tail call void @chunk_list(ptr noundef nonnull @destroy_small, ptr noundef %12, i32 noundef 0)
  %13 = getelementptr inbounds i8, ptr %0, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  tail call void @chunk_list(ptr noundef nonnull @destroy_small, ptr noundef %14, i32 noundef 0)
  %15 = getelementptr inbounds i8, ptr %0, i64 48
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  tail call void @chunk_list(ptr noundef nonnull @destroy_small, ptr noundef %16, i32 noundef 0)
  %17 = getelementptr inbounds i8, ptr %0, i64 16
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  tail call void @chunk_list(ptr noundef nonnull @destroy_small, ptr noundef %18, i32 noundef 0)
  %19 = getelementptr inbounds i8, ptr %0, i64 56
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  tail call void @chunk_list(ptr noundef nonnull @destroy_small, ptr noundef %20, i32 noundef 0)
  %21 = getelementptr inbounds i8, ptr %0, i64 24
  %22 = load ptr, ptr %21, align 8, !tbaa !11
  tail call void @chunk_list(ptr noundef nonnull @destroy_small, ptr noundef %22, i32 noundef 0)
  %23 = getelementptr inbounds i8, ptr %0, i64 64
  %24 = load ptr, ptr %23, align 8, !tbaa !11
  tail call void @chunk_list(ptr noundef nonnull @destroy_small, ptr noundef %24, i32 noundef 0)
  %25 = getelementptr inbounds i8, ptr %0, i64 32
  %26 = load ptr, ptr %25, align 8, !tbaa !11
  tail call void @chunk_list(ptr noundef nonnull @destroy_small, ptr noundef %26, i32 noundef 0)
  %27 = getelementptr inbounds i8, ptr %0, i64 72
  %28 = load ptr, ptr %27, align 8, !tbaa !11
  tail call void @chunk_list(ptr noundef nonnull @destroy_small, ptr noundef %28, i32 noundef 0)
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @chunk_list(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @destroy_large(ptr noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @destroy_mixed(ptr noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @destroy_small(ptr noundef, i32 noundef) #0

attributes #0 = { 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" }

!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, !9, i64 80}
!6 = !{!"heap_t", !7, i64 0, !7, i64 40, !9, i64 80, !7, i64 88, !10, i64 120, !10, i64 128}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"any pointer", !7, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!9, !9, i64 0}


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