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

; 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 }
%union.toggle_ref_u = type { ptr }

@GC_arrays = external global %struct._GC_arrays, align 8
@GC_on_abort = external global ptr, align 8
@GC_toggleref_callback = external hidden global ptr, align 8
@.str.3 = external hidden unnamed_addr constant [43 x i8], align 1
@GC_manual_vdb = external 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: noreturn nounwind
declare void @abort() #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 @GC_process_togglerefs() #2 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #5
  store i64 0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  store i32 0, ptr %3, align 4, !tbaa !9
  store i64 0, ptr %1, align 8, !tbaa !5
  br label %7

7:                                                ; preds = %54, %0
  %8 = load i64, ptr %1, align 8, !tbaa !5
  %9 = load i64, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 40), align 8, !tbaa !11
  %10 = icmp ult i64 %8, %9
  br i1 %10, label %11, label %57

11:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  %12 = load ptr, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 39), align 8, !tbaa !16
  %13 = load i64, ptr %1, align 8, !tbaa !5
  %14 = getelementptr inbounds %union.toggle_ref_u, ptr %12, i64 %13
  store ptr %14, ptr %4, align 8, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %15 = load ptr, ptr %4, align 8, !tbaa !17
  %16 = load ptr, ptr %15, align 8, !tbaa !18
  store ptr %16, ptr %5, align 8, !tbaa !17
  %17 = load ptr, ptr %5, align 8, !tbaa !17
  %18 = ptrtoint ptr %17 to i64
  %19 = and i64 %18, 1
  %20 = icmp ne i64 %19, 0
  br i1 %20, label %21, label %26

21:                                               ; preds = %11
  %22 = load ptr, ptr %4, align 8, !tbaa !17
  %23 = load i64, ptr %22, align 8, !tbaa !18
  %24 = xor i64 %23, -1
  %25 = inttoptr i64 %24 to ptr
  store ptr %25, ptr %5, align 8, !tbaa !17
  br label %26

26:                                               ; preds = %21, %11
  %27 = load ptr, ptr %5, align 8, !tbaa !17
  %28 = icmp eq ptr null, %27
  br i1 %28, label %29, label %30

29:                                               ; preds = %26
  store i32 4, ptr %6, align 4
  br label %51

30:                                               ; preds = %26
  %31 = load ptr, ptr @GC_toggleref_callback, align 8, !tbaa !17
  %32 = load ptr, ptr %5, align 8, !tbaa !17
  %33 = call i32 %31(ptr noundef %32)
  switch i32 %33, label %48 [
    i32 0, label %50
    i32 1, label %34
    i32 2, label %40
  ]

34:                                               ; preds = %30
  %35 = load ptr, ptr %5, align 8, !tbaa !17
  %36 = load ptr, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 39), align 8, !tbaa !16
  %37 = load i64, ptr %2, align 8, !tbaa !5
  %38 = add i64 %37, 1
  store i64 %38, ptr %2, align 8, !tbaa !5
  %39 = getelementptr inbounds %union.toggle_ref_u, ptr %36, i64 %37
  store ptr %35, ptr %39, align 8, !tbaa !18
  store i32 1, ptr %3, align 4, !tbaa !9
  br label %50

40:                                               ; preds = %30
  %41 = load ptr, ptr %5, align 8, !tbaa !17
  %42 = ptrtoint ptr %41 to i64
  %43 = xor i64 %42, -1
  %44 = load ptr, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 39), align 8, !tbaa !16
  %45 = load i64, ptr %2, align 8, !tbaa !5
  %46 = add i64 %45, 1
  store i64 %46, ptr %2, align 8, !tbaa !5
  %47 = getelementptr inbounds %union.toggle_ref_u, ptr %44, i64 %45
  store i64 %43, ptr %47, align 8, !tbaa !18
  br label %50

48:                                               ; preds = %30
  %49 = load ptr, ptr @GC_on_abort, align 8, !tbaa !17
  call void %49(ptr noundef @.str.3)
  call void @abort() #6
  unreachable

50:                                               ; preds = %40, %34, %30
  store i32 0, ptr %6, align 4
  br label %51

