; 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.QueueElement = type { ptr, i64 }
%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

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

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

declare i64 @xQueueGenericSend(ptr noundef, ptr noundef, i32 noundef, i64 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @mq_timedsend(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3, ptr noundef %4) #2 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca ptr, align 8
  %15 = alloca %struct.QueueElement, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !11
  store ptr %4, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  store i32 0, ptr %11, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  store i32 0, ptr %12, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  store i32 0, ptr %13, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #5
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %16, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %15) #5
  call void @llvm.memset.p0.i64(ptr align 8 %15, i8 0, i64 16, i1 false)
  %17 = load i32, ptr %9, align 4, !tbaa !11
  %18 = call i64 @xQueueSemaphoreTake(ptr noundef @xQueueListMutex, i32 noundef -1)
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = call i64 @prvFindQueueInList(ptr noundef null, ptr noundef null, ptr noundef %19)
  %21 = icmp eq i64 %20, 0
  br i1 %21, label %22, label %23

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

23:                                               ; preds = %22, %5
  %24 = load i32, ptr %11, align 4, !tbaa !11
  %25 = icmp eq i32 %24, 0
  br i1 %25, label %26, label %35

26:                                               ; preds = %23
  %27 = load i64, ptr %8, align 8, !tbaa !9
  %28 = load ptr, ptr %14, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.QueueListElement, ptr %28, i32 0, i32 4
  %30 = getelementptr inbounds %struct.mq_attr, ptr %29, i32 0, i32 2
  %31 = load i64, ptr %30, align 8, !tbaa !13
  %32 = icmp ugt i64 %27, %31
  br i1 %32, label %33, label %34

33:                                               ; preds = %26
  store i32 90, ptr @FreeRTOS_errno, align 4, !tbaa !11
  store i32 -1, ptr %11, align 4, !tbaa !11
  br label %34

34:                                               ; preds = %33, %26
  br label %35

35:                                               ; preds = %34, %23
  %36 = load i32, ptr %11, align 4, !tbaa !11
  %37 = icmp eq i32 %36, 0
  br i1 %37, label %38, label %50

38:                                               ; preds = %35
  %39 = load ptr, ptr %14, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.QueueListElement, ptr %39, i32 0, i32 4
  %41 = getelementptr inbounds %struct.mq_attr, ptr %40, i32 0, i32 0
  %42 = load i64, ptr %41, align 8, !tbaa !17
  %43 = load ptr, ptr %10, align 8, !tbaa !5
  %44 = call i32 @prvCalculateTickTimeout(i64 noundef %42, ptr noundef %43, ptr noundef %13)
  store i32 %44, ptr %12, align 4, !tbaa !11
  %45 = load i32, ptr %12, align 4, !tbaa !11
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %47, label %49

47:                                               ; preds = %38
  %48 = load i32, ptr %12, align 4, !tbaa !11
  store i32 %48, ptr @FreeRTOS_errno, align 4, !tbaa !11
  store i32 -1, ptr %11, align 4, !tbaa !11
  br label %49

49:                                               ; preds = %47, %38
  br label %50

50:                                               ; preds = %49, %35
  %51 = call i64 @xQueueGenericSend(ptr noundef @xQueueListMutex, ptr noundef null, i32 noundef 0, i64 noundef 0)
  %52 = load i32, ptr %11, align 4, !tbaa !11
  %53 = icmp eq i32 %52, 0
  br i1 %53, label %54, label %70

54:                                               ; preds = %50
  %55 = load i64, ptr %8, align 8, !tbaa !9
  %56 = getelementptr inbounds %struct.QueueElement, ptr %15, i32 0, i32 1
  store i64 %55, ptr %56, align 8, !tbaa !18
  %57 = load i64, ptr %8, align 8, !tbaa !9
  %58 = call ptr @pvPortMalloc(i64 noundef %57)
  %59 = getelementptr inbounds %struct.QueueElement, ptr %15, i32 0, i32 0
  store ptr %58, ptr %59, align 8, !tbaa !20
  %60 = getelementptr inbounds %struct.QueueElement, ptr %15, i32 0, i32 0
  %61 = load ptr, ptr %60, align 8, !tbaa !20
  %62 = icmp eq ptr %61, null
  br i1 %62, label %63, label %64

63:                                               ; preds = %54
  store i32 90, ptr @FreeRTOS_errno, align 4, !tbaa !11
  store i32 -1, ptr %11, align 4, !tbaa !11
  br label %69

64:                                               ; preds = %54
  %65 = getelementptr inbounds %struct.QueueElement, ptr %15, i32 0, i32 0
  %66 = load ptr, ptr %65, align 8, !tbaa !20
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = load i64, ptr %8, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %66, ptr align 1 %67, i64 %68, i1 false)
  br label %69

69:                                               ; preds = %64, %63
  br label %70

70:                                               ; preds = %69, %50
  %71 = load i32, ptr %11, align 4, !tbaa !11
  %72 = icmp eq i32 %71, 0
  br i1 %72, label %73, label %93

73:                                               ; preds = %70
  %74 = load ptr, ptr %14, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.QueueListElement, ptr %74, i32 0, i32 1
  %76 = load ptr, ptr %75, align 8, !tbaa !21
  %77 = load i32, ptr %13, align 4, !tbaa !11
  %78 = call i64 @xQueueGenericSend(ptr noundef %76, ptr noundef %15, i32 noundef %77, i64 noundef 0)
  %79 = icmp eq i64 %78, 0
  br i1 %79, label %80, label %92

80:                                               ; preds = %73
  %81 = load ptr, ptr %14, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.QueueListElement, ptr %81, i32 0, i32 4
  %83 = getelementptr inbounds %struct.mq_attr, ptr %82, i32 0, i32 0
  %84 = load i64, ptr %83, align 8, !tbaa !17
  %85 = and i64 %84, 1024
  %86 = icmp ne i64 %85, 0
  br i1 %86, label %87, label %88

87:                                               ; preds = %80
  store i32 11, ptr @FreeRTOS_errno, align 4, !tbaa !11
  br label %89

88:                                               ; preds = %80
  store i32 116, ptr @FreeRTOS_errno, align 4, !tbaa !11
  br label %89

89:                                               ; preds = %88, %87
  %90 = getelementptr inbounds %struct.QueueElement, ptr %15, i32 0, i32 0
  %91 = load ptr, ptr %90, align 8, !tbaa !20
  call void @vPortFree(ptr noundef %91)
  store i32 -1, ptr %11, align 4, !tbaa !11
  br label %92

92:                                               ; preds = %89, %73
  br label %93

93:                                               ; preds = %92, %70
  %94 = load i32, ptr %11, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 16, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  ret i32 %94
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

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

declare void @vPortFree(ptr noundef) #1

declare ptr @pvPortMalloc(i64 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !10, i64 56}
!14 = !{!"QueueListElement", !15, i64 0, !6, i64 16, !10, i64 24, !6, i64 32, !16, i64 40, !10, i64 72}
!15 = !{!"Link", !6, i64 0, !6, i64 8}
!16 = !{!"mq_attr", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!17 = !{!14, !10, i64 40}
!18 = !{!19, !10, i64 8}
!19 = !{!"QueueElement", !6, i64 0, !10, i64 8}
!20 = !{!19, !6, i64 0}
!21 = !{!14, !6, i64 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"

%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.QueueElement = type { ptr, i64 }

@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

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

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

declare i64 @xQueueGenericSend(ptr noundef, ptr noundef, i32 noundef, i64 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: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @mq_timedsend(ptr noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2, i32 noundef %3, ptr noundef %4) local_unnamed_addr #2 {
  %6 = alloca i32, align 4
  %7 = alloca %struct.QueueElement, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %6) #4
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %7) #4
  %8 = tail call i64 @xQueueSemaphoreTake(ptr noundef nonnull @xQueueListMutex, i32 noundef -1) #4
  %9 = tail call i64 @prvFindQueueInList(ptr noundef null, ptr noundef null, ptr noundef %0)
  %.not = icmp eq i64 %9, 0
  br i1 %.not, label %.critedge25, label %.critedge

.critedge:                                        ; preds = %5
  %10 = getelementptr inbounds i8, ptr %0, i64 56
  %11 = load i64, ptr %10, align 8, !tbaa !9
  %12 = icmp ult i64 %11, %2
  br i1 %12, label %.critedge25, label %13

13:                                               ; preds = %.critedge
  %14 = getelementptr inbounds i8, ptr %0, i64 40
  %15 = load i64, ptr %14, align 8, !tbaa !15
  %16 = call i32 @prvCalculateTickTimeout(i64 noundef %15, ptr noundef %4, ptr noundef nonnull %6)
  %.not21 = icmp eq i32 %16, 0
  br i1 %.not21, label %.critedge24, label %.critedge25

.critedge24:                                      ; preds = %13
  %17 = call i64 @xQueueGenericSend(ptr noundef nonnull @xQueueListMutex, ptr noundef null, i32 noundef 0, i64 noundef 0) #4
  %18 = getelementptr inbounds i8, ptr %7, i64 8
  store i64 %2, ptr %18, align 8, !tbaa !16
  %19 = call ptr @pvPortMalloc(i64 noundef %2) #4
  store ptr %19, ptr %7, align 8, !tbaa !18
  %20 = icmp eq ptr %19, null
  br i1 %20, label %21, label %22

21:                                               ; preds = %.critedge24
  store i32 90, ptr @FreeRTOS_errno, align 4, !tbaa !5
  br label %.critedge27

22:                                               ; preds = %.critedge24
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %19, ptr align 1 %1, i64 %2, i1 false)
  %23 = getelementptr inbounds i8, ptr %0, i64 16
  %24 = load ptr, ptr %23, align 8, !tbaa !19
  %25 = load i32, ptr %6, align 4, !tbaa !5
  %26 = call i64 @xQueueGenericSend(ptr noundef %24, ptr noundef nonnull %7, i32 noundef %25, i64 noundef 0) #4
  %27 = icmp eq i64 %26, 0
  br i1 %27, label %29, label %.critedge27

.critedge25:                                      ; preds = %13, %.critedge, %5
  %.sink = phi i32 [ 9, %5 ], [ 90, %.critedge ], [ %16, %13 ]
  store i32 %.sink, ptr @FreeRTOS_errno, align 4, !tbaa !5
  %28 = call i64 @xQueueGenericSend(ptr noundef nonnull @xQueueListMutex, ptr noundef null, i32 noundef 0, i64 noundef 0) #4
  br label %.critedge27

29:                                               ; preds = %22
  %30 = load i64, ptr %14, align 8, !tbaa !15
  %31 = and i64 %30, 1024
  %.not22 = icmp eq i64 %31, 0
  %. = select i1 %.not22, i32 116, i32 11
  store i32 %., ptr @FreeRTOS_errno, align 4, !tbaa !5
  %32 = load ptr, ptr %7, align 8, !tbaa !18
  call void @vPortFree(ptr noundef %32) #4
  br label %.critedge27

.critedge27:                                      ; preds = %21, %.critedge25, %22, %29
  %.4 = phi i32 [ -1, %29 ], [ 0, %22 ], [ -1, %.critedge25 ], [ -1, %21 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #4
  ret i32 %.4
}

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

declare void @vPortFree(ptr noundef) local_unnamed_addr #1

declare ptr @pvPortMalloc(i64 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !13, i64 56}
!10 = !{!"QueueListElement", !11, i64 0, !12, i64 16, !13, i64 24, !12, i64 32, !14, i64 40, !13, i64 72}
!11 = !{!"Link", !12, i64 0, !12, i64 8}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!"mq_attr", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24}
!15 = !{!10, !13, i64 40}
!16 = !{!17, !13, i64 8}
!17 = !{!"QueueElement", !12, i64 0, !13, i64 8}
!18 = !{!17, !12, i64 0}
!19 = !{!10, !12, i64 16}


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass