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

; 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.mm = type { i64, i32, ptr, ptr, ptr, %union.pthread_mutex_t, ptr, i32, ptr, [2 x i32] }
%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 }

@gcphase = external global i32, align 4
@allm = external global [10 x ptr], align 16
@ncpu = external global i32, align 4
@gcBlackenEnabled = external global i32, align 4

; 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 @gcMarkTermination() #1 {
  %1 = alloca i32, align 4
  call void @atomic_Store(ptr noundef @gcBlackenEnabled, i32 noundef 0)
  call void @atomic_Store(ptr noundef @gcphase, i32 noundef 3)
  call void @atomic_Store(ptr noundef @gcphase, i32 noundef 1)
  call void @gcSweep()
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %2

2:                                                ; preds = %21, %0
  %3 = load i32, ptr %1, align 4, !tbaa !5
  %4 = load i32, ptr @ncpu, align 4, !tbaa !5
  %5 = icmp slt i32 %3, %4
  br i1 %5, label %7, label %6

6:                                                ; preds = %2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  br label %24

7:                                                ; preds = %2
  %8 = load i32, ptr %1, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [10 x ptr], ptr @allm, i64 0, i64 %9
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  %12 = icmp eq ptr %11, null
  br i1 %12, label %13, label %14

13:                                               ; preds = %7
  br label %21

14:                                               ; preds = %7
  %15 = load i32, ptr %1, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [10 x ptr], ptr @allm, i64 0, i64 %16
  %18 = load ptr, ptr %17, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.mm, ptr %18, i32 0, i32 3
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  call void @cache_releaseAll(ptr noundef %20)
  br label %21

21:                                               ; preds = %14, %13
  %22 = load i32, ptr %1, align 4, !tbaa !5
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %1, align 4, !tbaa !5
  br label %2, !llvm.loop !14

24:                                               ; preds = %6
  ret void
}

declare void @atomic_Store(ptr noundef, i32 noundef) #2

declare void @cache_releaseAll(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @gcSweep() #1

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 = { "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 = !{!"int", !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, !10, i64 24}
!12 = !{!"mm", !13, i64 0, !6, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !7, i64 40, !10, i64 80, !6, i64 88, !10, i64 96, !7, i64 104}
!13 = !{!"long", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

@gcphase = external global i32, align 4
@allm = external local_unnamed_addr global [10 x ptr], align 16
@ncpu = external local_unnamed_addr global i32, align 4
@gcBlackenEnabled = external global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @gcMarkTermination() local_unnamed_addr #0 {
  tail call void @atomic_Store(ptr noundef nonnull @gcBlackenEnabled, i32 noundef 0) #2
  tail call void @atomic_Store(ptr noundef nonnull @gcphase, i32 noundef 3) #2
  tail call void @atomic_Store(ptr noundef nonnull @gcphase, i32 noundef 1) #2
  tail call void @gcSweep()
  %1 = load i32, ptr @ncpu, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %10, %0
  ret void

.lr.ph:                                           ; preds = %0, %10
  %3 = phi i32 [ %11, %10 ], [ %1, %0 ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %10 ], [ 0, %0 ]
  %4 = getelementptr inbounds [10 x ptr], ptr @allm, i64 0, i64 %indvars.iv
  %5 = load ptr, ptr %4, align 8, !tbaa !9
  %6 = icmp eq ptr %5, null
  br i1 %6, label %10, label %7

7:                                                ; preds = %.lr.ph
  %8 = getelementptr inbounds i8, ptr %5, i64 24
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  tail call void @cache_releaseAll(ptr noundef %9) #2
  %.pre = load i32, ptr @ncpu, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %.lr.ph, %7
  %11 = phi i32 [ %3, %.lr.ph ], [ %.pre, %7 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %12 = sext i32 %11 to i64
  %13 = icmp slt i64 %indvars.iv.next, %12
  br i1 %13, label %.lr.ph, label %._crit_edge, !llvm.loop !14
}

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

declare void @cache_releaseAll(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @gcSweep() local_unnamed_addr #0

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 = { "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 = !{!"int", !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, !10, i64 24}
!12 = !{!"mm", !13, i64 0, !6, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !7, i64 40, !10, i64 80, !6, i64 88, !10, i64 96, !7, i64 104}
!13 = !{!"long", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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