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

; 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_barrier_internal = type { i32, i32, %struct.xSTATIC_QUEUE, %struct.xSTATIC_EVENT_GROUP }
%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] }
%struct.xSTATIC_EVENT_GROUP = type { i32, %struct.xSTATIC_LIST, 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: nounwind uwtable
define dso_local i32 @pthread_barrier_wait(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = 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 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %7, 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
  %8 = load ptr, ptr %5, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.pthread_barrier_internal, ptr %8, i32 0, i32 2
  %10 = call i64 @xQueueSemaphoreTake(ptr noundef %9, i32 noundef -1)
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.pthread_barrier_internal, ptr %11, i32 0, i32 0
  %13 = call i32 @Atomic_Increment_u32(ptr noundef %12)
  store i32 %13, ptr %6, align 4, !tbaa !9
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.pthread_barrier_internal, ptr %14, i32 0, i32 3
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = shl i32 1, %16
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.pthread_barrier_internal, ptr %18, i32 0, i32 1
  %20 = load i32, ptr %19, align 4, !tbaa !11
  %21 = shl i32 1, %20
  %22 = sub nsw i32 %21, 1
  %23 = call i32 @xEventGroupSync(ptr noundef %15, i32 noundef %17, i32 noundef %22, i32 noundef -1)
  %24 = load i32, ptr %6, align 4, !tbaa !9
  %25 = icmp eq i32 %24, 0
  br i1 %25, label %26, label %43

26:                                               ; preds = %1
  store i32 -2, ptr %3, align 4, !tbaa !9
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.pthread_barrier_internal, ptr %27, i32 0, i32 0
  store i32 0, ptr %28, align 8, !tbaa !18
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %39, %26
  %30 = load i32, ptr %4, align 4, !tbaa !9
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.pthread_barrier_internal, ptr %31, i32 0, i32 1
  %33 = load i32, ptr %32, align 4, !tbaa !11
  %34 = icmp ult i32 %30, %33
  br i1 %34, label %35, label %42

35:                                               ; preds = %29
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.pthread_barrier_internal, ptr %36, i32 0, i32 2
  %38 = call i64 @xQueueGenericSend(ptr noundef %37, ptr noundef null, i32 noundef 0, i64 noundef 0)
  br label %39

39:                                               ; preds = %35
  %40 = load i32, ptr %4, align 4, !tbaa !9
  %41 = add i32 %40, 1
  store i32 %41, ptr %4, align 4, !tbaa !9
  br label %29, !llvm.loop !19

42:                                               ; preds = %29
  br label %43

43:                                               ; preds = %42, %1
  %44 = load i32, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  ret i32 %44
}

declare i64 @xQueueSemaphoreTake(ptr noundef, i32 noundef) #2

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

declare i32 @xEventGroupSync(ptr noundef, i32 noundef, i32 noundef, i32 noundef) #2

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #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 4}
!12 = !{!"pthread_barrier_internal", !10, i64 0, !10, i64 4, !13, i64 8, !14, i64 152}
!13 = !{!"xSTATIC_QUEUE", !7, i64 0, !7, i64 24, !7, i64 32, !7, i64 112, !7, i64 136, !7, i64 138}
!14 = !{!"xSTATIC_EVENT_GROUP", !10, i64 0, !15, i64 8, !7, i64 48}
!15 = !{!"xSTATIC_LIST", !16, i64 0, !6, i64 8, !17, i64 16}
!16 = !{!"long", !7, i64 0}
!17 = !{!"xSTATIC_MINI_LIST_ITEM", !10, i64 0, !7, i64 8}
!18 = !{!12, !10, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"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 uwtable
define dso_local range(i32 -2, 1) i32 @pthread_barrier_wait(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = tail call i64 @xQueueSemaphoreTake(ptr noundef nonnull %2, i32 noundef -1) #3
  %4 = tail call i32 @Atomic_Increment_u32(ptr noundef %0)
  %5 = getelementptr inbounds i8, ptr %0, i64 152
  %6 = shl nuw i32 1, %4
  %7 = getelementptr inbounds i8, ptr %0, i64 4
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %notmask = shl nsw i32 -1, %8
  %9 = xor i32 %notmask, -1
  %10 = tail call i32 @xEventGroupSync(ptr noundef nonnull %5, i32 noundef %6, i32 noundef %9, i32 noundef -1) #3
  %11 = icmp eq i32 %4, 0
  br i1 %11, label %12, label %.loopexit

12:                                               ; preds = %1
  store i32 0, ptr %0, align 8, !tbaa !16
  %13 = load i32, ptr %7, align 4, !tbaa !5
  %.not = icmp eq i32 %13, 0
  br i1 %.not, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %12, %.lr.ph
  %.01213 = phi i32 [ %15, %.lr.ph ], [ 0, %12 ]
  %14 = tail call i64 @xQueueGenericSend(ptr noundef nonnull %2, ptr noundef null, i32 noundef 0, i64 noundef 0) #3
  %15 = add nuw i32 %.01213, 1
  %16 = load i32, ptr %7, align 4, !tbaa !5
  %17 = icmp ult i32 %15, %16
  br i1 %17, label %.lr.ph, label %.loopexit, !llvm.loop !17

.loopexit:                                        ; preds = %.lr.ph, %12, %1
  %.0 = phi i32 [ 0, %1 ], [ -2, %12 ], [ -2, %.lr.ph ]
  ret i32 %.0
}

declare i64 @xQueueSemaphoreTake(ptr noundef, i32 noundef) local_unnamed_addr #1

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

declare i32 @xEventGroupSync(ptr noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

declare i64 @xQueueGenericSend(ptr noundef, ptr noundef, i32 noundef, i64 noundef) local_unnamed_addr #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 = { "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 = { 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 = { 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, !7, i64 4}
!6 = !{!"pthread_barrier_internal", !7, i64 0, !7, i64 4, !10, i64 8, !11, i64 152}
!7 = !{!"int", !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 = !{!"xSTATIC_EVENT_GROUP", !7, i64 0, !12, i64 8, !8, i64 48}
!12 = !{!"xSTATIC_LIST", !13, i64 0, !14, i64 8, !15, i64 16}
!13 = !{!"long", !8, i64 0}
!14 = !{!"any pointer", !8, i64 0}
!15 = !{!"xSTATIC_MINI_LIST_ITEM", !7, i64 0, !8, i64 8}
!16 = !{!6, !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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