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

@from = external dso_local global i32, align 4
@gc_heaps = external global [100000 x %struct.gc_heap], align 16
@free_list = external global ptr, align 8

; 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: nounwind uwtable
define dso_local void @remove_from() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  %5 = load ptr, ptr @free_list, align 8, !tbaa !5
  store ptr %5, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  %6 = load i32, ptr @from, align 4, !tbaa !9
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %7
  %9 = getelementptr inbounds %struct.gc_heap, ptr %8, i32 0, i32 0
  %10 = load ptr, ptr %9, align 16, !tbaa !11
  store ptr %10, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %11 = load i32, ptr @from, 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 0
  %15 = load ptr, ptr %14, align 16, !tbaa !11
  %16 = getelementptr inbounds %struct.header, ptr %15, i64 48
  %17 = load i32, ptr @from, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %18
  %20 = getelementptr inbounds %struct.gc_heap, ptr %19, i32 0, i32 1
  %21 = load i64, ptr %20, align 8, !tbaa !14
  %22 = getelementptr inbounds %struct.header, ptr %16, i64 %21
  store ptr %22, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %23 = load ptr, ptr %1, align 8, !tbaa !5
  store ptr %23, ptr %4, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %52, %0
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = icmp ne ptr %25, null
  br i1 %26, label %28, label %27

27:                                               ; preds = %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %57

28:                                               ; preds = %24
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = icmp ule ptr %29, %30
  br i1 %31, label %32, label %51

32:                                               ; preds = %28
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = icmp uge ptr %33, %34
  br i1 %35, label %36, label %51

36:                                               ; preds = %32
  %37 = load ptr, ptr @free_list, align 8, !tbaa !5
  %38 = load ptr, ptr %1, align 8, !tbaa !5
  %39 = icmp eq ptr %37, %38
  br i1 %39, label %40, label %44

40:                                               ; preds = %36
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.header, ptr %41, i32 0, i32 4
  %43 = load ptr, ptr %42, align 8, !tbaa !15
  store ptr %43, ptr @free_list, align 8, !tbaa !5
  br label %50

44:                                               ; preds = %36
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.header, ptr %45, i32 0, i32 4
  %47 = load ptr, ptr %46, align 8, !tbaa !15
  %48 = load ptr, ptr %1, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.header, ptr %48, i32 0, i32 4
  store ptr %47, ptr %49, align 8, !tbaa !15
  br label %50

50:                                               ; preds = %44, %40
  br label %51

51:                                               ; preds = %50, %32, %28
  br label %52

52:                                               ; preds = %51
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %53, ptr %1, align 8, !tbaa !5
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.header, ptr %54, i32 0, i32 4
  %56 = load ptr, ptr %55, align 8, !tbaa !15
  store ptr %56, ptr %4, align 8, !tbaa !5
  br label %24, !llvm.loop !17

57:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !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, !6, i64 0}
!12 = !{!"gc_heap", !6, i64 0, !13, i64 8}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !13, i64 8}
!15 = !{!16, !6, i64 32}
!16 = !{!"header", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !6, i64 32, !6, i64 40}
!17 = distinct !{!17, !18}
!18 = !{!"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.header = type { i64, i64, i64, i64, ptr, ptr }

@from = 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_list = external local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @remove_from() local_unnamed_addr #0 {
  %1 = load ptr, ptr @free_list, align 8, !tbaa !5
  %2 = load i32, ptr @from, align 4, !tbaa !9
  %3 = sext i32 %2 to i64
  %4 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %3
  %5 = load ptr, ptr %4, align 16, !tbaa !11
  %6 = getelementptr inbounds i8, ptr %5, i64 2304
  %7 = getelementptr inbounds i8, ptr %4, i64 8
  %8 = load i64, ptr %7, align 8, !tbaa !14
  %9 = getelementptr inbounds %struct.header, ptr %6, i64 %8
  %.not15 = icmp eq ptr %1, null
  br i1 %.not15, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %18, %0
  ret void

.lr.ph:                                           ; preds = %0, %18
  %.017 = phi ptr [ %19, %18 ], [ %1, %0 ]
  %.01216 = phi ptr [ %.017, %18 ], [ %1, %0 ]
  %10 = phi ptr [ %20, %18 ], [ %1, %0 ]
  %.not13 = icmp ugt ptr %.017, %9
  %.not14 = icmp ult ptr %.017, %5
  %or.cond = select i1 %.not13, i1 true, i1 %.not14
  br i1 %or.cond, label %.lr.ph._crit_edge, label %11

.lr.ph._crit_edge:                                ; preds = %.lr.ph
  %.phi.trans.insert = getelementptr inbounds i8, ptr %.017, i64 32
  %.pre = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !15
  br label %18

11:                                               ; preds = %.lr.ph
  %12 = icmp eq ptr %10, %.01216
  %13 = getelementptr inbounds i8, ptr %.017, i64 32
  %14 = load ptr, ptr %13, align 8, !tbaa !15
  br i1 %12, label %15, label %16

15:                                               ; preds = %11
  store ptr %14, ptr @free_list, align 8, !tbaa !5
  br label %18

16:                                               ; preds = %11
  %17 = getelementptr inbounds i8, ptr %.01216, i64 32
  store ptr %14, ptr %17, align 8, !tbaa !15
  br label %18

18:                                               ; preds = %.lr.ph._crit_edge, %16, %15
  %19 = phi ptr [ %.pre, %.lr.ph._crit_edge ], [ %14, %16 ], [ %14, %15 ]
  %20 = phi ptr [ %10, %.lr.ph._crit_edge ], [ %10, %16 ], [ %14, %15 ]
  %.not = icmp eq ptr %19, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !17
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, 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" }

!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, !6, i64 0}
!12 = !{!"gc_heap", !6, i64 0, !13, i64 8}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !13, i64 8}
!15 = !{!16, !6, i64 32}
!16 = !{!"header", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !6, i64 32, !6, i64 40}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass