; LLVM IR from microsoft/LightAIMD :: src (thread_pool)

; 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_context = type { i64, ptr, i64 }
%struct.threadpool_worker_desc = type { i64, i64, i64, i64, i64, ptr, %union.pthread_mutex_t, %union.pthread_cond_t, %union.pthread_cond_t, i64, ptr }
%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.threadpool_task = type { ptr, ptr }

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

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

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

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

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

; 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 @threadpool_add_task(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) #3 {
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.threadpool_context, ptr %11, i32 0, i32 1
  %13 = load ptr, ptr %12, align 8, !tbaa !11
  %14 = load i64, ptr %6, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %13, i64 %14
  store ptr %15, ptr %9, align 8, !tbaa !5
  %16 = load ptr, ptr %9, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %16, i32 0, i32 6
  %18 = call i32 @pthread_mutex_lock(ptr noundef %17) #4
  %19 = load ptr, ptr %9, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %19, i32 0, i32 2
  %21 = load i64, ptr %20, align 8, !tbaa !13
  %22 = load ptr, ptr %9, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %22, i32 0, i32 4
  %24 = load i64, ptr %23, align 8, !tbaa !15
  %25 = icmp eq i64 %21, %24
  br i1 %25, label %26, label %32

26:                                               ; preds = %4
  %27 = load ptr, ptr %9, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %27, i32 0, i32 8
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %29, i32 0, i32 6
  %31 = call i32 @pthread_cond_wait(ptr noundef %28, ptr noundef %30)
  br label %32

32:                                               ; preds = %26, %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %33, i32 0, i32 3
  %35 = load i64, ptr %34, align 8, !tbaa !16
  %36 = load ptr, ptr %9, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %36, i32 0, i32 2
  %38 = load i64, ptr %37, align 8, !tbaa !13
  %39 = add i64 %35, %38
  %40 = load ptr, ptr %9, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %40, i32 0, i32 4
  %42 = load i64, ptr %41, align 8, !tbaa !15
  %43 = urem i64 %39, %42
  store i64 %43, ptr %10, align 8, !tbaa !9
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = load ptr, ptr %9, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %45, i32 0, i32 5
  %47 = load ptr, ptr %46, align 8, !tbaa !17
  %48 = load i64, ptr %10, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.threadpool_task, ptr %47, i64 %48
  %50 = getelementptr inbounds %struct.threadpool_task, ptr %49, i32 0, i32 0
  store ptr %44, ptr %50, align 8, !tbaa !18
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  %52 = load ptr, ptr %9, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %52, i32 0, i32 5
  %54 = load ptr, ptr %53, align 8, !tbaa !17
  %55 = load i64, ptr %10, align 8, !tbaa !9
  %56 = getelementptr inbounds %struct.threadpool_task, ptr %54, i64 %55
  %57 = getelementptr inbounds %struct.threadpool_task, ptr %56, i32 0, i32 1
  store ptr %51, ptr %57, align 8, !tbaa !20
  %58 = load ptr, ptr %9, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %58, i32 0, i32 2
  %60 = load i64, ptr %59, align 8, !tbaa !13
  %61 = add i64 %60, 1
  store i64 %61, ptr %59, align 8, !tbaa !13
  %62 = load ptr, ptr %9, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %62, i32 0, i32 7
  %64 = call i32 @pthread_cond_signal(ptr noundef %63) #4
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %65, i32 0, i32 6
  %67 = call i32 @pthread_mutex_unlock(ptr noundef %66) #4
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.threadpool_context, ptr %68, i32 0, i32 2
  %70 = atomicrmw add ptr %69, i64 1 seq_cst, align 8
  %71 = add i64 %70, 1
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  ret void
}

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

!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 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"threadpool_context", !10, i64 0, !6, i64 8, !7, i64 16}
!13 = !{!14, !10, i64 16}
!14 = !{!"threadpool_worker_desc", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !6, i64 40, !7, i64 48, !7, i64 88, !7, i64 136, !10, i64 184, !6, i64 192}
!15 = !{!14, !10, i64 32}
!16 = !{!14, !10, i64 24}
!17 = !{!14, !6, i64 40}
!18 = !{!19, !6, i64 0}
!19 = !{!"threadpool_task", !6, i64 0, !6, i64 8}
!20 = !{!19, !6, i64 8}


; 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.threadpool_worker_desc = type { i64, i64, i64, i64, i64, ptr, %union.pthread_mutex_t, %union.pthread_cond_t, %union.pthread_cond_t, i64, ptr }
%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.threadpool_task = type { ptr, ptr }

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

; Function Attrs: nounwind uwtable
define dso_local void @threadpool_add_task(ptr nocapture noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #2 {
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %6, i64 %1
  %8 = getelementptr inbounds i8, ptr %7, i64 48
  %9 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull %8) #3
  %10 = getelementptr inbounds i8, ptr %7, i64 16
  %11 = load i64, ptr %10, align 8, !tbaa !11
  %12 = getelementptr inbounds i8, ptr %7, i64 32
  %13 = load i64, ptr %12, align 8, !tbaa !13
  %14 = icmp eq i64 %11, %13
  br i1 %14, label %15, label %18

15:                                               ; preds = %4
  %16 = getelementptr inbounds i8, ptr %7, i64 136
  %17 = tail call i32 @pthread_cond_wait(ptr noundef nonnull %16, ptr noundef nonnull %8) #3
  %.pre = load i64, ptr %10, align 8, !tbaa !11
  %.pre19 = load i64, ptr %12, align 8, !tbaa !13
  br label %18

18:                                               ; preds = %15, %4
  %19 = phi i64 [ %.pre19, %15 ], [ %13, %4 ]
  %20 = phi i64 [ %.pre, %15 ], [ %11, %4 ]
  %21 = getelementptr inbounds i8, ptr %7, i64 24
  %22 = load i64, ptr %21, align 8, !tbaa !14
  %23 = add i64 %20, %22
  %24 = urem i64 %23, %19
  %25 = getelementptr inbounds i8, ptr %7, i64 40
  %26 = load ptr, ptr %25, align 8, !tbaa !15
  %27 = getelementptr inbounds %struct.threadpool_task, ptr %26, i64 %24
  store ptr %2, ptr %27, align 8, !tbaa !16
  %28 = getelementptr inbounds %struct.threadpool_task, ptr %26, i64 %24, i32 1
  store ptr %3, ptr %28, align 8, !tbaa !18
  %29 = add i64 %20, 1
  store i64 %29, ptr %10, align 8, !tbaa !11
  %30 = getelementptr inbounds i8, ptr %7, i64 88
  %31 = tail call i32 @pthread_cond_signal(ptr noundef nonnull %30) #3
  %32 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %8) #3
  %33 = getelementptr inbounds i8, ptr %0, i64 16
  %34 = atomicrmw add ptr %33, i64 1 seq_cst, align 8
  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 8}
!6 = !{!"threadpool_context", !7, i64 0, !10, i64 8, !8, i64 16}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!12, !7, i64 16}
!12 = !{!"threadpool_worker_desc", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !10, i64 40, !8, i64 48, !8, i64 88, !8, i64 136, !7, i64 184, !10, i64 192}
!13 = !{!12, !7, i64 32}
!14 = !{!12, !7, i64 24}
!15 = !{!12, !10, i64 40}
!16 = !{!17, !10, i64 0}
!17 = !{!"threadpool_task", !10, i64 0, !10, i64 8}
!18 = !{!17, !10, i64 8}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,TailCallElimPass