; 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.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.THREADPOOLATTR = type { i32, i32, i64, i32, i32, i32, i32, i32 }
%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: nounwind uwtable
declare hidden i32 @CreateWorker(ptr noundef) #2

; 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 i32 @ThreadPoolAddPersistent(ptr noundef %0, ptr noundef %1, ptr noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 -1, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  store ptr null, ptr %10, align 8, !tbaa !5
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %17

14:                                               ; preds = %3
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %18, label %17

17:                                               ; preds = %14, %3
  store i32 22, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %89

18:                                               ; preds = %14
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = icmp ne ptr %19, null
  br i1 %20, label %22, label %21

21:                                               ; preds = %18
  store ptr %9, ptr %7, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %21, %18
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 536870912, ptr %23, align 4, !tbaa !9
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.THREADPOOL, ptr %24, i32 0, i32 0
  %26 = call i32 @pthread_mutex_lock(ptr noundef %25) #4
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.THREADPOOL, ptr %27, i32 0, i32 5
  %29 = load i32, ptr %28, align 8, !tbaa !11
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.THREADPOOL, ptr %30, i32 0, i32 14
  %32 = getelementptr inbounds %struct.THREADPOOLATTR, ptr %31, i32 0, i32 1
  %33 = load i32, ptr %32, align 4, !tbaa !20
  %34 = icmp slt i32 %29, %33
  br i1 %34, label %35, label %38

35:                                               ; preds = %22
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = call i32 @CreateWorker(ptr noundef %36)
  br label %50

38:                                               ; preds = %22
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.THREADPOOL, ptr %39, i32 0, i32 5
  %41 = load i32, ptr %40, align 8, !tbaa !11
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.THREADPOOL, ptr %42, i32 0, i32 8
  %44 = load i32, ptr %43, align 4, !tbaa !21
  %45 = sub nsw i32 %41, %44
  %46 = sub nsw i32 %45, 1
  %47 = icmp eq i32 %46, 0
  br i1 %47, label %48, label %49

48:                                               ; preds = %38
  store i32 536870912, ptr %8, align 4, !tbaa !9
  br label %84

49:                                               ; preds = %38
  br label %50

50:                                               ; preds = %49, %35
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.THREADPOOL, ptr %52, i32 0, i32 3
  %54 = load i32, ptr %53, align 8, !tbaa !22
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = call ptr @CreateThreadPoolJob(ptr noundef %51, i32 noundef %54, ptr noundef %55)
  store ptr %56, ptr %10, align 8, !tbaa !5
  %57 = load ptr, ptr %10, align 8, !tbaa !5
  %58 = icmp ne ptr %57, null
  br i1 %58, label %60, label %59

59:                                               ; preds = %50
  store i32 536870912, ptr %8, align 4, !tbaa !9
  br label %84

60:                                               ; preds = %50
  %61 = load ptr, ptr %10, align 8, !tbaa !5
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.THREADPOOL, ptr %62, i32 0, i32 13
  store ptr %61, ptr %63, align 8, !tbaa !23
  %64 = load ptr, ptr %5, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.THREADPOOL, ptr %64, i32 0, i32 1
  %66 = call i32 @pthread_cond_signal(ptr noundef %65) #4
  br label %67

67:                                               ; preds = %72, %60
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.THREADPOOL, ptr %68, i32 0, i32 13
  %70 = load ptr, ptr %69, align 8, !tbaa !23
  %71 = icmp ne ptr %70, null
  br i1 %71, label %72, label %78

72:                                               ; preds = %67
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.THREADPOOL, ptr %73, i32 0, i32 2
  %75 = load ptr, ptr %5, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.THREADPOOL, ptr %75, i32 0, i32 0
  %77 = call i32 @pthread_cond_wait(ptr noundef %74, ptr noundef %76)
  br label %67, !llvm.loop !24

78:                                               ; preds = %67
  %79 = load ptr, ptr %5, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.THREADPOOL, ptr %79, i32 0, i32 3
  %81 = load i32, ptr %80, align 8, !tbaa !22
  %82 = add nsw i32 %81, 1
  store i32 %82, ptr %80, align 8, !tbaa !22
  %83 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 %81, ptr %83, align 4, !tbaa !9
  br label %84

84:                                               ; preds = %78, %59, %48
  %85 = load ptr, ptr %5, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.THREADPOOL, ptr %85, i32 0, i32 0
  %87 = call i32 @pthread_mutex_unlock(ptr noundef %86) #4
  %88 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %88, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %89

89:                                               ; preds = %84, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  %90 = load i32, ptr %4, align 4
  ret i32 %90
}

; Function Attrs: nounwind uwtable
declare hidden ptr @CreateThreadPoolJob(ptr noundef, i32 noundef, ptr noundef) #2

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

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

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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 144}
!12 = !{!"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, !13, i64 160, !15, i64 184, !15, i64 280, !15, i64 376, !6, i64 472, !17, i64 480, !18, i64 520}
!13 = !{!"FREELIST", !6, i64 0, !14, i64 8, !10, i64 16, !10, i64 20}
!14 = !{!"long", !7, i64 0}
!15 = !{!"LINKEDLIST", !16, i64 0, !16, i64 24, !14, i64 48, !13, i64 56, !6, i64 80, !6, i64 88}
!16 = !{!"LISTNODE", !6, i64 0, !6, i64 8, !6, i64 16}
!17 = !{!"THREADPOOLATTR", !10, i64 0, !10, i64 4, !14, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32}
!18 = !{!"TPOOLSTATS", !19, i64 0, !10, i64 8, !19, i64 16, !19, i64 24, !10, i64 32, !19, i64 40, !19, i64 48, !10, i64 56, !19, i64 64, !19, i64 72, !19, 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}
!19 = !{!"double", !7, i64 0}
!20 = !{!12, !10, i64 484}
!21 = !{!12, !10, i64 156}
!22 = !{!12, !10, i64 136}
!23 = !{!12, !6, i64 472}
!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"

; 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 uwtable
declare hidden i32 @CreateWorker(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 536870913) i32 @ThreadPoolAddPersistent(ptr noundef %0, ptr noundef %1, ptr noundef writeonly %2) local_unnamed_addr #1 {
  %4 = icmp ne ptr %0, null
  %5 = icmp ne ptr %1, null
  %or.cond = and i1 %4, %5
  br i1 %or.cond, label %6, label %35

6:                                                ; preds = %3
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %.cont32, label %.else34

.else34:                                          ; preds = %6
  store i32 536870912, ptr %2, align 4, !tbaa !5
  br label %.cont32

.cont32:                                          ; preds = %6, %.else34
  %7 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull %0) #3
  %8 = getelementptr inbounds i8, ptr %0, i64 144
  %9 = load i32, ptr %8, align 8, !tbaa !9
  %10 = getelementptr inbounds i8, ptr %0, i64 484
  %11 = load i32, ptr %10, align 4, !tbaa !19
  %12 = icmp slt i32 %9, %11
  br i1 %12, label %13, label %15

13:                                               ; preds = %.cont32
  %14 = tail call i32 @CreateWorker(ptr noundef nonnull %0)
  br label %19

15:                                               ; preds = %.cont32
  %16 = getelementptr inbounds i8, ptr %0, i64 156
  %17 = load i32, ptr %16, align 4, !tbaa !20
  %.neg = add i32 %17, 1
  %18 = icmp eq i32 %9, %.neg
  br i1 %18, label %.cont, label %19

19:                                               ; preds = %15, %13
  %20 = getelementptr inbounds i8, ptr %0, i64 136
  %21 = load i32, ptr %20, align 8, !tbaa !21
  %22 = tail call ptr @CreateThreadPoolJob(ptr noundef nonnull %1, i32 noundef %21, ptr noundef nonnull %0)
  %.not30 = icmp eq ptr %22, null
  br i1 %.not30, label %.cont, label %23

23:                                               ; preds = %19
  %24 = getelementptr inbounds i8, ptr %0, i64 472
  store ptr %22, ptr %24, align 8, !tbaa !22
  %25 = getelementptr inbounds i8, ptr %0, i64 40
  %26 = tail call i32 @pthread_cond_signal(ptr noundef nonnull %25) #3
  %27 = load ptr, ptr %24, align 8, !tbaa !22
  %.not3135 = icmp eq ptr %27, null
  br i1 %.not3135, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %23
  %28 = getelementptr inbounds i8, ptr %0, i64 88
  br label %29

29:                                               ; preds = %.lr.ph, %29
  %30 = tail call i32 @pthread_cond_wait(ptr noundef nonnull %28, ptr noundef nonnull %0) #3
  %31 = load ptr, ptr %24, align 8, !tbaa !22
  %.not31 = icmp eq ptr %31, null
  br i1 %.not31, label %._crit_edge, label %29, !llvm.loop !23

._crit_edge:                                      ; preds = %29, %23
  %32 = load i32, ptr %20, align 8, !tbaa !21
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %20, align 8, !tbaa !21
  br i1 %.not, label %.cont, label %.else

.else:                                            ; preds = %._crit_edge
  store i32 %32, ptr %2, align 4, !tbaa !5
  br label %.cont

.cont:                                            ; preds = %.else, %._crit_edge, %19, %15
  %.0 = phi i32 [ 536870912, %15 ], [ 536870912, %19 ], [ 0, %._crit_edge ], [ 0, %.else ]
  %34 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %0) #3
  br label %35

35:                                               ; preds = %3, %.cont
  %.025 = phi i32 [ %.0, %.cont ], [ 22, %3 ]
  ret i32 %.025
}

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

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

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

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


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