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

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

@gc_heaps_used = external global i64, align 8
@gc_heaps = external global [100000 x %struct.gc_heap], 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 @set_forwarding_ptr() #1 {
  %1 = alloca i64, align 8
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i64 0, ptr %1, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %68, %0
  %7 = load i64, ptr %1, align 8, !tbaa !5
  %8 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %9 = icmp ult i64 %7, %8
  br i1 %9, label %10, label %71

10:                                               ; preds = %6
  %11 = load i64, ptr %1, align 8, !tbaa !5
  %12 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %11
  %13 = getelementptr inbounds %struct.gc_heap, ptr %12, i32 0, i32 0
  %14 = load ptr, ptr %13, align 16, !tbaa !9
  %15 = ptrtoint ptr %14 to i64
  %16 = load i64, ptr %1, align 8, !tbaa !5
  %17 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %16
  %18 = getelementptr inbounds %struct.gc_heap, ptr %17, i32 0, i32 1
  %19 = load i64, ptr %18, align 8, !tbaa !12
  %20 = add i64 %15, %19
  %21 = inttoptr i64 %20 to ptr
  store ptr %21, ptr %3, align 8, !tbaa !13
  %22 = load i64, ptr %1, 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
  %25 = load ptr, ptr %24, align 16, !tbaa !9
  store ptr %25, ptr %2, align 8, !tbaa !13
  %26 = load i64, ptr %1, 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 0
  %29 = load ptr, ptr %28, align 16, !tbaa !9
  store ptr %29, ptr %5, align 8, !tbaa !13
  br label %30

30:                                               ; preds = %57, %10
  %31 = load ptr, ptr %2, align 8, !tbaa !13
  %32 = load ptr, ptr %3, align 8, !tbaa !13
  %33 = icmp ult ptr %31, %32
  br i1 %33, label %34, label %67

34:                                               ; preds = %30
  %35 = load ptr, ptr %2, align 8, !tbaa !13
  %36 = getelementptr inbounds %struct.header, ptr %35, i32 0, i32 1
  %37 = load i64, ptr %36, align 8, !tbaa !14
  %38 = and i64 %37, 1
  %39 = icmp ne i64 %38, 0
  br i1 %39, label %40, label %56

40:                                               ; preds = %34
  %41 = load ptr, ptr %2, align 8, !tbaa !13
  %42 = getelementptr inbounds %struct.header, ptr %41, i32 0, i32 1
  %43 = load i64, ptr %42, align 8, !tbaa !14
  %44 = and i64 %43, 2
  %45 = icmp ne i64 %44, 0
  br i1 %45, label %46, label %55

46:                                               ; preds = %40
  %47 = load ptr, ptr %5, align 8, !tbaa !13
  %48 = load ptr, ptr %2, align 8, !tbaa !13
  %49 = getelementptr inbounds %struct.header, ptr %48, i32 0, i32 5
  store ptr %47, ptr %49, align 8, !tbaa !16
  %50 = load ptr, ptr %5, align 8, !tbaa !13
  %51 = load ptr, ptr %2, align 8, !tbaa !13
  %52 = getelementptr inbounds %struct.header, ptr %51, i32 0, i32 2
  %53 = load i64, ptr %52, align 8, !tbaa !17
  %54 = getelementptr inbounds i8, ptr %50, i64 %53
  store ptr %54, ptr %5, align 8, !tbaa !13
  br label %55

55:                                               ; preds = %46, %40
  br label %56

56:                                               ; preds = %55, %34
  br label %57

57:                                               ; preds = %56
  %58 = load ptr, ptr %2, align 8, !tbaa !13
  %59 = getelementptr inbounds %struct.header, ptr %58, i64 1
  %60 = ptrtoint ptr %59 to i64
  %61 = load ptr, ptr %2, align 8, !tbaa !13
  %62 = getelementptr inbounds %struct.header, ptr %61, i32 0, i32 2
  %63 = load i64, ptr %62, align 8, !tbaa !17
  %64 = sub i64 %63, 48
  %65 = add i64 %60, %64
  %66 = inttoptr i64 %65 to ptr
  store ptr %66, ptr %2, align 8, !tbaa !13
  br label %30, !llvm.loop !18

67:                                               ; preds = %30
  br label %68

68:                                               ; preds = %67
  %69 = load i64, ptr %1, align 8, !tbaa !5
  %70 = add i64 %69, 1
  store i64 %70, ptr %1, align 8, !tbaa !5
  br label %6, !llvm.loop !20

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

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


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

@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

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @set_forwarding_ptr() local_unnamed_addr #0 {
  %1 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %._crit_edge23, label %.lr.ph22

.lr.ph22:                                         ; preds = %0, %._crit_edge
  %.01520 = phi i64 [ %26, %._crit_edge ], [ 0, %0 ]
  %2 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %.01520
  %3 = load ptr, ptr %2, align 16, !tbaa !9
  %4 = ptrtoint ptr %3 to i64
  %5 = getelementptr inbounds i8, ptr %2, i64 8
  %6 = load i64, ptr %5, align 8, !tbaa !12
  %7 = add i64 %6, %4
  %8 = inttoptr i64 %7 to ptr
  %9 = icmp ult ptr %3, %8
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.lr.ph22, %18
  %.019 = phi ptr [ %.1, %18 ], [ %3, %.lr.ph22 ]
  %.01618 = phi ptr [ %24, %18 ], [ %3, %.lr.ph22 ]
  %10 = getelementptr inbounds i8, ptr %.01618, i64 8
  %11 = load i64, ptr %10, align 8, !tbaa !13
  %12 = and i64 %11, 3
  %or.cond.not = icmp eq i64 %12, 3
  br i1 %or.cond.not, label %13, label %.lr.ph._crit_edge

.lr.ph._crit_edge:                                ; preds = %.lr.ph
  %.phi.trans.insert = getelementptr inbounds i8, ptr %.01618, i64 16
  %.pre = load i64, ptr %.phi.trans.insert, align 8, !tbaa !15
  br label %18

13:                                               ; preds = %.lr.ph
  %14 = getelementptr inbounds i8, ptr %.01618, i64 40
  store ptr %.019, ptr %14, align 8, !tbaa !16
  %15 = getelementptr inbounds i8, ptr %.01618, i64 16
  %16 = load i64, ptr %15, align 8, !tbaa !15
  %17 = getelementptr inbounds i8, ptr %.019, i64 %16
  br label %18

18:                                               ; preds = %.lr.ph._crit_edge, %13
  %19 = phi i64 [ %16, %13 ], [ %.pre, %.lr.ph._crit_edge ]
  %.1 = phi ptr [ %17, %13 ], [ %.019, %.lr.ph._crit_edge ]
  %20 = getelementptr inbounds i8, ptr %.01618, i64 48
  %21 = ptrtoint ptr %20 to i64
  %22 = add i64 %21, -48
  %23 = add i64 %22, %19
  %24 = inttoptr i64 %23 to ptr
  %25 = icmp ult ptr %24, %8
  br i1 %25, label %.lr.ph, label %._crit_edge, !llvm.loop !17

._crit_edge:                                      ; preds = %18, %.lr.ph22
  %26 = add nuw i64 %.01520, 1
  %exitcond.not = icmp eq i64 %26, %1
  br i1 %exitcond.not, label %._crit_edge23, label %.lr.ph22, !llvm.loop !19

._crit_edge23:                                    ; preds = %._crit_edge, %0
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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" }

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


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