; 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.task = type { ptr, ptr }
%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: 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
declare dso_local zeroext i1 @varray_taskpop(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @threadpool_worker(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca %struct.task, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %5, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %4) #5
  call void @llvm.memset.p0.i64(ptr align 8 %4, i8 0, i64 16, i1 false)
  br label %6

6:                                                ; preds = %84, %1
  br label %7

7:                                                ; preds = %6
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.threadpool, ptr %8, i32 0, i32 0
  %10 = call i32 @pthread_mutex_lock(ptr noundef %9) #5
  br label %11

11:                                               ; preds = %25, %7
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.threadpool, ptr %12, i32 0, i32 6
  %14 = getelementptr inbounds %struct.varray_task, ptr %13, i32 0, i32 0
  %15 = load i32, ptr %14, align 8, !tbaa !9
  %16 = icmp eq i32 %15, 0
  br i1 %16, label %17, label %23

17:                                               ; preds = %11
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.threadpool, ptr %18, i32 0, i32 5
  %20 = load i8, ptr %19, align 8, !tbaa !14, !range !15, !noundef !16
  %21 = trunc i8 %20 to i1
  %22 = xor i1 %21, true
  br label %23

23:                                               ; preds = %17, %11
  %24 = phi i1 [ false, %11 ], [ %22, %17 ]
  br i1 %24, label %25, label %31

25:                                               ; preds = %23
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.threadpool, ptr %26, i32 0, i32 1
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.threadpool, ptr %28, i32 0, i32 0
  %30 = call i32 @pthread_cond_wait(ptr noundef %27, ptr noundef %29)
  br label %11, !llvm.loop !17

31:                                               ; preds = %23
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.threadpool, ptr %32, i32 0, i32 5
  %34 = load i8, ptr %33, align 8, !tbaa !14, !range !15, !noundef !16
  %35 = trunc i8 %34 to i1
  br i1 %35, label %36, label %37

36:                                               ; preds = %31
  br label %88

37:                                               ; preds = %31
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.threadpool, ptr %38, i32 0, i32 6
  %40 = call zeroext i1 @varray_taskpop(ptr noundef %39, ptr noundef %4)
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.threadpool, ptr %41, i32 0, i32 3
  %43 = load i32, ptr %42, align 8, !tbaa !19
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %42, align 8, !tbaa !19
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.threadpool, ptr %45, i32 0, i32 0
  %47 = call i32 @pthread_mutex_unlock(ptr noundef %46) #5
  %48 = getelementptr inbounds %struct.task, ptr %4, i32 0, i32 0
  %49 = load ptr, ptr %48, align 8, !tbaa !20
  %50 = icmp ne ptr %49, null
  br i1 %50, label %51, label %57

51:                                               ; preds = %37
  %52 = getelementptr inbounds %struct.task, ptr %4, i32 0, i32 0
  %53 = load ptr, ptr %52, align 8, !tbaa !20
  %54 = getelementptr inbounds %struct.task, ptr %4, i32 0, i32 1
  %55 = load ptr, ptr %54, align 8, !tbaa !22
  %56 = call zeroext i1 %53(ptr noundef %55)
  br label %57

57:                                               ; preds = %51, %37
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.threadpool, ptr %58, i32 0, i32 0
  %60 = call i32 @pthread_mutex_lock(ptr noundef %59) #5
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.threadpool, ptr %61, i32 0, i32 3
  %63 = load i32, ptr %62, align 8, !tbaa !19
  %64 = add nsw i32 %63, -1
  store i32 %64, ptr %62, align 8, !tbaa !19
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.threadpool, ptr %65, i32 0, i32 5
  %67 = load i8, ptr %66, align 8, !tbaa !14, !range !15, !noundef !16
  %68 = trunc i8 %67 to i1
  br i1 %68, label %84, label %69

69:                                               ; preds = %57
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.threadpool, ptr %70, i32 0, i32 3
  %72 = load i32, ptr %71, align 8, !tbaa !19
  %73 = icmp eq i32 %72, 0
  br i1 %73, label %74, label %84

74:                                               ; preds = %69
  %75 = load ptr, ptr %3, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.threadpool, ptr %75, i32 0, i32 6
  %77 = getelementptr inbounds %struct.varray_task, ptr %76, i32 0, i32 0
  %78 = load i32, ptr %77, align 8, !tbaa !9
  %79 = icmp eq i32 %78, 0
  br i1 %79, label %80, label %84

80:                                               ; preds = %74
  %81 = load ptr, ptr %3, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.threadpool, ptr %81, i32 0, i32 2
  %83 = call i32 @pthread_cond_signal(ptr noundef %82) #5
  br label %84

84:                                               ; preds = %80, %74, %69, %57
  %85 = load ptr, ptr %3, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.threadpool, ptr %85, i32 0, i32 0
  %87 = call i32 @pthread_mutex_unlock(ptr noundef %86) #5
  br label %6

88:                                               ; preds = %36
  %89 = load ptr, ptr %3, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.threadpool, ptr %89, i32 0, i32 4
  %91 = load i32, ptr %90, align 4, !tbaa !23
  %92 = add nsw i32 %91, -1
  store i32 %92, ptr %90, align 4, !tbaa !23
  %93 = load ptr, ptr %3, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.threadpool, ptr %93, i32 0, i32 2
  %95 = call i32 @pthread_cond_signal(ptr noundef %94) #5
  %96 = load ptr, ptr %3, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.threadpool, ptr %96, i32 0, i32 0
  %98 = call i32 @pthread_mutex_unlock(ptr noundef %97) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  ret ptr null
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

declare i32 @pthread_cond_wait(ptr noundef, ptr noundef) #4

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

; Function Attrs: nounwind
declare i32 @pthread_cond_signal(ptr noundef) #3

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 #4 = { "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 #5 = { 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, !11, i64 152}
!10 = !{!"", !7, i64 0, !7, i64 40, !7, i64 88, !11, i64 136, !11, i64 140, !12, i64 144, !13, i64 152, !13, i64 168}
!11 = !{!"int", !7, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!"", !11, i64 0, !11, i64 4, !6, i64 8}
!14 = !{!10, !12, i64 144}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!10, !11, i64 136}
!20 = !{!21, !6, i64 0}
!21 = !{!"", !6, i64 0, !6, i64 8}
!22 = !{!21, !6, i64 8}
!23 = !{!10, !11, i64 140}


