; 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_timedreceive(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, ptr noundef %4) #2 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %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 ptr %3, ptr %9, align 8, !tbaa !5
  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 ptr, ptr %9, align 8, !tbaa !5
  %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 ult 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 %72

54:                                               ; preds = %50
  %55 = load ptr, ptr %14, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.QueueListElement, ptr %55, i32 0, i32 1
  %57 = load ptr, ptr %56, align 8, !tbaa !18
  %58 = load i32, ptr %13, align 4, !tbaa !11
  %59 = call i64 @xQueueReceive(ptr noundef %57, ptr noundef %15, i32 noundef %58)
  %60 = icmp eq i64 %59, 0
  br i1 %60, label %61, label %71

61:                                               ; preds = %54
  %62 = load ptr, ptr %14, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.QueueListElement, ptr %62, i32 0, i32 4
  %64 = getelementptr inbounds %struct.mq_attr, ptr %63, i32 0, i32 0
  %65 = load i64, ptr %64, align 8, !tbaa !17
  %66 = and i64 %65, 1024
  %67 = icmp ne i64 %66, 0
  br i1 %67, label %68, label %69

68:                                               ; preds = %61
  store i32 11, ptr @FreeRTOS_errno, align 4, !tbaa !11
  br label %70

69:                                               ; preds = %61
  store i32 116, ptr @FreeRTOS_errno, align 4, !tbaa !11
  br label %70

70:                                               ; preds = %69, %68
  store i32 -1, ptr %11, align 4, !tbaa !11
  br label %71

71:                                               ; preds = %70, %54
  br label %72

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

75:                                               ; preds = %72
  %76 = getelementptr inbounds %struct.QueueElement, ptr %15, i32 0, i32 1
  %77 = load i64, ptr %76, align 8, !tbaa !19
  %78 = trunc i64 %77 to i32
  store i32 %78, ptr %11, align 4, !tbaa !11
  %79 = load ptr, ptr %7, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.QueueElement, ptr %15, i32 0, i32 0
  %81 = load ptr, ptr %80, align 8, !tbaa !21
  %82 = getelementptr inbounds %struct.QueueElement, ptr %15, i32 0, i32 1
  %83 = load i64, ptr %82, align 8, !tbaa !19
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %79, ptr align 1 %81, i64 %83, i1 false)
  %84 = getelementptr inbounds %struct.QueueElement, ptr %15, i32 0, i32 0
  %85 = load ptr, ptr %84, align 8, !tbaa !21
  call void @vPortFree(ptr noundef %85)
  br label %86

86:                                               ; preds = %75, %72
  %87 = 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 %87
}

; 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 i64 @xQueueReceive(ptr noundef, ptr noundef, i32 noundef) #1

declare void @vPortFree(ptr 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 = !{!14, !6, i64 16}
!19 = !{!20, !10, i64 8}
!20 = !{!"QueueElement", !6, i64 0, !10, i64 8}
!21 = !{!20, !6, 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"

%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 i32 @mq_timedreceive(ptr noundef %0, ptr nocapture noundef writeonly %1, i64 noundef %2, ptr nocapture noundef readnone %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) #5
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %7) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %7, i8 0, i64 16, i1 false)
  %8 = tail call i64 @xQueueSemaphoreTake(ptr noundef nonnull @xQueueListMutex, i32 noundef -1) #5
  %9 = tail call i64 @prvFindQueueInList(ptr noundef null, ptr noundef null, ptr noundef %0)
  %.not = icmp eq i64 %9, 0
  br i1 %.not, label %.critedge21, label %.critedge

.critedge:                                        ; preds = %5
  %10 = getelementptr inbounds i8, ptr %0, i64 56
  %11 = load i64, ptr %10, align 8, !tbaa !9
  %12 = icmp ugt i64 %11, %2
  br i1 %12, label %.critedge21, 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)
  %.not17 = icmp eq i32 %16, 0
  br i1 %.not17, label %.critedge20, label %.critedge21

.critedge20:                                      ; preds = %13
  %17 = call i64 @xQueueGenericSend(ptr noundef nonnull @xQueueListMutex, ptr noundef null, i32 noundef 0, i64 noundef 0) #5
  %18 = getelementptr inbounds i8, ptr %0, i64 16
  %19 = load ptr, ptr %18, align 8, !tbaa !16
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = call i64 @xQueueReceive(ptr noundef %19, ptr noundef nonnull %7, i32 noundef %20) #5
  %22 = icmp eq i64 %21, 0
  br i1 %22, label %23, label %27

23:                                               ; preds = %.critedge20
  %24 = load i64, ptr %14, align 8, !tbaa !15
  %25 = and i64 %24, 1024
  %.not18 = icmp eq i64 %25, 0
  %. = select i1 %.not18, i32 116, i32 11
  store i32 %., ptr @FreeRTOS_errno, align 4, !tbaa !5
  br label %.critedge23

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

27:                                               ; preds = %.critedge20
  %28 = getelementptr inbounds i8, ptr %7, i64 8
  %29 = load i64, ptr %28, align 8, !tbaa !17
  %30 = trunc i64 %29 to i32
  %31 = load ptr, ptr %7, align 8, !tbaa !19
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %1, ptr align 1 %31, i64 %29, i1 false)
  call void @vPortFree(ptr noundef %31) #5
  br label %.critedge23

.critedge23:                                      ; preds = %23, %.critedge21, %27
  %.4 = phi i32 [ %30, %27 ], [ -1, %.critedge21 ], [ -1, %23 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #5
  ret i32 %.4
}

; Function Attrs: mustprogress 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) local_unnamed_addr #2

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

declare void @vPortFree(ptr 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 = { mustprogress 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 = !{!"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 = !{!10, !12, i64 16}
!17 = !{!18, !13, i64 8}
!18 = !{!"QueueElement", !12, i64 0, !13, i64 8}
!19 = !{!18, !12, i64 0}


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