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

@newg = external dso_local global i32, align 4
@oldg = external dso_local global i32, align 4
@auto_grow = external global i32, align 4
@gc_heaps_used = external global i64, align 8
@gc_heaps = external global [100000 x %struct.gc_heap], align 16
@new_free_p = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @gc_init(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  store i32 0, ptr @auto_grow, align 4, !tbaa !9
  store i64 3, ptr @gc_heaps_used, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %5

5:                                                ; preds = %40, %1
  %6 = load i64, ptr %3, align 8, !tbaa !5
  %7 = icmp ult i64 %6, 4
  br i1 %7, label %9, label %8

8:                                                ; preds = %5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  br label %43

9:                                                ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  %10 = load i64, ptr %2, align 8, !tbaa !5
  %11 = add i64 %10, 8
  %12 = call ptr @sbrk(i64 noundef %11) #5
  store ptr %12, ptr %4, align 8, !tbaa !11
  %13 = load ptr, ptr %4, align 8, !tbaa !11
  %14 = icmp eq ptr %13, null
  br i1 %14, label %15, label %16

15:                                               ; preds = %9
  call void @exit(i32 noundef -1) #6
  unreachable

16:                                               ; preds = %9
  %17 = load ptr, ptr %4, align 8, !tbaa !11
  %18 = ptrtoint ptr %17 to i64
  %19 = add i64 %18, 7
  %20 = and i64 %19, -8
  %21 = inttoptr i64 %20 to ptr
  %22 = load i64, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %22
  %24 = getelementptr inbounds %struct.gc_heap, ptr %23, i32 0, i32 0
  store ptr %21, ptr %24, align 16, !tbaa !13
  %25 = load i64, ptr %2, align 8, !tbaa !5
  %26 = load i64, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %26
  %28 = getelementptr inbounds %struct.gc_heap, ptr %27, i32 0, i32 1
  store i64 %25, ptr %28, align 8, !tbaa !15
  %29 = load i64, ptr %2, align 8, !tbaa !5
  %30 = load i64, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %30
  %32 = getelementptr inbounds %struct.gc_heap, ptr %31, i32 0, i32 0
  %33 = load ptr, ptr %32, align 16, !tbaa !13
  %34 = getelementptr inbounds %struct.header, ptr %33, i32 0, i32 2
  store i64 %29, ptr %34, align 8, !tbaa !16
  %35 = load i64, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %35
  %37 = getelementptr inbounds %struct.gc_heap, ptr %36, i32 0, i32 0
  %38 = load ptr, ptr %37, align 16, !tbaa !13
  %39 = getelementptr inbounds %struct.header, ptr %38, i32 0, i32 4
  store ptr null, ptr %39, align 8, !tbaa !18
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  br label %40

40:                                               ; preds = %16
  %41 = load i64, ptr %3, align 8, !tbaa !5
  %42 = add i64 %41, 1
  store i64 %42, ptr %3, align 8, !tbaa !5
  br label %5, !llvm.loop !19

43:                                               ; preds = %8
  %44 = load i32, ptr @newg, align 4, !tbaa !9
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %45
  %47 = getelementptr inbounds %struct.gc_heap, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 16, !tbaa !13
  store ptr %48, ptr @new_free_p, align 8, !tbaa !11
  %49 = load i32, ptr @oldg, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %50
  %52 = getelementptr inbounds %struct.gc_heap, ptr %51, i32 0, i32 0
  %53 = load ptr, ptr %52, align 16, !tbaa !13
  %54 = getelementptr inbounds %struct.header, ptr %53, i64 1
  call void @gc_free(ptr noundef %54)
  ret void
}

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

; Function Attrs: nounwind
declare ptr @sbrk(i64 noundef) #2

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #3

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

declare void @gc_free(ptr noundef) #4

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 = { 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 = { 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 = { "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 = !{!"long", !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, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"gc_heap", !12, i64 0, !6, i64 8}
!15 = !{!14, !6, i64 8}
!16 = !{!17, !6, i64 16}
!17 = !{!"header", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !12, i64 32, !12, i64 40}
!18 = !{!17, !12, i64 32}
!19 = distinct !{!19, !20}
!20 = !{!"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"

%struct.gc_heap = type { ptr, i64 }

@newg = external dso_local local_unnamed_addr global i32, align 4
@oldg = external dso_local local_unnamed_addr global i32, align 4
@auto_grow = external local_unnamed_addr global i32, align 4
@gc_heaps_used = external local_unnamed_addr global i64, align 8
@gc_heaps = external local_unnamed_addr global [100000 x %struct.gc_heap], align 16
@new_free_p = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @gc_init(i64 noundef %0) local_unnamed_addr #0 {
  store i32 0, ptr @auto_grow, align 4, !tbaa !5
  store i64 3, ptr @gc_heaps_used, align 8, !tbaa !9
  %2 = add i64 %0, 8
  %3 = tail call ptr @sbrk(i64 noundef %2) #4
  %4 = icmp eq ptr %3, null
  br i1 %4, label %5, label %6

5:                                                ; preds = %24, %15, %6, %1
  tail call void @exit(i32 noundef -1) #5
  unreachable

6:                                                ; preds = %1
  %7 = ptrtoint ptr %3 to i64
  %8 = add i64 %7, 7
  %9 = and i64 %8, -8
  %10 = inttoptr i64 %9 to ptr
  store ptr %10, ptr @gc_heaps, align 16, !tbaa !11
  store i64 %0, ptr getelementptr inbounds (i8, ptr @gc_heaps, i64 8), align 8, !tbaa !14
  %11 = getelementptr inbounds i8, ptr %10, i64 16
  store i64 %0, ptr %11, align 8, !tbaa !15
  %12 = getelementptr inbounds i8, ptr %10, i64 32
  store ptr null, ptr %12, align 8, !tbaa !17
  %13 = tail call ptr @sbrk(i64 noundef %2) #4
  %14 = icmp eq ptr %13, null
  br i1 %14, label %5, label %15

15:                                               ; preds = %6
  %16 = ptrtoint ptr %13 to i64
  %17 = add i64 %16, 7
  %18 = and i64 %17, -8
  %19 = inttoptr i64 %18 to ptr
  store ptr %19, ptr getelementptr inbounds (i8, ptr @gc_heaps, i64 16), align 16, !tbaa !11
  store i64 %0, ptr getelementptr inbounds (i8, ptr @gc_heaps, i64 24), align 8, !tbaa !14
  %20 = getelementptr inbounds i8, ptr %19, i64 16
  store i64 %0, ptr %20, align 8, !tbaa !15
  %21 = getelementptr inbounds i8, ptr %19, i64 32
  store ptr null, ptr %21, align 8, !tbaa !17
  %22 = tail call ptr @sbrk(i64 noundef %2) #4
  %23 = icmp eq ptr %22, null
  br i1 %23, label %5, label %24

24:                                               ; preds = %15
  %25 = ptrtoint ptr %22 to i64
  %26 = add i64 %25, 7
  %27 = and i64 %26, -8
  %28 = inttoptr i64 %27 to ptr
  store ptr %28, ptr getelementptr inbounds (i8, ptr @gc_heaps, i64 32), align 16, !tbaa !11
  store i64 %0, ptr getelementptr inbounds (i8, ptr @gc_heaps, i64 40), align 8, !tbaa !14
  %29 = getelementptr inbounds i8, ptr %28, i64 16
  store i64 %0, ptr %29, align 8, !tbaa !15
  %30 = getelementptr inbounds i8, ptr %28, i64 32
  store ptr null, ptr %30, align 8, !tbaa !17
  %31 = tail call ptr @sbrk(i64 noundef %2) #4
  %32 = icmp eq ptr %31, null
  br i1 %32, label %5, label %33

33:                                               ; preds = %24
  %34 = ptrtoint ptr %31 to i64
  %35 = add i64 %34, 7
  %36 = and i64 %35, -8
  %37 = inttoptr i64 %36 to ptr
  store ptr %37, ptr getelementptr inbounds (i8, ptr @gc_heaps, i64 48), align 16, !tbaa !11
  store i64 %0, ptr getelementptr inbounds (i8, ptr @gc_heaps, i64 56), align 8, !tbaa !14
  %38 = getelementptr inbounds i8, ptr %37, i64 16
  store i64 %0, ptr %38, align 8, !tbaa !15
  %39 = getelementptr inbounds i8, ptr %37, i64 32
  store ptr null, ptr %39, align 8, !tbaa !17
  %40 = load i32, ptr @newg, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %41
  %43 = load ptr, ptr %42, align 16, !tbaa !11
  store ptr %43, ptr @new_free_p, align 8, !tbaa !18
  %44 = load i32, ptr @oldg, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %45
  %47 = load ptr, ptr %46, align 16, !tbaa !11
  %48 = getelementptr inbounds i8, ptr %47, i64 48
  tail call void @gc_free(ptr noundef nonnull %48) #4
  ret void
}

; Function Attrs: nounwind
declare ptr @sbrk(i64 noundef) local_unnamed_addr #1

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

declare void @gc_free(ptr noundef) local_unnamed_addr #3

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,SROAPass,SimplifyCFGPass,TailCallElimPass