; LLVM IR from humz2k/sugaryc :: ext/bdwgc/extra (gc)

; 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._GC_arrays = type { i64, i64, i64, ptr, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i32, i32, i64, i64, i64, ptr, ptr, i64, i32, i64, i64, i64, %struct.dl_hashtbl_s, %struct.dl_hashtbl_s, %struct.fnlz_roots_s, i32, ptr, i64, i64, i32, i64, i64, i64, ptr, [64 x ptr], [8 x i8], [64 x ptr], [129 x ptr], [129 x ptr], [129 x ptr], [2049 x i64], [129 x ptr], [4096 x i8], [4096 x i64], [4096 x i64], ptr, [2048 x %struct.roots], [512 x %struct.exclusion], [2048 x ptr] }
%struct.dl_hashtbl_s = type { ptr, i64, i32 }
%struct.fnlz_roots_s = type { ptr, ptr }
%struct.roots = type { ptr, ptr, ptr, i32 }
%struct.exclusion = type { ptr, ptr }
%struct.finalizable_object = type { %struct.hash_chain_entry, ptr, ptr, i64, ptr }
%struct.hash_chain_entry = type { i64, ptr }

@GC_arrays = external dso_local global %struct._GC_arrays, align 8
@GC_interrupt_finalizers = external hidden global i32, align 4

; 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: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #1

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

6:                                                ; preds = %54, %0
  %7 = call i32 @GC_should_invoke_finalizers()
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %9, label %55

9:                                                ; preds = %6
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %12, label %14

12:                                               ; preds = %9
  %13 = load i64, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 12), align 8, !tbaa !11
  store i64 %13, ptr %2, align 8, !tbaa !9
  br label %27

14:                                               ; preds = %9
  %15 = load i32, ptr @GC_interrupt_finalizers, align 4, !tbaa !5
  %16 = icmp ne i32 %15, 0
  %17 = zext i1 %16 to i32
  %18 = sext i32 %17 to i64
  %19 = call i64 @llvm.expect.i64(i64 %18, i64 0)
  %20 = icmp ne i64 %19, 0
  br i1 %20, label %21, label %26

21:                                               ; preds = %14
  %22 = load i32, ptr %1, align 4, !tbaa !5
  %23 = load i32, ptr @GC_interrupt_finalizers, align 4, !tbaa !5
  %24 = icmp uge i32 %22, %23
  br i1 %24, label %25, label %26

25:                                               ; preds = %21
  store i32 3, ptr %5, align 4
  br label %52

26:                                               ; preds = %21, %14
  br label %27

27:                                               ; preds = %26, %12
  %28 = load ptr, ptr getelementptr inbounds (%struct.fnlz_roots_s, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 37), i32 0, i32 1), align 8, !tbaa !16
  store ptr %28, ptr %3, align 8, !tbaa !17
  %29 = load ptr, ptr %3, align 8, !tbaa !17
  %30 = getelementptr inbounds %struct.finalizable_object, ptr %29, i32 0, i32 0
  %31 = getelementptr inbounds %struct.hash_chain_entry, ptr %30, i32 0, i32 1
  %32 = load ptr, ptr %31, align 8, !tbaa !18
  store ptr %32, ptr getelementptr inbounds (%struct.fnlz_roots_s, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 37), i32 0, i32 1), align 8, !tbaa !16
  %33 = load ptr, ptr %3, align 8, !tbaa !17
  %34 = getelementptr inbounds %struct.finalizable_object, ptr %33, i32 0, i32 0
  %35 = getelementptr inbounds %struct.hash_chain_entry, ptr %34, i32 0, i32 1
  store ptr null, ptr %35, align 8, !tbaa !18
  %36 = load ptr, ptr %3, align 8, !tbaa !17
  %37 = getelementptr inbounds %struct.finalizable_object, ptr %36, i32 0, i32 0
  %38 = getelementptr inbounds %struct.hash_chain_entry, ptr %37, i32 0, i32 0
  %39 = load i64, ptr %38, align 8, !tbaa !21
  %40 = inttoptr i64 %39 to ptr
  store ptr %40, ptr %4, align 8, !tbaa !17
  %41 = load ptr, ptr %3, align 8, !tbaa !17
  %42 = getelementptr inbounds %struct.finalizable_object, ptr %41, i32 0, i32 1
  %43 = load ptr, ptr %42, align 8, !tbaa !22
  %44 = load ptr, ptr %4, align 8, !tbaa !17
  %45 = load ptr, ptr %3, align 8, !tbaa !17
  %46 = getelementptr inbounds %struct.finalizable_object, ptr %45, i32 0, i32 2
  %47 = load ptr, ptr %46, align 8, !tbaa !23
  call void %43(ptr noundef %44, ptr noundef %47)
  %48 = load ptr, ptr %3, align 8, !tbaa !17
  %49 = getelementptr inbounds %struct.finalizable_object, ptr %48, i32 0, i32 2
  store ptr null, ptr %49, align 8, !tbaa !23
  %50 = load i32, ptr %1, align 4, !tbaa !5
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %1, align 4, !tbaa !5
  store i32 0, ptr %5, align 4
  br label %52

