; 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._work = type { i64, i64, i8, i32, %struct.workerSpans, i32 }
%struct.workerSpans = type { %union.pthread_mutex_t, %struct.mspanlist, %struct.mspanlist }
%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.mspanlist = type { ptr, ptr }
%struct._gcWork = type { ptr, ptr, i64, i64, i8, i32, i32, [16 x i64] }

@gcphase = external global i32, align 4
@work = external global %struct._work, align 8
@worldsema = external dso_local global %union.pthread_mutex_t, align 8
@allm = external global [10 x ptr], align 16
@ncpu = external global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @gcStart() #0 {
  %1 = alloca %struct._gcWork, align 8
  br label %2

2:                                                ; preds = %10, %0
  %3 = load i32, ptr @gcphase, align 4, !tbaa !5
  %4 = icmp eq i32 %3, 1
  br i1 %4, label %5, label %8

5:                                                ; preds = %2
  %6 = call i64 @sweepone()
  %7 = icmp ne i64 %6, -1
  br label %8

8:                                                ; preds = %5, %2
  %9 = phi i1 [ false, %2 ], [ %7, %5 ]
  br i1 %9, label %10, label %11

10:                                               ; preds = %8
  br label %2, !llvm.loop !9

11:                                               ; preds = %8
  %12 = load i32, ptr getelementptr inbounds (%struct._work, ptr @work, i32 0, i32 5), align 8, !tbaa !11
  %13 = icmp ugt i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %11
  call void @sighandler(i32 noundef 0)
  br label %26

15:                                               ; preds = %11
  %16 = call i32 @pthread_mutex_lock(ptr noundef @worldsema) #4
  %17 = call i32 @atomic_Xadd(ptr noundef getelementptr inbounds (%struct._work, ptr @work, i32 0, i32 5), i32 noundef 1)
  %18 = load i32, ptr @gcphase, align 4, !tbaa !5
  %19 = icmp ne i32 %18, 1
  br i1 %19, label %20, label %23

20:                                               ; preds = %15
  %21 = call i32 @pthread_mutex_unlock(ptr noundef @worldsema) #4
  %22 = call i32 @atomic_Xadd(ptr noundef getelementptr inbounds (%struct._work, ptr @work, i32 0, i32 5), i32 noundef -1)
  br label %26

23:                                               ; preds = %15
  store i32 2, ptr @gcphase, align 4, !tbaa !5
  store i8 1, ptr getelementptr inbounds (%struct._work, ptr @work, i32 0, i32 2), align 8, !tbaa !17
  %24 = load i32, ptr @ncpu, align 4, !tbaa !5
  call void @stopTheWorld(ptr noundef @allm, i32 noundef %24)
  call void @llvm.lifetime.start.p0(i64 176, ptr %1) #4
  call void @gcwork_init(ptr noundef %1)
  call void @gcDrain(ptr noundef %1)
  call void @gcMarkDone()
  call void (...) @startTheWorld()
  store i32 0, ptr getelementptr inbounds (%struct._work, ptr @work, i32 0, i32 5), align 8, !tbaa !11
  %25 = call i32 @pthread_mutex_unlock(ptr noundef @worldsema) #4
  call void @llvm.lifetime.end.p0(i64 176, ptr %1) #4
  br label %26

26:                                               ; preds = %23, %20, %14
  ret void
}

declare void @sighandler(i32 noundef) #1

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) #2

declare i32 @atomic_Xadd(ptr noundef, i32 noundef) #1

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) #2

declare void @stopTheWorld(ptr noundef, i32 noundef) #1

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

declare void @gcwork_init(ptr noundef) #1

declare void @gcDrain(ptr noundef) #1

declare void @startTheWorld(...) #1

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

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @sweepone() #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 "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !6, i64 96}
!12 = !{!"", !13, i64 0, !13, i64 8, !7, i64 16, !6, i64 20, !14, i64 24, !6, i64 96}
!13 = !{!"long", !7, i64 0}
!14 = !{!"", !7, i64 0, !15, i64 40, !15, i64 56}
!15 = !{!"mspanlist", !16, i64 0, !16, i64 8}
!16 = !{!"any pointer", !7, i64 0}
!17 = !{!12, !7, 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._work = type { i64, i64, i8, i32, %struct.workerSpans, i32 }
%struct.workerSpans = type { %union.pthread_mutex_t, %struct.mspanlist, %struct.mspanlist }
%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.mspanlist = type { ptr, ptr }
%struct._gcWork = type { ptr, ptr, i64, i64, i8, i32, i32, [16 x i64] }

@gcphase = external local_unnamed_addr global i32, align 4
@work = external global %struct._work, align 8
@worldsema = external dso_local global %union.pthread_mutex_t, align 8
@allm = external global [10 x ptr], align 16
@ncpu = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @gcStart() local_unnamed_addr #0 {
  %1 = alloca %struct._gcWork, align 8
  %.old = load i32, ptr @gcphase, align 4, !tbaa !5
  %.old1 = icmp eq i32 %.old, 1
  br i1 %.old1, label %.preheader, label %.critedge

.preheader:                                       ; preds = %0, %.preheader
  %2 = tail call i64 @sweepone()
  %3 = icmp ne i64 %2, -1
  %4 = load i32, ptr @gcphase, align 4
  %5 = icmp eq i32 %4, 1
  %or.cond = select i1 %3, i1 %5, i1 false
  br i1 %or.cond, label %.preheader, label %.critedge, !llvm.loop !9

.critedge:                                        ; preds = %.preheader, %0
  %6 = load i32, ptr getelementptr inbounds (i8, ptr @work, i64 96), align 8, !tbaa !11
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %8, label %7

7:                                                ; preds = %.critedge
  tail call void @sighandler(i32 noundef 0) #4
  br label %18

8:                                                ; preds = %.critedge
  %9 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull @worldsema) #4
  %10 = tail call i32 @atomic_Xadd(ptr noundef nonnull getelementptr inbounds (i8, ptr @work, i64 96), i32 noundef 1) #4
  %11 = load i32, ptr @gcphase, align 4, !tbaa !5
  %.not2 = icmp eq i32 %11, 1
  br i1 %.not2, label %15, label %12

12:                                               ; preds = %8
  %13 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull @worldsema) #4
  %14 = tail call i32 @atomic_Xadd(ptr noundef nonnull getelementptr inbounds (i8, ptr @work, i64 96), i32 noundef -1) #4
  br label %18

15:                                               ; preds = %8
  store i32 2, ptr @gcphase, align 4, !tbaa !5
  store i8 1, ptr getelementptr inbounds (i8, ptr @work, i64 16), align 8, !tbaa !17
  %16 = load i32, ptr @ncpu, align 4, !tbaa !5
  tail call void @stopTheWorld(ptr noundef nonnull @allm, i32 noundef %16) #4
  call void @llvm.lifetime.start.p0(i64 176, ptr nonnull %1) #4
  call void @gcwork_init(ptr noundef nonnull %1) #4
  call void @gcDrain(ptr noundef nonnull %1) #4
  call void @gcMarkDone()
  call void (...) @startTheWorld() #4
  store i32 0, ptr getelementptr inbounds (i8, ptr @work, i64 96), align 8, !tbaa !11
  %17 = call i32 @pthread_mutex_unlock(ptr noundef nonnull @worldsema) #4
  call void @llvm.lifetime.end.p0(i64 176, ptr nonnull %1) #4
  br label %18

18:                                               ; preds = %15, %12, %7
  ret void
}

declare void @sighandler(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) local_unnamed_addr #2

declare i32 @atomic_Xadd(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) local_unnamed_addr #2

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

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

declare void @gcwork_init(ptr noundef) local_unnamed_addr #1

declare void @gcDrain(ptr noundef) local_unnamed_addr #1

declare void @startTheWorld(...) local_unnamed_addr #1

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

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @sweepone() 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 "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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !6, i64 96}
!12 = !{!"", !13, i64 0, !13, i64 8, !7, i64 16, !6, i64 20, !14, i64 24, !6, i64 96}
!13 = !{!"long", !7, i64 0}
!14 = !{!"", !7, i64 0, !15, i64 40, !15, i64 56}
!15 = !{!"mspanlist", !16, i64 0, !16, i64 8}
!16 = !{!"any pointer", !7, i64 0}
!17 = !{!12, !7, i64 16}


; Effective O3 passes: GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopSimplifyPass,LoopUnrollPass,SimplifyCFGPass,TailCallElimPass