; LLVM IR from brewlin/garbage-collect :: copying (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 %struct.gc_heap, align 8
@to = external dso_local global %struct.gc_heap, align 8
@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.3 = external hidden unnamed_addr constant [20 x i8], align 1
@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

declare i32 @printf(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 ptr @gc_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 = alloca ptr, align 8
  %4 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  %5 = load i64, ptr getelementptr inbounds (%struct.gc_heap, ptr @to, i32 0, i32 1), align 8, !tbaa !5
  %6 = load ptr, ptr @to, align 8, !tbaa !11
  %7 = getelementptr inbounds %struct.header, ptr %6, i32 0, i32 2
  store i64 %5, ptr %7, align 8, !tbaa !12
  %8 = load ptr, ptr @to, align 8, !tbaa !11
  store ptr %8, ptr @free_p, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  store i32 0, ptr %1, align 4, !tbaa !15
  br label %9

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

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

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

33:                                               ; preds = %15
  %34 = load i32, ptr %1, align 4, !tbaa !15
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %1, align 4, !tbaa !15
  br label %9, !llvm.loop !21

36:                                               ; preds = %14
  call void @copy_reference()
  %37 = load ptr, ptr @from, align 8, !tbaa !11
  %38 = load i64, ptr getelementptr inbounds (%struct.gc_heap, ptr @from, i32 0, i32 1), align 8, !tbaa !5
  %39 = add i64 %38, 48
  call void @llvm.memset.p0.i64(ptr align 8 %37, i8 0, i64 %39, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %40 = load ptr, ptr @from, align 8, !tbaa !11
  store ptr %40, ptr %3, align 8, !tbaa !14
  %41 = load ptr, ptr @to, align 8, !tbaa !11
  store ptr %41, ptr @from, align 8, !tbaa !11
  %42 = load ptr, ptr %3, align 8, !tbaa !14
  store ptr %42, ptr @to, align 8, !tbaa !11
  %43 = load ptr, ptr @free_p, align 8, !tbaa !14
  store ptr %43, ptr @free_list, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret void
}

; 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, !10, i64 8}
!6 = !{!"gc_heap", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!13, !10, i64 16}
!13 = !{!"header", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !7, i64 32, !7, i64 40}
!14 = !{!7, !7, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"int", !8, i64 0}
!17 = !{!10, !10, i64 0}
!18 = !{!19, !7, i64 0}
!19 = !{!"root_", !7, i64 0, !7, i64 8}
!20 = !{!19, !7, 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 %struct.gc_heap, align 8
@to = external dso_local local_unnamed_addr global %struct.gc_heap, align 8
@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.3 = external hidden unnamed_addr constant [20 x i8], align 1
@free_list = external local_unnamed_addr global ptr, align 8

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @gc_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.3)
  %2 = load i64, ptr getelementptr inbounds (i8, ptr @to, i64 8), align 8, !tbaa !5
  %3 = load ptr, ptr @to, align 8, !tbaa !11
  %4 = getelementptr inbounds i8, ptr %3, i64 16
  store i64 %2, ptr %4, align 8, !tbaa !12
  store ptr %3, ptr @free_p, align 8, !tbaa !14
  %5 = load i64, ptr @root_used, align 8, !tbaa !15
  %.not = icmp eq i64 %5, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %0
  tail call void @copy_reference()
  %6 = load ptr, ptr @from, align 8, !tbaa !11
  %7 = load i64, ptr getelementptr inbounds (i8, ptr @from, i64 8), align 8, !tbaa !5
  %8 = add i64 %7, 48
  tail call void @llvm.memset.p0.i64(ptr align 8 %6, i8 0, i64 %8, i1 false)
  %9 = load ptr, ptr @from, align 8, !tbaa !11
  %10 = load ptr, ptr @to, align 8, !tbaa !11
  store ptr %10, ptr @from, align 8, !tbaa !11
  store ptr %9, ptr @to, align 8, !tbaa !11
  %11 = load ptr, ptr @free_p, align 8, !tbaa !14
  store ptr %11, ptr @free_list, align 8, !tbaa !14
  ret void

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

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

attributes #0 = { 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 #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 = { mustprogress 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, !10, i64 8}
!6 = !{!"gc_heap", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!13, !10, i64 16}
!13 = !{!"header", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !7, i64 32, !7, i64 40}
!14 = !{!7, !7, i64 0}
!15 = !{!10, !10, i64 0}
!16 = !{!17, !7, i64 0}
!17 = !{!"root_", !7, i64 0, !7, i64 8}
!18 = !{!17, !7, i64 8}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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