; 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
@oldg = external dso_local global i32, align 4
@rs = external dso_local global [100000 x ptr], align 16

; 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 void @write_barrier(ptr noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.header, ptr %9, i64 -1
  store ptr %10, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %11 = load ptr, ptr %6, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.header, ptr %11, i64 -1
  store ptr %12, ptr %8, align 8, !tbaa !5
  %13 = load ptr, ptr %7, align 8, !tbaa !5
  %14 = load i32, ptr @oldg, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = call ptr @is_pointer_to_space(ptr noundef %13, i64 noundef %15)
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %46

18:                                               ; preds = %3
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = load i32, ptr @oldg, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = call ptr @is_pointer_to_space(ptr noundef %19, i64 noundef %21)
  %23 = icmp ne ptr %22, null
  br i1 %23, label %46, label %24

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

30:                                               ; preds = %24
  %31 = load ptr, ptr %7, 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, 8
  %35 = icmp ne i64 %34, 0
  br i1 %35, label %46, label %36

36:                                               ; preds = %30, %24
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = load i32, ptr @rs_index, align 4, !tbaa !9
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr @rs_index, align 4, !tbaa !9
  %40 = sext i32 %38 to i64
  %41 = getelementptr inbounds [100000 x ptr], ptr @rs, i64 0, i64 %40
  store ptr %37, ptr %41, align 8, !tbaa !5
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.header, ptr %42, i32 0, i32 1
  %44 = load i64, ptr %43, align 8, !tbaa !11
  %45 = or i64 %44, 8
  store i64 %45, ptr %43, align 8, !tbaa !11
  br label %46

46:                                               ; preds = %36, %30, %18, %3
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %47, ptr %48, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret void
}

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

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 = { 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}


; 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
@oldg = external dso_local local_unnamed_addr global i32, align 4
@rs = external dso_local local_unnamed_addr global [100000 x ptr], align 16

; Function Attrs: nounwind uwtable
define dso_local void @write_barrier(ptr noundef %0, ptr nocapture noundef writeonly %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 -48
  %5 = load i32, ptr @oldg, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = tail call ptr @is_pointer_to_space(ptr noundef nonnull %4, i64 noundef %6) #2
  %.not = icmp eq ptr %7, null
  br i1 %.not, label %23, label %8

8:                                                ; preds = %3
  %9 = getelementptr inbounds i8, ptr %2, i64 -48
  %10 = load i32, ptr @oldg, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = tail call ptr @is_pointer_to_space(ptr noundef nonnull %9, i64 noundef %11) #2
  %.not10 = icmp eq ptr %12, null
  br i1 %.not10, label %13, label %23

13:                                               ; preds = %8
  %14 = getelementptr inbounds i8, ptr %0, i64 -40
  %15 = load i64, ptr %14, align 8, !tbaa !9
  %16 = and i64 %15, 9
  %or.cond.not = icmp eq i64 %16, 9
  br i1 %or.cond.not, label %23, label %17

17:                                               ; preds = %13
  %18 = load i32, ptr @rs_index, align 4, !tbaa !5
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr @rs_index, align 4, !tbaa !5
  %20 = sext i32 %18 to i64
  %21 = getelementptr inbounds [100000 x ptr], ptr @rs, i64 0, i64 %20
  store ptr %4, ptr %21, align 8, !tbaa !13
  %22 = or i64 %15, 8
  store i64 %22, ptr %14, align 8, !tbaa !9
  br label %23

23:                                               ; preds = %13, %17, %8, %3
  store ptr %2, ptr %1, align 8, !tbaa !13
  ret void
}

declare ptr @is_pointer_to_space(ptr noundef, i64 noundef) local_unnamed_addr #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 = { "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 }

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass