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

@from = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [22 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [83 x i8], align 1
@__PRETTY_FUNCTION__.gc_copy = external hidden unnamed_addr constant [22 x i8], align 1
@free_p = external dso_local global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [44 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

; Function Attrs: nounwind uwtable
define dso_local ptr @gc_copy(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = load i32, ptr @from, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = call ptr @is_pointer_to_space(ptr noundef %10, i64 noundef %12)
  store ptr %13, ptr %5, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %16, label %15

15:                                               ; preds = %1
  store ptr null, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %105

16:                                               ; preds = %1
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = call ptr @get_header(ptr noundef %17, ptr noundef %18)
  store ptr %19, ptr %4, align 8, !tbaa !5
  %20 = icmp ne ptr %19, null
  br i1 %20, label %22, label %21

21:                                               ; preds = %16
  store ptr null, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %105

22:                                               ; preds = %16
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.header, ptr %23, i32 0, i32 1
  %25 = load i64, ptr %24, align 8, !tbaa !11
  %26 = and i64 %25, 1
  %27 = icmp ne i64 %26, 0
  br i1 %27, label %28, label %29

28:                                               ; preds = %22
  br label %30

29:                                               ; preds = %22
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 66, ptr noundef @__PRETTY_FUNCTION__.gc_copy) #6
  unreachable

30:                                               ; preds = %28
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.header, ptr %31, i32 0, i32 1
  %33 = load i64, ptr %32, align 8, !tbaa !11
  %34 = and i64 %33, 1
  %35 = icmp ne i64 %34, 0
  br i1 %35, label %36, label %42

36:                                               ; preds = %30
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.header, ptr %37, i32 0, i32 1
  %39 = load i64, ptr %38, align 8, !tbaa !11
  %40 = and i64 %39, 4
  %41 = icmp ne i64 %40, 0
  br i1 %41, label %100, label %42

42:                                               ; preds = %36, %30
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %43 = load ptr, ptr @free_p, align 8, !tbaa !5
  store ptr %43, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.header, ptr %44, i32 0, i32 2
  %46 = load i64, ptr %45, align 8, !tbaa !14
  store i64 %46, ptr %8, align 8, !tbaa !15
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.header, ptr %49, i32 0, i32 2
  %51 = load i64, ptr %50, align 8, !tbaa !14
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %47, ptr align 8 %48, i64 %51, i1 false)
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.header, ptr %52, i32 0, i32 1
  %54 = load i64, ptr %53, align 8, !tbaa !11
  %55 = or i64 %54, 4
  store i64 %55, ptr %53, align 8, !tbaa !11
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.header, ptr %56, i32 0, i32 2
  %58 = load i64, ptr %57, align 8, !tbaa !14
  %59 = load ptr, ptr @free_p, align 8, !tbaa !5
  %60 = getelementptr inbounds i8, ptr %59, i64 %58
  store ptr %60, ptr @free_p, align 8, !tbaa !5
  %61 = load i64, ptr %8, align 8, !tbaa !15
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.header, ptr %62, i32 0, i32 2
  %64 = load i64, ptr %63, align 8, !tbaa !14
  %65 = sub i64 %61, %64
  %66 = load ptr, ptr @free_p, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.header, ptr %66, i32 0, i32 2
  store i64 %65, ptr %67, align 8, !tbaa !14
  %68 = load ptr, ptr %7, align 8, !tbaa !5
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.header, ptr %69, i32 0, i32 5
  store ptr %68, ptr %70, align 8, !tbaa !16
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = load ptr, ptr %4, align 8, !tbaa !5
  %73 = load ptr, ptr %7, align 8, !tbaa !5
  %74 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, ptr noundef %71, ptr noundef %72, ptr noundef %73)
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %75 = load ptr, ptr %7, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.header, ptr %75, i64 1
  store ptr %76, ptr %9, align 8, !tbaa !5
  br label %77

77:                                               ; preds = %94, %42
  %78 = load ptr, ptr %9, align 8, !tbaa !5
  %79 = load ptr, ptr %7, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.header, ptr %79, i64 1
  %81 = ptrtoint ptr %80 to i64
  %82 = load ptr, ptr %7, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.header, ptr %82, i32 0, i32 2
  %84 = load i64, ptr %83, align 8, !tbaa !14
  %85 = sub i64 %84, 48
  %86 = add i64 %81, %85
  %87 = inttoptr i64 %86 to ptr
  %88 = icmp ult ptr %78, %87
  br i1 %88, label %90, label %89

89:                                               ; preds = %77
  store i32 2, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  br label %97

90:                                               ; preds = %77
  %91 = load ptr, ptr %9, align 8, !tbaa !5
  %92 = load ptr, ptr %91, align 8, !tbaa !5
  %93 = call ptr @gc_mark_or_copy(ptr noundef %92)
  br label %94

94:                                               ; preds = %90
  %95 = load ptr, ptr %9, align 8, !tbaa !5
  %96 = getelementptr inbounds i8, ptr %95, i32 1
  store ptr %96, ptr %9, align 8, !tbaa !5
  br label %77, !llvm.loop !17

97:                                               ; preds = %89
  %98 = load ptr, ptr %7, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.header, ptr %98, i64 1
  store ptr %99, ptr %2, align 8
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  br label %105

100:                                              ; preds = %36
  %101 = load ptr, ptr %4, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.header, ptr %101, i32 0, i32 5
  %103 = load ptr, ptr %102, align 8, !tbaa !16
  %104 = getelementptr inbounds %struct.header, ptr %103, i64 1
  store ptr %104, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %105

105:                                              ; preds = %100, %97, %21, %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  %106 = load ptr, ptr %2, align 8
  ret ptr %106
}

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

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

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #3

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #4

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

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

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 = { "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 = { noreturn 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { nounwind }
attributes #6 = { noreturn 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, !13, i64 8}
!12 = !{!"header", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !6, i64 32, !6, i64 40}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !13, i64 16}
!15 = !{!13, !13, i64 0}
!16 = !{!12, !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"

@from = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [22 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [83 x i8], align 1
@__PRETTY_FUNCTION__.gc_copy = external hidden unnamed_addr constant [22 x i8], align 1
@free_p = external dso_local local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [44 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @gc_copy(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @from, align 4, !tbaa !5
  %3 = sext i32 %2 to i64
  %4 = tail call ptr @is_pointer_to_space(ptr noundef %0, i64 noundef %3) #5
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %.loopexit, label %5

5:                                                ; preds = %1
  %6 = tail call ptr @get_header(ptr noundef nonnull %4, ptr noundef %0) #5
  %.not30 = icmp eq ptr %6, null
  br i1 %.not30, label %.loopexit, label %7

7:                                                ; preds = %5
  %8 = getelementptr inbounds i8, ptr %6, i64 8
  %9 = load i64, ptr %8, align 8, !tbaa !9
  %10 = and i64 %9, 1
  %.not31 = icmp eq i64 %10, 0
  br i1 %.not31, label %11, label %12

11:                                               ; preds = %7
  tail call void @__assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 66, ptr noundef nonnull @__PRETTY_FUNCTION__.gc_copy) #6
  unreachable

12:                                               ; preds = %7
  %13 = and i64 %9, 4
  %.not32 = icmp eq i64 %13, 0
  br i1 %.not32, label %14, label %43

14:                                               ; preds = %12
  %15 = load ptr, ptr @free_p, align 8, !tbaa !13
  %16 = getelementptr inbounds i8, ptr %15, i64 16
  %17 = load i64, ptr %16, align 8, !tbaa !14
  %18 = getelementptr inbounds i8, ptr %6, i64 16
  %19 = load i64, ptr %18, align 8, !tbaa !14
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 8 %15, ptr nonnull align 8 %6, i64 %19, i1 false)
  %20 = load i64, ptr %8, align 8, !tbaa !9
  %21 = or i64 %20, 4
  store i64 %21, ptr %8, align 8, !tbaa !9
  %22 = load i64, ptr %18, align 8, !tbaa !14
  %23 = load ptr, ptr @free_p, align 8, !tbaa !13
  %24 = getelementptr inbounds i8, ptr %23, i64 %22
  store ptr %24, ptr @free_p, align 8, !tbaa !13
  %25 = sub i64 %17, %22
  %26 = getelementptr inbounds i8, ptr %24, i64 16
  store i64 %25, ptr %26, align 8, !tbaa !14
  %27 = getelementptr inbounds i8, ptr %6, i64 40
  store ptr %15, ptr %27, align 8, !tbaa !15
  %28 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, ptr noundef %0, ptr noundef nonnull %6, ptr noundef %15)
  %29 = getelementptr inbounds i8, ptr %15, i64 48
  %30 = ptrtoint ptr %29 to i64
  %31 = add i64 %30, -48
  %32 = load i64, ptr %16, align 8, !tbaa !14
  %33 = add i64 %31, %32
  %34 = inttoptr i64 %33 to ptr
  %35 = icmp ult ptr %29, %34
  br i1 %35, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %14, %.lr.ph
  %.033 = phi ptr [ %38, %.lr.ph ], [ %29, %14 ]
  %36 = load ptr, ptr %.033, align 8, !tbaa !13
  %37 = tail call ptr @gc_mark_or_copy(ptr noundef %36)
  %38 = getelementptr inbounds i8, ptr %.033, i64 1
  %39 = load i64, ptr %16, align 8, !tbaa !14
  %40 = add i64 %31, %39
  %41 = inttoptr i64 %40 to ptr
  %42 = icmp ult ptr %38, %41
  br i1 %42, label %.lr.ph, label %.loopexit, !llvm.loop !16

43:                                               ; preds = %12
  %44 = getelementptr inbounds i8, ptr %6, i64 40
  %45 = load ptr, ptr %44, align 8, !tbaa !15
  %46 = getelementptr inbounds i8, ptr %45, i64 48
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %14, %5, %1, %43
  %.027 = phi ptr [ %46, %43 ], [ null, %1 ], [ null, %5 ], [ %29, %14 ], [ %29, %.lr.ph ]
  ret ptr %.027
}

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

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

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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

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

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" }
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 = { noreturn 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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { nounwind }
attributes #6 = { noreturn 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, !11, i64 8}
!10 = !{!"header", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !12, i64 32, !12, i64 40}
!11 = !{!"long", !7, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!10, !11, i64 16}
!15 = !{!10, !12, i64 40}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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