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

@from = external dso_local global i32, align 4
@root_used = external global i64, align 8
@roots = external global [100000 x %struct.root_], align 16
@.str.3 = external hidden unnamed_addr constant [35 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

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

declare ptr @is_pointer_to_space(ptr noundef, i64 noundef) #1

declare ptr @get_header(ptr noundef, ptr noundef) #1

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

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

8:                                                ; preds = %77, %0
  %9 = load i32, ptr %1, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = load i64, ptr @root_used, align 8, !tbaa !9
  %12 = icmp ult i64 %10, %11
  br i1 %12, label %14, label %13

13:                                               ; preds = %8
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  br label %80

14:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %15 = load i32, ptr %1, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [100000 x %struct.root_], ptr @roots, i64 0, i64 %16
  %18 = getelementptr inbounds %struct.root_, ptr %17, i32 0, i32 0
  %19 = load ptr, ptr %18, align 16, !tbaa !11
  store ptr %19, ptr %3, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %20 = load ptr, ptr %3, align 8, !tbaa !14
  %21 = getelementptr inbounds %struct.header, ptr %20, i64 -1
  %22 = getelementptr inbounds %struct.header, ptr %21, i64 1
  %23 = ptrtoint ptr %22 to i64
  %24 = load ptr, ptr %3, align 8, !tbaa !14
  %25 = getelementptr inbounds %struct.header, ptr %24, i64 -1
  %26 = getelementptr inbounds %struct.header, ptr %25, i32 0, i32 2
  %27 = load i64, ptr %26, align 8, !tbaa !15
  %28 = sub i64 %27, 48
  %29 = add i64 %23, %28
  %30 = inttoptr i64 %29 to ptr
  store ptr %30, ptr %4, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %31 = load ptr, ptr %3, align 8, !tbaa !14
  store ptr %31, ptr %5, align 8, !tbaa !14
  br label %32

32:                                               ; preds = %72, %14
  %33 = load ptr, ptr %5, align 8, !tbaa !14
  %34 = load ptr, ptr %4, align 8, !tbaa !14
  %35 = icmp ult ptr %33, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %32
  store i32 5, ptr %2, align 4
  br label %75

37:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %38 = load ptr, ptr %5, align 8, !tbaa !14
  %39 = load ptr, ptr %38, align 8, !tbaa !14
  %40 = load i32, ptr @from, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = call ptr @is_pointer_to_space(ptr noundef %39, i64 noundef %41)
  store ptr %42, ptr %7, align 8, !tbaa !14
  %43 = icmp ne ptr %42, null
  br i1 %43, label %45, label %44

44:                                               ; preds = %37
  store i32 7, ptr %2, align 4
  br label %69

45:                                               ; preds = %37
  %46 = load ptr, ptr %7, align 8, !tbaa !14
  %47 = load ptr, ptr %5, align 8, !tbaa !14
  %48 = load ptr, ptr %47, align 8, !tbaa !14
  %49 = call ptr @get_header(ptr noundef %46, ptr noundef %48)
  store ptr %49, ptr %6, align 8, !tbaa !14
  %50 = icmp ne ptr %49, null
  br i1 %50, label %52, label %51

51:                                               ; preds = %45
  store i32 7, ptr %2, align 4
  br label %69

52:                                               ; preds = %45
  %53 = load ptr, ptr %6, align 8, !tbaa !14
  %54 = getelementptr inbounds %struct.header, ptr %53, i32 0, i32 5
  %55 = load ptr, ptr %54, align 8, !tbaa !17
  %56 = icmp ne ptr %55, null
  br i1 %56, label %57, label %68

57:                                               ; preds = %52
  %58 = load ptr, ptr %6, align 8, !tbaa !14
  %59 = load ptr, ptr %6, align 8, !tbaa !14
  %60 = getelementptr inbounds %struct.header, ptr %59, i32 0, i32 5
  %61 = load ptr, ptr %60, align 8, !tbaa !17
  %62 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, ptr noundef %58, ptr noundef %61)
  %63 = load ptr, ptr %6, align 8, !tbaa !14
  %64 = getelementptr inbounds %struct.header, ptr %63, i32 0, i32 5
  %65 = load ptr, ptr %64, align 8, !tbaa !17
  %66 = getelementptr inbounds %struct.header, ptr %65, i64 1
  %67 = load ptr, ptr %5, align 8, !tbaa !14
  store ptr %66, ptr %67, align 8, !tbaa !14
  store i32 5, ptr %2, align 4
  br label %69

68:                                               ; preds = %52
  store i32 0, ptr %2, align 4
  br label %69

69:                                               ; preds = %68, %57, %51, %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %70 = load i32, ptr %2, align 4
  switch i32 %70, label %75 [
    i32 0, label %71
    i32 7, label %72
  ]

71:                                               ; preds = %69
  br label %72

72:                                               ; preds = %71, %69
  %73 = load ptr, ptr %5, align 8, !tbaa !14
  %74 = getelementptr inbounds i8, ptr %73, i32 1
  store ptr %74, ptr %5, align 8, !tbaa !14
  br label %32, !llvm.loop !18

75:                                               ; preds = %69, %36
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  br label %76

76:                                               ; preds = %75
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %77

77:                                               ; preds = %76
  %78 = load i32, ptr %1, align 4, !tbaa !5
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %1, align 4, !tbaa !5
  br label %8, !llvm.loop !20

80:                                               ; preds = %13
  ret void
}

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 = { 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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"root_", !13, i64 0, !13, i64 8}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!16, !10, i64 16}
!16 = !{!"header", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !13, i64 32, !13, i64 40}
!17 = !{!16, !13, i64 40}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19}


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

@from = external dso_local local_unnamed_addr global i32, align 4
@root_used = external local_unnamed_addr global i64, align 8
@roots = external local_unnamed_addr global [100000 x %struct.root_], align 16
@.str.3 = external hidden unnamed_addr constant [35 x i8], align 1

declare ptr @is_pointer_to_space(ptr noundef, i64 noundef) local_unnamed_addr #0

declare ptr @get_header(ptr noundef, ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local void @copy_reference() local_unnamed_addr #2 {
  %1 = load i64, ptr @root_used, align 8, !tbaa !5
  %.not33 = icmp eq i64 %1, 0
  br i1 %.not33, label %._crit_edge, label %.lr.ph32

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

.lr.ph32:                                         ; preds = %0, %.loopexit
  %indvars.iv = phi i64 [ %indvars.iv.next, %.loopexit ], [ 0, %0 ]
  %2 = getelementptr inbounds [100000 x %struct.root_], ptr @roots, i64 0, i64 %indvars.iv
  %3 = load ptr, ptr %2, align 16, !tbaa !9
  %4 = ptrtoint ptr %3 to i64
  %5 = getelementptr inbounds i8, ptr %3, i64 -32
  %6 = load i64, ptr %5, align 8, !tbaa !12
  %7 = add i64 %6, -48
  %8 = add i64 %7, %4
  %9 = inttoptr i64 %8 to ptr
  %10 = icmp ult ptr %3, %9
  br i1 %10, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %.lr.ph32
  %scevgep = getelementptr i8, ptr %3, i64 %7
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %26
  %.01830 = phi ptr [ %27, %26 ], [ %3, %.lr.ph.preheader ]
  %11 = load ptr, ptr %.01830, align 8, !tbaa !14
  %12 = load i32, ptr @from, align 4, !tbaa !15
  %13 = sext i32 %12 to i64
  %14 = tail call ptr @is_pointer_to_space(ptr noundef %11, i64 noundef %13) #3
  %.not = icmp eq ptr %14, null
  br i1 %.not, label %26, label %15

15:                                               ; preds = %.lr.ph
  %16 = load ptr, ptr %.01830, align 8, !tbaa !14
  %17 = tail call ptr @get_header(ptr noundef nonnull %14, ptr noundef %16) #3
  %.not23 = icmp eq ptr %17, null
  br i1 %.not23, label %26, label %18

18:                                               ; preds = %15
  %19 = getelementptr inbounds i8, ptr %17, i64 40
  %20 = load ptr, ptr %19, align 8, !tbaa !17
  %.not24 = icmp eq ptr %20, null
  br i1 %.not24, label %26, label %21

21:                                               ; preds = %18
  %22 = getelementptr inbounds i8, ptr %17, i64 40
  %23 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, ptr noundef nonnull %17, ptr noundef nonnull %20)
  %24 = load ptr, ptr %22, align 8, !tbaa !17
  %25 = getelementptr inbounds i8, ptr %24, i64 48
  store ptr %25, ptr %.01830, align 8, !tbaa !14
  br label %.loopexit

26:                                               ; preds = %.lr.ph, %15, %18
  %27 = getelementptr inbounds i8, ptr %.01830, i64 1
  %exitcond.not = icmp eq ptr %27, %scevgep
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph, !llvm.loop !18

.loopexit:                                        ; preds = %26, %.lr.ph32, %21
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %28 = load i64, ptr @root_used, align 8, !tbaa !5
  %29 = icmp ugt i64 %28, %indvars.iv.next
  br i1 %29, label %.lr.ph32, label %._crit_edge, !llvm.loop !20
}

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 = { 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 #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 = { 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, !11, i64 0}
!10 = !{!"root_", !11, i64 0, !11, i64 8}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!13, !6, i64 16}
!13 = !{!"header", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !11, i64 40}
!14 = !{!11, !11, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"int", !7, i64 0}
!17 = !{!13, !11, i64 40}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19}


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