52:                                               ; preds = %27, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  %53 = load i32, ptr %5, align 4
  switch i32 %53, label %66 [
    i32 0, label %54
    i32 3, label %55
  ]

54:                                               ; preds = %52
  br label %6, !llvm.loop !24

55:                                               ; preds = %52, %6
  %56 = load i32, ptr %1, align 4, !tbaa !5
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %58, label %64

58:                                               ; preds = %55
  %59 = load i64, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 12), align 8, !tbaa !11
  %60 = load i64, ptr %2, align 8, !tbaa !9
  %61 = sub i64 %59, %60
  %62 = load i64, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 13), align 8, !tbaa !26
  %63 = add i64 %62, %61
  store i64 %63, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 13), align 8, !tbaa !26
  br label %64

64:                                               ; preds = %58, %55
  %65 = load i32, ptr %1, align 4, !tbaa !5
  store i32 1, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret i32 %65

66:                                               ; preds = %52
  unreachable
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @GC_should_invoke_finalizers() #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 96}
!12 = !{!"_GC_arrays", !10, i64 0, !10, i64 8, !10, i64 16, !13, 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, !10, i64 96, !10, i64 104, !13, i64 112, !13, i64 120, !13, i64 128, !13, i64 136, !13, i64 144, !13, i64 152, !13, i64 160, !13, i64 168, !6, i64 176, !6, i64 180, !6, i64 184, !10, i64 192, !10, i64 200, !10, i64 208, !13, i64 216, !13, i64 224, !10, i64 232, !6, i64 240, !10, i64 248, !10, i64 256, !10, i64 264, !14, i64 272, !14, i64 296, !15, i64 320, !6, i64 336, !13, i64 344, !10, i64 352, !10, i64 360, !6, i64 368, !10, i64 376, !10, i64 384, !10, i64 392, !13, i64 400, !7, i64 408, !7, i64 920, !7, i64 928, !7, i64 1440, !7, i64 2472, !7, i64 3504, !7, i64 4536, !7, i64 20928, !7, i64 21960, !7, i64 26056, !7, i64 58824, !13, i64 91592, !7, i64 91600, !7, i64 157136, !7, i64 165328}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!"dl_hashtbl_s", !13, i64 0, !10, i64 8, !6, i64 16}
!15 = !{!"fnlz_roots_s", !13, i64 0, !13, i64 8}
!16 = !{!12, !13, i64 328}
!17 = !{!13, !13, i64 0}
!18 = !{!19, !13, i64 8}
!19 = !{!"finalizable_object", !20, i64 0, !13, i64 16, !13, i64 24, !10, i64 32, !13, i64 40}
!20 = !{!"hash_chain_entry", !10, i64 0, !13, i64 8}
!21 = !{!19, !10, i64 0}
!22 = !{!19, !13, i64 16}
!23 = !{!19, !13, i64 24}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = !{!12, !10, i64 104}


; 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._GC_arrays = type { i64, i64, i64, ptr, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i32, i32, i64, i64, i64, ptr, ptr, i64, i32, i64, i64, i64, %struct.dl_hashtbl_s, %struct.dl_hashtbl_s, %struct.fnlz_roots_s, i32, ptr, i64, i64, i32, i64, i64, i64, ptr, [64 x ptr], [8 x i8], [64 x ptr], [129 x ptr], [129 x ptr], [129 x ptr], [2049 x i64], [129 x ptr], [4096 x i8], [4096 x i64], [4096 x i64], ptr, [2048 x %struct.roots], [512 x %struct.exclusion], [2048 x ptr] }
%struct.dl_hashtbl_s = type { ptr, i64, i32 }
%struct.fnlz_roots_s = type { ptr, ptr }
%struct.roots = type { ptr, ptr, ptr, i32 }
%struct.exclusion = type { ptr, ptr }

@GC_arrays = external dso_local local_unnamed_addr global %struct._GC_arrays, align 8
@GC_interrupt_finalizers = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @GC_invoke_finalizers() local_unnamed_addr #0 {
  %1 = tail call i32 @GC_should_invoke_finalizers()
  %.not32 = icmp eq i32 %1, 0
  br i1 %.not32, label %._crit_edge, label %2