51:                                               ; preds = %50, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  %52 = load i32, ptr %6, align 4
  switch i32 %52, label %81 [
    i32 0, label %53
    i32 4, label %54
  ]

53:                                               ; preds = %51
  br label %54

54:                                               ; preds = %53, %51
  %55 = load i64, ptr %1, align 8, !tbaa !5
  %56 = add i64 %55, 1
  store i64 %56, ptr %1, align 8, !tbaa !5
  br label %7, !llvm.loop !19

57:                                               ; preds = %7
  %58 = load i64, ptr %2, align 8, !tbaa !5
  %59 = load i64, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 40), align 8, !tbaa !11
  %60 = icmp ult i64 %58, %59
  br i1 %60, label %61, label %70

61:                                               ; preds = %57
  %62 = load ptr, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 39), align 8, !tbaa !16
  %63 = load i64, ptr %2, align 8, !tbaa !5
  %64 = getelementptr inbounds %union.toggle_ref_u, ptr %62, i64 %63
  %65 = load i64, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 40), align 8, !tbaa !11
  %66 = load i64, ptr %2, align 8, !tbaa !5
  %67 = sub i64 %65, %66
  %68 = mul i64 %67, 8
  call void @llvm.memset.p0.i64(ptr align 8 %64, i8 0, i64 %68, i1 false)
  %69 = load i64, ptr %2, align 8, !tbaa !5
  store i64 %69, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 40), align 8, !tbaa !11
  br label %70

70:                                               ; preds = %61, %57
  %71 = load i32, ptr %3, align 4, !tbaa !9
  %72 = icmp ne i32 %71, 0
  br i1 %72, label %73, label %80

73:                                               ; preds = %70
  %74 = load i32, ptr @GC_manual_vdb, align 4, !tbaa !9
  %75 = icmp ne i32 %74, 0
  br i1 %75, label %76, label %78

76:                                               ; preds = %73
  %77 = load ptr, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 39), align 8, !tbaa !16
  call void @GC_dirty_inner(ptr noundef %77)
  br label %79

78:                                               ; preds = %73
  br label %79

79:                                               ; preds = %78, %76
  br label %80

80:                                               ; preds = %79, %70
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #5
  ret void

81:                                               ; preds = %51
  unreachable
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

declare void @GC_dirty_inner(ptr noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { noreturn nounwind "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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { "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 #5 = { nounwind }
attributes #6 = { noreturn 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 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 352}
!12 = !{!"_GC_arrays", !6, i64 0, !6, i64 8, !6, i64 16, !13, 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, !13, i64 112, !13, i64 120, !13, i64 128, !13, i64 136, !13, i64 144, !13, i64 152, !13, i64 160, !13, i64 168, !10, i64 176, !10, i64 180, !10, i64 184, !6, i64 192, !6, i64 200, !6, i64 208, !13, i64 216, !13, i64 224, !6, i64 232, !10, i64 240, !6, i64 248, !6, i64 256, !6, i64 264, !14, i64 272, !14, i64 296, !15, i64 320, !10, i64 336, !13, i64 344, !6, i64 352, !6, i64 360, !10, i64 368, !6, i64 376, !6, i64 384, !6, 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, !6, i64 8, !10, i64 16}
!15 = !{!"fnlz_roots_s", !13, i64 0, !13, i64 8}
!16 = !{!12, !13, i64 344}
!17 = !{!13, !13, i64 0}
!18 = !{!7, !7, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"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"

%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 }
%union.toggle_ref_u = type { ptr }

@GC_arrays = external local_unnamed_addr global %struct._GC_arrays, align 8
@GC_on_abort = external local_unnamed_addr global ptr, align 8
@GC_toggleref_callback = external hidden local_unnamed_addr global ptr, align 8
@.str.3 = external hidden unnamed_addr constant [43 x i8], align 1
@GC_manual_vdb = external local_unnamed_addr global i32, align 4

; Function Attrs: noreturn nounwind
declare void @abort() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @GC_process_togglerefs() local_unnamed_addr #1 {
  %1 = load i64, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 352), align 8, !tbaa !5
  %.not31 = icmp eq i64 %1, 0
  br i1 %.not31, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %0, %25
  %.01728 = phi i64 [ %26, %25 ], [ 0, %0 ]
  %.01827 = phi i32 [ %.2, %25 ], [ 0, %0 ]
  %.01926 = phi i64 [ %.221, %25 ], [ 0, %0 ]
  %2 = load ptr, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 344), align 8, !tbaa !14
  %3 = getelementptr inbounds %union.toggle_ref_u, ptr %2, i64 %.01728
  %4 = load ptr, ptr %3, align 8
  %5 = ptrtoint ptr %4 to i64
  %6 = and i64 %5, 1
  %.not = icmp eq i64 %6, 0
  %7 = xor i64 %5, -1
  %8 = inttoptr i64 %7 to ptr
  %.0 = select i1 %.not, ptr %4, ptr %8
  %9 = icmp eq ptr %.0, null
  br i1 %9, label %25, label %10

