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

@xQueueListMutex = external hidden global %struct.xSTATIC_QUEUE, align 8
@FreeRTOS_errno = external global i32, align 4
@__const.mq_open.xQueueCreationAttr = external hidden unnamed_addr constant %struct.mq_attr, align 8

; 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: 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 ptr @mq_open(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca %struct.mq_attr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  store ptr null, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  store i64 0, ptr %10, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 32, ptr %11) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %11, ptr align 8 @__const.mq_open.xQueueCreationAttr, i64 32, i1 false)
  %12 = load i32, ptr %7, align 4, !tbaa !9
  call void @prvInitializeQueueList()
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = call i64 @prvValidateQueueName(ptr noundef %13, ptr noundef %10)
  %15 = icmp eq i64 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %4
  store i32 22, ptr @FreeRTOS_errno, align 4, !tbaa !9
  store ptr inttoptr (i64 -1 to ptr), ptr %9, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %16, %4
  %18 = load ptr, ptr %9, align 8, !tbaa !5
  %19 = icmp eq ptr %18, null
  br i1 %19, label %20, label %39

20:                                               ; preds = %17
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = and i32 %21, 2
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %24, label %38

24:                                               ; preds = %20
  %25 = load ptr, ptr %8, align 8, !tbaa !5
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %38

27:                                               ; preds = %24
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.mq_attr, ptr %28, i32 0, i32 1
  %30 = load i64, ptr %29, align 8, !tbaa !13
  %31 = icmp sle i64 %30, 0
  br i1 %31, label %37, label %32

32:                                               ; preds = %27
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.mq_attr, ptr %33, i32 0, i32 2
  %35 = load i64, ptr %34, align 8, !tbaa !15
  %36 = icmp sle i64 %35, 0
  br i1 %36, label %37, label %38

37:                                               ; preds = %32, %27
  store i32 22, ptr @FreeRTOS_errno, align 4, !tbaa !9
  store ptr inttoptr (i64 -1 to ptr), ptr %9, align 8, !tbaa !5
  br label %38

38:                                               ; preds = %37, %32, %24, %20
  br label %39

39:                                               ; preds = %38, %17
  %40 = load ptr, ptr %9, align 8, !tbaa !5
  %41 = icmp eq ptr %40, null
  br i1 %41, label %42, label %92

42:                                               ; preds = %39
  %43 = call i64 @xQueueSemaphoreTake(ptr noundef @xQueueListMutex, i32 noundef -1)
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = call i64 @prvFindQueueInList(ptr noundef %9, ptr noundef %44, ptr noundef null)
  %46 = icmp eq i64 %45, 1
  br i1 %46, label %47, label %69

47:                                               ; preds = %42
  %48 = load i32, ptr %6, align 4, !tbaa !9
  %49 = and i32 %48, 8
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %51, label %56

51:                                               ; preds = %47
  %52 = load i32, ptr %6, align 4, !tbaa !9
  %53 = and i32 %52, 2
  %54 = icmp ne i32 %53, 0
  br i1 %54, label %55, label %56

55:                                               ; preds = %51
  store i32 17, ptr @FreeRTOS_errno, align 4, !tbaa !9
  store ptr inttoptr (i64 -1 to ptr), ptr %9, align 8, !tbaa !5
  br label %68

56:                                               ; preds = %51, %47
  %57 = load ptr, ptr %9, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.QueueListElement, ptr %57, i32 0, i32 5
  %59 = load i64, ptr %58, align 8, !tbaa !16
  %60 = icmp eq i64 %59, 1
  br i1 %60, label %61, label %62

61:                                               ; preds = %56
  store i32 22, ptr @FreeRTOS_errno, align 4, !tbaa !9
  store ptr inttoptr (i64 -1 to ptr), ptr %9, align 8, !tbaa !5
  br label %67

62:                                               ; preds = %56
  %63 = load ptr, ptr %9, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.QueueListElement, ptr %63, i32 0, i32 2
  %65 = load i64, ptr %64, align 8, !tbaa !19
  %66 = add i64 %65, 1
  store i64 %66, ptr %64, align 8, !tbaa !19
  br label %67

67:                                               ; preds = %62, %61
  br label %68

68:                                               ; preds = %67, %55
  br label %90

69:                                               ; preds = %42
  %70 = load i32, ptr %6, align 4, !tbaa !9
  %71 = and i32 %70, 2
  %72 = icmp ne i32 %71, 0
  br i1 %72, label %73, label %88

73:                                               ; preds = %69
  %74 = load ptr, ptr %8, align 8, !tbaa !5
  %75 = icmp ne ptr %74, null
  br i1 %75, label %76, label %78

76:                                               ; preds = %73
  %77 = load ptr, ptr %8, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %11, ptr align 8 %77, i64 32, i1 false), !tbaa.struct !20
  br label %78

78:                                               ; preds = %76, %73
  %79 = load i32, ptr %6, align 4, !tbaa !9
  %80 = sext i32 %79 to i64
  %81 = getelementptr inbounds %struct.mq_attr, ptr %11, i32 0, i32 0
  store i64 %80, ptr %81, align 8, !tbaa !21
  %82 = load ptr, ptr %5, align 8, !tbaa !5
  %83 = load i64, ptr %10, align 8, !tbaa !11
  %84 = call i64 @prvCreateNewMessageQueue(ptr noundef %9, ptr noundef %11, ptr noundef %82, i64 noundef %83)
  %85 = icmp eq i64 %84, 0
  br i1 %85, label %86, label %87

86:                                               ; preds = %78
  store i32 28, ptr @FreeRTOS_errno, align 4, !tbaa !9
  store ptr inttoptr (i64 -1 to ptr), ptr %9, align 8, !tbaa !5
  br label %87

87:                                               ; preds = %86, %78
  br label %89

88:                                               ; preds = %69
  store i32 2, ptr @FreeRTOS_errno, align 4, !tbaa !9
  store ptr inttoptr (i64 -1 to ptr), ptr %9, align 8, !tbaa !5
  br label %89

89:                                               ; preds = %88, %87
  br label %90

90:                                               ; preds = %89, %68
  %91 = call i64 @xQueueGenericSend(ptr noundef @xQueueListMutex, ptr noundef null, i32 noundef 0, i64 noundef 0)
  br label %92

92:                                               ; preds = %90, %39
  %93 = load ptr, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 32, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  ret ptr %93
}

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

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

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

@xQueueListMutex = external hidden global %struct.xSTATIC_QUEUE, align 8
@FreeRTOS_errno = external local_unnamed_addr global i32, align 4
@__const.mq_open.xQueueCreationAttr = external hidden unnamed_addr constant %struct.mq_attr, align 8

; 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: 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 ptr @mq_open(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef readonly %3) local_unnamed_addr #1 {
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca %struct.mq_attr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #4
  store ptr null, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #4
  store i64 0, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %7) #4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %7, ptr noundef nonnull align 8 dereferenceable(32) @__const.mq_open.xQueueCreationAttr, i64 32, i1 false)
  tail call void @prvInitializeQueueList()
  %8 = call i64 @prvValidateQueueName(ptr noundef %0, ptr noundef nonnull %6)
  %9 = icmp eq i64 %8, 0
  br i1 %9, label %.thread, label %10

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

10:                                               ; preds = %4
  %11 = and i32 %1, 2
  %12 = icmp ne i32 %11, 0
  %13 = icmp ne ptr %3, null
  %or.cond = and i1 %12, %13
  br i1 %or.cond, label %14, label %23

14:                                               ; preds = %10
  %15 = getelementptr inbounds i8, ptr %3, i64 8
  %16 = load i64, ptr %15, align 8, !tbaa !13
  %17 = icmp slt i64 %16, 1
  br i1 %17, label %22, label %18

18:                                               ; preds = %14
  %19 = getelementptr inbounds i8, ptr %3, i64 16
  %20 = load i64, ptr %19, align 8, !tbaa !15
  %21 = icmp slt i64 %20, 1
  br i1 %21, label %22, label %23

22:                                               ; preds = %18, %14
  store i32 22, ptr @FreeRTOS_errno, align 4, !tbaa !11
  br label %.thread20

23:                                               ; preds = %18, %10
  %24 = call i64 @xQueueSemaphoreTake(ptr noundef nonnull @xQueueListMutex, i32 noundef -1) #4
  %25 = call i64 @prvFindQueueInList(ptr noundef nonnull %5, ptr noundef %0, ptr noundef null)
  %26 = icmp eq i64 %25, 1
  br i1 %26, label %27, label %40

27:                                               ; preds = %23
  %28 = and i32 %1, 10
  %or.cond17.not = icmp eq i32 %28, 10
  br i1 %or.cond17.not, label %29, label %30

29:                                               ; preds = %27
  store i32 17, ptr @FreeRTOS_errno, align 4, !tbaa !11
  store ptr inttoptr (i64 -1 to ptr), ptr %5, align 8, !tbaa !5
  br label %50

30:                                               ; preds = %27
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %31, i64 72
  %33 = load i64, ptr %32, align 8, !tbaa !16
  %34 = icmp eq i64 %33, 1
  br i1 %34, label %35, label %36

35:                                               ; preds = %30
  store i32 22, ptr @FreeRTOS_errno, align 4, !tbaa !11
  store ptr inttoptr (i64 -1 to ptr), ptr %5, align 8, !tbaa !5
  br label %50

36:                                               ; preds = %30
  %37 = getelementptr inbounds i8, ptr %31, i64 24
  %38 = load i64, ptr %37, align 8, !tbaa !19
  %39 = add i64 %38, 1
  store i64 %39, ptr %37, align 8, !tbaa !19
  br label %50

40:                                               ; preds = %23
  %.not = icmp eq i32 %11, 0
  br i1 %.not, label %49, label %41

41:                                               ; preds = %40
  %.not14 = icmp eq ptr %3, null
  br i1 %.not14, label %43, label %42

42:                                               ; preds = %41
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %7, ptr noundef nonnull align 8 dereferenceable(32) %3, i64 32, i1 false), !tbaa.struct !20
  br label %43

43:                                               ; preds = %42, %41
  %44 = sext i32 %1 to i64
  store i64 %44, ptr %7, align 8, !tbaa !21
  %45 = load i64, ptr %6, align 8, !tbaa !9
  %46 = call i64 @prvCreateNewMessageQueue(ptr noundef nonnull %5, ptr noundef nonnull %7, ptr noundef %0, i64 noundef %45)
  %47 = icmp eq i64 %46, 0
  br i1 %47, label %48, label %50

48:                                               ; preds = %43
  store i32 28, ptr @FreeRTOS_errno, align 4, !tbaa !11
  store ptr inttoptr (i64 -1 to ptr), ptr %5, align 8, !tbaa !5
  br label %50

49:                                               ; preds = %40
  store i32 2, ptr @FreeRTOS_errno, align 4, !tbaa !11
  store ptr inttoptr (i64 -1 to ptr), ptr %5, align 8, !tbaa !5
  br label %50

50:                                               ; preds = %49, %48, %43, %29, %36, %35
  %51 = call i64 @xQueueGenericSend(ptr noundef nonnull @xQueueListMutex, ptr noundef null, i32 noundef 0, i64 noundef 0) #4
  %.pre = load ptr, ptr %5, align 8, !tbaa !5
  br label %.thread20

.thread20:                                        ; preds = %22, %.thread, %50
  %52 = phi ptr [ inttoptr (i64 -1 to ptr), %22 ], [ inttoptr (i64 -1 to ptr), %.thread ], [ %.pre, %50 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #4
  ret ptr %52
}

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

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

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


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass