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

; 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.systematic_testing_thread_t = type { i64, i32, i8 }

@waiting_to_start_count = external hidden global i32, align 4
@total_threads = external hidden global i32, align 4
@threads_to_track = external hidden global ptr, align 8
@active_thread = external hidden global ptr, align 8
@stopped_threads = external hidden 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: inlinehint nounwind willreturn memory(none) uwtable
declare i32 @pthread_equal(i64 noundef, i64 noundef) #1

declare void @ponyint_thread_suspend(i32 noundef) #2

declare i32 @ponyint_thread_wake(i64 noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @ponyint_systematic_testing_yield() #3 {
  %1 = alloca i64, align 8
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = load i32, ptr @stopped_threads, align 4, !tbaa !5
  %7 = load i32, ptr @total_threads, align 4, !tbaa !5
  %8 = icmp eq i32 %6, %7
  br i1 %8, label %9, label %16

9:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #4
  %10 = load i32, ptr @total_threads, align 4, !tbaa !5
  %11 = zext i32 %10 to i64
  %12 = mul i64 %11, 16
  store i64 %12, ptr %1, align 8, !tbaa !9
  %13 = load i64, ptr %1, align 8, !tbaa !9
  %14 = load ptr, ptr @threads_to_track, align 8, !tbaa !11
  call void @ponyint_pool_free_size(i64 noundef %13, ptr noundef %14)
  store ptr null, ptr @active_thread, align 8, !tbaa !11
  store ptr null, ptr @threads_to_track, align 8, !tbaa !11
  store i32 0, ptr @total_threads, align 4, !tbaa !5
  store i32 0, ptr @stopped_threads, align 4, !tbaa !5
  store i32 0, ptr %2, align 4, !tbaa !5
  %15 = load i32, ptr %2, align 4
  store atomic i32 %15, ptr @waiting_to_start_count monotonic, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #4
  br label %51

16:                                               ; preds = %0
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  %17 = call i32 @get_next_index()
  store i32 %17, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %18 = load ptr, ptr @threads_to_track, align 8, !tbaa !11
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = zext i32 %19 to i64
  %21 = getelementptr inbounds %struct.systematic_testing_thread_t, ptr %18, i64 %20
  store ptr %21, ptr %4, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %22 = load ptr, ptr @active_thread, align 8, !tbaa !11
  store ptr %22, ptr %5, align 8, !tbaa !11
  %23 = load ptr, ptr @active_thread, align 8, !tbaa !11
  %24 = getelementptr inbounds %struct.systematic_testing_thread_t, ptr %23, i32 0, i32 0
  %25 = load i64, ptr %24, align 8, !tbaa !13
  %26 = load ptr, ptr %4, align 8, !tbaa !11
  %27 = getelementptr inbounds %struct.systematic_testing_thread_t, ptr %26, i32 0, i32 0
  %28 = load i64, ptr %27, align 8, !tbaa !13
  %29 = call i32 @pthread_equal(i64 noundef %25, i64 noundef %28) #5
  %30 = icmp eq i32 0, %29
  br i1 %30, label %31, label %50

31:                                               ; preds = %16
  %32 = load ptr, ptr %4, align 8, !tbaa !11
  store ptr %32, ptr @active_thread, align 8, !tbaa !11
  %33 = load ptr, ptr @active_thread, align 8, !tbaa !11
  %34 = getelementptr inbounds %struct.systematic_testing_thread_t, ptr %33, i32 0, i32 0
  %35 = load i64, ptr %34, align 8, !tbaa !13
  %36 = load ptr, ptr @active_thread, align 8, !tbaa !11
  %37 = getelementptr inbounds %struct.systematic_testing_thread_t, ptr %36, i32 0, i32 1
  %38 = load i32, ptr %37, align 8, !tbaa !16
  %39 = call i32 @ponyint_thread_wake(i64 noundef %35, i32 noundef %38)
  %40 = load ptr, ptr %5, align 8, !tbaa !11
  %41 = getelementptr inbounds %struct.systematic_testing_thread_t, ptr %40, i32 0, i32 2
  %42 = load i8, ptr %41, align 4, !tbaa !17, !range !18, !noundef !19
  %43 = trunc i8 %42 to i1
  br i1 %43, label %48, label %44

44:                                               ; preds = %31
  %45 = load ptr, ptr %5, align 8, !tbaa !11
  %46 = getelementptr inbounds %struct.systematic_testing_thread_t, ptr %45, i32 0, i32 1
  %47 = load i32, ptr %46, align 8, !tbaa !16
  call void @ponyint_thread_suspend(i32 noundef %47)
  br label %49

48:                                               ; preds = %31
  br label %49

49:                                               ; preds = %48, %44
  br label %50

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

51:                                               ; preds = %50, %9
  ret void
}

declare void @ponyint_pool_free_size(i64 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @get_next_index() #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint nounwind willreturn memory(none) 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 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 #4 = { nounwind }
attributes #5 = { nounwind willreturn memory(none) }

!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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"systematic_testing_thread_t", !10, i64 0, !6, i64 8, !15, i64 12}
!15 = !{!"_Bool", !7, i64 0}
!16 = !{!14, !6, i64 8}
!17 = !{!14, !15, i64 12}
!18 = !{i8 0, i8 2}
!19 = !{}


; 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.systematic_testing_thread_t = type { i64, i32, i8 }

@waiting_to_start_count = external hidden local_unnamed_addr global i32, align 4
@total_threads = external hidden local_unnamed_addr global i32, align 4
@threads_to_track = external hidden local_unnamed_addr global ptr, align 8
@active_thread = external hidden local_unnamed_addr global ptr, align 8
@stopped_threads = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: inlinehint mustprogress nofree nosync nounwind willreturn memory(none) uwtable
declare i32 @pthread_equal(i64 noundef, i64 noundef) local_unnamed_addr #0

declare void @ponyint_thread_suspend(i32 noundef) local_unnamed_addr #1

declare i32 @ponyint_thread_wake(i64 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @ponyint_systematic_testing_yield() local_unnamed_addr #2 {
  %1 = load i32, ptr @stopped_threads, align 4, !tbaa !5
  %2 = load i32, ptr @total_threads, align 4, !tbaa !5
  %3 = icmp eq i32 %1, %2
  br i1 %3, label %4, label %8

4:                                                ; preds = %0
  %5 = zext i32 %1 to i64
  %6 = shl nuw nsw i64 %5, 4
  %7 = load ptr, ptr @threads_to_track, align 8, !tbaa !9
  tail call void @ponyint_pool_free_size(i64 noundef %6, ptr noundef %7) #3
  store ptr null, ptr @active_thread, align 8, !tbaa !9
  store ptr null, ptr @threads_to_track, align 8, !tbaa !9
  store i32 0, ptr @total_threads, align 4, !tbaa !5
  store i32 0, ptr @stopped_threads, align 4, !tbaa !5
  store atomic i32 0, ptr @waiting_to_start_count monotonic, align 4
  br label %28

8:                                                ; preds = %0
  %9 = tail call i32 @get_next_index()
  %10 = load ptr, ptr @threads_to_track, align 8, !tbaa !9
  %11 = zext i32 %9 to i64
  %12 = getelementptr inbounds %struct.systematic_testing_thread_t, ptr %10, i64 %11
  %13 = load ptr, ptr @active_thread, align 8, !tbaa !9
  %14 = load i64, ptr %13, align 8, !tbaa !11
  %15 = load i64, ptr %12, align 8, !tbaa !11
  %16 = tail call i32 @pthread_equal(i64 noundef %14, i64 noundef %15) #4
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %28

18:                                               ; preds = %8
  store ptr %12, ptr @active_thread, align 8, !tbaa !9
  %19 = getelementptr inbounds i8, ptr %12, i64 8
  %20 = load i32, ptr %19, align 8, !tbaa !15
  %21 = tail call i32 @ponyint_thread_wake(i64 noundef %15, i32 noundef %20) #3
  %22 = getelementptr inbounds i8, ptr %13, i64 12
  %23 = load i8, ptr %22, align 4, !tbaa !16, !range !17, !noundef !18
  %24 = trunc nuw i8 %23 to i1
  br i1 %24, label %28, label %25

25:                                               ; preds = %18
  %26 = getelementptr inbounds i8, ptr %13, i64 8
  %27 = load i32, ptr %26, align 8, !tbaa !15
  tail call void @ponyint_thread_suspend(i32 noundef %27) #3
  br label %28

28:                                               ; preds = %8, %18, %25, %4
  ret void
}

declare void @ponyint_pool_free_size(i64 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i32 @get_next_index() local_unnamed_addr #2

attributes #0 = { inlinehint mustprogress nofree nosync nounwind willreturn memory(none) 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 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 }
attributes #4 = { nounwind willreturn memory(none) }

!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, !13, i64 0}
!12 = !{!"systematic_testing_thread_t", !13, i64 0, !6, i64 8, !14, i64 12}
!13 = !{!"long", !7, i64 0}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{!12, !6, i64 8}
!16 = !{!12, !14, i64 12}
!17 = !{i8 0, i8 2}
!18 = !{}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass