; LLVM IR from Overload-Technologies/Overload :: Dependencies/soloud/src/core/soloud_thread.ll (ZN6SoLoud6Thread4Pool7getWorkEv)

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

%"class.SoLoud::Thread::Pool" = type <{ i32, [4 x i8], ptr, ptr, [1024 x ptr], i32, i32, i32, [4 x i8] }>

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6SoLoud6Thread9lockMutexEPv(ptr noundef) #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6SoLoud6Thread11unlockMutexEPv(ptr noundef) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_ZN6SoLoud6Thread4Pool7getWorkEv(ptr noundef nonnull align 8 dereferenceable(8228) %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %5 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store ptr null, ptr %3, align 8, !tbaa !5
  %6 = getelementptr inbounds %"class.SoLoud::Thread::Pool", ptr %5, i32 0, i32 3
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  %8 = icmp ne ptr %7, null
  br i1 %8, label %9, label %12

9:                                                ; preds = %1
  %10 = getelementptr inbounds %"class.SoLoud::Thread::Pool", ptr %5, i32 0, i32 3
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  call void @_ZN6SoLoud6Thread9lockMutexEPv(ptr noundef %11)
  br label %12

12:                                               ; preds = %9, %1
  %13 = getelementptr inbounds %"class.SoLoud::Thread::Pool", ptr %5, i32 0, i32 5
  %14 = load i32, ptr %13, align 8, !tbaa !12
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %16, label %44

16:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %17 = getelementptr inbounds %"class.SoLoud::Thread::Pool", ptr %5, i32 0, i32 6
  %18 = load i32, ptr %17, align 4, !tbaa !13
  %19 = getelementptr inbounds %"class.SoLoud::Thread::Pool", ptr %5, i32 0, i32 5
  %20 = load i32, ptr %19, align 8, !tbaa !12
  %21 = srem i32 %18, %20
  store i32 %21, ptr %4, align 4, !tbaa !14
  %22 = getelementptr inbounds %"class.SoLoud::Thread::Pool", ptr %5, i32 0, i32 6
  %23 = load i32, ptr %22, align 4, !tbaa !13
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %22, align 4, !tbaa !13
  %25 = getelementptr inbounds %"class.SoLoud::Thread::Pool", ptr %5, i32 0, i32 4
  %26 = load i32, ptr %4, align 4, !tbaa !14
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [1024 x ptr], ptr %25, i64 0, i64 %27
  %29 = load ptr, ptr %28, align 8, !tbaa !5
  store ptr %29, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %"class.SoLoud::Thread::Pool", ptr %5, i32 0, i32 4
  %31 = getelementptr inbounds %"class.SoLoud::Thread::Pool", ptr %5, i32 0, i32 5
  %32 = load i32, ptr %31, align 8, !tbaa !12
  %33 = sub nsw i32 %32, 1
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [1024 x ptr], ptr %30, i64 0, i64 %34
  %36 = load ptr, ptr %35, align 8, !tbaa !5
  %37 = getelementptr inbounds %"class.SoLoud::Thread::Pool", ptr %5, i32 0, i32 4
  %38 = load i32, ptr %4, align 4, !tbaa !14
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [1024 x ptr], ptr %37, i64 0, i64 %39
  store ptr %36, ptr %40, align 8, !tbaa !5
  %41 = getelementptr inbounds %"class.SoLoud::Thread::Pool", ptr %5, i32 0, i32 5
  %42 = load i32, ptr %41, align 8, !tbaa !12
  %43 = add nsw i32 %42, -1
  store i32 %43, ptr %41, align 8, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %44

44:                                               ; preds = %16, %12
  %45 = getelementptr inbounds %"class.SoLoud::Thread::Pool", ptr %5, i32 0, i32 3
  %46 = load ptr, ptr %45, align 8, !tbaa !9
  %47 = icmp ne ptr %46, null
  br i1 %47, label %48, label %51

48:                                               ; preds = %44
  %49 = getelementptr inbounds %"class.SoLoud::Thread::Pool", ptr %5, i32 0, i32 3
  %50 = load ptr, ptr %49, align 8, !tbaa !9
  call void @_ZN6SoLoud6Thread11unlockMutexEPv(ptr noundef %50)
  br label %51

51:                                               ; preds = %48, %44
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret ptr %52
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { 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++ TBAA"}
!9 = !{!10, !6, i64 16}
!10 = !{!"_ZTSN6SoLoud6Thread4PoolE", !11, i64 0, !6, i64 8, !6, i64 16, !7, i64 24, !11, i64 8216, !11, i64 8220, !11, i64 8224}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 8216}
!13 = !{!10, !11, i64 8220}
!14 = !{!11, !11, i64 0}


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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6SoLoud6Thread11unlockMutexEPv(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_ZN6SoLoud6Thread4Pool7getWorkEv(ptr nocapture noundef nonnull align 8 dereferenceable(8228) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 16
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %5, label %4

4:                                                ; preds = %1
  tail call void @_ZN6SoLoud6Thread9lockMutexEPv(ptr noundef nonnull %3)
  br label %5

5:                                                ; preds = %4, %1
  %6 = getelementptr inbounds i8, ptr %0, i64 8216
  %7 = load i32, ptr %6, align 8, !tbaa !11
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %9, label %22

9:                                                ; preds = %5
  %10 = getelementptr inbounds i8, ptr %0, i64 8220
  %11 = load i32, ptr %10, align 4, !tbaa !12
  %12 = srem i32 %11, %7
  %13 = add nsw i32 %11, 1
  store i32 %13, ptr %10, align 4, !tbaa !12
  %14 = getelementptr inbounds i8, ptr %0, i64 24
  %15 = sext i32 %12 to i64
  %16 = getelementptr inbounds [1024 x ptr], ptr %14, i64 0, i64 %15
  %17 = load ptr, ptr %16, align 8, !tbaa !13
  %18 = add nsw i32 %7, -1
  %19 = zext nneg i32 %18 to i64
  %20 = getelementptr inbounds [1024 x ptr], ptr %14, i64 0, i64 %19
  %21 = load ptr, ptr %20, align 8, !tbaa !13
  store ptr %21, ptr %16, align 8, !tbaa !13
  store i32 %18, ptr %6, align 8, !tbaa !11
  br label %22

22:                                               ; preds = %9, %5
  %.0 = phi ptr [ %17, %9 ], [ null, %5 ]
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  %.not6 = icmp eq ptr %23, null
  br i1 %.not6, label %25, label %24

24:                                               ; preds = %22
  tail call void @_ZN6SoLoud6Thread11unlockMutexEPv(ptr noundef nonnull %23)
  br label %25

25:                                               ; preds = %24, %22
  ret ptr %.0
}

attributes #0 = { mustprogress 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" }

!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 16}
!6 = !{!"_ZTSN6SoLoud6Thread4PoolE", !7, i64 0, !10, i64 8, !10, i64 16, !8, i64 24, !7, i64 8216, !7, i64 8220, !7, i64 8224}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 8216}
!12 = !{!6, !7, i64 8220}
!13 = !{!10, !10, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,TailCallElimPass