; LLVM IR from pupnp/pupnp :: upnp/src/threadutil (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.THREADPOOLATTR = type { i32, i32, i64, i32, i32, i32, i32, i32 }
%struct.THREADPOOL = type { %union.pthread_mutex_t, %union.pthread_cond_t, %union.pthread_cond_t, i32, i32, i32, i32, i32, i32, %struct.FREELIST, %struct.LINKEDLIST, %struct.LINKEDLIST, %struct.LINKEDLIST, ptr, %struct.THREADPOOLATTR, %struct.TPOOLSTATS }
%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.FREELIST = type { ptr, i64, i32, i32 }
%struct.LINKEDLIST = type { %struct.LISTNODE, %struct.LISTNODE, i64, %struct.FREELIST, ptr, ptr }
%struct.LISTNODE = type { ptr, ptr, ptr }
%struct.TPOOLSTATS = type { double, i32, double, double, i32, double, double, i32, double, double, double, i32, i32, i32, i32, i32, i32, i32, i32 }

; 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @TPAttrInit(ptr noundef) #3

; Function Attrs: nounwind uwtable
declare hidden i32 @SetPolicyType(i32 noundef) #3

; Function Attrs: nounwind uwtable
declare hidden i32 @CreateWorker(ptr noundef) #3

; Function Attrs: nounwind uwtable
declare dso_local i32 @ThreadPoolShutdown(ptr noundef) #3

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

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

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

12:                                               ; preds = %2
  store i32 22, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %79

13:                                               ; preds = %2
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.THREADPOOL, ptr %14, i32 0, i32 0
  %16 = call i32 @pthread_mutex_lock(ptr noundef %15) #4
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %21

19:                                               ; preds = %13
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %7, ptr align 8 %20, i64 40, i1 false), !tbaa.struct !11
  br label %23

21:                                               ; preds = %13
  %22 = call i32 @TPAttrInit(ptr noundef %7)
  br label %23

23:                                               ; preds = %21, %19
  %24 = getelementptr inbounds %struct.THREADPOOLATTR, ptr %7, i32 0, i32 7
  %25 = load i32, ptr %24, align 8, !tbaa !14
  %26 = call i32 @SetPolicyType(i32 noundef %25)
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %32

28:                                               ; preds = %23
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.THREADPOOL, ptr %29, i32 0, i32 0
  %31 = call i32 @pthread_mutex_unlock(ptr noundef %30) #4
  store i32 536870912, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %79

32:                                               ; preds = %23
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.THREADPOOL, ptr %33, i32 0, i32 14
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %34, ptr align 8 %7, i64 40, i1 false), !tbaa.struct !11
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.THREADPOOL, ptr %35, i32 0, i32 5
  %37 = load i32, ptr %36, align 8, !tbaa !16
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.THREADPOOL, ptr %38, i32 0, i32 14
  %40 = getelementptr inbounds %struct.THREADPOOLATTR, ptr %39, i32 0, i32 0
  %41 = load i32, ptr %40, align 8, !tbaa !23
  %42 = icmp slt i32 %37, %41
  br i1 %42, label %43, label %65

43:                                               ; preds = %32
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.THREADPOOL, ptr %44, i32 0, i32 5
  %46 = load i32, ptr %45, align 8, !tbaa !16
  store i32 %46, ptr %8, align 4, !tbaa !9
  br label %47

47:                                               ; preds = %61, %43
  %48 = load i32, ptr %8, align 4, !tbaa !9
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.THREADPOOL, ptr %49, i32 0, i32 14
  %51 = getelementptr inbounds %struct.THREADPOOLATTR, ptr %50, i32 0, i32 0
  %52 = load i32, ptr %51, align 8, !tbaa !23
  %53 = icmp slt i32 %48, %52
  br i1 %53, label %54, label %64

54:                                               ; preds = %47
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = call i32 @CreateWorker(ptr noundef %55)
  store i32 %56, ptr %6, align 4, !tbaa !9
  %57 = load i32, ptr %6, align 4, !tbaa !9
  %58 = icmp ne i32 %57, 0
  br i1 %58, label %59, label %60

59:                                               ; preds = %54
  br label %64

60:                                               ; preds = %54
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %8, align 4, !tbaa !9
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %8, align 4, !tbaa !9
  br label %47, !llvm.loop !24

64:                                               ; preds = %59, %47
  br label %65

65:                                               ; preds = %64, %32
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.THREADPOOL, ptr %66, i32 0, i32 1
  %68 = call i32 @pthread_cond_signal(ptr noundef %67) #4
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.THREADPOOL, ptr %69, i32 0, i32 0
  %71 = call i32 @pthread_mutex_unlock(ptr noundef %70) #4
  %72 = load i32, ptr %6, align 4, !tbaa !9
  %73 = icmp ne i32 %72, 0
  br i1 %73, label %74, label %77

74:                                               ; preds = %65
  %75 = load ptr, ptr %4, align 8, !tbaa !5
  %76 = call i32 @ThreadPoolShutdown(ptr noundef %75)
  br label %77

77:                                               ; preds = %74, %65
  %78 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %78, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %79

79:                                               ; preds = %77, %28, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 40, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  %80 = load i32, ptr %3, align 4
  ret i32 %80
}

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!11 = !{i64 0, i64 4, !9, i64 4, i64 4, !9, i64 8, i64 8, !12, i64 16, i64 4, !9, i64 20, i64 4, !9, i64 24, i64 4, !9, i64 28, i64 4, !9, i64 32, i64 4, !9}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !10, i64 32}
!15 = !{!"THREADPOOLATTR", !10, i64 0, !10, i64 4, !13, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32}
!16 = !{!17, !10, i64 144}
!17 = !{!"THREADPOOL", !7, i64 0, !7, i64 40, !7, i64 88, !10, i64 136, !10, i64 140, !10, i64 144, !10, i64 148, !10, i64 152, !10, i64 156, !18, i64 160, !19, i64 184, !19, i64 280, !19, i64 376, !6, i64 472, !15, i64 480, !21, i64 520}
!18 = !{!"FREELIST", !6, i64 0, !13, i64 8, !10, i64 16, !10, i64 20}
!19 = !{!"LINKEDLIST", !20, i64 0, !20, i64 24, !13, i64 48, !18, i64 56, !6, i64 80, !6, i64 88}
!20 = !{!"LISTNODE", !6, i64 0, !6, i64 8, !6, i64 16}
!21 = !{!"TPOOLSTATS", !22, i64 0, !10, i64 8, !22, i64 16, !22, i64 24, !10, i64 32, !22, i64 40, !22, i64 48, !10, i64 56, !22, i64 64, !22, i64 72, !22, i64 80, !10, i64 88, !10, i64 92, !10, i64 96, !10, i64 100, !10, i64 104, !10, i64 108, !10, i64 112, !10, i64 116}
!22 = !{!"double", !7, i64 0}
!23 = !{!17, !10, i64 480}
!24 = distinct !{!24, !25}
!25 = !{!"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.THREADPOOLATTR = type { i32, i32, i64, i32, i32, i32, i32, i32 }

; Function Attrs: mustprogress 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) local_unnamed_addr #1

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @TPAttrInit(ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare hidden i32 @SetPolicyType(i32 noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare hidden i32 @CreateWorker(ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare dso_local i32 @ThreadPoolShutdown(ptr noundef) local_unnamed_addr #3

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @ThreadPoolSetAttr(ptr noundef %0, ptr noundef readonly %1) local_unnamed_addr #3 {
  %3 = alloca %struct.THREADPOOLATTR, align 8
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %3) #4
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %31, label %4

4:                                                ; preds = %2
  %5 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull %0) #4
  %.not26 = icmp eq ptr %1, null
  br i1 %.not26, label %7, label %6

6:                                                ; preds = %4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(40) %3, ptr noundef nonnull align 8 dereferenceable(40) %1, i64 40, i1 false), !tbaa.struct !5
  br label %9

7:                                                ; preds = %4
  %8 = call i32 @TPAttrInit(ptr noundef nonnull %3)
  br label %9

9:                                                ; preds = %7, %6
  %10 = getelementptr inbounds i8, ptr %3, i64 32
  %11 = load i32, ptr %10, align 8, !tbaa !12
  %12 = call i32 @SetPolicyType(i32 noundef %11)
  %.not27 = icmp eq i32 %12, 0
  br i1 %.not27, label %15, label %13

13:                                               ; preds = %9
  %14 = call i32 @pthread_mutex_unlock(ptr noundef nonnull %0) #4
  br label %31

15:                                               ; preds = %9
  %16 = getelementptr inbounds i8, ptr %0, i64 480
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(40) %16, ptr noundef nonnull align 8 dereferenceable(40) %3, i64 40, i1 false), !tbaa.struct !5
  %17 = getelementptr inbounds i8, ptr %0, i64 144
  %18 = load i32, ptr %17, align 8, !tbaa !14
  %19 = load i32, ptr %16, align 8, !tbaa !22
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %.lr.ph, label %.loopexit

21:                                               ; preds = %.lr.ph
  %22 = add nsw i32 %.030, 1
  %23 = load i32, ptr %16, align 8, !tbaa !22
  %24 = icmp slt i32 %22, %23
  br i1 %24, label %.lr.ph, label %.loopexit, !llvm.loop !23

.lr.ph:                                           ; preds = %15, %21
  %.030 = phi i32 [ %22, %21 ], [ %18, %15 ]
  %25 = call i32 @CreateWorker(ptr noundef nonnull %0)
  %.not28 = icmp eq i32 %25, 0
  br i1 %.not28, label %21, label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %21, %15
  %.1 = phi i32 [ 0, %15 ], [ %25, %.lr.ph ], [ 0, %21 ]
  %26 = getelementptr inbounds i8, ptr %0, i64 40
  %27 = call i32 @pthread_cond_signal(ptr noundef nonnull %26) #4
  %28 = call i32 @pthread_mutex_unlock(ptr noundef nonnull %0) #4
  %.not29 = icmp eq i32 %.1, 0
  br i1 %.not29, label %31, label %29

29:                                               ; preds = %.loopexit
  %30 = call i32 @ThreadPoolShutdown(ptr noundef nonnull %0)
  br label %31

31:                                               ; preds = %.loopexit, %29, %2, %13
  %.020 = phi i32 [ 536870912, %13 ], [ 22, %2 ], [ %.1, %29 ], [ 0, %.loopexit ]
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %3) #4
  ret i32 %.020
}

attributes #0 = { mustprogress 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{i64 0, i64 4, !6, i64 4, i64 4, !6, i64 8, i64 8, !10, i64 16, i64 4, !6, i64 20, i64 4, !6, i64 24, i64 4, !6, i64 28, i64 4, !6, i64 32, i64 4, !6}
!6 = !{!7, !7, i64 0}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!13, !7, i64 32}
!13 = !{!"THREADPOOLATTR", !7, i64 0, !7, i64 4, !11, i64 8, !7, i64 16, !7, i64 20, !7, i64 24, !7, i64 28, !7, i64 32}
!14 = !{!15, !7, i64 144}
!15 = !{!"THREADPOOL", !8, i64 0, !8, i64 40, !8, i64 88, !7, i64 136, !7, i64 140, !7, i64 144, !7, i64 148, !7, i64 152, !7, i64 156, !16, i64 160, !18, i64 184, !18, i64 280, !18, i64 376, !17, i64 472, !13, i64 480, !20, i64 520}
!16 = !{!"FREELIST", !17, i64 0, !11, i64 8, !7, i64 16, !7, i64 20}
!17 = !{!"any pointer", !8, i64 0}
!18 = !{!"LINKEDLIST", !19, i64 0, !19, i64 24, !11, i64 48, !16, i64 56, !17, i64 80, !17, i64 88}
!19 = !{!"LISTNODE", !17, i64 0, !17, i64 8, !17, i64 16}
!20 = !{!"TPOOLSTATS", !21, i64 0, !7, i64 8, !21, i64 16, !21, i64 24, !7, i64 32, !21, i64 40, !21, i64 48, !7, i64 56, !21, i64 64, !21, i64 72, !21, i64 80, !7, i64 88, !7, i64 92, !7, i64 96, !7, i64 100, !7, i64 104, !7, i64 108, !7, i64 112, !7, i64 116}
!21 = !{!"double", !8, i64 0}
!22 = !{!15, !7, i64 480}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass