; LLVM IR from ponylang/ponyc :: src/libponyrt/sched (scheduler)

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

@scheduler_count = external hidden global i32, align 4
@active_scheduler_count = external hidden global i32, align 4
@scheduler_count_changing = external hidden global i8, align 1

; 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 @ponyint_sched_maybe_wakeup(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i8, align 1
  %5 = alloca i8, align 1
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  %8 = alloca i8, align 1
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %9 = call i32 @get_active_scheduler_count()
  store i32 %9, ptr %3, align 4, !tbaa !5
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = load i32, ptr @scheduler_count, align 4, !tbaa !5
  %12 = icmp ult i32 %10, %11
  br i1 %12, label %13, label %36

13:                                               ; preds = %1
  %14 = load atomic i8, ptr @scheduler_count_changing monotonic, align 1
  store i8 %14, ptr %4, align 1
  %15 = load i8, ptr %4, align 1, !tbaa !9, !range !11, !noundef !12
  %16 = trunc i8 %15 to i1
  br i1 %16, label %36, label %17

17:                                               ; preds = %13
  store i8 1, ptr %5, align 1, !tbaa !9
  %18 = load i8, ptr %5, align 1
  %19 = atomicrmw xchg ptr @scheduler_count_changing, i8 %18 acquire, align 1
  store i8 %19, ptr %6, align 1
  %20 = load i8, ptr %6, align 1, !tbaa !9, !range !11, !noundef !12
  %21 = trunc i8 %20 to i1
  br i1 %21, label %36, label %22

22:                                               ; preds = %17
  %23 = call i32 @get_active_scheduler_count()
  store i32 %23, ptr %3, align 4, !tbaa !5
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = load i32, ptr @scheduler_count, align 4, !tbaa !5
  %26 = icmp ult i32 %24, %25
  br i1 %26, label %27, label %32

27:                                               ; preds = %22
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = add i32 %28, 1
  store i32 %29, ptr %3, align 4, !tbaa !5
  %30 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %30, ptr %7, align 4, !tbaa !5
  %31 = load i32, ptr %7, align 4
  store atomic i32 %31, ptr @active_scheduler_count monotonic, align 4
  br label %32

32:                                               ; preds = %27, %22
  store i8 0, ptr %8, align 1, !tbaa !9
  %33 = load i8, ptr %8, align 1
  store atomic i8 %33, ptr @scheduler_count_changing release, align 1
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = load i32, ptr %2, align 4, !tbaa !5
  call void @signal_suspended_threads(i32 noundef %34, i32 noundef %35)
  br label %36

36:                                               ; preds = %32, %17, %13, %1
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @signal_suspended_threads(i32 noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i32 @get_active_scheduler_count() #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 = { 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 = !{!"_Bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}


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

@scheduler_count = external hidden local_unnamed_addr global i32, align 4
@active_scheduler_count = external hidden local_unnamed_addr global i32, align 4
@scheduler_count_changing = external hidden global i8, align 1

; Function Attrs: nounwind uwtable
define dso_local void @ponyint_sched_maybe_wakeup(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call i32 @get_active_scheduler_count()
  %3 = load i32, ptr @scheduler_count, align 4, !tbaa !5
  %4 = icmp ult i32 %2, %3
  br i1 %4, label %5, label %18

5:                                                ; preds = %1
  %6 = load atomic i8, ptr @scheduler_count_changing monotonic, align 1
  %7 = trunc i8 %6 to i1
  br i1 %7, label %18, label %8

8:                                                ; preds = %5
  %9 = atomicrmw xchg ptr @scheduler_count_changing, i8 1 acquire, align 1
  %10 = trunc i8 %9 to i1
  br i1 %10, label %18, label %11

11:                                               ; preds = %8
  %12 = tail call i32 @get_active_scheduler_count()
  %13 = load i32, ptr @scheduler_count, align 4, !tbaa !5
  %14 = icmp ult i32 %12, %13
  br i1 %14, label %15, label %17

15:                                               ; preds = %11
  %16 = add nuw i32 %12, 1
  store atomic i32 %16, ptr @active_scheduler_count monotonic, align 4
  br label %17

17:                                               ; preds = %15, %11
  %.0 = phi i32 [ %16, %15 ], [ %12, %11 ]
  store atomic i8 0, ptr @scheduler_count_changing release, align 1
  tail call void @signal_suspended_threads(i32 noundef %.0, i32 noundef %0)
  br label %18

18:                                               ; preds = %17, %8, %5, %1
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @signal_suspended_threads(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden i32 @get_active_scheduler_count() 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" }

!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"}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,InferFunctionAttrsPass,SROAPass,TailCallElimPass