; LLVM IR from brewlin/garbage-collect :: mark-sweep (test)

; 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_heap = type { ptr, i64 }
%struct.root_ = type { ptr, ptr }

@free_list = external global ptr, align 8
@gc_heaps_used = external global i64, align 8
@gc_heaps = external global [100000 x %struct.gc_heap], align 16
@root_used = external global i64, align 8
@roots = external global [100000 x %struct.root_], align 16

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

4:                                                ; preds = %19, %0
  %5 = load i32, ptr %2, align 4, !tbaa !9
  %6 = sext i32 %5 to i64
  %7 = load i64, ptr @gc_heaps_used, align 8, !tbaa !11
  %8 = icmp ule i64 %6, %7
  br i1 %8, label %10, label %9

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

10:                                               ; preds = %4
  %11 = load i32, ptr %2, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %12
  %14 = getelementptr inbounds %struct.gc_heap, ptr %13, i32 0, i32 1
  store i64 0, ptr %14, align 8, !tbaa !13
  %15 = load i32, ptr %2, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %16
  %18 = getelementptr inbounds %struct.gc_heap, ptr %17, i32 0, i32 0
  store ptr null, ptr %18, align 16, !tbaa !15
  br label %19

19:                                               ; preds = %10
  %20 = load i32, ptr %2, align 4, !tbaa !9
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %2, align 4, !tbaa !9
  br label %4, !llvm.loop !16

22:                                               ; preds = %9
  store i64 0, ptr @gc_heaps_used, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %23

23:                                               ; preds = %38, %22
  %24 = load i32, ptr %3, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = load i64, ptr @root_used, align 8, !tbaa !11
  %27 = icmp ule i64 %25, %26
  br i1 %27, label %29, label %28

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

29:                                               ; preds = %23
  %30 = load i32, ptr %3, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [100000 x %struct.root_], ptr @roots, i64 0, i64 %31
  %33 = getelementptr inbounds %struct.root_, ptr %32, i32 0, i32 0
  store ptr null, ptr %33, align 16, !tbaa !18
  %34 = load i32, ptr %3, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [100000 x %struct.root_], ptr @roots, i64 0, i64 %35
  %37 = getelementptr inbounds %struct.root_, ptr %36, i32 0, i32 1
  store ptr null, ptr %37, align 8, !tbaa !20
  br label %38

38:                                               ; preds = %29
  %39 = load i32, ptr %3, align 4, !tbaa !9
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %3, align 4, !tbaa !9
  br label %23, !llvm.loop !21

41:                                               ; preds = %28
  store i64 0, ptr @root_used, align 8, !tbaa !11
  %42 = load i32, ptr %1, align 4
  ret i32 %42
}

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !12, i64 8}
!14 = !{!"gc_heap", !6, i64 0, !12, i64 8}
!15 = !{!14, !6, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!19, !6, i64 0}
!19 = !{!"root_", !6, i64 0, !6, i64 8}
!20 = !{!19, !6, i64 8}
!21 = distinct !{!21, !17}


; 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_heap = type { ptr, i64 }
%struct.root_ = type { ptr, ptr }

@free_list = external local_unnamed_addr global ptr, align 8
@gc_heaps_used = external local_unnamed_addr global i64, align 8
@gc_heaps = external local_unnamed_addr global [100000 x %struct.gc_heap], align 16
@root_used = external local_unnamed_addr global i64, align 8
@roots = external local_unnamed_addr global [100000 x %struct.root_], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local i32 @clear() local_unnamed_addr #0 {
  store ptr null, ptr @free_list, align 8, !tbaa !5
  %1 = load i64, ptr @gc_heaps_used, align 8, !tbaa !9
  %2 = shl i64 %1, 4
  %3 = add i64 %2, 16
  tail call void @llvm.memset.p0.i64(ptr nonnull align 16 @gc_heaps, i8 0, i64 %3, i1 false), !tbaa !11
  store i64 0, ptr @gc_heaps_used, align 8, !tbaa !9
  %4 = load i64, ptr @root_used, align 8, !tbaa !9
  %5 = shl i64 %4, 4
  %6 = add i64 %5, 16
  tail call void @llvm.memset.p0.i64(ptr nonnull align 16 @roots, i8 0, i64 %6, i1 false), !tbaa !5
  store i64 0, ptr @root_used, align 8, !tbaa !9
  ret i32 undef
}

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

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, inaccessiblemem: none) 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 nounwind willreturn memory(argmem: write) }

!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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass