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

@root_used = external global i64, align 8
@roots = external global [100000 x %struct.root_], align 16
@.str.2 = 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

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 @get_header_by_from(ptr noundef) #2

; 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 = %69, %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 %72

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 = %64, %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 %67

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
  store ptr %39, ptr %7, align 8, !tbaa !14
  %40 = load ptr, ptr %7, align 8, !tbaa !14
  %41 = call ptr @get_header_by_from(ptr noundef %40)
  store ptr %41, ptr %6, align 8, !tbaa !14
  %42 = icmp ne ptr %41, null
  br i1 %42, label %44, label %43

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

44:                                               ; preds = %37
  %45 = load ptr, ptr %6, align 8, !tbaa !14
  %46 = getelementptr inbounds %struct.header, ptr %45, i32 0, i32 5
  %47 = load ptr, ptr %46, align 8, !tbaa !17
  %48 = icmp ne ptr %47, null
  br i1 %48, label %49, label %60

49:                                               ; preds = %44
  %50 = load ptr, ptr %6, align 8, !tbaa !14
  %51 = load ptr, ptr %6, align 8, !tbaa !14
  %52 = getelementptr inbounds %struct.header, ptr %51, i32 0, i32 5
  %53 = load ptr, ptr %52, align 8, !tbaa !17
  %54 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, ptr noundef %50, ptr noundef %53)
  %55 = load ptr, ptr %6, align 8, !tbaa !14
  %56 = getelementptr inbounds %struct.header, ptr %55, i32 0, i32 5
  %57 = load ptr, ptr %56, align 8, !tbaa !17
  %58 = getelementptr inbounds %struct.header, ptr %57, i64 1
  %59 = load ptr, ptr %5, align 8, !tbaa !14
  store ptr %58, ptr %59, align 8, !tbaa !14
  store i32 5, ptr %2, align 4
  br label %61

60:                                               ; preds = %44
  store i32 0, ptr %2, align 4
  br label %61

61:                                               ; preds = %60, %49, %43
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %62 = load i32, ptr %2, align 4
  switch i32 %62, label %67 [
    i32 0, label %63
    i32 7, label %64
  ]

63:                                               ; preds = %61
  br label %64

64:                                               ; preds = %63, %61
  %65 = load ptr, ptr %5, align 8, !tbaa !14
  %66 = getelementptr inbounds i8, ptr %65, i32 1
  store ptr %66, ptr %5, align 8, !tbaa !14
  br label %32, !llvm.loop !18

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

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

69:                                               ; preds = %68
  %70 = load i32, ptr %1, align 4, !tbaa !5
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %1, align 4, !tbaa !5
  br label %8, !llvm.loop !20

72:                                               ; 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 }

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

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

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

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

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

.lr.ph29:                                         ; 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.ph29
  %scevgep = getelementptr i8, ptr %3, i64 %7
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %21
  %.01727 = phi ptr [ %22, %21 ], [ %3, %.lr.ph.preheader ]
  %11 = load ptr, ptr %.01727, align 8, !tbaa !14
  %12 = tail call ptr @get_header_by_from(ptr noundef %11)
  %.not = icmp eq ptr %12, null
  br i1 %.not, label %21, label %13

13:                                               ; preds = %.lr.ph
  %14 = getelementptr inbounds i8, ptr %12, i64 40
  %15 = load ptr, ptr %14, align 8, !tbaa !15
  %.not21 = icmp eq ptr %15, null
  br i1 %.not21, label %21, label %16

16:                                               ; preds = %13
  %17 = getelementptr inbounds i8, ptr %12, i64 40
  %18 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, ptr noundef nonnull %12, ptr noundef nonnull %15)
  %19 = load ptr, ptr %17, align 8, !tbaa !15
  %20 = getelementptr inbounds i8, ptr %19, i64 48
  store ptr %20, ptr %.01727, align 8, !tbaa !14
  br label %.loopexit

21:                                               ; preds = %.lr.ph, %13
  %22 = getelementptr inbounds i8, ptr %.01727, i64 1
  %exitcond.not = icmp eq ptr %22, %scevgep
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph, !llvm.loop !16

.loopexit:                                        ; preds = %21, %.lr.ph29, %16
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %23 = load i64, ptr @root_used, align 8, !tbaa !5
  %24 = icmp ugt i64 %23, %indvars.iv.next
  br i1 %24, label %.lr.ph29, label %._crit_edge, !llvm.loop !18
}

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

!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 = !{!13, !11, i64 40}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


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