; LLVM IR from humz2k/sugaryc :: ext/bdwgc (mark)

; 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.hblkhdr = type { ptr, ptr, ptr, i8, i8, i16, i64, i64, ptr, i64, [5 x i64] }

; 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 @GC_set_hdr_marks(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.hblkhdr, ptr %6, i32 0, i32 6
  %8 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %8, ptr %4, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %9 = load i64, ptr %4, align 8, !tbaa !13
  %10 = icmp ugt i64 %9, 2048
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  br label %18

12:                                               ; preds = %1
  %13 = load i64, ptr %4, align 8, !tbaa !13
  %14 = load i64, ptr %4, align 8, !tbaa !13
  %15 = udiv i64 4096, %14
  %16 = mul i64 %13, %15
  %17 = lshr i64 %16, 4
  br label %18

18:                                               ; preds = %12, %11
  %19 = phi i64 [ 256, %11 ], [ %17, %12 ]
  %20 = trunc i64 %19 to i32
  store i32 %20, ptr %5, align 4, !tbaa !14
  store i32 0, ptr %3, align 4, !tbaa !14
  br label %21

21:                                               ; preds = %32, %18
  %22 = load i32, ptr %3, align 4, !tbaa !14
  %23 = load i32, ptr %5, align 4, !tbaa !14
  %24 = lshr i32 %23, 6
  %25 = icmp ult i32 %22, %24
  br i1 %25, label %26, label %35

26:                                               ; preds = %21
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.hblkhdr, ptr %27, i32 0, i32 10
  %29 = load i32, ptr %3, align 4, !tbaa !14
  %30 = zext i32 %29 to i64
  %31 = getelementptr inbounds [5 x i64], ptr %28, i64 0, i64 %30
  store i64 -1, ptr %31, align 8, !tbaa !13
  br label %32

32:                                               ; preds = %26
  %33 = load i32, ptr %3, align 4, !tbaa !14
  %34 = add i32 %33, 1
  store i32 %34, ptr %3, align 4, !tbaa !14
  br label %21, !llvm.loop !16

35:                                               ; preds = %21
  %36 = load i32, ptr %5, align 4, !tbaa !14
  %37 = and i32 %36, 63
  %38 = zext i32 %37 to i64
  %39 = shl i64 1, %38
  %40 = sub i64 %39, 1
  %41 = shl i64 %40, 1
  %42 = or i64 %41, 1
  %43 = load ptr, ptr %2, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.hblkhdr, ptr %43, i32 0, i32 10
  %45 = load i32, ptr %3, align 4, !tbaa !14
  %46 = zext i32 %45 to i64
  %47 = getelementptr inbounds [5 x i64], ptr %44, i64 0, i64 %46
  store i64 %42, ptr %47, align 8, !tbaa !13
  %48 = load i64, ptr %4, align 8, !tbaa !13
  %49 = udiv i64 4096, %48
  %50 = load ptr, ptr %2, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.hblkhdr, ptr %50, i32 0, i32 9
  store i64 %49, ptr %51, align 8, !tbaa !18
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 32}
!10 = !{!"hblkhdr", !6, i64 0, !6, i64 8, !6, i64 16, !7, i64 24, !7, i64 25, !11, i64 26, !12, i64 32, !12, i64 40, !6, i64 48, !12, i64 56, !7, i64 64}
!11 = !{!"short", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!10, !12, i64 56}


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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) uwtable
define dso_local void @GC_set_hdr_marks(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 32
  %3 = load i64, ptr %2, align 8, !tbaa !5
  %4 = icmp ugt i64 %3, 2048
  br i1 %4, label %._crit_edge, label %5

5:                                                ; preds = %1
  %.rhs.trunc = trunc nuw i64 %3 to i16
  %6 = urem i16 4096, %.rhs.trunc
  %narrow = sub nuw nsw i16 4096, %6
  %7 = lshr i16 %narrow, 4
  %8 = zext nneg i16 %7 to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %1, %5
  %9 = phi i32 [ %8, %5 ], [ 256, %1 ]
  %10 = lshr i32 %9, 6
  %11 = getelementptr inbounds i8, ptr %0, i64 64
  %12 = add nsw i32 %10, -1
  %13 = zext nneg i32 %12 to i64
  %14 = shl nuw nsw i64 %13, 3
  %15 = add nuw nsw i64 %14, 8
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(1) %11, i8 -1, i64 %15, i1 false), !tbaa !12
  %16 = zext nneg i32 %10 to i64
  %17 = and i32 %9, 63
  %18 = zext nneg i32 %17 to i64
  %notmask = shl nsw i64 -1, %18
  %19 = xor i64 %notmask, -1
  %20 = shl nuw i64 %19, 1
  %21 = or disjoint i64 %20, 1
  %22 = getelementptr inbounds i8, ptr %0, i64 64
  %23 = getelementptr inbounds [5 x i64], ptr %22, i64 0, i64 %16
  store i64 %21, ptr %23, align 8, !tbaa !12
  %24 = udiv i64 4096, %3
  %25 = getelementptr inbounds i8, ptr %0, i64 56
  store i64 %24, ptr %25, align 8, !tbaa !13
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) 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 nounwind willreturn memory(argmem: write) }

!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, !11, i64 32}
!6 = !{!"hblkhdr", !7, i64 0, !7, i64 8, !7, i64 16, !8, i64 24, !8, i64 25, !10, i64 26, !11, i64 32, !11, i64 40, !7, i64 48, !11, i64 56, !8, i64 64}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!6, !11, i64 56}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass