; 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: nounwind uwtable
declare dso_local ptr @threadpool_worker(ptr noundef) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @threadpool_initialize(i64 noundef %0, i64 noundef %1) #0 {
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %9 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %9, ptr %5, align 8, !tbaa !5
  %10 = load i64, ptr %3, align 8, !tbaa !5
  %11 = icmp eq i64 %10, 0
  br i1 %11, label %12, label %15

12:                                               ; preds = %2
  %13 = call i32 @get_nprocs() #4
  %14 = sext i32 %13 to i64
  store i64 %14, ptr %5, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %12, %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %16 = call ptr @x_malloc(i64 noundef 24)
  store ptr %16, ptr %6, align 8, !tbaa !9
  %17 = load i64, ptr %5, align 8, !tbaa !5
  %18 = mul i64 %17, 200
  %19 = call ptr @x_malloc(i64 noundef %18)
  %20 = load ptr, ptr %6, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.threadpool_context, ptr %20, i32 0, i32 1
  store ptr %19, ptr %21, align 8, !tbaa !11
  %22 = load i64, ptr %5, align 8, !tbaa !5
  %23 = load ptr, ptr %6, align 8, !tbaa !9
  %24 = getelementptr inbounds %struct.threadpool_context, ptr %23, i32 0, i32 0
  store i64 %22, ptr %24, align 8, !tbaa !13
  %25 = load ptr, ptr %6, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.threadpool_context, ptr %25, i32 0, i32 2
  store atomic i64 0, ptr %26 seq_cst, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store i64 0, ptr %7, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %71, %15
  %28 = load i64, ptr %7, align 8, !tbaa !5
  %29 = load i64, ptr %5, align 8, !tbaa !5
  %30 = icmp ult i64 %28, %29
  br i1 %30, label %32, label %31

31:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %74

32:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %33 = load ptr, ptr %6, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.threadpool_context, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !11
  %36 = load i64, ptr %7, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %35, i64 %36
  store ptr %37, ptr %8, align 8, !tbaa !9
  %38 = load i64, ptr %7, align 8, !tbaa !5
  %39 = load ptr, ptr %8, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %39, i32 0, i32 0
  store i64 %38, ptr %40, align 8, !tbaa !15
  %41 = load i64, ptr %4, align 8, !tbaa !5
  %42 = mul i64 %41, 16
  %43 = call ptr @x_malloc(i64 noundef %42)
  %44 = load ptr, ptr %8, align 8, !tbaa !9
  %45 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %44, i32 0, i32 5
  store ptr %43, ptr %45, align 8, !tbaa !17
  %46 = load ptr, ptr %8, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %46, i32 0, i32 2
  store i64 0, ptr %47, align 8, !tbaa !18
  %48 = load ptr, ptr %8, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %48, i32 0, i32 3
  store i64 0, ptr %49, align 8, !tbaa !19
  %50 = load i64, ptr %4, align 8, !tbaa !5
  %51 = load ptr, ptr %8, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %51, i32 0, i32 4
  store i64 %50, ptr %52, align 8, !tbaa !20
  %53 = load ptr, ptr %8, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %53, i32 0, i32 9
  store i64 0, ptr %54, align 8, !tbaa !21
  %55 = load ptr, ptr %6, align 8, !tbaa !9
  %56 = load ptr, ptr %8, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %56, i32 0, i32 10
  store ptr %55, ptr %57, align 8, !tbaa !22
  %58 = load ptr, ptr %8, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %58, i32 0, i32 6
  %60 = call i32 @pthread_mutex_init(ptr noundef %59, ptr noundef null) #4
  %61 = load ptr, ptr %8, align 8, !tbaa !9
  %62 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %61, i32 0, i32 7
  %63 = call i32 @pthread_cond_init(ptr noundef %62, ptr noundef null) #4
  %64 = load ptr, ptr %8, align 8, !tbaa !9
  %65 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %64, i32 0, i32 8
  %66 = call i32 @pthread_cond_init(ptr noundef %65, ptr noundef null) #4
  %67 = load ptr, ptr %8, align 8, !tbaa !9
  %68 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %67, i32 0, i32 1
  %69 = load ptr, ptr %8, align 8, !tbaa !9
  %70 = call i32 @pthread_create(ptr noundef %68, ptr noundef null, ptr noundef @threadpool_worker, ptr noundef %69) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %71

71:                                               ; preds = %32
  %72 = load i64, ptr %7, align 8, !tbaa !5
  %73 = add i64 %72, 1
  store i64 %73, ptr %7, align 8, !tbaa !5
  br label %27, !llvm.loop !23

74:                                               ; preds = %31
  %75 = load ptr, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret ptr %75
}

; Function Attrs: nounwind
declare i32 @get_nprocs() #2

declare ptr @x_malloc(i64 noundef) #3

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

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

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

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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"threadpool_context", !6, i64 0, !10, i64 8, !7, i64 16}
!13 = !{!12, !6, i64 0}
!14 = !{!12, !7, i64 16}
!15 = !{!16, !6, i64 0}
!16 = !{!"threadpool_worker_desc", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !10, i64 40, !7, i64 48, !7, i64 88, !7, i64 136, !6, i64 184, !10, i64 192}
!17 = !{!16, !10, i64 40}
!18 = !{!16, !6, i64 16}
!19 = !{!16, !6, i64 24}
!20 = !{!16, !6, i64 32}
!21 = !{!16, !6, i64 184}
!22 = !{!16, !10, i64 192}
!23 = distinct !{!23, !24}
!24 = !{!"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 uwtable
declare dso_local ptr @threadpool_worker(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @threadpool_initialize(i64 noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq i64 %0, 0
  br i1 %3, label %4, label %7

4:                                                ; preds = %2
  %5 = tail call i32 @get_nprocs() #4
  %6 = sext i32 %5 to i64
  br label %7

7:                                                ; preds = %4, %2
  %.0 = phi i64 [ %6, %4 ], [ %0, %2 ]
  %8 = tail call ptr @x_malloc(i64 noundef 24) #4
  %9 = mul i64 %.0, 200
  %10 = tail call ptr @x_malloc(i64 noundef %9) #4
  %11 = getelementptr inbounds i8, ptr %8, i64 8
  store ptr %10, ptr %11, align 8, !tbaa !5
  store i64 %.0, ptr %8, align 8, !tbaa !11
  %12 = getelementptr inbounds i8, ptr %8, i64 16
  store atomic i64 0, ptr %12 seq_cst, align 8, !tbaa !12
  %.not = icmp eq i64 %.0, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %7
  %13 = shl i64 %1, 4
  br label %14

._crit_edge:                                      ; preds = %14, %7
  ret ptr %8

14:                                               ; preds = %.lr.ph, %14
  %.02830 = phi i64 [ 0, %.lr.ph ], [ %31, %14 ]
  %15 = load ptr, ptr %11, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.threadpool_worker_desc, ptr %15, i64 %.02830
  store i64 %.02830, ptr %16, align 8, !tbaa !13
  %17 = tail call ptr @x_malloc(i64 noundef %13) #4
  %18 = getelementptr inbounds i8, ptr %16, i64 40
  store ptr %17, ptr %18, align 8, !tbaa !15
  %19 = getelementptr inbounds i8, ptr %16, i64 16
  %20 = getelementptr inbounds i8, ptr %16, i64 32
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %19, i8 0, i64 16, i1 false)
  store i64 %1, ptr %20, align 8, !tbaa !16
  %21 = getelementptr inbounds i8, ptr %16, i64 184
  store i64 0, ptr %21, align 8, !tbaa !17
  %22 = getelementptr inbounds i8, ptr %16, i64 192
  store ptr %8, ptr %22, align 8, !tbaa !18
  %23 = getelementptr inbounds i8, ptr %16, i64 48
  %24 = tail call i32 @pthread_mutex_init(ptr noundef nonnull %23, ptr noundef null) #4
  %25 = getelementptr inbounds i8, ptr %16, i64 88
  %26 = tail call i32 @pthread_cond_init(ptr noundef nonnull %25, ptr noundef null) #4
  %27 = getelementptr inbounds i8, ptr %16, i64 136
  %28 = tail call i32 @pthread_cond_init(ptr noundef nonnull %27, ptr noundef null) #4
  %29 = getelementptr inbounds i8, ptr %16, i64 8
  %30 = tail call i32 @pthread_create(ptr noundef nonnull %29, ptr noundef null, ptr noundef nonnull @threadpool_worker, ptr noundef nonnull %16) #4
  %31 = add nuw i64 %.02830, 1
  %exitcond.not = icmp eq i64 %31, %.0
  br i1 %exitcond.not, label %._crit_edge, label %14, !llvm.loop !19
}

; Function Attrs: nounwind
declare i32 @get_nprocs() local_unnamed_addr #1

declare ptr @x_malloc(i64 noundef) local_unnamed_addr #2

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

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

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

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

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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, !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 = !{!6, !7, i64 0}
!12 = !{!6, !8, i64 16}
!13 = !{!14, !7, i64 0}
!14 = !{!"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}
!15 = !{!14, !10, i64 40}
!16 = !{!14, !7, i64 32}
!17 = !{!14, !7, i64 184}
!18 = !{!14, !10, i64 192}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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