; LLVM IR from krig/LISP :: komplott (gc_collect)

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

@fromspace = external dso_local global ptr, align 8
@allocptr = external dso_local global ptr, align 8
@tospace = external dso_local global ptr, align 8
@scanptr = external dso_local global ptr, align 8
@numroots = external dso_local global i64, align 8
@roots = external dso_local global [500 x ptr], align 16

; 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
declare dso_local void @gc_copy(ptr noundef) #1

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

7:                                                ; preds = %16, %0
  %8 = load i64, ptr %2, align 8, !tbaa !9
  %9 = load i64, ptr @numroots, align 8, !tbaa !9
  %10 = icmp ult i64 %8, %9
  br i1 %10, label %12, label %11

11:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  br label %19

12:                                               ; preds = %7
  %13 = load i64, ptr %2, align 8, !tbaa !9
  %14 = getelementptr inbounds [500 x ptr], ptr @roots, i64 0, i64 %13
  %15 = load ptr, ptr %14, align 8, !tbaa !5
  call void @gc_copy(ptr noundef %15)
  br label %16

16:                                               ; preds = %12
  %17 = load i64, ptr %2, align 8, !tbaa !9
  %18 = add i64 %17, 1
  store i64 %18, ptr %2, align 8, !tbaa !9
  br label %7, !llvm.loop !11

19:                                               ; preds = %11
  br label %20

20:                                               ; preds = %40, %19
  %21 = load ptr, ptr @scanptr, align 8, !tbaa !5
  %22 = load ptr, ptr @allocptr, align 8, !tbaa !5
  %23 = icmp ult ptr %21, %22
  br i1 %23, label %24, label %43

24:                                               ; preds = %20
  %25 = load ptr, ptr @scanptr, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.object_t, ptr %25, i32 0, i32 2
  %27 = load i32, ptr %26, align 8, !tbaa !13
  %28 = icmp eq i32 %27, 0
  br i1 %28, label %34, label %29

29:                                               ; preds = %24
  %30 = load ptr, ptr @scanptr, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.object_t, ptr %30, i32 0, i32 2
  %32 = load i32, ptr %31, align 8, !tbaa !13
  %33 = icmp eq i32 %32, 3
  br i1 %33, label %34, label %39

34:                                               ; preds = %29, %24
  %35 = load ptr, ptr @scanptr, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.object_t, ptr %35, i32 0, i32 0
  call void @gc_copy(ptr noundef %36)
  %37 = load ptr, ptr @scanptr, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.object_t, ptr %37, i32 0, i32 1
  call void @gc_copy(ptr noundef %38)
  br label %39

39:                                               ; preds = %34, %29
  br label %40

40:                                               ; preds = %39
  %41 = load ptr, ptr @scanptr, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.object_t, ptr %41, i32 1
  store ptr %42, ptr @scanptr, align 8, !tbaa !5
  br label %20, !llvm.loop !16

43:                                               ; preds = %20
  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 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !15, i64 16}
!14 = !{!"object_t", !6, i64 0, !6, i64 8, !15, i64 16}
!15 = !{!"int", !7, i64 0}
!16 = distinct !{!16, !12}


; 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"

@fromspace = external dso_local local_unnamed_addr global ptr, align 8
@allocptr = external dso_local local_unnamed_addr global ptr, align 8
@tospace = external dso_local local_unnamed_addr global ptr, align 8
@scanptr = external dso_local local_unnamed_addr global ptr, align 8
@numroots = external dso_local local_unnamed_addr global i64, align 8
@roots = external dso_local local_unnamed_addr global [500 x ptr], align 16

; Function Attrs: nounwind uwtable
declare dso_local void @gc_copy(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @gc_collect() local_unnamed_addr #0 {
  %1 = load ptr, ptr @fromspace, align 8, !tbaa !5
  %2 = load ptr, ptr @tospace, align 8, !tbaa !5
  store ptr %2, ptr @fromspace, align 8, !tbaa !5
  store ptr %1, ptr @tospace, align 8, !tbaa !5
  store ptr %2, ptr @scanptr, align 8, !tbaa !5
  store ptr %2, ptr @allocptr, align 8, !tbaa !5
  %3 = load i64, ptr @numroots, align 8, !tbaa !9
  %.not = icmp eq i64 %3, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.preheader:                                       ; preds = %.lr.ph
  %.pre = load ptr, ptr @scanptr, align 8, !tbaa !5
  %.pre7 = load ptr, ptr @allocptr, align 8, !tbaa !5
  %4 = icmp ult ptr %.pre, %.pre7
  br i1 %4, label %.lr.ph6, label %._crit_edge

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.05 = phi i64 [ %7, %.lr.ph ], [ 0, %0 ]
  %5 = getelementptr inbounds [500 x ptr], ptr @roots, i64 0, i64 %.05
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  tail call void @gc_copy(ptr noundef %6)
  %7 = add nuw i64 %.05, 1
  %8 = load i64, ptr @numroots, align 8, !tbaa !9
  %9 = icmp ult i64 %7, %8
  br i1 %9, label %.lr.ph, label %.preheader, !llvm.loop !11

.lr.ph6:                                          ; preds = %.preheader, %17
  %10 = phi ptr [ %18, %17 ], [ %.pre7, %.preheader ]
  %11 = phi ptr [ %20, %17 ], [ %.pre, %.preheader ]
  %12 = getelementptr inbounds i8, ptr %11, i64 16
  %13 = load i32, ptr %12, align 8, !tbaa !13
  switch i32 %13, label %17 [
    i32 0, label %14
    i32 3, label %14
  ]

14:                                               ; preds = %.lr.ph6, %.lr.ph6
  tail call void @gc_copy(ptr noundef nonnull %11)
  %15 = load ptr, ptr @scanptr, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %15, i64 8
  tail call void @gc_copy(ptr noundef nonnull %16)
  %.pre8 = load ptr, ptr @scanptr, align 8, !tbaa !5
  %.pre9 = load ptr, ptr @allocptr, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %.lr.ph6, %14
  %18 = phi ptr [ %10, %.lr.ph6 ], [ %.pre9, %14 ]
  %19 = phi ptr [ %11, %.lr.ph6 ], [ %.pre8, %14 ]
  %20 = getelementptr inbounds i8, ptr %19, i64 24
  store ptr %20, ptr @scanptr, align 8, !tbaa !5
  %21 = icmp ult ptr %20, %18
  br i1 %21, label %.lr.ph6, label %._crit_edge, !llvm.loop !16

._crit_edge:                                      ; preds = %17, %0, %.preheader
  ret void
}

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" }

!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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !15, i64 16}
!14 = !{!"object_t", !6, i64 0, !6, i64 8, !15, i64 16}
!15 = !{!"int", !7, i64 0}
!16 = distinct !{!16, !12}


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