; LLVM IR from brewlin/garbage-collect :: generational (mark_sweep)

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

@oldg = external global i32, align 4
@.str = external hidden unnamed_addr constant [17 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [20 x i8], align 1

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

12:                                               ; preds = %1
  store i32 1, ptr %5, align 4
  br label %62

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

18:                                               ; preds = %13
  %19 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  store i32 1, ptr %5, align 4
  br label %62

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

26:                                               ; preds = %20
  %27 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  store i32 1, ptr %5, align 4
  br label %62

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

34:                                               ; preds = %28
  store i32 1, ptr %5, align 4
  br label %62

35:                                               ; preds = %28
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.header, ptr %36, i32 0, i32 1
  %38 = load i64, ptr %37, align 8, !tbaa !11
  %39 = or i64 %38, 2
  store i64 %39, ptr %37, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %40, i64 1
  store ptr %41, ptr %6, align 8, !tbaa !5
  br label %42

42:                                               ; preds = %58, %35
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.header, ptr %44, i64 1
  %46 = ptrtoint ptr %45 to i64
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.header, ptr %47, i32 0, i32 2
  %49 = load i64, ptr %48, align 8, !tbaa !14
  %50 = sub i64 %49, 48
  %51 = add i64 %46, %50
  %52 = inttoptr i64 %51 to ptr
  %53 = icmp ult ptr %43, %52
  br i1 %53, label %55, label %54

54:                                               ; preds = %42
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %61

55:                                               ; preds = %42
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = load ptr, ptr %56, align 8, !tbaa !5
  call void @gc_mark(ptr noundef %57)
  br label %58

58:                                               ; preds = %55
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = getelementptr inbounds i8, ptr %59, i32 1
  store ptr %60, ptr %6, align 8, !tbaa !5
  br label %42, !llvm.loop !15

61:                                               ; preds = %54
  store i32 0, ptr %5, align 4
  br label %62

62:                                               ; preds = %61, %34, %26, %18, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  %63 = load i32, ptr %5, align 4
  switch i32 %63, label %65 [
    i32 0, label %64
    i32 1, label %64
  ]

64:                                               ; preds = %62, %62
  ret void

65:                                               ; preds = %62
  unreachable
}

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

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

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

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

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = distinct !{!15, !16}
!16 = !{!"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"

@oldg = external local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [17 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [20 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @gc_mark(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @oldg, align 4, !tbaa !5
  %3 = sext i32 %2 to i64
  %4 = tail call ptr @is_pointer_to_space(ptr noundef %0, i64 noundef %3) #3
  %.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) #3
  %.not14 = icmp eq ptr %6, null
  br i1 %.not14, label %.loopexit.sink.split, 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
  %.not15 = icmp eq i64 %10, 0
  br i1 %.not15, label %.loopexit.sink.split, label %11

11:                                               ; preds = %7
  %12 = and i64 %9, 2
  %.not16 = icmp eq i64 %12, 0
  br i1 %.not16, label %13, label %.loopexit

13:                                               ; preds = %11
  %14 = or disjoint i64 %9, 2
  store i64 %14, ptr %8, align 8, !tbaa !9
  %15 = getelementptr inbounds i8, ptr %6, i64 48
  %16 = ptrtoint ptr %15 to i64
  %17 = getelementptr inbounds i8, ptr %6, i64 16
  %18 = add i64 %16, -48
  %.017 = getelementptr inbounds i8, ptr %0, i64 1
  %19 = load i64, ptr %17, align 8, !tbaa !13
  %20 = add i64 %18, %19
  %21 = inttoptr i64 %20 to ptr
  %22 = icmp ult ptr %.017, %21
  br i1 %22, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %13, %.lr.ph
  %.018 = phi ptr [ %.0, %.lr.ph ], [ %.017, %13 ]
  %23 = load ptr, ptr %.018, align 8, !tbaa !14
  tail call void @gc_mark(ptr noundef %23)
  %.0 = getelementptr inbounds i8, ptr %.018, i64 1
  %24 = load i64, ptr %17, align 8, !tbaa !13
  %25 = add i64 %18, %24
  %26 = inttoptr i64 %25 to ptr
  %27 = icmp ult ptr %.0, %26
  br i1 %27, label %.lr.ph, label %.loopexit, !llvm.loop !15

.loopexit.sink.split:                             ; preds = %7, %5
  %.str.1.sink = phi ptr [ @.str, %5 ], [ @.str.1, %7 ]
  %28 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) %.str.1.sink)
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %.loopexit.sink.split, %13, %11, %1
  ret void
}

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: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

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 = { 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 #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, !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 = !{!10, !11, i64 16}
!14 = !{!12, !12, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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