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

; 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.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.QueueListElement = type { %struct.Link, ptr, i64, ptr, %struct.mq_attr, i64 }
%struct.Link = type { ptr, ptr }
%struct.mq_attr = type { i64, i64, i64, i64 }

@xQueueListMutex = external hidden global %struct.xSTATIC_QUEUE, align 8
@FreeRTOS_errno = external global i32, align 4

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

; Function Attrs: nounwind uwtable
declare hidden void @prvInitializeQueueList() #1

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

; Function Attrs: nounwind uwtable
declare hidden i64 @prvFindQueueInList(ptr noundef, ptr noundef, ptr noundef) #1

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

; Function Attrs: nounwind uwtable
declare hidden void @prvDeleteMessageQueue(ptr noundef) #1

; 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
declare hidden i64 @prvValidateQueueName(ptr noundef, ptr noundef) #1

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

10:                                               ; preds = %1
  store i32 22, ptr @FreeRTOS_errno, align 4, !tbaa !9
  store i32 -1, ptr %3, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %10, %1
  %12 = load i32, ptr %3, align 4, !tbaa !9
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %69

14:                                               ; preds = %11
  %15 = call i64 @xQueueSemaphoreTake(ptr noundef @xQueueListMutex, i32 noundef -1)
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = call i64 @prvFindQueueInList(ptr noundef %6, ptr noundef %16, ptr noundef null)
  %18 = icmp eq i64 %17, 1
  br i1 %18, label %19, label %66

19:                                               ; preds = %14
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.QueueListElement, ptr %20, i32 0, i32 2
  %22 = load i64, ptr %21, align 8, !tbaa !13
  %23 = icmp eq i64 %22, 0
  br i1 %23, label %24, label %62

24:                                               ; preds = %19
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.QueueListElement, ptr %25, i32 0, i32 0
  %27 = getelementptr inbounds %struct.Link, ptr %26, i32 0, i32 1
  %28 = load ptr, ptr %27, align 8, !tbaa !17
  %29 = icmp ne ptr %28, null
  br i1 %29, label %30, label %55

30:                                               ; preds = %24
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.QueueListElement, ptr %31, i32 0, i32 0
  %33 = getelementptr inbounds %struct.Link, ptr %32, i32 0, i32 0
  %34 = load ptr, ptr %33, align 8, !tbaa !18
  %35 = icmp ne ptr %34, null
  br i1 %35, label %36, label %55

36:                                               ; preds = %30
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.QueueListElement, ptr %37, i32 0, i32 0
  %39 = getelementptr inbounds %struct.Link, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !17
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.QueueListElement, ptr %41, i32 0, i32 0
  %43 = getelementptr inbounds %struct.Link, ptr %42, i32 0, i32 0
  %44 = load ptr, ptr %43, align 8, !tbaa !18
  %45 = getelementptr inbounds %struct.Link, ptr %44, i32 0, i32 1
  store ptr %40, ptr %45, align 8, !tbaa !19
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.QueueListElement, ptr %46, i32 0, i32 0
  %48 = getelementptr inbounds %struct.Link, ptr %47, i32 0, i32 0
  %49 = load ptr, ptr %48, align 8, !tbaa !18
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.QueueListElement, ptr %50, i32 0, i32 0
  %52 = getelementptr inbounds %struct.Link, ptr %51, i32 0, i32 1
  %53 = load ptr, ptr %52, align 8, !tbaa !17
  %54 = getelementptr inbounds %struct.Link, ptr %53, i32 0, i32 0
  store ptr %49, ptr %54, align 8, !tbaa !20
  br label %55

55:                                               ; preds = %36, %30, %24
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.QueueListElement, ptr %56, i32 0, i32 0
  %58 = getelementptr inbounds %struct.Link, ptr %57, i32 0, i32 0
  store ptr null, ptr %58, align 8, !tbaa !18
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.QueueListElement, ptr %59, i32 0, i32 0
  %61 = getelementptr inbounds %struct.Link, ptr %60, i32 0, i32 1
  store ptr null, ptr %61, align 8, !tbaa !17
  store i64 1, ptr %5, align 8, !tbaa !11
  br label %65

62:                                               ; preds = %19
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.QueueListElement, ptr %63, i32 0, i32 5
  store i64 1, ptr %64, align 8, !tbaa !21
  br label %65

65:                                               ; preds = %62, %55
  br label %67

66:                                               ; preds = %14
  store i32 2, ptr @FreeRTOS_errno, align 4, !tbaa !9
  store i32 -1, ptr %3, align 4, !tbaa !9
  br label %67

67:                                               ; preds = %66, %65
  %68 = call i64 @xQueueGenericSend(ptr noundef @xQueueListMutex, ptr noundef null, i32 noundef 0, i64 noundef 0)
  br label %69

69:                                               ; preds = %67, %11
  %70 = load i64, ptr %5, align 8, !tbaa !11
  %71 = icmp eq i64 %70, 1
  br i1 %71, label %72, label %74

72:                                               ; preds = %69
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  call void @prvDeleteMessageQueue(ptr noundef %73)
  br label %74

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

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 = { 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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !12, i64 24}
!14 = !{!"QueueListElement", !15, i64 0, !6, i64 16, !12, i64 24, !6, i64 32, !16, i64 40, !12, i64 72}
!15 = !{!"Link", !6, i64 0, !6, i64 8}
!16 = !{!"mq_attr", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24}
!17 = !{!14, !6, i64 8}
!18 = !{!14, !6, i64 0}
!19 = !{!15, !6, i64 8}
!20 = !{!15, !6, i64 0}
!21 = !{!14, !12, i64 72}


; 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.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] }

@xQueueListMutex = external hidden global %struct.xSTATIC_QUEUE, align 8
@FreeRTOS_errno = external local_unnamed_addr global i32, align 4

; 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 uwtable
declare hidden void @prvInitializeQueueList() local_unnamed_addr #1

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

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @mq_unlink(ptr noundef %0) local_unnamed_addr #1 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  store i64 0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  store ptr null, ptr %3, align 8, !tbaa !9
  tail call void @prvInitializeQueueList()
  %4 = call i64 @prvValidateQueueName(ptr noundef %0, ptr noundef nonnull %2)
  %.not = icmp eq i64 %4, 0
  br i1 %.not, label %.thread, label %.critedge

.thread:                                          ; preds = %1
  store i32 22, ptr @FreeRTOS_errno, align 4, !tbaa !11
  br label %31

.critedge:                                        ; preds = %1
  %5 = call i64 @xQueueSemaphoreTake(ptr noundef nonnull @xQueueListMutex, i32 noundef -1) #3
  %6 = call i64 @prvFindQueueInList(ptr noundef nonnull %3, ptr noundef %0, ptr noundef null)
  %7 = icmp eq i64 %6, 1
  br i1 %7, label %8, label %23

8:                                                ; preds = %.critedge
  %9 = load ptr, ptr %3, align 8, !tbaa !9
  %10 = getelementptr inbounds i8, ptr %9, i64 24
  %11 = load i64, ptr %10, align 8, !tbaa !13
  %12 = icmp eq i64 %11, 0
  br i1 %12, label %13, label %21

13:                                               ; preds = %8
  %14 = getelementptr inbounds i8, ptr %9, i64 8
  %15 = load ptr, ptr %14, align 8, !tbaa !17
  %.not8 = icmp eq ptr %15, null
  br i1 %.not8, label %25, label %16

16:                                               ; preds = %13
  %17 = load ptr, ptr %9, align 8, !tbaa !18
  %.not9 = icmp eq ptr %17, null
  br i1 %.not9, label %25, label %18

18:                                               ; preds = %16
  %19 = getelementptr inbounds i8, ptr %17, i64 8
  store ptr %15, ptr %19, align 8, !tbaa !19
  %20 = load ptr, ptr %14, align 8, !tbaa !17
  store ptr %17, ptr %20, align 8, !tbaa !20
  %.pre = load ptr, ptr %3, align 8, !tbaa !9
  br label %25

21:                                               ; preds = %8
  %22 = getelementptr inbounds i8, ptr %9, i64 72
  store i64 1, ptr %22, align 8, !tbaa !21
  br label %.thread13

23:                                               ; preds = %.critedge
  store i32 2, ptr @FreeRTOS_errno, align 4, !tbaa !11
  br label %.thread13

.thread13:                                        ; preds = %21, %23
  %.15.ph = phi i32 [ -1, %23 ], [ 0, %21 ]
  %24 = call i64 @xQueueGenericSend(ptr noundef nonnull @xQueueListMutex, ptr noundef null, i32 noundef 0, i64 noundef 0) #3
  br label %31

25:                                               ; preds = %18, %16, %13
  %26 = phi ptr [ %.pre, %18 ], [ %9, %16 ], [ %9, %13 ]
  store ptr null, ptr %26, align 8, !tbaa !18
  %27 = load ptr, ptr %3, align 8, !tbaa !9
  %28 = getelementptr inbounds i8, ptr %27, i64 8
  store ptr null, ptr %28, align 8, !tbaa !17
  %29 = call i64 @xQueueGenericSend(ptr noundef nonnull @xQueueListMutex, ptr noundef null, i32 noundef 0, i64 noundef 0) #3
  %30 = load ptr, ptr %3, align 8, !tbaa !9
  call void @prvDeleteMessageQueue(ptr noundef %30)
  br label %31

31:                                               ; preds = %.thread13, %.thread, %25
  %.212 = phi i32 [ -1, %.thread ], [ 0, %25 ], [ %.15.ph, %.thread13 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #3
  ret i32 %.212
}

attributes #0 = { mustprogress 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 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 24}
!14 = !{!"QueueListElement", !15, i64 0, !10, i64 16, !6, i64 24, !10, i64 32, !16, i64 40, !6, i64 72}
!15 = !{!"Link", !10, i64 0, !10, i64 8}
!16 = !{!"mq_attr", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!17 = !{!14, !10, i64 8}
!18 = !{!14, !10, i64 0}
!19 = !{!15, !10, i64 8}
!20 = !{!15, !10, i64 0}
!21 = !{!14, !6, i64 72}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass