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

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

@rs_index = external dso_local global i32, align 4
@rs = external dso_local global [100000 x ptr], align 16
@.str.4 = external hidden unnamed_addr constant [32 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_heap(ptr noundef) #1

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @gc_copy(ptr noundef) #2

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

; Function Attrs: nounwind uwtable
define dso_local void @update_reference() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = 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 %11

11:                                               ; preds = %108, %0
  %12 = load i32, ptr %1, align 4, !tbaa !5
  %13 = load i32, ptr @rs_index, align 4, !tbaa !5
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %16, label %15

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

16:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %17 = load i32, ptr %1, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [100000 x ptr], ptr @rs, i64 0, i64 %18
  %20 = load ptr, ptr %19, align 8, !tbaa !9
  store ptr %20, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %21 = load ptr, ptr %3, align 8, !tbaa !9
  store ptr %21, ptr %5, align 8, !tbaa !9
  br label %22

22:                                               ; preds = %75, %16
  %23 = load ptr, ptr %5, align 8, !tbaa !9
  %24 = load ptr, ptr %3, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.header, ptr %24, i64 -1
  %26 = getelementptr inbounds %struct.header, ptr %25, i64 1
  %27 = ptrtoint ptr %26 to i64
  %28 = load ptr, ptr %3, align 8, !tbaa !9
  %29 = getelementptr inbounds %struct.header, ptr %28, i64 -1
  %30 = getelementptr inbounds %struct.header, ptr %29, i32 0, i32 2
  %31 = load i64, ptr %30, align 8, !tbaa !11
  %32 = sub i64 %31, 48
  %33 = add i64 %27, %32
  %34 = inttoptr i64 %33 to ptr
  %35 = icmp ult ptr %23, %34
  br i1 %35, label %37, label %36

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

37:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %38 = load ptr, ptr %5, align 8, !tbaa !9
  %39 = load ptr, ptr %38, align 8, !tbaa !9
  store ptr %39, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %40 = load ptr, ptr %6, align 8, !tbaa !9
  %41 = call ptr @is_pointer_to_heap(ptr noundef %40)
  store ptr %41, ptr %7, align 8, !tbaa !9
  %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 %72

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

49:                                               ; preds = %44
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %50 = load ptr, ptr %6, align 8, !tbaa !9
  %51 = call ptr @gc_copy(ptr noundef %50)
  store ptr %51, ptr %9, align 8, !tbaa !9
  %52 = load ptr, ptr %8, align 8, !tbaa !9
  %53 = load ptr, ptr %9, align 8, !tbaa !9
  %54 = call i32 (ptr, ...) @printf(ptr noundef @.str.4, ptr noundef %52, ptr noundef %53)
  %55 = load ptr, ptr %9, align 8, !tbaa !9
  %56 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %55, ptr %56, align 8, !tbaa !9
  %57 = load ptr, ptr %9, align 8, !tbaa !9
  %58 = call ptr @is_pointer_to_heap(ptr noundef %57)
  store ptr %58, ptr %7, align 8, !tbaa !9
  %59 = icmp ne ptr %58, null
  br i1 %59, label %61, label %60

60:                                               ; preds = %49
  store i32 7, ptr %2, align 4
  br label %68

61:                                               ; preds = %49
  %62 = load ptr, ptr %7, align 8, !tbaa !9
  %63 = load ptr, ptr %9, align 8, !tbaa !9
  %64 = call ptr @get_header(ptr noundef %62, ptr noundef %63)
  store ptr %64, ptr %8, align 8, !tbaa !9
  %65 = icmp ne ptr %64, null
  br i1 %65, label %66, label %67

66:                                               ; preds = %61
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %67

67:                                               ; preds = %66, %61
  store i32 0, ptr %2, align 4
  br label %68

68:                                               ; preds = %67, %60
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  %69 = load i32, ptr %2, align 4
  switch i32 %69, label %72 [
    i32 0, label %70
  ]

70:                                               ; preds = %68
  br label %71

71:                                               ; preds = %70, %44
  store i32 0, ptr %2, align 4
  br label %72

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

74:                                               ; preds = %72
  br label %75

75:                                               ; preds = %74, %72
  %76 = load ptr, ptr %5, align 8, !tbaa !9
  %77 = getelementptr inbounds i8, ptr %76, i32 1
  store ptr %77, ptr %5, align 8, !tbaa !9
  br label %22, !llvm.loop !14

78:                                               ; preds = %36
  %79 = load i32, ptr %4, align 4, !tbaa !5
  %80 = icmp ne i32 %79, 0
  br i1 %80, label %107, label %81

81:                                               ; preds = %78
  %82 = load i32, ptr %1, align 4, !tbaa !5
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds [100000 x ptr], ptr @rs, i64 0, i64 %83
  %85 = load ptr, ptr %84, align 8, !tbaa !9
  %86 = getelementptr inbounds %struct.header, ptr %85, i64 -1
  %87 = getelementptr inbounds %struct.header, ptr %86, i32 0, i32 1
  %88 = load i64, ptr %87, align 8, !tbaa !16
  %89 = and i64 %88, -9
  store i64 %89, ptr %87, align 8, !tbaa !16
  %90 = load i32, ptr @rs_index, align 4, !tbaa !5
  %91 = add nsw i32 %90, -1
  store i32 %91, ptr @rs_index, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %92 = load i32, ptr @rs_index, align 4, !tbaa !5
  %93 = sext i32 %92 to i64
  %94 = getelementptr inbounds [100000 x ptr], ptr @rs, i64 0, i64 %93
  %95 = load ptr, ptr %94, align 8, !tbaa !9
  store ptr %95, ptr %10, align 8, !tbaa !9
  %96 = load i32, ptr %1, align 4, !tbaa !5
  %97 = sext i32 %96 to i64
  %98 = getelementptr inbounds [100000 x ptr], ptr @rs, i64 0, i64 %97
  %99 = load ptr, ptr %98, align 8, !tbaa !9
  %100 = load i32, ptr @rs_index, align 4, !tbaa !5
  %101 = sext i32 %100 to i64
  %102 = getelementptr inbounds [100000 x ptr], ptr @rs, i64 0, i64 %101
  store ptr %99, ptr %102, align 8, !tbaa !9
  %103 = load ptr, ptr %10, align 8, !tbaa !9
  %104 = load i32, ptr %1, align 4, !tbaa !5
  %105 = sext i32 %104 to i64
  %106 = getelementptr inbounds [100000 x ptr], ptr @rs, i64 0, i64 %105
  store ptr %103, ptr %106, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %107

107:                                              ; preds = %81, %78
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %108

108:                                              ; preds = %107
  %109 = load i32, ptr %1, align 4, !tbaa !5
  %110 = add nsw i32 %109, 1
  store i32 %110, ptr %1, align 4, !tbaa !5
  br label %11, !llvm.loop !17

111:                                              ; preds = %15
  ret void

112:                                              ; preds = %72
  unreachable
}

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


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

@rs_index = external dso_local local_unnamed_addr global i32, align 4
@rs = external dso_local local_unnamed_addr global [100000 x ptr], align 16
@.str.4 = external hidden unnamed_addr constant [32 x i8], align 1

declare ptr @is_pointer_to_heap(ptr noundef) local_unnamed_addr #0

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @update_reference() local_unnamed_addr #1 {
  %1 = load i32, ptr @rs_index, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph39, label %._crit_edge40

._crit_edge40:                                    ; preds = %36, %0
  ret void

.lr.ph39:                                         ; preds = %0, %36
  %.pre42 = phi i32 [ %.pre43, %36 ], [ %1, %0 ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %36 ], [ 0, %0 ]
  %3 = getelementptr inbounds [100000 x ptr], ptr @rs, i64 0, i64 %indvars.iv
  %4 = load ptr, ptr %3, align 8, !tbaa !9
  %5 = ptrtoint ptr %4 to i64
  %6 = getelementptr inbounds i8, ptr %4, i64 -32
  %7 = add i64 %5, -48
  %8 = load i64, ptr %6, align 8, !tbaa !11
  %9 = add i64 %7, %8
  %10 = inttoptr i64 %9 to ptr
  %11 = icmp ult ptr %4, %10
  br i1 %11, label %.lr.ph, label %._crit_edge.thread

._crit_edge:                                      ; preds = %22
  %.pre.pre = load i32, ptr @rs_index, align 4, !tbaa !5
  %.not = icmp eq i32 %.4, 0
  br i1 %.not, label %._crit_edge.thread, label %._crit_edge._crit_edge

._crit_edge._crit_edge:                           ; preds = %._crit_edge
  %.pre45 = sext i32 %.pre.pre to i64
  br label %36

.lr.ph:                                           ; preds = %.lr.ph39, %22
  %.02636 = phi i32 [ %.4, %22 ], [ 0, %.lr.ph39 ]
  %.02735 = phi ptr [ %23, %22 ], [ %4, %.lr.ph39 ]
  %12 = load ptr, ptr %.02735, align 8, !tbaa !9
  %13 = tail call ptr @is_pointer_to_heap(ptr noundef %12) #3
  %.not31 = icmp eq ptr %13, null
  br i1 %.not31, label %22, label %14

14:                                               ; preds = %.lr.ph
  %15 = tail call ptr @get_header(ptr noundef nonnull %13, ptr noundef %12) #3
  %.not32 = icmp eq ptr %15, null
  br i1 %.not32, label %22, label %16

16:                                               ; preds = %14
  %17 = tail call ptr @gc_copy(ptr noundef %12)
  %18 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, ptr noundef nonnull %15, ptr noundef %17)
  store ptr %17, ptr %.02735, align 8, !tbaa !9
  %19 = tail call ptr @is_pointer_to_heap(ptr noundef %17) #3
  %.not33.not = icmp eq ptr %19, null
  br i1 %.not33.not, label %22, label %20

20:                                               ; preds = %16
  %21 = tail call ptr @get_header(ptr noundef nonnull %19, ptr noundef %17) #3
  %.not34 = icmp eq ptr %21, null
  %spec.select = select i1 %.not34, i32 %.02636, i32 1
  br label %22

22:                                               ; preds = %20, %16, %14, %.lr.ph
  %.4 = phi i32 [ %.02636, %.lr.ph ], [ %.02636, %14 ], [ %spec.select, %20 ], [ %.02636, %16 ]
  %23 = getelementptr inbounds i8, ptr %.02735, i64 1
  %24 = load i64, ptr %6, align 8, !tbaa !11
  %25 = add i64 %7, %24
  %26 = inttoptr i64 %25 to ptr
  %27 = icmp ult ptr %23, %26
  br i1 %27, label %.lr.ph, label %._crit_edge, !llvm.loop !14

._crit_edge.thread:                               ; preds = %.lr.ph39, %._crit_edge
  %.pre49 = phi i32 [ %.pre.pre, %._crit_edge ], [ %.pre42, %.lr.ph39 ]
  %28 = load ptr, ptr %3, align 8, !tbaa !9
  %29 = getelementptr inbounds i8, ptr %28, i64 -40
  %30 = load i64, ptr %29, align 8, !tbaa !16
  %31 = and i64 %30, -9
  store i64 %31, ptr %29, align 8, !tbaa !16
  %32 = add nsw i32 %.pre49, -1
  store i32 %32, ptr @rs_index, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [100000 x ptr], ptr @rs, i64 0, i64 %33
  %35 = load ptr, ptr %34, align 8, !tbaa !9
  store ptr %28, ptr %34, align 8, !tbaa !9
  store ptr %35, ptr %3, align 8, !tbaa !9
  br label %36

36:                                               ; preds = %._crit_edge._crit_edge, %._crit_edge.thread
  %.pre-phi = phi i64 [ %.pre45, %._crit_edge._crit_edge ], [ %33, %._crit_edge.thread ]
  %.pre43 = phi i32 [ %.pre.pre, %._crit_edge._crit_edge ], [ %32, %._crit_edge.thread ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %37 = icmp slt i64 %indvars.iv.next, %.pre-phi
  br i1 %37, label %.lr.ph39, label %._crit_edge40, !llvm.loop !17
}

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


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