; LLVM IR from ctlst-tech/catpilot :: os/freertos/osa/FreeRTOS-Plus-POSIX/source (FreeRTOS_POSIX_pthread_cond)

; 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.pthread_cond_internal = type { i64, %struct.xSTATIC_QUEUE, i32 }
%struct.xSTATIC_QUEUE = type { [3 x ptr], %union.anon, [2 x %struct.xSTATIC_LIST], [3 x i64], [2 x i8], i8 }
%union.anon = type { ptr }
%struct.xSTATIC_LIST = type { i64, ptr, %struct.xSTATIC_MINI_LIST_ITEM }
%struct.xSTATIC_MINI_LIST_ITEM = type { i32, [2 x ptr] }

; Function Attrs: nounwind uwtable
define dso_local i32 @pthread_cond_broadcast(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 0, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %6, ptr %4, align 8, !tbaa !5
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  call void @prvInitializeStaticCond(ptr noundef %7)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.pthread_cond_internal, ptr %8, i32 0, i32 2
  %10 = load i32, ptr %9, align 8, !tbaa !11
  store i32 %10, ptr %5, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %33, %1
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = icmp ugt i32 %12, 0
  br i1 %13, label %14, label %37

14:                                               ; preds = %11
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.pthread_cond_internal, ptr %15, i32 0, i32 2
  %17 = load i32, ptr %5, align 4, !tbaa !9
  %18 = call i32 @Atomic_CompareAndSwap_u32(ptr noundef %16, i32 noundef 0, i32 noundef %17)
  %19 = icmp eq i32 1, %18
  br i1 %19, label %20, label %33

20:                                               ; preds = %14
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %21

21:                                               ; preds = %29, %20
  %22 = load i32, ptr %3, align 4, !tbaa !9
  %23 = load i32, ptr %5, align 4, !tbaa !9
  %24 = icmp ult i32 %22, %23
  br i1 %24, label %25, label %32

25:                                               ; preds = %21
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.pthread_cond_internal, ptr %26, i32 0, i32 1
  %28 = call i64 @xQueueGenericSend(ptr noundef %27, ptr noundef null, i32 noundef 0, i64 noundef 0)
  br label %29

29:                                               ; preds = %25
  %30 = load i32, ptr %3, align 4, !tbaa !9
  %31 = add i32 %30, 1
  store i32 %31, ptr %3, align 4, !tbaa !9
  br label %21, !llvm.loop !15

32:                                               ; preds = %21
  br label %37

33:                                               ; preds = %14
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.pthread_cond_internal, ptr %34, i32 0, i32 2
  %36 = load i32, ptr %35, align 8, !tbaa !11
  store i32 %36, ptr %5, align 4, !tbaa !9
  br label %11, !llvm.loop !17

37:                                               ; preds = %32, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  ret i32 0
}

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

; Function Attrs: nounwind uwtable
declare hidden void @prvInitializeStaticCond(ptr noundef) #0

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

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

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

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 = { alwaysinline 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 152}
!12 = !{!"pthread_cond_internal", !13, i64 0, !14, i64 8, !10, i64 152}
!13 = !{!"long", !7, i64 0}
!14 = !{!"xSTATIC_QUEUE", !7, i64 0, !7, i64 24, !7, i64 32, !7, i64 112, !7, i64 136, !7, i64 138}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


; 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 uwtable
define dso_local noundef i32 @pthread_cond_broadcast(ptr noundef %0) local_unnamed_addr #0 {
  tail call void @prvInitializeStaticCond(ptr noundef %0)
  %2 = getelementptr inbounds i8, ptr %0, i64 152
  br label %3

3:                                                ; preds = %4, %1
  %.0 = load i32, ptr %2, align 8, !tbaa !5
  %.not = icmp eq i32 %.0, 0
  br i1 %.not, label %.loopexit, label %4

4:                                                ; preds = %3
  %5 = tail call i32 @Atomic_CompareAndSwap_u32(ptr noundef nonnull %2, i32 noundef 0, i32 noundef %.0)
  %6 = icmp eq i32 %5, 1
  br i1 %6, label %.preheader, label %3, !llvm.loop !12

.preheader:                                       ; preds = %4
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  br label %8

8:                                                ; preds = %.preheader, %8
  %.01013 = phi i32 [ 0, %.preheader ], [ %10, %8 ]
  %9 = tail call i64 @xQueueGenericSend(ptr noundef nonnull %7, ptr noundef null, i32 noundef 0, i64 noundef 0) #3
  %10 = add nuw i32 %.01013, 1
  %exitcond.not = icmp eq i32 %10, %.0
  br i1 %exitcond.not, label %.loopexit, label %8, !llvm.loop !14

.loopexit:                                        ; preds = %3, %8
  ret i32 0
}

; Function Attrs: nounwind uwtable
declare hidden void @prvInitializeStaticCond(ptr noundef) local_unnamed_addr #0

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

declare i64 @xQueueGenericSend(ptr noundef, ptr noundef, i32 noundef, i64 noundef) local_unnamed_addr #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 = { alwaysinline 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, !11, i64 152}
!6 = !{!"pthread_cond_internal", !7, i64 0, !10, i64 8, !11, i64 152}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"xSTATIC_QUEUE", !8, i64 0, !8, i64 24, !8, i64 32, !8, i64 112, !8, i64 136, !8, i64 138}
!11 = !{!"int", !8, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


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