; 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 }

; 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

; 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_finalize(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  store i64 0, ptr %3, align 8, !tbaa !9
  br label %5

5:                                                ; preds = %45, %1
  %6 = load i64, ptr %3, align 8, !tbaa !9
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.threadpool_context, ptr %7, i32 0, i32 0
  %9 = load i64, ptr %8, align 8, !tbaa !11
  %10 = icmp ult i64 %6, %9
  br i1 %10, label %12, label %11

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

12:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.threadpool_context, ptr %13, i32 0, i32 1
  %15 = load ptr, ptr %14, align 8, !tbaa !13
  %16 = load i64, ptr %3, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %15, i64 %16
  store ptr %17, ptr %4, align 8, !tbaa !5
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %18, i32 0, i32 6
  %20 = call i32 @pthread_mutex_lock(ptr noundef %19) #4
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %21, i32 0, i32 9
  store i64 1, ptr %22, align 8, !tbaa !14
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %23, i32 0, i32 7
  %25 = call i32 @pthread_cond_signal(ptr noundef %24) #4
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %26, i32 0, i32 6
  %28 = call i32 @pthread_mutex_unlock(ptr noundef %27) #4
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %29, i32 0, i32 1
  %31 = load i64, ptr %30, align 8, !tbaa !16
  %32 = call i32 @pthread_join(i64 noundef %31, ptr noundef null)
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %33, i32 0, i32 5
  %35 = load ptr, ptr %34, align 8, !tbaa !17
  call void @x_free(ptr noundef %35)
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %36, i32 0, i32 6
  %38 = call i32 @pthread_mutex_destroy(ptr noundef %37) #4
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %39, i32 0, i32 7
  %41 = call i32 @pthread_cond_destroy(ptr noundef %40) #4
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %42, i32 0, i32 8
  %44 = call i32 @pthread_cond_destroy(ptr noundef %43) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %45

45:                                               ; preds = %12
  %46 = load i64, ptr %3, align 8, !tbaa !9
  %47 = add i64 %46, 1
  store i64 %47, ptr %3, align 8, !tbaa !9
  br label %5, !llvm.loop !18

48:                                               ; preds = %11
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.threadpool_context, ptr %49, i32 0, i32 1
  %51 = load ptr, ptr %50, align 8, !tbaa !13
  call void @x_free(ptr noundef %51)
  %52 = load ptr, ptr %2, align 8, !tbaa !5
  call void @x_free(ptr noundef %52)
  ret void
}

declare i32 @pthread_join(i64 noundef, ptr noundef) #3

declare void @x_free(ptr noundef) #3

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

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

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 = { 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 = { "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, !10, i64 0}
!12 = !{!"threadpool_context", !10, i64 0, !6, i64 8, !7, i64 16}
!13 = !{!12, !6, i64 8}
!14 = !{!15, !10, i64 184}
!15 = !{!"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}
!16 = !{!15, !10, i64 8}
!17 = !{!15, !6, i64 40}
!18 = distinct !{!18, !19}
!19 = !{!"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"

%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 }

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

; 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_finalize(ptr noundef %0) local_unnamed_addr #1 {
  %2 = load i64, ptr %0, align 8, !tbaa !5
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  br label %6

._crit_edge:                                      ; preds = %6, %1
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load ptr, ptr %4, align 8, !tbaa !11
  tail call void @x_free(ptr noundef %5) #3
  tail call void @x_free(ptr noundef nonnull %0) #3
  ret void

6:                                                ; preds = %.lr.ph, %6
  %.015 = phi i64 [ 0, %.lr.ph ], [ %24, %6 ]
  %7 = load ptr, ptr %3, align 8, !tbaa !11
  %8 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %7, i64 %.015
  %9 = getelementptr inbounds i8, ptr %8, i64 48
  %10 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull %9) #3
  %11 = getelementptr inbounds i8, ptr %8, i64 184
  store i64 1, ptr %11, align 8, !tbaa !12
  %12 = getelementptr inbounds i8, ptr %8, i64 88
  %13 = tail call i32 @pthread_cond_signal(ptr noundef nonnull %12) #3
  %14 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %9) #3
  %15 = getelementptr inbounds i8, ptr %8, i64 8
  %16 = load i64, ptr %15, align 8, !tbaa !14
  %17 = tail call i32 @pthread_join(i64 noundef %16, ptr noundef null) #3
  %18 = getelementptr inbounds i8, ptr %8, i64 40
  %19 = load ptr, ptr %18, align 8, !tbaa !15
  tail call void @x_free(ptr noundef %19) #3
  %20 = tail call i32 @pthread_mutex_destroy(ptr noundef nonnull %9) #3
  %21 = tail call i32 @pthread_cond_destroy(ptr noundef nonnull %12) #3
  %22 = getelementptr inbounds i8, ptr %8, i64 136
  %23 = tail call i32 @pthread_cond_destroy(ptr noundef nonnull %22) #3
  %24 = add nuw i64 %.015, 1
  %25 = load i64, ptr %0, align 8, !tbaa !5
  %26 = icmp ult i64 %24, %25
  br i1 %26, label %6, label %._crit_edge, !llvm.loop !16
}

declare i32 @pthread_join(i64 noundef, ptr noundef) local_unnamed_addr #2

declare void @x_free(ptr noundef) local_unnamed_addr #2

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

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

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 = { 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 = { "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 0}
!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 = !{!6, !10, i64 8}
!12 = !{!13, !7, i64 184}
!13 = !{!"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}
!14 = !{!13, !7, i64 8}
!15 = !{!13, !10, i64 40}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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