; 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
declare i32 @pthread_mutex_lock(ptr noundef) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local void @threadpool_fence(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds %struct.threadpool, ptr %3, i32 0, i32 0
  %5 = call i32 @pthread_mutex_lock(ptr noundef %4) #3
  br label %6

6:                                                ; preds = %39, %1
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.threadpool, ptr %7, i32 0, i32 5
  %9 = load i8, ptr %8, align 8, !tbaa !9, !range !14, !noundef !15
  %10 = trunc i8 %9 to i1
  br i1 %10, label %22, label %11

11:                                               ; preds = %6
  %12 = load ptr, ptr %2, 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 !16
  %16 = icmp ugt i32 %15, 0
  br i1 %16, label %32, label %17

17:                                               ; preds = %11
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.threadpool, ptr %18, i32 0, i32 3
  %20 = load i32, ptr %19, align 8, !tbaa !17
  %21 = icmp sgt i32 %20, 0
  br i1 %21, label %32, label %22

22:                                               ; preds = %17, %6
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.threadpool, ptr %23, i32 0, i32 5
  %25 = load i8, ptr %24, align 8, !tbaa !9, !range !14, !noundef !15
  %26 = trunc i8 %25 to i1
  br i1 %26, label %27, label %38

27:                                               ; preds = %22
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.threadpool, ptr %28, i32 0, i32 4
  %30 = load i32, ptr %29, align 4, !tbaa !18
  %31 = icmp sgt i32 %30, 0
  br i1 %31, label %32, label %38

32:                                               ; preds = %27, %17, %11
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.threadpool, ptr %33, i32 0, i32 2
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.threadpool, ptr %35, i32 0, i32 0
  %37 = call i32 @pthread_cond_wait(ptr noundef %34, ptr noundef %36)
  br label %39

38:                                               ; preds = %27, %22
  br label %40

39:                                               ; preds = %32
  br label %6

40:                                               ; preds = %38
  %41 = load ptr, ptr %2, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.threadpool, ptr %41, i32 0, i32 0
  %43 = call i32 @pthread_mutex_unlock(ptr noundef %42) #3
  ret void
}

attributes #0 = { 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 #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 }

!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, !12, i64 144}
!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 = !{i8 0, i8 2}
!15 = !{}
!16 = !{!10, !11, i64 152}
!17 = !{!10, !11, i64 136}
!18 = !{!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"

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @threadpool_fence(ptr noundef %0) local_unnamed_addr #2 {
  %2 = tail call i32 @pthread_mutex_lock(ptr noundef %0) #3
  %3 = getelementptr inbounds i8, ptr %0, i64 136
  %4 = getelementptr inbounds i8, ptr %0, i64 140
  %5 = getelementptr inbounds i8, ptr %0, i64 144
  %6 = getelementptr inbounds i8, ptr %0, i64 152
  %7 = getelementptr inbounds i8, ptr %0, i64 88
  br label %8

8:                                                ; preds = %19, %1
  %9 = load i8, ptr %5, align 8, !tbaa !5, !range !13, !noundef !14
  %10 = trunc nuw i8 %9 to i1
  br i1 %10, label %16, label %11

11:                                               ; preds = %8
  %12 = load i32, ptr %6, align 8, !tbaa !15
  %.not = icmp eq i32 %12, 0
  br i1 %.not, label %13, label %19

13:                                               ; preds = %11
  %14 = load i32, ptr %3, align 8, !tbaa !16
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %19, label %21

16:                                               ; preds = %8
  %17 = load i32, ptr %4, align 4, !tbaa !17
  %18 = icmp sgt i32 %17, 0
  br i1 %18, label %19, label %21

19:                                               ; preds = %16, %13, %11
  %20 = tail call i32 @pthread_cond_wait(ptr noundef nonnull %7, ptr noundef nonnull %0) #3
  br label %8

21:                                               ; preds = %13, %16
  %22 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %0) #3
  ret void
}

attributes #0 = { 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 #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 }

!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, !10, i64 144}
!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 = !{i8 0, i8 2}
!14 = !{}
!15 = !{!6, !9, i64 152}
!16 = !{!6, !9, i64 136}
!17 = !{!6, !9, i64 140}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopSimplifyPass,SROAPass,SimplifyCFGPass,TailCallElimPass