10:                                               ; preds = %.lr.ph
  %11 = load ptr, ptr @GC_toggleref_callback, align 8, !tbaa !15
  %12 = tail call i32 %11(ptr noundef nonnull %.0) #4
  switch i32 %12, label %23 [
    i32 0, label %25
    i32 1, label %13
    i32 2, label %17
  ]

13:                                               ; preds = %10
  %14 = load ptr, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 344), align 8, !tbaa !14
  %15 = add i64 %.01926, 1
  %16 = getelementptr inbounds %union.toggle_ref_u, ptr %14, i64 %.01926
  store ptr %.0, ptr %16, align 8, !tbaa !16
  br label %25

17:                                               ; preds = %10
  %18 = ptrtoint ptr %.0 to i64
  %19 = xor i64 %18, -1
  %20 = load ptr, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 344), align 8, !tbaa !14
  %21 = add i64 %.01926, 1
  %22 = getelementptr inbounds %union.toggle_ref_u, ptr %20, i64 %.01926
  store i64 %19, ptr %22, align 8, !tbaa !16
  br label %25

23:                                               ; preds = %10
  %24 = load ptr, ptr @GC_on_abort, align 8, !tbaa !15
  tail call void %24(ptr noundef nonnull @.str.3) #4
  tail call void @abort() #5
  unreachable

25:                                               ; preds = %10, %13, %17, %.lr.ph
  %.221 = phi i64 [ %.01926, %.lr.ph ], [ %21, %17 ], [ %15, %13 ], [ %.01926, %10 ]
  %.2 = phi i32 [ %.01827, %.lr.ph ], [ %.01827, %17 ], [ 1, %13 ], [ %.01827, %10 ]
  %26 = add nuw i64 %.01728, 1
  %27 = load i64, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 352), align 8, !tbaa !5
  %28 = icmp ult i64 %26, %27
  br i1 %28, label %.lr.ph, label %._crit_edge, !llvm.loop !17

._crit_edge:                                      ; preds = %25
  %29 = icmp ne i32 %.2, 0
  %30 = icmp ult i64 %.221, %27
  br i1 %30, label %31, label %36

31:                                               ; preds = %._crit_edge
  %32 = load ptr, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 344), align 8, !tbaa !14
  %33 = getelementptr inbounds %union.toggle_ref_u, ptr %32, i64 %.221
  %34 = sub i64 %27, %.221
  %35 = shl i64 %34, 3
  tail call void @llvm.memset.p0.i64(ptr align 8 %33, i8 0, i64 %35, i1 false)
  store i64 %.221, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 352), align 8, !tbaa !5
  br label %36

36:                                               ; preds = %31, %._crit_edge
  %37 = load i32, ptr @GC_manual_vdb, align 4
  %38 = icmp ne i32 %37, 0
  %or.cond = select i1 %29, i1 %38, i1 false
  br i1 %or.cond, label %39, label %.thread

39:                                               ; preds = %36
  %40 = load ptr, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 344), align 8, !tbaa !14
  tail call void @GC_dirty_inner(ptr noundef %40) #4
  br label %.thread

.thread:                                          ; preds = %0, %39, %36
  ret void
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

declare void @GC_dirty_inner(ptr noundef) local_unnamed_addr #3

attributes #0 = { noreturn nounwind "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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 }
attributes #5 = { noreturn 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 352}
!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 344}
!15 = !{!10, !10, i64 0}
!16 = !{!8, !8, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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