; 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

; 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 @promote(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 ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.header, ptr %11, i64 -1
  store ptr %12, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.header, ptr %13, i64 -1
  %15 = getelementptr inbounds %struct.header, ptr %14, i32 0, i32 2
  %16 = load i64, ptr %15, align 8, !tbaa !9
  %17 = call ptr @major_malloc(i64 noundef %16)
  store ptr %17, ptr %4, align 8, !tbaa !5
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = icmp eq ptr %18, null
  br i1 %19, label %20, label %21

20:                                               ; preds = %1
  call void @abort() #6
  unreachable

21:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.header, ptr %22, i64 -1
  store ptr %23, ptr %5, align 8, !tbaa !5
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.header, ptr %26, i32 0, i32 2
  %28 = load i64, ptr %27, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %24, ptr align 8 %25, i64 %28, i1 false)
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.header, ptr %30, i32 0, i32 5
  store ptr %29, ptr %31, align 8, !tbaa !12
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.header, ptr %32, i32 0, i32 1
  %34 = load i64, ptr %33, align 8, !tbaa !13
  %35 = or i64 %34, 4
  store i64 %35, ptr %33, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %36, ptr %6, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %71, %21
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.header, ptr %39, i64 1
  %41 = ptrtoint ptr %40 to i64
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.header, ptr %42, i32 0, i32 2
  %44 = load i64, ptr %43, align 8, !tbaa !9
  %45 = sub i64 %44, 48
  %46 = add i64 %41, %45
  %47 = inttoptr i64 %46 to ptr
  %48 = icmp ult ptr %38, %47
  br i1 %48, label %50, label %49

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

50:                                               ; preds = %37
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = load ptr, ptr %51, align 8, !tbaa !5
  store ptr %52, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  %54 = call ptr @is_pointer_to_heap(ptr noundef %53)
  store ptr %54, ptr %9, align 8, !tbaa !5
  %55 = icmp ne ptr %54, null
  br i1 %55, label %57, label %56

56:                                               ; preds = %50
  store i32 4, ptr %7, align 4
  br label %69

57:                                               ; preds = %50
  %58 = load ptr, ptr %9, align 8, !tbaa !5
  %59 = load ptr, ptr %8, align 8, !tbaa !5
  %60 = call ptr @get_header(ptr noundef %58, ptr noundef %59)
  store ptr %60, ptr %10, align 8, !tbaa !5
  %61 = icmp ne ptr %60, null
  br i1 %61, label %63, label %62

62:                                               ; preds = %57
  store i32 4, ptr %7, align 4
  br label %69

63:                                               ; preds = %57
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = load i32, ptr @rs_index, align 4, !tbaa !14
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr @rs_index, align 4, !tbaa !14
  %67 = sext i32 %65 to i64
  %68 = getelementptr inbounds [100000 x ptr], ptr @rs, i64 0, i64 %67
  store ptr %64, ptr %68, align 8, !tbaa !5
  store i32 2, ptr %7, align 4
  br label %69

69:                                               ; preds = %63, %62, %56
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %70 = load i32, ptr %7, align 4
  switch i32 %70, label %74 [
    i32 4, label %71
  ]

71:                                               ; preds = %69
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = getelementptr inbounds i8, ptr %72, i32 1
  store ptr %73, ptr %6, align 8, !tbaa !5
  br label %37, !llvm.loop !16

74:                                               ; preds = %69, %49
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  br label %75

75:                                               ; preds = %74
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  ret void
}

declare ptr @major_malloc(i64 noundef) #2

; Function Attrs: noreturn nounwind
declare void @abort() #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 ptr @is_pointer_to_heap(ptr noundef) #2

declare ptr @get_header(ptr noundef, ptr 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 = { 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, !11, i64 16}
!10 = !{!"header", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !6, i64 32, !6, i64 40}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !6, i64 40}
!13 = !{!10, !11, i64 8}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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"

@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

; Function Attrs: nounwind uwtable
define dso_local void @promote(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 -32
  %3 = load i64, ptr %2, align 8, !tbaa !5
  %4 = tail call ptr @major_malloc(i64 noundef %3) #4
  %5 = icmp eq ptr %4, null
  br i1 %5, label %6, label %7

6:                                                ; preds = %1
  tail call void @abort() #5
  unreachable

7:                                                ; preds = %1
  %8 = getelementptr inbounds i8, ptr %0, i64 -48
  %9 = getelementptr inbounds i8, ptr %4, i64 -48
  %10 = load i64, ptr %2, align 8, !tbaa !5
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 8 %9, ptr nonnull align 8 %8, i64 %10, i1 false)
  %11 = getelementptr inbounds i8, ptr %0, i64 -8
  store ptr %9, ptr %11, align 8, !tbaa !11
  %12 = getelementptr inbounds i8, ptr %0, i64 -40
  %13 = load i64, ptr %12, align 8, !tbaa !12
  %14 = or i64 %13, 4
  store i64 %14, ptr %12, align 8, !tbaa !12
  %15 = ptrtoint ptr %0 to i64
  %16 = add i64 %15, -48
  %17 = load i64, ptr %2, align 8, !tbaa !5
  %18 = add i64 %16, %17
  %19 = inttoptr i64 %18 to ptr
  %20 = icmp ugt ptr %19, %0
  br i1 %20, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %7, %29
  %.024 = phi ptr [ %30, %29 ], [ %0, %7 ]
  %21 = load ptr, ptr %.024, align 8, !tbaa !13
  %22 = tail call ptr @is_pointer_to_heap(ptr noundef %21) #4
  %.not = icmp eq ptr %22, null
  br i1 %.not, label %29, label %23

23:                                               ; preds = %.lr.ph
  %24 = tail call ptr @get_header(ptr noundef nonnull %22, ptr noundef %21) #4
  %.not23 = icmp eq ptr %24, null
  br i1 %.not23, label %29, label %.critedge

.critedge:                                        ; preds = %23
  %25 = load i32, ptr @rs_index, align 4, !tbaa !14
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr @rs_index, align 4, !tbaa !14
  %27 = sext i32 %25 to i64
  %28 = getelementptr inbounds [100000 x ptr], ptr @rs, i64 0, i64 %27
  store ptr %4, ptr %28, align 8, !tbaa !13
  br label %.loopexit

29:                                               ; preds = %.lr.ph, %23
  %30 = getelementptr inbounds i8, ptr %.024, i64 1
  %31 = load i64, ptr %2, align 8, !tbaa !5
  %32 = add i64 %16, %31
  %33 = inttoptr i64 %32 to ptr
  %34 = icmp ult ptr %30, %33
  br i1 %34, label %.lr.ph, label %.loopexit, !llvm.loop !16

.loopexit:                                        ; preds = %29, %7, %.critedge
  ret void
}

declare ptr @major_malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: noreturn nounwind
declare void @abort() 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

declare ptr @is_pointer_to_heap(ptr noundef) local_unnamed_addr #1

declare ptr @get_header(ptr noundef, ptr 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 = { 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 = { nounwind }
attributes #5 = { 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, !7, i64 16}
!6 = !{!"header", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !10, i64 40}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 40}
!12 = !{!6, !7, i64 8}
!13 = !{!10, !10, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !8, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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