; LLVM IR from Morpho-lang/morpho :: src/support (threadpool)

; 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.threadpool = type { %union.pthread_mutex_t, %union.pthread_cond_t, %union.pthread_cond_t, i32, i32, i8, %struct.varray_task, %struct.varray_pthread_t }
%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 }
%union.pthread_cond_t = type { %struct.__pthread_cond_s }
%struct.__pthread_cond_s = type { %union.__atomic_wide_counter, %union.__atomic_wide_counter, [2 x i32], [2 x i32], i32, i32, [2 x i32] }
%union.__atomic_wide_counter = type { i64 }
%struct.varray_task = type { i32, i32, ptr }
%struct.varray_pthread_t = type { i32, i32, ptr }

; Function Attrs: nounwind uwtable
declare dso_local void @varray_pthread_tinit(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @varray_pthread_tadd(ptr noundef, ptr noundef, i32 noundef) #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @varray_taskinit(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @threadpool_worker(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @threadpool_init(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca i1, align 1
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %8 = load i32, ptr %5, align 4, !tbaa !9
  %9 = icmp slt i32 %8, 1
  br i1 %9, label %10, label %11

10:                                               ; preds = %2
  store i1 false, ptr %3, align 1
  br label %49

11:                                               ; preds = %2
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.threadpool, ptr %12, i32 0, i32 6
  call void @varray_taskinit(ptr noundef %13)
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.threadpool, ptr %14, i32 0, i32 7
  call void @varray_pthread_tinit(ptr noundef %15)
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.threadpool, ptr %16, i32 0, i32 0
  %18 = call i32 @pthread_mutex_init(ptr noundef %17, ptr noundef null) #3
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.threadpool, ptr %19, i32 0, i32 1
  %21 = call i32 @pthread_cond_init(ptr noundef %20, ptr noundef null) #3
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.threadpool, ptr %22, i32 0, i32 2
  %24 = call i32 @pthread_cond_init(ptr noundef %23, ptr noundef null) #3
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.threadpool, ptr %26, i32 0, i32 4
  store i32 %25, ptr %27, align 4, !tbaa !11
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.threadpool, ptr %28, i32 0, i32 5
  store i8 0, ptr %29, align 8, !tbaa !15
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.threadpool, ptr %30, i32 0, i32 3
  store i32 0, ptr %31, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %32

32:                                               ; preds = %45, %11
  %33 = load i32, ptr %6, align 4, !tbaa !9
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.threadpool, ptr %34, i32 0, i32 4
  %36 = load i32, ptr %35, align 4, !tbaa !11
  %37 = icmp slt i32 %33, %36
  br i1 %37, label %39, label %38

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

39:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = call i32 @pthread_create(ptr noundef %7, ptr noundef null, ptr noundef @threadpool_worker, ptr noundef %40) #3
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.threadpool, ptr %42, i32 0, i32 7
  %44 = call zeroext i1 @varray_pthread_tadd(ptr noundef %43, ptr noundef %7, i32 noundef 1)
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %45

45:                                               ; preds = %39
  %46 = load i32, ptr %6, align 4, !tbaa !9
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %6, align 4, !tbaa !9
  br label %32, !llvm.loop !17

48:                                               ; preds = %38
  store i1 true, ptr %3, align 1
  br label %49

49:                                               ; preds = %48, %10
  %50 = load i1, ptr %3, align 1
  ret i1 %50
}

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

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

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!12, !10, i64 140}
!12 = !{!"", !7, i64 0, !7, i64 40, !7, i64 88, !10, i64 136, !10, i64 140, !13, i64 144, !14, i64 152, !14, i64 168}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{!"", !10, i64 0, !10, i64 4, !6, i64 8}
!15 = !{!12, !13, i64 144}
!16 = !{!12, !10, i64 136}
!17 = distinct !{!17, !18}
!18 = !{!"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"

; Function Attrs: nounwind uwtable
declare dso_local void @varray_pthread_tinit(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @varray_pthread_tadd(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @varray_taskinit(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @threadpool_worker(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @threadpool_init(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = alloca i64, align 8
  %4 = icmp sgt i32 %1, 0
  br i1 %4, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %2
  %5 = getelementptr inbounds i8, ptr %0, i64 152
  tail call void @varray_taskinit(ptr noundef nonnull %5)
  %6 = getelementptr inbounds i8, ptr %0, i64 168
  tail call void @varray_pthread_tinit(ptr noundef nonnull %6)
  %7 = tail call i32 @pthread_mutex_init(ptr noundef %0, ptr noundef null) #3
  %8 = getelementptr inbounds i8, ptr %0, i64 40
  %9 = tail call i32 @pthread_cond_init(ptr noundef nonnull %8, ptr noundef null) #3
  %10 = getelementptr inbounds i8, ptr %0, i64 88
  %11 = tail call i32 @pthread_cond_init(ptr noundef nonnull %10, ptr noundef null) #3
  %12 = getelementptr inbounds i8, ptr %0, i64 140
  store i32 %1, ptr %12, align 4, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %0, i64 144
  store i8 0, ptr %13, align 8, !tbaa !13
  %14 = getelementptr inbounds i8, ptr %0, i64 136
  store i32 0, ptr %14, align 8, !tbaa !14
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.017 = phi i32 [ %17, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %15 = call i32 @pthread_create(ptr noundef nonnull %3, ptr noundef null, ptr noundef nonnull @threadpool_worker, ptr noundef nonnull %0) #3
  %16 = call zeroext i1 @varray_pthread_tadd(ptr noundef nonnull %6, ptr noundef nonnull %3, i32 noundef 1)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  %17 = add nuw nsw i32 %.017, 1
  %18 = load i32, ptr %12, align 4, !tbaa !5
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %.lr.ph, label %.loopexit, !llvm.loop !15

.loopexit:                                        ; preds = %.lr.ph, %2
  ret i1 %4
}

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

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

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

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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, !9, i64 140}
!6 = !{!"", !7, i64 0, !7, i64 40, !7, i64 88, !9, i64 136, !9, i64 140, !10, i64 144, !11, i64 152, !11, i64 168}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"_Bool", !7, i64 0}
!11 = !{!"", !9, i64 0, !9, i64 4, !12, i64 8}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!6, !10, i64 144}
!14 = !{!6, !9, i64 136}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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