; 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.chunk_t = type { ptr, %union.anon, ptr }
%union.anon = type { %struct.anon.0, [8 x i8] }
%struct.anon.0 = type { i64 }
%struct.anon = type { i32, i32, i32 }
%struct._pony_type_t = type { i32, i32, i64, i32, i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i8, ptr, ptr, 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: nounwind uwtable
declare hidden void @final_large(ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden ptr @get_m(ptr noundef) #1

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

; Function Attrs: nounwind uwtable
declare hidden zeroext i1 @get_chunk_is_small_chunk(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i64 @get_small_chunk_size(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @ponyint_heap_free(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = call zeroext i1 @get_chunk_is_small_chunk(ptr noundef %7)
  br i1 %8, label %26, label %9

9:                                                ; preds = %2
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = call ptr @get_m(ptr noundef %11)
  %13 = icmp eq ptr %10, %12
  br i1 %13, label %14, label %25

14:                                               ; preds = %9
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  call void @final_large(ptr noundef %15, i32 noundef 0)
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.chunk_t, ptr %16, i32 0, i32 1
  %18 = getelementptr inbounds %struct.anon.0, ptr %17, i32 0, i32 0
  %19 = load i64, ptr %18, align 8, !tbaa !9
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = call ptr @get_m(ptr noundef %20)
  call void @ponyint_pool_free_size(i64 noundef %19, ptr noundef %21)
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.chunk_t, ptr %22, i32 0, i32 0
  store ptr null, ptr %23, align 8, !tbaa !10
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  call void @set_large_chunk_slot(ptr noundef %24, i32 noundef 1)
  br label %25

25:                                               ; preds = %14, %9
  br label %79

26:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = ptrtoint ptr %27 to i64
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = call i64 @get_small_chunk_size(ptr noundef %29)
  %31 = trunc i64 %30 to i32
  %32 = shl i32 32, %31
  %33 = sub nsw i32 %32, 1
  %34 = xor i32 %33, -1
  %35 = sext i32 %34 to i64
  %36 = and i64 %28, %35
  %37 = inttoptr i64 %36 to ptr
  store ptr %37, ptr %5, align 8, !tbaa !5
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = icmp eq ptr %38, %39
  br i1 %40, label %41, label %78

41:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = call ptr @get_m(ptr noundef %43)
  %45 = ptrtoint ptr %42 to i64
  %46 = ptrtoint ptr %44 to i64
  %47 = sub i64 %45, %46
  %48 = lshr i64 %47, 5
  %49 = trunc i64 %48 to i32
  %50 = shl i32 1, %49
  store i32 %50, ptr %6, align 4, !tbaa !12
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.chunk_t, ptr %51, i32 0, i32 1
  %53 = getelementptr inbounds %struct.anon, ptr %52, i32 0, i32 2
  %54 = load i32, ptr %53, align 8, !tbaa !9
  %55 = load i32, ptr %6, align 4, !tbaa !12
  %56 = and i32 %54, %55
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %58, label %71

58:                                               ; preds = %41
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = load ptr, ptr %59, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct._pony_type_t, ptr %60, i32 0, i32 13
  %62 = load ptr, ptr %61, align 8, !tbaa !14
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  call void %62(ptr noundef %63)
  %64 = load i32, ptr %6, align 4, !tbaa !12
  %65 = xor i32 %64, -1
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.chunk_t, ptr %66, i32 0, i32 1
  %68 = getelementptr inbounds %struct.anon, ptr %67, i32 0, i32 2
  %69 = load i32, ptr %68, align 8, !tbaa !9
  %70 = and i32 %69, %65
  store i32 %70, ptr %68, align 8, !tbaa !9
  br label %71

71:                                               ; preds = %58, %41
  %72 = load i32, ptr %6, align 4, !tbaa !12
  %73 = load ptr, ptr %3, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.chunk_t, ptr %73, i32 0, i32 1
  %75 = getelementptr inbounds %struct.anon, ptr %74, i32 0, i32 0
  %76 = load i32, ptr %75, align 8, !tbaa !9
  %77 = or i32 %76, %72
  store i32 %77, ptr %75, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %78

78:                                               ; preds = %71, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  br label %79

79:                                               ; preds = %78, %25
  ret void
}

declare void @ponyint_pool_free_size(i64 noundef, ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !6, i64 0}
!11 = !{!"chunk_t", !6, i64 0, !7, i64 8, !6, i64 24}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !6, i64 88}
!15 = !{!"_pony_type_t", !13, i64 0, !13, i64 4, !16, i64 8, !13, i64 16, !13, i64 20, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !13, i64 96, !17, i64 100, !6, i64 104, !6, i64 112, !6, i64 120}
!16 = !{!"long", !7, i64 0}
!17 = !{!"_Bool", !7, 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: nounwind uwtable
declare hidden void @final_large(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden ptr @get_m(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare hidden zeroext i1 @get_chunk_is_small_chunk(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden i64 @get_small_chunk_size(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @ponyint_heap_free(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = tail call zeroext i1 @get_chunk_is_small_chunk(ptr noundef %0)
  br i1 %3, label %11, label %4

4:                                                ; preds = %2
  %5 = tail call ptr @get_m(ptr noundef %0)
  %6 = icmp eq ptr %5, %1
  br i1 %6, label %7, label %40

7:                                                ; preds = %4
  tail call void @final_large(ptr noundef %0, i32 noundef 0)
  %8 = getelementptr inbounds i8, ptr %0, i64 8
  %9 = load i64, ptr %8, align 8, !tbaa !5
  %10 = tail call ptr @get_m(ptr noundef %0)
  tail call void @ponyint_pool_free_size(i64 noundef %9, ptr noundef %10) #2
  store ptr null, ptr %0, align 8, !tbaa !8
  tail call void @set_large_chunk_slot(ptr noundef nonnull %0, i32 noundef 1)
  br label %40

11:                                               ; preds = %2
  %12 = ptrtoint ptr %1 to i64
  %13 = tail call i64 @get_small_chunk_size(ptr noundef %0)
  %14 = trunc i64 %13 to i32
  %.neg = shl i32 -32, %14
  %15 = sext i32 %.neg to i64
  %16 = and i64 %15, %12
  %17 = inttoptr i64 %16 to ptr
  %18 = icmp eq ptr %17, %1
  br i1 %18, label %19, label %40

19:                                               ; preds = %11
  %20 = tail call ptr @get_m(ptr noundef %0)
  %21 = ptrtoint ptr %20 to i64
  %22 = sub i64 %16, %21
  %23 = lshr i64 %22, 5
  %24 = trunc i64 %23 to i32
  %25 = shl nuw i32 1, %24
  %26 = getelementptr inbounds i8, ptr %0, i64 8
  %27 = getelementptr inbounds i8, ptr %0, i64 16
  %28 = load i32, ptr %27, align 8, !tbaa !5
  %29 = and i32 %25, %28
  %.not = icmp eq i32 %29, 0
  br i1 %.not, label %37, label %30

30:                                               ; preds = %19
  %31 = load ptr, ptr %1, align 8, !tbaa !11
  %32 = getelementptr inbounds i8, ptr %31, i64 88
  %33 = load ptr, ptr %32, align 8, !tbaa !12
  tail call void %33(ptr noundef nonnull %1) #2
  %34 = xor i32 %25, -1
  %35 = load i32, ptr %27, align 8, !tbaa !5
  %36 = and i32 %35, %34
  store i32 %36, ptr %27, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %30, %19
  %38 = load i32, ptr %26, align 8, !tbaa !5
  %39 = or i32 %38, %25
  store i32 %39, ptr %26, align 8, !tbaa !5
  br label %40

40:                                               ; preds = %11, %37, %4, %7
  ret void
}

declare void @ponyint_pool_free_size(i64 noundef, ptr noundef) local_unnamed_addr #1

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 = { "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 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !10, i64 0}
!9 = !{!"chunk_t", !10, i64 0, !6, i64 8, !10, i64 24}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!10, !10, i64 0}
!12 = !{!13, !10, i64 88}
!13 = !{!"_pony_type_t", !14, i64 0, !14, i64 4, !15, i64 8, !14, i64 16, !14, i64 20, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !10, i64 72, !10, i64 80, !10, i64 88, !14, i64 96, !16, i64 100, !10, i64 104, !10, i64 112, !10, i64 120}
!14 = !{!"int", !6, i64 0}
!15 = !{!"long", !6, i64 0}
!16 = !{!"_Bool", !6, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass