; LLVM IR from brewlin/garbage-collect :: gc-golang/heap (heap_common)

; 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.mheap = type { %union.pthread_mutex_t, %struct.mtreap, %struct.mtreap, %struct.array_t, [2 x %struct.gcSweepBuf], %struct.mfixalloc, %struct.mfixalloc, %struct.mfixalloc, %struct.mfixalloc, %struct.mfixalloc, %struct.mfixalloc, %struct.lineralloc, %struct.array_t, ptr, [1 x %struct.array_t], i64, i32, i32, i32, ptr, i64, i64, i64, [134 x %struct.mcentral] }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct.mtreap = type { ptr }
%struct.gcSweepBuf = type { %union.pthread_mutex_t, ptr, i64, i64, i32 }
%struct.mfixalloc = type { i64, ptr, ptr, ptr, i64, i32, i64, i8 }
%struct.lineralloc = type { i64, i64, i64 }
%struct.array_t = type { ptr, i32, i64, i32 }
%struct.mcentral = type { %union.pthread_mutex_t, i8, %struct.mspanlist, %struct.mspanlist, i64 }
%struct.mspanlist = type { ptr, ptr }
%struct.mheapArena = type { [2097152 x i8], [8192 x ptr], [1024 x i8], [1024 x i8] }

@heap_ = external global %struct.mheap, align 8

; 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

declare i32 @arenaIndex(i64 noundef) #1

declare i32 @arena_l2(i32 noundef) #1

declare i32 @arena_l1(i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @heap_setSpans(i64 noundef %0, i64 noundef %1, ptr noundef %2) #2 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store ptr @heap_, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %14 = load i64, ptr %4, align 8, !tbaa !5
  %15 = udiv i64 %14, 8192
  store i64 %15, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %16 = load i64, ptr %4, align 8, !tbaa !5
  %17 = call i32 @arenaIndex(i64 noundef %16)
  store i32 %17, ptr %9, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %18 = load ptr, ptr %7, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.mheap, ptr %18, i32 0, i32 14
  %20 = load i32, ptr %9, align 4, !tbaa !11
  %21 = call i32 @arena_l1(i32 noundef %20)
  %22 = zext i32 %21 to i64
  %23 = getelementptr inbounds [1 x %struct.array_t], ptr %19, i64 0, i64 %22
  store ptr %23, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %24 = load ptr, ptr %10, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.array_t, ptr %24, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !13
  %27 = load ptr, ptr %10, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.array_t, ptr %27, i32 0, i32 2
  %29 = load i64, ptr %28, align 8, !tbaa !15
  %30 = load i32, ptr %9, align 4, !tbaa !11
  %31 = call i32 @arena_l2(i32 noundef %30)
  %32 = zext i32 %31 to i64
  %33 = mul i64 %29, %32
  %34 = getelementptr inbounds i8, ptr %26, i64 %33
  store ptr %34, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  store i64 0, ptr %12, align 8, !tbaa !5
  br label %35

35:                                               ; preds = %77, %3
  %36 = load i64, ptr %12, align 8, !tbaa !5
  %37 = load i64, ptr %5, align 8, !tbaa !5
  %38 = icmp ult i64 %36, %37
  br i1 %38, label %40, label %39

39:                                               ; preds = %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %80

40:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %41 = load i64, ptr %8, align 8, !tbaa !5
  %42 = load i64, ptr %12, align 8, !tbaa !5
  %43 = add i64 %41, %42
  %44 = urem i64 %43, 8192
  store i64 %44, ptr %13, align 8, !tbaa !5
  %45 = load i64, ptr %13, align 8, !tbaa !5
  %46 = icmp eq i64 %45, 0
  br i1 %46, label %47, label %70

47:                                               ; preds = %40
  %48 = load i64, ptr %4, align 8, !tbaa !5
  %49 = load i64, ptr %12, align 8, !tbaa !5
  %50 = mul i64 %49, 8192
  %51 = add i64 %48, %50
  %52 = call i32 @arenaIndex(i64 noundef %51)
  store i32 %52, ptr %9, align 4, !tbaa !11
  %53 = load ptr, ptr %7, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.mheap, ptr %53, i32 0, i32 14
  %55 = load i32, ptr %9, align 4, !tbaa !11
  %56 = call i32 @arena_l1(i32 noundef %55)
  %57 = zext i32 %56 to i64
  %58 = getelementptr inbounds [1 x %struct.array_t], ptr %54, i64 0, i64 %57
  store ptr %58, ptr %10, align 8, !tbaa !9
  %59 = load ptr, ptr %10, align 8, !tbaa !9
  %60 = getelementptr inbounds %struct.array_t, ptr %59, i32 0, i32 0
  %61 = load ptr, ptr %60, align 8, !tbaa !13
  %62 = load ptr, ptr %10, align 8, !tbaa !9
  %63 = getelementptr inbounds %struct.array_t, ptr %62, i32 0, i32 2
  %64 = load i64, ptr %63, align 8, !tbaa !15
  %65 = load i32, ptr %9, align 4, !tbaa !11
  %66 = call i32 @arena_l2(i32 noundef %65)
  %67 = zext i32 %66 to i64
  %68 = mul i64 %64, %67
  %69 = getelementptr inbounds i8, ptr %61, i64 %68
  store ptr %69, ptr %11, align 8, !tbaa !9
  br label %70

70:                                               ; preds = %47, %40
  %71 = load ptr, ptr %6, align 8, !tbaa !9
  %72 = load ptr, ptr %11, align 8, !tbaa !9
  %73 = load ptr, ptr %72, align 8, !tbaa !9
  %74 = getelementptr inbounds %struct.mheapArena, ptr %73, i32 0, i32 1
  %75 = load i64, ptr %13, align 8, !tbaa !5
  %76 = getelementptr inbounds [8192 x ptr], ptr %74, i64 0, i64 %75
  store ptr %71, ptr %76, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  br label %77

77:                                               ; preds = %70
  %78 = load i64, ptr %12, align 8, !tbaa !5
  %79 = add i64 %78, 1
  store i64 %79, ptr %12, align 8, !tbaa !5
  br label %35, !llvm.loop !16

80:                                               ; preds = %39
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #3 = { 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 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"", !10, i64 0, !12, i64 8, !6, i64 16, !12, i64 24}
!15 = !{!14, !6, i64 16}
!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"

%struct.mheap = type { %union.pthread_mutex_t, %struct.mtreap, %struct.mtreap, %struct.array_t, [2 x %struct.gcSweepBuf], %struct.mfixalloc, %struct.mfixalloc, %struct.mfixalloc, %struct.mfixalloc, %struct.mfixalloc, %struct.mfixalloc, %struct.lineralloc, %struct.array_t, ptr, [1 x %struct.array_t], i64, i32, i32, i32, ptr, i64, i64, i64, [134 x %struct.mcentral] }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct.mtreap = type { ptr }
%struct.gcSweepBuf = type { %union.pthread_mutex_t, ptr, i64, i64, i32 }
%struct.mfixalloc = type { i64, ptr, ptr, ptr, i64, i32, i64, i8 }
%struct.lineralloc = type { i64, i64, i64 }
%struct.array_t = type { ptr, i32, i64, i32 }
%struct.mcentral = type { %union.pthread_mutex_t, i8, %struct.mspanlist, %struct.mspanlist, i64 }
%struct.mspanlist = type { ptr, ptr }

@heap_ = external local_unnamed_addr global %struct.mheap, align 8

declare i32 @arenaIndex(i64 noundef) local_unnamed_addr #0

declare i32 @arena_l2(i32 noundef) local_unnamed_addr #0

declare i32 @arena_l1(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @heap_setSpans(i64 noundef %0, i64 noundef %1, ptr noundef %2) local_unnamed_addr #1 {
  %4 = lshr i64 %0, 13
  %5 = tail call i32 @arenaIndex(i64 noundef %0) #2
  %6 = tail call i32 @arena_l1(i32 noundef %5) #2
  %7 = zext i32 %6 to i64
  %8 = getelementptr inbounds [1 x %struct.array_t], ptr getelementptr inbounds (i8, ptr @heap_, i64 680), i64 0, i64 %7
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %8, i64 16
  %11 = load i64, ptr %10, align 8, !tbaa !12
  %12 = tail call i32 @arena_l2(i32 noundef %5) #2
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %13 = zext i32 %12 to i64
  %14 = mul i64 %11, %13
  %15 = getelementptr inbounds i8, ptr %9, i64 %14
  br label %.lr.ph

._crit_edge:                                      ; preds = %33, %3
  ret void

.lr.ph:                                           ; preds = %.lr.ph.preheader, %33
  %.024 = phi i64 [ %37, %33 ], [ 0, %.lr.ph.preheader ]
  %.02223 = phi ptr [ %.1, %33 ], [ %15, %.lr.ph.preheader ]
  %16 = add i64 %.024, %4
  %17 = and i64 %16, 8191
  %18 = icmp eq i64 %17, 0
  br i1 %18, label %19, label %33

19:                                               ; preds = %.lr.ph
  %20 = shl i64 %.024, 13
  %21 = add i64 %20, %0
  %22 = tail call i32 @arenaIndex(i64 noundef %21) #2
  %23 = tail call i32 @arena_l1(i32 noundef %22) #2
  %24 = zext i32 %23 to i64
  %25 = getelementptr inbounds [1 x %struct.array_t], ptr getelementptr inbounds (i8, ptr @heap_, i64 680), i64 0, i64 %24
  %26 = load ptr, ptr %25, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %25, i64 16
  %28 = load i64, ptr %27, align 8, !tbaa !12
  %29 = tail call i32 @arena_l2(i32 noundef %22) #2
  %30 = zext i32 %29 to i64
  %31 = mul i64 %28, %30
  %32 = getelementptr inbounds i8, ptr %26, i64 %31
  br label %33

33:                                               ; preds = %19, %.lr.ph
  %.1 = phi ptr [ %32, %19 ], [ %.02223, %.lr.ph ]
  %34 = load ptr, ptr %.1, align 8, !tbaa !13
  %35 = getelementptr inbounds i8, ptr %34, i64 2097152
  %36 = getelementptr inbounds [8192 x ptr], ptr %35, i64 0, i64 %17
  store ptr %2, ptr %36, align 8, !tbaa !13
  %37 = add nuw i64 %.024, 1
  %exitcond.not = icmp eq i64 %37, %1
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14
}

attributes #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 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8, !11, i64 16, !10, i64 24}
!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, !11, i64 16}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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