; LLVM IR from brewlin/garbage-collect :: mark-sweep-bitmap (mark_sweep)

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

@heap = external global %struct.gc_heap, align 8

; Function Attrs: nounwind uwtable
define dso_local void @gc_mark(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %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) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %11 = load ptr, ptr getelementptr inbounds (%struct.gc_heap, ptr @heap, i32 0, i32 1), align 8, !tbaa !9
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = icmp ule ptr %11, %12
  br i1 %13, label %14, label %20

14:                                               ; preds = %1
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = ptrtoint ptr %15 to i64
  %17 = load ptr, ptr getelementptr inbounds (%struct.gc_heap, ptr @heap, i32 0, i32 3), align 8, !tbaa !13
  %18 = ptrtoint ptr %17 to i64
  %19 = icmp ult i64 %16, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %14, %1
  store i32 1, ptr %5, align 4
  br label %72

21:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %22 = load ptr, ptr %2, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.header, ptr %22, i64 -1
  %24 = load ptr, ptr getelementptr inbounds (%struct.gc_heap, ptr @heap, i32 0, i32 1), align 8, !tbaa !9
  %25 = ptrtoint ptr %23 to i64
  %26 = ptrtoint ptr %24 to i64
  %27 = sub i64 %25, %26
  %28 = udiv i64 %27, 8
  %29 = trunc i64 %28 to i32
  store i32 %29, ptr %6, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %30 = load i32, ptr %6, align 4, !tbaa !14
  %31 = sext i32 %30 to i64
  %32 = udiv i64 %31, 8
  %33 = trunc i64 %32 to i32
  store i32 %33, ptr %7, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %34 = load i32, ptr %6, align 4, !tbaa !14
  %35 = sext i32 %34 to i64
  %36 = urem i64 %35, 8
  %37 = trunc i64 %36 to i32
  store i32 %37, ptr %8, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %38 = load ptr, ptr @heap, align 8, !tbaa !15
  %39 = load i32, ptr %7, align 4, !tbaa !14
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i8, ptr %38, i64 %40
  store ptr %41, ptr %9, align 8, !tbaa !5
  %42 = load i32, ptr %8, align 4, !tbaa !14
  %43 = shl i32 1, %42
  %44 = load ptr, ptr %9, align 8, !tbaa !5
  %45 = load i8, ptr %44, align 1, !tbaa !16
  %46 = sext i8 %45 to i32
  %47 = or i32 %46, %43
  %48 = trunc i32 %47 to i8
  store i8 %48, ptr %44, align 1, !tbaa !16
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.header, ptr %49, i64 -1
  store ptr %50, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %51 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %51, ptr %10, align 8, !tbaa !5
  br label %52

52:                                               ; preds = %68, %21
  %53 = load ptr, ptr %10, align 8, !tbaa !5
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.header, ptr %54, i64 1
  %56 = ptrtoint ptr %55 to i64
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.header, ptr %57, i32 0, i32 0
  %59 = load i64, ptr %58, align 8, !tbaa !17
  %60 = sub i64 %59, 16
  %61 = add i64 %56, %60
  %62 = inttoptr i64 %61 to ptr
  %63 = icmp ult ptr %53, %62
  br i1 %63, label %65, label %64

64:                                               ; preds = %52
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  br label %71

65:                                               ; preds = %52
  %66 = load ptr, ptr %10, align 8, !tbaa !5
  %67 = load ptr, ptr %66, align 8, !tbaa !5
  call void @gc_mark(ptr noundef %67)
  br label %68

68:                                               ; preds = %65
  %69 = load ptr, ptr %10, align 8, !tbaa !5
  %70 = getelementptr inbounds i8, ptr %69, i32 1
  store ptr %70, ptr %10, align 8, !tbaa !5
  br label %52, !llvm.loop !19

71:                                               ; preds = %64
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  store i32 0, ptr %5, align 4
  br label %72

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

74:                                               ; preds = %72, %72
  ret void

75:                                               ; preds = %72
  unreachable
}

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"gc_heap", !6, i64 0, !6, i64 8, !11, i64 16, !6, i64 24, !12, i64 32}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!10, !6, i64 24}
!14 = !{!11, !11, i64 0}
!15 = !{!10, !6, i64 0}
!16 = !{!7, !7, i64 0}
!17 = !{!18, !12, i64 0}
!18 = !{!"header", !12, i64 0, !6, i64 8}
!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, ptr, i32, ptr, i64 }

@heap = external local_unnamed_addr global %struct.gc_heap, align 8

; Function Attrs: nofree nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @gc_mark(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr getelementptr inbounds (i8, ptr @heap, i64 8), align 8, !tbaa !5
  %.not = icmp ule ptr %2, %0
  %3 = load ptr, ptr getelementptr inbounds (i8, ptr @heap, i64 24), align 8
  %4 = icmp ugt ptr %3, %0
  %or.cond = select i1 %.not, i1 %4, i1 false
  br i1 %or.cond, label %5, label %.loopexit

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 -16
  %7 = ptrtoint ptr %6 to i64
  %8 = ptrtoint ptr %2 to i64
  %9 = sub i64 %7, %8
  %10 = shl i64 %9, 29
  %11 = lshr i64 %10, 32
  %12 = trunc i64 %11 to i8
  %13 = and i8 %12, 7
  %14 = load ptr, ptr @heap, align 8, !tbaa !12
  %15 = ashr i64 %10, 35
  %16 = getelementptr inbounds i8, ptr %14, i64 %15
  %17 = shl nuw i8 1, %13
  %18 = load i8, ptr %16, align 1, !tbaa !13
  %19 = or i8 %18, %17
  store i8 %19, ptr %16, align 1, !tbaa !13
  %20 = ptrtoint ptr %0 to i64
  %21 = add i64 %20, -16
  %22 = load i64, ptr %6, align 8, !tbaa !14
  %23 = add i64 %21, %22
  %24 = inttoptr i64 %23 to ptr
  %25 = icmp ugt ptr %24, %0
  br i1 %25, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %5, %.lr.ph
  %.017 = phi ptr [ %27, %.lr.ph ], [ %0, %5 ]
  %26 = load ptr, ptr %.017, align 8, !tbaa !16
  tail call void @gc_mark(ptr noundef %26)
  %27 = getelementptr inbounds i8, ptr %.017, i64 1
  %28 = load i64, ptr %6, align 8, !tbaa !14
  %29 = add i64 %21, %28
  %30 = inttoptr i64 %29 to ptr
  %31 = icmp ult ptr %27, %30
  br i1 %31, label %.lr.ph, label %.loopexit, !llvm.loop !17

.loopexit:                                        ; preds = %.lr.ph, %5, %1
  ret void
}

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


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