; 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_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 }
%struct.threadpool_context = type { i64, ptr, i64 }

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

8:                                                ; preds = %91, %1
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %9, i32 0, i32 6
  %11 = call i32 @pthread_mutex_lock(ptr noundef %10) #4
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %12, i32 0, i32 9
  %14 = load i64, ptr %13, align 8, !tbaa !9
  %15 = icmp eq i64 %14, 1
  br i1 %15, label %16, label %17

16:                                               ; preds = %8
  br label %92

17:                                               ; preds = %8
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %18, i32 0, i32 2
  %20 = load i64, ptr %19, align 8, !tbaa !12
  %21 = icmp eq i64 %20, 0
  br i1 %21, label %22, label %28

22:                                               ; preds = %17
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %23, i32 0, i32 7
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %25, i32 0, i32 6
  %27 = call i32 @pthread_cond_wait(ptr noundef %24, ptr noundef %26)
  br label %28

28:                                               ; preds = %22, %17
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %29, i32 0, i32 9
  %31 = load i64, ptr %30, align 8, !tbaa !9
  %32 = icmp eq i64 %31, 1
  br i1 %32, label %33, label %34

33:                                               ; preds = %28
  br label %92

34:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %35, i32 0, i32 5
  %37 = load ptr, ptr %36, align 8, !tbaa !13
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %38, i32 0, i32 3
  %40 = load i64, ptr %39, align 8, !tbaa !14
  %41 = getelementptr inbounds %struct.threadpool_task, ptr %37, i64 %40
  store ptr %41, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.threadpool_task, ptr %42, i32 0, i32 0
  %44 = load ptr, ptr %43, align 8, !tbaa !15
  store ptr %44, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.threadpool_task, ptr %45, i32 0, i32 1
  %47 = load ptr, ptr %46, align 8, !tbaa !17
  store ptr %47, ptr %6, align 8, !tbaa !5
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %48, i32 0, i32 6
  %50 = call i32 @pthread_mutex_unlock(ptr noundef %49) #4
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  call void %51(ptr noundef %52)
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %53, i32 0, i32 6
  %55 = call i32 @pthread_mutex_lock(ptr noundef %54) #4
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %56, i32 0, i32 3
  %58 = load i64, ptr %57, align 8, !tbaa !14
  %59 = add i64 %58, 1
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %60, i32 0, i32 4
  %62 = load i64, ptr %61, align 8, !tbaa !18
  %63 = urem i64 %59, %62
  %64 = load ptr, ptr %3, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %64, i32 0, i32 3
  store i64 %63, ptr %65, align 8, !tbaa !14
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %66, i32 0, i32 2
  %68 = load i64, ptr %67, align 8, !tbaa !12
  %69 = add i64 %68, -1
  store i64 %69, ptr %67, align 8, !tbaa !12
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %70, i32 0, i32 2
  %72 = load i64, ptr %71, align 8, !tbaa !12
  %73 = load ptr, ptr %3, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %73, i32 0, i32 4
  %75 = load i64, ptr %74, align 8, !tbaa !18
  %76 = icmp ult i64 %72, %75
  br i1 %76, label %77, label %81

77:                                               ; preds = %34
  %78 = load ptr, ptr %3, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %78, i32 0, i32 8
  %80 = call i32 @pthread_cond_signal(ptr noundef %79) #4
  br label %81

81:                                               ; preds = %77, %34
  %82 = load ptr, ptr %3, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %82, i32 0, i32 6
  %84 = call i32 @pthread_mutex_unlock(ptr noundef %83) #4
  %85 = load ptr, ptr %3, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %85, i32 0, i32 10
  %87 = load ptr, ptr %86, align 8, !tbaa !19
  %88 = getelementptr inbounds %struct.threadpool_context, ptr %87, i32 0, i32 2
  %89 = atomicrmw sub ptr %88, i64 1 seq_cst, align 8
  %90 = sub i64 %89, 1
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %91

91:                                               ; preds = %81
  br i1 true, label %8, label %92

92:                                               ; preds = %91, %33, %16
  %93 = load ptr, ptr %3, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %93, i32 0, i32 6
  %95 = call i32 @pthread_mutex_unlock(ptr noundef %94) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret ptr null
}

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

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

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

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

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

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

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


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

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @threadpool_worker(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 48
  %3 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull %2) #3
  %4 = getelementptr inbounds i8, ptr %0, i64 184
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = icmp eq i64 %5, 1
  br i1 %6, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %8 = getelementptr inbounds i8, ptr %0, i64 88
  %9 = getelementptr inbounds i8, ptr %0, i64 40
  %10 = getelementptr inbounds i8, ptr %0, i64 24
  %11 = getelementptr inbounds i8, ptr %0, i64 32
  %12 = getelementptr inbounds i8, ptr %0, i64 136
  %13 = getelementptr inbounds i8, ptr %0, i64 192
  br label %14

14:                                               ; preds = %.lr.ph, %37
  %15 = load i64, ptr %7, align 8, !tbaa !11
  %16 = icmp eq i64 %15, 0
  br i1 %16, label %17, label %.thread

17:                                               ; preds = %14
  %18 = tail call i32 @pthread_cond_wait(ptr noundef nonnull %8, ptr noundef nonnull %2) #3
  %.pre = load i64, ptr %4, align 8, !tbaa !5
  %19 = icmp eq i64 %.pre, 1
  br i1 %19, label %._crit_edge, label %.thread

.thread:                                          ; preds = %14, %17
  %20 = load ptr, ptr %9, align 8, !tbaa !12
  %21 = load i64, ptr %10, align 8, !tbaa !13
  %22 = getelementptr inbounds %struct.threadpool_task, ptr %20, i64 %21
  %23 = load ptr, ptr %22, align 8, !tbaa !14
  %24 = getelementptr inbounds i8, ptr %22, i64 8
  %25 = load ptr, ptr %24, align 8, !tbaa !16
  %26 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %2) #3
  tail call void %23(ptr noundef %25) #3
  %27 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull %2) #3
  %28 = load i64, ptr %10, align 8, !tbaa !13
  %29 = add i64 %28, 1
  %30 = load i64, ptr %11, align 8, !tbaa !17
  %31 = urem i64 %29, %30
  store i64 %31, ptr %10, align 8, !tbaa !13
  %32 = load i64, ptr %7, align 8, !tbaa !11
  %33 = add i64 %32, -1
  store i64 %33, ptr %7, align 8, !tbaa !11
  %34 = icmp ult i64 %33, %30
  br i1 %34, label %35, label %37

35:                                               ; preds = %.thread
  %36 = tail call i32 @pthread_cond_signal(ptr noundef nonnull %12) #3
  br label %37

37:                                               ; preds = %35, %.thread
  %38 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %2) #3
  %39 = load ptr, ptr %13, align 8, !tbaa !18
  %40 = getelementptr inbounds i8, ptr %39, i64 16
  %41 = atomicrmw sub ptr %40, i64 1 seq_cst, align 8
  %42 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull %2) #3
  %43 = load i64, ptr %4, align 8, !tbaa !5
  %44 = icmp eq i64 %43, 1
  br i1 %44, label %._crit_edge, label %14

._crit_edge:                                      ; preds = %37, %17, %1
  %45 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %2) #3
  ret ptr null
}

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

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

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

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

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 = { 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 }

!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, !7, i64 184}
!6 = !{!"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}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 16}
!12 = !{!6, !10, i64 40}
!13 = !{!6, !7, i64 24}
!14 = !{!15, !10, i64 0}
!15 = !{!"threadpool_task", !10, i64 0, !10, i64 8}
!16 = !{!15, !10, i64 8}
!17 = !{!6, !7, i64 32}
!18 = !{!6, !10, i64 192}


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