; 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.task = type { ptr, ptr }

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

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @varray_taskpop(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @threadpool_worker(ptr noundef %0) local_unnamed_addr #1 {
  %2 = alloca %struct.task, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %2) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %2, i8 0, i64 16, i1 false)
  %3 = getelementptr inbounds i8, ptr %0, i64 152
  %4 = getelementptr inbounds i8, ptr %0, i64 144
  %5 = getelementptr inbounds i8, ptr %0, i64 40
  %6 = getelementptr inbounds i8, ptr %0, i64 136
  %7 = getelementptr inbounds i8, ptr %2, i64 8
  %8 = getelementptr inbounds i8, ptr %0, i64 88
  br label %9

9:                                                ; preds = %42, %1
  %10 = call i32 @pthread_mutex_lock(ptr noundef %0) #5
  %11 = load i32, ptr %3, align 8, !tbaa !5
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %9, %15
  %13 = load i8, ptr %4, align 8, !tbaa !13, !range !14, !noundef !15
  %14 = trunc nuw i8 %13 to i1
  br i1 %14, label %.critedge, label %15

15:                                               ; preds = %.lr.ph
  %16 = call i32 @pthread_cond_wait(ptr noundef nonnull %5, ptr noundef nonnull %0) #5
  %17 = load i32, ptr %3, align 8, !tbaa !5
  %18 = icmp eq i32 %17, 0
  br i1 %18, label %.lr.ph, label %.critedge, !llvm.loop !16

.critedge:                                        ; preds = %.lr.ph, %15, %9
  %19 = load i8, ptr %4, align 8, !tbaa !13, !range !14, !noundef !15
  %20 = trunc nuw i8 %19 to i1
  br i1 %20, label %44, label %21

21:                                               ; preds = %.critedge
  %22 = call zeroext i1 @varray_taskpop(ptr noundef nonnull %3, ptr noundef nonnull %2)
  %23 = load i32, ptr %6, align 8, !tbaa !18
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %6, align 8, !tbaa !18
  %25 = call i32 @pthread_mutex_unlock(ptr noundef nonnull %0) #5
  %26 = load ptr, ptr %2, align 8, !tbaa !19
  %.not = icmp eq ptr %26, null
  br i1 %.not, label %30, label %27

27:                                               ; preds = %21
  %28 = load ptr, ptr %7, align 8, !tbaa !21
  %29 = call zeroext i1 %26(ptr noundef %28) #5
  br label %30

30:                                               ; preds = %27, %21
  %31 = call i32 @pthread_mutex_lock(ptr noundef nonnull %0) #5
  %32 = load i32, ptr %6, align 8, !tbaa !18
  %33 = add nsw i32 %32, -1
  store i32 %33, ptr %6, align 8, !tbaa !18
  %34 = load i8, ptr %4, align 8, !tbaa !13, !range !14, !noundef !15
  %35 = trunc nuw i8 %34 to i1
  %36 = icmp ne i32 %33, 0
  %or.cond.not = select i1 %35, i1 true, i1 %36
  br i1 %or.cond.not, label %42, label %37

37:                                               ; preds = %30
  %38 = load i32, ptr %3, align 8, !tbaa !5
  %39 = icmp eq i32 %38, 0
  br i1 %39, label %40, label %42

40:                                               ; preds = %37
  %41 = call i32 @pthread_cond_signal(ptr noundef nonnull %8) #5
  br label %42

42:                                               ; preds = %40, %37, %30
  %43 = call i32 @pthread_mutex_unlock(ptr noundef nonnull %0) #5
  br label %9

44:                                               ; preds = %.critedge
  %45 = getelementptr inbounds i8, ptr %0, i64 140
  %46 = load i32, ptr %45, align 4, !tbaa !22
  %47 = add nsw i32 %46, -1
  store i32 %47, ptr %45, align 4, !tbaa !22
  %48 = call i32 @pthread_cond_signal(ptr noundef nonnull %8) #5
  %49 = call i32 @pthread_mutex_unlock(ptr noundef nonnull %0) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %2) #5
  ret ptr null
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

declare i32 @pthread_cond_wait(ptr noundef, ptr noundef) local_unnamed_addr #4

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

; Function Attrs: nounwind
declare i32 @pthread_cond_signal(ptr noundef) local_unnamed_addr #3

attributes #0 = { mustprogress 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 #4 = { "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 #5 = { 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 152}
!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 = !{i8 0, i8 2}
!15 = !{}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!6, !9, i64 136}
!19 = !{!20, !12, i64 0}
!20 = !{!"", !12, i64 0, !12, i64 8}
!21 = !{!20, !12, i64 8}
!22 = !{!6, !9, i64 140}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass