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

; 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.mspan = type { ptr, ptr, ptr, i64, i64, i64, i8, i8, i8, i16, i32, i64, i8, i16, i8, i8, i16, i64, i64, i64, ptr, ptr, i64 }
%struct.mcache = type { i64, i64, i64, i64, [134 x ptr], [67 x i64], i32 }

@heap_ = external global %struct.mheap, align 8
@emptyspan = external dso_local global %struct.mspan, 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 void @atomic_Store(ptr noundef, i32 noundef) #1

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

5:                                                ; preds = %29, %1
  %6 = load i32, ptr %3, align 4, !tbaa !9
  %7 = icmp slt i32 %6, 134
  br i1 %7, label %9, label %8

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

9:                                                ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.mcache, ptr %10, i32 0, i32 4
  %12 = load i32, ptr %3, align 4, !tbaa !9
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [134 x ptr], ptr %11, i64 0, i64 %13
  %15 = load ptr, ptr %14, align 8, !tbaa !5
  store ptr %15, ptr %4, align 8, !tbaa !5
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = icmp ne ptr %16, @emptyspan
  br i1 %17, label %18, label %28

18:                                               ; preds = %9
  %19 = load i32, ptr %3, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [134 x %struct.mcentral], ptr getelementptr inbounds (%struct.mheap, ptr @heap_, i32 0, i32 23), i64 0, i64 %20
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  call void @central_uncacheSpan(ptr noundef %21, ptr noundef %22)
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.mcache, ptr %23, i32 0, i32 4
  %25 = load i32, ptr %3, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [134 x ptr], ptr %24, i64 0, i64 %26
  store ptr @emptyspan, ptr %27, align 8, !tbaa !5
  br label %28

28:                                               ; preds = %18, %9
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %29

29:                                               ; preds = %28
  %30 = load i32, ptr %3, align 4, !tbaa !9
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr %3, align 4, !tbaa !9
  br label %5, !llvm.loop !11

32:                                               ; preds = %8
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.mcache, ptr %33, i32 0, i32 6
  %35 = load i32, ptr getelementptr inbounds (%struct.mheap, ptr @heap_, i32 0, i32 16), align 8, !tbaa !13
  call void @atomic_Store(ptr noundef %34, i32 noundef %35)
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.mcache, ptr %36, i32 0, i32 1
  store i64 0, ptr %37, align 8, !tbaa !20
  %38 = load ptr, ptr %2, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.mcache, ptr %38, i32 0, i32 2
  store i64 0, ptr %39, align 8, !tbaa !22
  ret void
}

declare void @central_uncacheSpan(ptr noundef, ptr noundef) #1

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 = !{!"any pointer", !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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !10, i64 720}
!14 = !{!"mheap", !7, i64 0, !15, i64 40, !15, i64 48, !16, i64 56, !7, i64 88, !18, i64 232, !18, i64 296, !18, i64 360, !18, i64 424, !18, i64 488, !18, i64 552, !19, i64 616, !16, i64 640, !6, i64 672, !7, i64 680, !17, i64 712, !10, i64 720, !10, i64 724, !10, i64 728, !6, i64 736, !17, i64 744, !17, i64 752, !17, i64 760, !7, i64 768}
!15 = !{!"mtreap", !6, i64 0}
!16 = !{!"", !6, i64 0, !10, i64 8, !17, i64 16, !10, i64 24}
!17 = !{!"long", !7, i64 0}
!18 = !{!"mfixalloc", !17, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !17, i64 32, !10, i64 40, !17, i64 48, !7, i64 56}
!19 = !{!"", !17, i64 0, !17, i64 8, !17, i64 16}
!20 = !{!21, !17, i64 8}
!21 = !{!"mcache", !17, i64 0, !17, i64 8, !17, i64 16, !17, i64 24, !7, i64 32, !7, i64 1104, !10, i64 1640}
!22 = !{!21, !17, i64 16}


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

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

declare void @atomic_Store(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @cache_releaseAll(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 32
  br label %7

3:                                                ; preds = %12
  %4 = getelementptr inbounds i8, ptr %0, i64 1640
  %5 = load i32, ptr getelementptr inbounds (i8, ptr @heap_, i64 720), align 8, !tbaa !5
  tail call void @atomic_Store(ptr noundef nonnull %4, i32 noundef %5) #3
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %6, i8 0, i64 16, i1 false)
  ret void

7:                                                ; preds = %1, %12
  %indvars.iv = phi i64 [ 0, %1 ], [ %indvars.iv.next, %12 ]
  %8 = getelementptr inbounds [134 x ptr], ptr %2, i64 0, i64 %indvars.iv
  %9 = load ptr, ptr %8, align 8, !tbaa !16
  %.not = icmp eq ptr %9, @emptyspan
  br i1 %.not, label %12, label %10

10:                                               ; preds = %7
  %11 = getelementptr inbounds [134 x %struct.mcentral], ptr getelementptr inbounds (i8, ptr @heap_, i64 768), i64 0, i64 %indvars.iv
  tail call void @central_uncacheSpan(ptr noundef nonnull %11, ptr noundef %9) #3
  store ptr @emptyspan, ptr %8, align 8, !tbaa !16
  br label %12

12:                                               ; preds = %10, %7
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 134
  br i1 %exitcond.not, label %3, label %7, !llvm.loop !17
}

declare void @central_uncacheSpan(ptr noundef, ptr noundef) local_unnamed_addr #0

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

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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, !12, i64 720}
!6 = !{!"mheap", !7, i64 0, !9, i64 40, !9, i64 48, !11, i64 56, !7, i64 88, !14, i64 232, !14, i64 296, !14, i64 360, !14, i64 424, !14, i64 488, !14, i64 552, !15, i64 616, !11, i64 640, !10, i64 672, !7, i64 680, !13, i64 712, !12, i64 720, !12, i64 724, !12, i64 728, !10, i64 736, !13, i64 744, !13, i64 752, !13, i64 760, !7, i64 768}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"mtreap", !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!"", !10, i64 0, !12, i64 8, !13, i64 16, !12, i64 24}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!"mfixalloc", !13, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !13, i64 32, !12, i64 40, !13, i64 48, !7, i64 56}
!15 = !{!"", !13, i64 0, !13, i64 8, !13, i64 16}
!16 = !{!10, !10, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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