2:                                                ; preds = %0
  %3 = load i64, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 96), align 8, !tbaa !5
  %4 = load ptr, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 328), align 8, !tbaa !14
  %5 = getelementptr inbounds i8, ptr %4, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !15
  store ptr %6, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 328), align 8, !tbaa !14
  store ptr null, ptr %5, align 8, !tbaa !15
  %7 = load i64, ptr %4, align 8, !tbaa !18
  %8 = inttoptr i64 %7 to ptr
  %9 = getelementptr inbounds i8, ptr %4, i64 16
  %10 = load ptr, ptr %9, align 8, !tbaa !19
  %11 = getelementptr inbounds i8, ptr %4, i64 24
  %12 = load ptr, ptr %11, align 8, !tbaa !20
  tail call void %10(ptr noundef %8, ptr noundef %12) #1
  store ptr null, ptr %11, align 8, !tbaa !20
  %13 = tail call i32 @GC_should_invoke_finalizers()
  %.not.peel = icmp eq i32 %13, 0
  br i1 %.not.peel, label %.thread25, label %.lr.ph.peel.next

.lr.ph.peel.next:                                 ; preds = %2, %16
  %.01334 = phi i32 [ %26, %16 ], [ 1, %2 ]
  %14 = load i32, ptr @GC_interrupt_finalizers, align 4, !tbaa !21
  %15 = add i32 %14, -1
  %or.cond.not = icmp ult i32 %15, %.01334
  br i1 %or.cond.not, label %.thread25, label %16, !prof !22

16:                                               ; preds = %.lr.ph.peel.next
  %17 = load ptr, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 328), align 8, !tbaa !14
  %18 = getelementptr inbounds i8, ptr %17, i64 8
  %19 = load ptr, ptr %18, align 8, !tbaa !15
  store ptr %19, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 328), align 8, !tbaa !14
  store ptr null, ptr %18, align 8, !tbaa !15
  %20 = load i64, ptr %17, align 8, !tbaa !18
  %21 = inttoptr i64 %20 to ptr
  %22 = getelementptr inbounds i8, ptr %17, i64 16
  %23 = load ptr, ptr %22, align 8, !tbaa !19
  %24 = getelementptr inbounds i8, ptr %17, i64 24
  %25 = load ptr, ptr %24, align 8, !tbaa !20
  tail call void %23(ptr noundef %21, ptr noundef %25) #1
  store ptr null, ptr %24, align 8, !tbaa !20
  %26 = add i32 %.01334, 1
  %27 = tail call i32 @GC_should_invoke_finalizers()
  %.not = icmp eq i32 %27, 0
  br i1 %.not, label %.thread25, label %.lr.ph.peel.next, !llvm.loop !23

.thread25:                                        ; preds = %16, %.lr.ph.peel.next, %2
  %.01328 = phi i32 [ 1, %2 ], [ %26, %16 ], [ %.01334, %.lr.ph.peel.next ]
  %28 = load i64, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 96), align 8, !tbaa !5
  %29 = sub i64 %28, %3
  %30 = load i64, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 104), align 8, !tbaa !25
  %31 = add i64 %29, %30
  store i64 %31, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 104), align 8, !tbaa !25
  br label %._crit_edge

._crit_edge:                                      ; preds = %0, %.thread25
  %.01329 = phi i32 [ %.01328, %.thread25 ], [ 0, %0 ]
  ret i32 %.01329
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @GC_should_invoke_finalizers() local_unnamed_addr #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 = { 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 96}
!6 = !{!"_GC_arrays", !7, i64 0, !7, i64 8, !7, i64 16, !10, 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, !10, i64 112, !10, i64 120, !10, i64 128, !10, i64 136, !10, i64 144, !10, i64 152, !10, i64 160, !10, i64 168, !11, i64 176, !11, i64 180, !11, i64 184, !7, i64 192, !7, i64 200, !7, i64 208, !10, i64 216, !10, i64 224, !7, i64 232, !11, i64 240, !7, i64 248, !7, i64 256, !7, i64 264, !12, i64 272, !12, i64 296, !13, i64 320, !11, i64 336, !10, i64 344, !7, i64 352, !7, i64 360, !11, i64 368, !7, i64 376, !7, i64 384, !7, i64 392, !10, i64 400, !8, i64 408, !8, i64 920, !8, i64 928, !8, i64 1440, !8, i64 2472, !8, i64 3504, !8, i64 4536, !8, i64 20928, !8, i64 21960, !8, i64 26056, !8, i64 58824, !10, i64 91592, !8, i64 91600, !8, i64 157136, !8, i64 165328}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!"dl_hashtbl_s", !10, i64 0, !7, i64 8, !11, i64 16}
!13 = !{!"fnlz_roots_s", !10, i64 0, !10, i64 8}
!14 = !{!6, !10, i64 328}
!15 = !{!16, !10, i64 8}
!16 = !{!"finalizable_object", !17, i64 0, !10, i64 16, !10, i64 24, !7, i64 32, !10, i64 40}
!17 = !{!"hash_chain_entry", !7, i64 0, !10, i64 8}
!18 = !{!16, !7, i64 0}
!19 = !{!16, !10, i64 16}
!20 = !{!16, !10, i64 24}
!21 = !{!11, !11, i64 0}
!22 = !{!"branch_weights", i32 1, i32 4001}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.peeled.count", i32 1}
!25 = !{!6, !7, i64 104}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopFullUnrollPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LowerExpectIntrinsicPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass