; LLVM IR from brewlin/garbage-collect :: copying-or-mark (copying)

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

@from = external dso_local global i32, align 4
@to = external dso_local global i32, align 4
@gc_heaps = external global [100000 x %struct.gc_heap], align 16
@free_p = external dso_local global ptr, align 8
@root_used = external global i64, align 8
@roots = external global [100000 x %struct.root_], align 16
@.str.4 = external hidden unnamed_addr constant [20 x i8], align 1

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

declare void @gc_free(ptr noundef) #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
declare dso_local void @remove_from() #2

declare i32 @printf(ptr noundef, ...) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @gc_mark_or_copy(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @copy_reference() #2

; Function Attrs: nounwind uwtable
define dso_local void @gc() #2 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = call i32 (ptr, ...) @printf(ptr noundef @.str.4)
  %4 = load i32, ptr @to, align 4, !tbaa !5
  %5 = sext i32 %4 to i64
  %6 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %5
  %7 = getelementptr inbounds %struct.gc_heap, ptr %6, i32 0, i32 1
  %8 = load i64, ptr %7, align 8, !tbaa !9
  %9 = load i32, ptr @to, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %10
  %12 = getelementptr inbounds %struct.gc_heap, ptr %11, i32 0, i32 0
  %13 = load ptr, ptr %12, align 16, !tbaa !13
  %14 = getelementptr inbounds %struct.header, ptr %13, i32 0, i32 2
  store i64 %8, ptr %14, align 8, !tbaa !14
  %15 = load i32, ptr @to, align 4, !tbaa !5
  %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
  %19 = load ptr, ptr %18, align 16, !tbaa !13
  store ptr %19, ptr @free_p, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %44, %0
  %21 = load i32, ptr %1, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = load i64, ptr @root_used, align 8, !tbaa !17
  %24 = icmp ult i64 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  br label %47

26:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  %27 = load i32, ptr %1, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [100000 x %struct.root_], ptr @roots, i64 0, i64 %28
  %30 = getelementptr inbounds %struct.root_, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 16, !tbaa !18
  %32 = call ptr @gc_mark_or_copy(ptr noundef %31)
  store ptr %32, ptr %2, align 8, !tbaa !16
  %33 = load ptr, ptr %2, align 8, !tbaa !16
  %34 = load i32, ptr %1, align 4, !tbaa !5
  %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
  %38 = load ptr, ptr %37, align 8, !tbaa !20
  store ptr %33, ptr %38, align 8, !tbaa !16
  %39 = load ptr, ptr %2, align 8, !tbaa !16
  %40 = load i32, ptr %1, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [100000 x %struct.root_], ptr @roots, i64 0, i64 %41
  %43 = getelementptr inbounds %struct.root_, ptr %42, i32 0, i32 0
  store ptr %39, ptr %43, align 16, !tbaa !18
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  br label %44

44:                                               ; preds = %26
  %45 = load i32, ptr %1, align 4, !tbaa !5
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %1, align 4, !tbaa !5
  br label %20, !llvm.loop !21

47:                                               ; preds = %25
  call void @copy_reference()
  call void @gc_sweep()
  %48 = load ptr, ptr @free_p, align 8, !tbaa !16
  %49 = load i32, ptr @to, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %50
  %52 = getelementptr inbounds %struct.gc_heap, ptr %51, i32 0, i32 0
  %53 = load ptr, ptr %52, align 16, !tbaa !13
  %54 = load i32, ptr @to, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %55
  %57 = getelementptr inbounds %struct.gc_heap, ptr %56, i32 0, i32 1
  %58 = load i64, ptr %57, align 8, !tbaa !9
  %59 = getelementptr inbounds i8, ptr %53, i64 %58
  %60 = icmp ult ptr %48, %59
  br i1 %60, label %61, label %64

61:                                               ; preds = %47
  %62 = load ptr, ptr @free_p, align 8, !tbaa !16
  %63 = getelementptr inbounds %struct.header, ptr %62, i64 1
  call void @gc_free(ptr noundef %63)
  br label %64

64:                                               ; preds = %61, %47
  call void @remove_from()
  %65 = load i32, ptr @from, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %66
  %68 = getelementptr inbounds %struct.gc_heap, ptr %67, i32 0, i32 0
  %69 = load ptr, ptr %68, align 16, !tbaa !13
  %70 = load i32, ptr @from, align 4, !tbaa !5
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %71
  %73 = getelementptr inbounds %struct.gc_heap, ptr %72, i32 0, i32 1
  %74 = load i64, ptr %73, align 8, !tbaa !9
  %75 = add i64 %74, 48
  call void @llvm.memset.p0.i64(ptr align 8 %69, i8 0, i64 %75, i1 false)
  %76 = load i32, ptr @from, align 4, !tbaa !5
  store i32 %76, ptr @to, align 4, !tbaa !5
  %77 = load i32, ptr @from, align 4, !tbaa !5
  %78 = add nsw i32 %77, 1
  %79 = srem i32 %78, 10
  store i32 %79, ptr @from, align 4, !tbaa !5
  ret void
}

declare void @gc_sweep() #1

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

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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, !12, i64 8}
!10 = !{!"gc_heap", !11, i64 0, !12, i64 8}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!10, !11, i64 0}
!14 = !{!15, !12, i64 16}
!15 = !{!"header", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !11, i64 32, !11, i64 40}
!16 = !{!11, !11, i64 0}
!17 = !{!12, !12, i64 0}
!18 = !{!19, !11, i64 0}
!19 = !{!"root_", !11, i64 0, !11, i64 8}
!20 = !{!19, !11, i64 8}
!21 = distinct !{!21, !22}
!22 = !{!"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_heap = type { ptr, i64 }
%struct.root_ = type { ptr, ptr }

@from = external dso_local local_unnamed_addr global i32, align 4
@to = external dso_local local_unnamed_addr global i32, align 4
@gc_heaps = external local_unnamed_addr global [100000 x %struct.gc_heap], align 16
@free_p = external dso_local local_unnamed_addr global ptr, align 8
@root_used = external local_unnamed_addr global i64, align 8
@roots = external local_unnamed_addr global [100000 x %struct.root_], align 16
@.str.4 = external hidden unnamed_addr constant [20 x i8], align 1

declare void @gc_free(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @remove_from() local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @gc_mark_or_copy(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @copy_reference() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @gc() local_unnamed_addr #1 {
  %1 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4)
  %2 = load i32, ptr @to, align 4, !tbaa !5
  %3 = sext i32 %2 to i64
  %4 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %3
  %5 = getelementptr inbounds i8, ptr %4, i64 8
  %6 = load i64, ptr %5, align 8, !tbaa !9
  %7 = load ptr, ptr %4, align 16, !tbaa !13
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  store i64 %6, ptr %8, align 8, !tbaa !14
  store ptr %7, ptr @free_p, align 8, !tbaa !16
  %9 = load i64, ptr @root_used, align 8, !tbaa !17
  %.not = icmp eq i64 %9, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %0
  tail call void @copy_reference()
  tail call void @gc_sweep() #4
  %10 = load ptr, ptr @free_p, align 8, !tbaa !16
  %11 = load i32, ptr @to, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %12
  %14 = load ptr, ptr %13, align 16, !tbaa !13
  %15 = getelementptr inbounds i8, ptr %13, i64 8
  %16 = load i64, ptr %15, align 8, !tbaa !9
  %17 = getelementptr inbounds i8, ptr %14, i64 %16
  %18 = icmp ult ptr %10, %17
  br i1 %18, label %26, label %28

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %19 = getelementptr inbounds [100000 x %struct.root_], ptr @roots, i64 0, i64 %indvars.iv
  %20 = load ptr, ptr %19, align 16, !tbaa !18
  %21 = tail call ptr @gc_mark_or_copy(ptr noundef %20)
  %22 = getelementptr inbounds i8, ptr %19, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !20
  store ptr %21, ptr %23, align 8, !tbaa !16
  store ptr %21, ptr %19, align 16, !tbaa !18
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %24 = load i64, ptr @root_used, align 8, !tbaa !17
  %25 = icmp ugt i64 %24, %indvars.iv.next
  br i1 %25, label %.lr.ph, label %._crit_edge, !llvm.loop !21

26:                                               ; preds = %._crit_edge
  %27 = getelementptr inbounds i8, ptr %10, i64 48
  tail call void @gc_free(ptr noundef nonnull %27) #4
  br label %28

28:                                               ; preds = %26, %._crit_edge
  tail call void @remove_from()
  %29 = load i32, ptr @from, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %30
  %32 = load ptr, ptr %31, align 16, !tbaa !13
  %33 = getelementptr inbounds i8, ptr %31, i64 8
  %34 = load i64, ptr %33, align 8, !tbaa !9
  %35 = add i64 %34, 48
  tail call void @llvm.memset.p0.i64(ptr align 8 %32, i8 0, i64 %35, i1 false)
  %36 = load i32, ptr @from, align 4, !tbaa !5
  store i32 %36, ptr @to, align 4, !tbaa !5
  %37 = add nsw i32 %36, 1
  %38 = srem i32 %37, 10
  store i32 %38, ptr @from, align 4, !tbaa !5
  ret void
}

declare void @gc_sweep() local_unnamed_addr #0

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

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 = { nofree 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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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, !12, i64 8}
!10 = !{!"gc_heap", !11, i64 0, !12, i64 8}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!10, !11, i64 0}
!14 = !{!15, !12, i64 16}
!15 = !{!"header", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !11, i64 32, !11, i64 40}
!16 = !{!11, !11, i64 0}
!17 = !{!12, !12, i64 0}
!18 = !{!19, !11, i64 0}
!19 = !{!"root_", !11, i64 0, !11, i64 8}
!20 = !{!19, !11, i64 8}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


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