; LLVM IR from buzz-lang/Buzz :: src/buzz (buzzheap)

; 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.buzzvm_s = type { ptr, i32, i32, i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i16, ptr, ptr, ptr, ptr, i32, i32, ptr, i16, ptr, i32 }
%struct.buzzheap_s = type { ptr, i32, i16 }
%struct.buzzdarray_s = type { ptr, i64, i32, i32, ptr }
%struct.anon = type { i16, i16 }

; 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

declare void @buzzdict_foreach(ptr noundef, ptr noundef, ptr noundef) #1

declare void @buzzdarray_foreach(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @buzzheap_stack_mark(i32 noundef, ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @buzzheap_lsyms_mark(i32 noundef, ptr noundef, ptr noundef) #2

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @buzzheap_gc(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.buzzvm_s, ptr %6, i32 0, i32 10
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  store ptr %8, ptr %3, align 8, !tbaa !5
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.buzzheap_s, ptr %9, i32 0, i32 0
  %11 = load ptr, ptr %10, align 8, !tbaa !13
  %12 = getelementptr inbounds %struct.buzzdarray_s, ptr %11, i32 0, i32 1
  %13 = load i64, ptr %12, align 8, !tbaa !15
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.buzzheap_s, ptr %14, i32 0, i32 1
  %16 = load i32, ptr %15, align 8, !tbaa !18
  %17 = zext i32 %16 to i64
  %18 = icmp slt i64 %13, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %1
  store i32 1, ptr %4, align 4
  br label %107

20:                                               ; preds = %1
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.buzzheap_s, ptr %21, i32 0, i32 2
  %23 = load i16, ptr %22, align 4, !tbaa !19
  %24 = add i16 %23, 1
  store i16 %24, ptr %22, align 4, !tbaa !19
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.buzzvm_s, ptr %25, i32 0, i32 9
  %27 = load ptr, ptr %26, align 8, !tbaa !20
  call void @buzzstrman_gc_clear(ptr noundef %27)
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.buzzvm_s, ptr %28, i32 0, i32 8
  %30 = load ptr, ptr %29, align 8, !tbaa !21
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  call void @buzzdict_foreach(ptr noundef %30, ptr noundef @buzzheap_gsymobj_mark, ptr noundef %31)
  %32 = load ptr, ptr %2, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.buzzvm_s, ptr %32, i32 0, i32 5
  %34 = load ptr, ptr %33, align 8, !tbaa !22
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  call void @buzzdarray_foreach(ptr noundef %34, ptr noundef @buzzheap_stack_mark, ptr noundef %35)
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.buzzvm_s, ptr %36, i32 0, i32 7
  %38 = load ptr, ptr %37, align 8, !tbaa !23
  %39 = load ptr, ptr %2, align 8, !tbaa !5
  call void @buzzdarray_foreach(ptr noundef %38, ptr noundef @buzzheap_lsyms_mark, ptr noundef %39)
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.buzzvm_s, ptr %40, i32 0, i32 18
  %42 = load ptr, ptr %41, align 8, !tbaa !24
  %43 = load ptr, ptr %2, align 8, !tbaa !5
  call void @buzzdict_foreach(ptr noundef %42, ptr noundef @buzzheap_vstig_mark, ptr noundef %43)
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.buzzvm_s, ptr %44, i32 0, i32 19
  %46 = load ptr, ptr %45, align 8, !tbaa !25
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  call void @buzzdict_foreach(ptr noundef %46, ptr noundef @buzzheap_listener_mark, ptr noundef %47)
  %48 = load ptr, ptr %2, align 8, !tbaa !5
  call void @buzzoutmsg_gc(ptr noundef %48)
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.buzzheap_s, ptr %49, i32 0, i32 0
  %51 = load ptr, ptr %50, align 8, !tbaa !13
  %52 = getelementptr inbounds %struct.buzzdarray_s, ptr %51, i32 0, i32 1
  %53 = load i64, ptr %52, align 8, !tbaa !15
  %54 = sub nsw i64 %53, 1
  store i64 %54, ptr %5, align 8, !tbaa !26
  br label %55

55:                                               ; preds = %81, %20
  %56 = load i64, ptr %5, align 8, !tbaa !26
  %57 = icmp sge i64 %56, 0
  br i1 %57, label %58, label %84

58:                                               ; preds = %55
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.buzzheap_s, ptr %59, i32 0, i32 0
  %61 = load ptr, ptr %60, align 8, !tbaa !13
  %62 = getelementptr inbounds %struct.buzzdarray_s, ptr %61, i32 0, i32 0
  %63 = load ptr, ptr %62, align 8, !tbaa !27
  %64 = load i64, ptr %5, align 8, !tbaa !26
  %65 = getelementptr inbounds ptr, ptr %63, i64 %64
  %66 = load ptr, ptr %65, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.anon, ptr %66, i32 0, i32 1
  %68 = load i16, ptr %67, align 2, !tbaa !28
  %69 = zext i16 %68 to i32
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.buzzheap_s, ptr %70, i32 0, i32 2
  %72 = load i16, ptr %71, align 4, !tbaa !19
  %73 = zext i16 %72 to i32
  %74 = icmp ne i32 %69, %73
  br i1 %74, label %75, label %81

75:                                               ; preds = %58
  %76 = load ptr, ptr %3, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.buzzheap_s, ptr %76, i32 0, i32 0
  %78 = load ptr, ptr %77, align 8, !tbaa !13
  %79 = load i64, ptr %5, align 8, !tbaa !26
  %80 = trunc i64 %79 to i32
  call void @buzzdarray_remove(ptr noundef %78, i32 noundef %80)
  br label %81

81:                                               ; preds = %75, %58
  %82 = load i64, ptr %5, align 8, !tbaa !26
  %83 = add nsw i64 %82, -1
  store i64 %83, ptr %5, align 8, !tbaa !26
  br label %55, !llvm.loop !29

84:                                               ; preds = %55
  %85 = load ptr, ptr %2, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.buzzvm_s, ptr %85, i32 0, i32 9
  %87 = load ptr, ptr %86, align 8, !tbaa !20
  call void @buzzstrman_gc_prune(ptr noundef %87)
  %88 = load ptr, ptr %3, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.buzzheap_s, ptr %88, i32 0, i32 0
  %90 = load ptr, ptr %89, align 8, !tbaa !13
  %91 = getelementptr inbounds %struct.buzzdarray_s, ptr %90, i32 0, i32 1
  %92 = load i64, ptr %91, align 8, !tbaa !15
  %93 = icmp eq i64 %92, 0
  br i1 %93, label %94, label %95

94:                                               ; preds = %84
  br label %102

95:                                               ; preds = %84
  %96 = load ptr, ptr %3, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.buzzheap_s, ptr %96, i32 0, i32 0
  %98 = load ptr, ptr %97, align 8, !tbaa !13
  %99 = getelementptr inbounds %struct.buzzdarray_s, ptr %98, i32 0, i32 1
  %100 = load i64, ptr %99, align 8, !tbaa !15
  %101 = mul nsw i64 2, %100
  br label %102

102:                                              ; preds = %95, %94
  %103 = phi i64 [ 1, %94 ], [ %101, %95 ]
  %104 = trunc i64 %103 to i32
  %105 = load ptr, ptr %3, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.buzzheap_s, ptr %105, i32 0, i32 1
  store i32 %104, ptr %106, align 8, !tbaa !18
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  store i32 0, ptr %4, align 4
  br label %107

107:                                              ; preds = %102, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  %108 = load i32, ptr %4, align 4
  switch i32 %108, label %110 [
    i32 0, label %109
    i32 1, label %109
  ]

109:                                              ; preds = %107, %107
  ret void

110:                                              ; preds = %107
  unreachable
}

declare void @buzzstrman_gc_clear(ptr noundef) #1

declare void @buzzoutmsg_gc(ptr noundef) #1

declare void @buzzdarray_remove(ptr noundef, i32 noundef) #1

declare void @buzzstrman_gc_prune(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 72}
!10 = !{!"buzzvm_s", !6, i64 0, !11, i64 8, !11, i64 12, !11, i64 16, !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, !6, i64 96, !6, i64 104, !12, i64 112, !6, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !11, i64 152, !11, i64 156, !6, i64 160, !12, i64 168, !6, i64 176, !11, i64 184}
!11 = !{!"int", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"buzzheap_s", !6, i64 0, !11, i64 8, !12, i64 12}
!15 = !{!16, !17, i64 8}
!16 = !{!"buzzdarray_s", !6, i64 0, !17, i64 8, !11, i64 16, !11, i64 20, !6, i64 24}
!17 = !{!"long", !7, i64 0}
!18 = !{!14, !11, i64 8}
!19 = !{!14, !12, i64 12}
!20 = !{!10, !6, i64 64}
!21 = !{!10, !6, i64 56}
!22 = !{!10, !6, i64 32}
!23 = !{!10, !6, i64 48}
!24 = !{!10, !6, i64 136}
!25 = !{!10, !6, i64 144}
!26 = !{!17, !17, i64 0}
!27 = !{!16, !6, i64 0}
!28 = !{!7, !7, i64 0}
!29 = distinct !{!29, !30}
!30 = !{!"llvm.loop.mustprogress"}


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

declare void @buzzdict_foreach(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

declare void @buzzdarray_foreach(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @buzzheap_vstig_mark(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @buzzheap_listener_mark(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @buzzheap_gsymobj_mark(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @buzzheap_gc(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 72
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = load ptr, ptr %3, align 8, !tbaa !12
  %5 = getelementptr inbounds i8, ptr %4, i64 8
  %6 = load i64, ptr %5, align 8, !tbaa !14
  %7 = getelementptr inbounds i8, ptr %3, i64 8
  %8 = load i32, ptr %7, align 8, !tbaa !17
  %9 = zext i32 %8 to i64
  %10 = icmp slt i64 %6, %9
  br i1 %10, label %49, label %11

11:                                               ; preds = %1
  %12 = getelementptr inbounds i8, ptr %3, i64 12
  %13 = load i16, ptr %12, align 4, !tbaa !18
  %14 = add i16 %13, 1
  store i16 %14, ptr %12, align 4, !tbaa !18
  %15 = getelementptr inbounds i8, ptr %0, i64 64
  %16 = load ptr, ptr %15, align 8, !tbaa !19
  tail call void @buzzstrman_gc_clear(ptr noundef %16) #2
  %17 = getelementptr inbounds i8, ptr %0, i64 56
  %18 = load ptr, ptr %17, align 8, !tbaa !20
  tail call void @buzzdict_foreach(ptr noundef %18, ptr noundef nonnull @buzzheap_gsymobj_mark, ptr noundef nonnull %0) #2
  %19 = getelementptr inbounds i8, ptr %0, i64 32
  %20 = load ptr, ptr %19, align 8, !tbaa !21
  tail call void @buzzdarray_foreach(ptr noundef %20, ptr noundef nonnull @buzzheap_stack_mark, ptr noundef nonnull %0) #2
  %21 = getelementptr inbounds i8, ptr %0, i64 48
  %22 = load ptr, ptr %21, align 8, !tbaa !22
  tail call void @buzzdarray_foreach(ptr noundef %22, ptr noundef nonnull @buzzheap_lsyms_mark, ptr noundef nonnull %0) #2
  %23 = getelementptr inbounds i8, ptr %0, i64 136
  %24 = load ptr, ptr %23, align 8, !tbaa !23
  tail call void @buzzdict_foreach(ptr noundef %24, ptr noundef nonnull @buzzheap_vstig_mark, ptr noundef nonnull %0) #2
  %25 = getelementptr inbounds i8, ptr %0, i64 144
  %26 = load ptr, ptr %25, align 8, !tbaa !24
  tail call void @buzzdict_foreach(ptr noundef %26, ptr noundef nonnull @buzzheap_listener_mark, ptr noundef nonnull %0) #2
  tail call void @buzzoutmsg_gc(ptr noundef nonnull %0) #2
  %27 = load ptr, ptr %3, align 8, !tbaa !12
  %28 = getelementptr inbounds i8, ptr %27, i64 8
  %29 = load i64, ptr %28, align 8, !tbaa !14
  %30 = icmp sgt i64 %29, 0
  br i1 %30, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %11, %40
  %.029.in = phi i64 [ %.029, %40 ], [ %29, %11 ]
  %.029 = add nsw i64 %.029.in, -1
  %31 = load ptr, ptr %3, align 8, !tbaa !12
  %32 = load ptr, ptr %31, align 8, !tbaa !25
  %33 = getelementptr inbounds ptr, ptr %32, i64 %.029
  %34 = load ptr, ptr %33, align 8, !tbaa !26
  %35 = getelementptr inbounds i8, ptr %34, i64 2
  %36 = load i16, ptr %35, align 2, !tbaa !27
  %37 = load i16, ptr %12, align 4, !tbaa !18
  %.not = icmp eq i16 %36, %37
  br i1 %.not, label %40, label %38

38:                                               ; preds = %.lr.ph
  %39 = trunc i64 %.029 to i32
  tail call void @buzzdarray_remove(ptr noundef nonnull %31, i32 noundef %39) #2
  br label %40

40:                                               ; preds = %38, %.lr.ph
  %41 = icmp ugt i64 %.029.in, 1
  br i1 %41, label %.lr.ph, label %._crit_edge, !llvm.loop !28

._crit_edge:                                      ; preds = %40, %11
  %42 = load ptr, ptr %15, align 8, !tbaa !19
  tail call void @buzzstrman_gc_prune(ptr noundef %42) #2
  %43 = load ptr, ptr %3, align 8, !tbaa !12
  %44 = getelementptr inbounds i8, ptr %43, i64 8
  %45 = load i64, ptr %44, align 8, !tbaa !14
  %46 = icmp eq i64 %45, 0
  %.tr = trunc i64 %45 to i32
  %47 = shl i32 %.tr, 1
  %48 = select i1 %46, i32 1, i32 %47
  store i32 %48, ptr %7, align 8, !tbaa !17
  br label %49

49:                                               ; preds = %1, %._crit_edge
  ret void
}

declare void @buzzstrman_gc_clear(ptr noundef) local_unnamed_addr #0

declare void @buzzoutmsg_gc(ptr noundef) local_unnamed_addr #0

declare void @buzzdarray_remove(ptr noundef, i32 noundef) local_unnamed_addr #0

declare void @buzzstrman_gc_prune(ptr noundef) local_unnamed_addr #0

attributes #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 = { 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 = { 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, !7, i64 72}
!6 = !{!"buzzvm_s", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !7, i64 88, !7, i64 96, !7, i64 104, !11, i64 112, !7, i64 120, !7, i64 128, !7, i64 136, !7, i64 144, !10, i64 152, !10, i64 156, !7, i64 160, !11, i64 168, !7, i64 176, !10, i64 184}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"short", !8, i64 0}
!12 = !{!13, !7, i64 0}
!13 = !{!"buzzheap_s", !7, i64 0, !10, i64 8, !11, i64 12}
!14 = !{!15, !16, i64 8}
!15 = !{!"buzzdarray_s", !7, i64 0, !16, i64 8, !10, i64 16, !10, i64 20, !7, i64 24}
!16 = !{!"long", !8, i64 0}
!17 = !{!13, !10, i64 8}
!18 = !{!13, !11, i64 12}
!19 = !{!6, !7, i64 64}
!20 = !{!6, !7, i64 56}
!21 = !{!6, !7, i64 32}
!22 = !{!6, !7, i64 48}
!23 = !{!6, !7, i64 136}
!24 = !{!6, !7, i64 144}
!25 = !{!15, !7, i64 0}
!26 = !{!7, !7, i64 0}
!27 = !{!8, !8, i64 0}
!28 = distinct !{!28, !29}
!29 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass