; 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: nounwind uwtable
define dso_local i32 @mq_close(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i64, 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
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %6, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store i64 0, ptr %5, align 8, !tbaa !11
  call void @prvInitializeQueueList()
  %7 = call i64 @xQueueSemaphoreTake(ptr noundef @xQueueListMutex, i32 noundef -1)
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = call i64 @prvFindQueueInList(ptr noundef null, ptr noundef null, ptr noundef %8)
  %10 = icmp eq i64 %9, 1
  br i1 %10, label %11, label %74

11:                                               ; preds = %1
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.QueueListElement, ptr %12, i32 0, i32 2
  %14 = load i64, ptr %13, align 8, !tbaa !13
  %15 = icmp ugt i64 %14, 0
  br i1 %15, label %16, label %21

16:                                               ; preds = %11
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.QueueListElement, ptr %17, i32 0, i32 2
  %19 = load i64, ptr %18, align 8, !tbaa !13
  %20 = add i64 %19, -1
  store i64 %20, ptr %18, align 8, !tbaa !13
  br label %21

21:                                               ; preds = %16, %11
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.QueueListElement, ptr %22, i32 0, i32 2
  %24 = load i64, ptr %23, align 8, !tbaa !13
  %25 = icmp eq i64 %24, 0
  br i1 %25, label %26, label %73

26:                                               ; preds = %21
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.QueueListElement, ptr %27, i32 0, i32 5
  %29 = load i64, ptr %28, align 8, !tbaa !17
  %30 = icmp eq i64 %29, 1
  br i1 %30, label %31, label %69

31:                                               ; preds = %26
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.QueueListElement, ptr %32, i32 0, i32 0
  %34 = getelementptr inbounds %struct.Link, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !18
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %62

37:                                               ; preds = %31
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.QueueListElement, ptr %38, i32 0, i32 0
  %40 = getelementptr inbounds %struct.Link, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !19
  %42 = icmp ne ptr %41, null
  br i1 %42, label %43, label %62

43:                                               ; preds = %37
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.QueueListElement, ptr %44, i32 0, i32 0
  %46 = getelementptr inbounds %struct.Link, ptr %45, i32 0, i32 1
  %47 = load ptr, ptr %46, align 8, !tbaa !18
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.QueueListElement, ptr %48, i32 0, i32 0
  %50 = getelementptr inbounds %struct.Link, ptr %49, i32 0, i32 0
  %51 = load ptr, ptr %50, align 8, !tbaa !19
  %52 = getelementptr inbounds %struct.Link, ptr %51, i32 0, i32 1
  store ptr %47, ptr %52, align 8, !tbaa !20
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.QueueListElement, ptr %53, i32 0, i32 0
  %55 = getelementptr inbounds %struct.Link, ptr %54, i32 0, i32 0
  %56 = load ptr, ptr %55, align 8, !tbaa !19
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.QueueListElement, ptr %57, i32 0, i32 0
  %59 = getelementptr inbounds %struct.Link, ptr %58, i32 0, i32 1
  %60 = load ptr, ptr %59, align 8, !tbaa !18
  %61 = getelementptr inbounds %struct.Link, ptr %60, i32 0, i32 0
  store ptr %56, ptr %61, align 8, !tbaa !21
  br label %62

62:                                               ; preds = %43, %37, %31
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.QueueListElement, ptr %63, i32 0, i32 0
  %65 = getelementptr inbounds %struct.Link, ptr %64, i32 0, i32 0
  store ptr null, ptr %65, align 8, !tbaa !19
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.QueueListElement, ptr %66, i32 0, i32 0
  %68 = getelementptr inbounds %struct.Link, ptr %67, i32 0, i32 1
  store ptr null, ptr %68, align 8, !tbaa !18
  store i64 1, ptr %5, align 8, !tbaa !11
  br label %72

69:                                               ; preds = %26
  %70 = load ptr, ptr %4, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.QueueListElement, ptr %70, i32 0, i32 5
  store i64 1, ptr %71, align 8, !tbaa !17
  br label %72

72:                                               ; preds = %69, %62
  br label %73

73:                                               ; preds = %72, %21
  br label %75

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

75:                                               ; preds = %74, %73
  %76 = call i64 @xQueueGenericSend(ptr noundef @xQueueListMutex, ptr noundef null, i32 noundef 0, i64 noundef 0)
  %77 = load i64, ptr %5, align 8, !tbaa !11
  %78 = icmp eq i64 %77, 1
  br i1 %78, label %79, label %81

79:                                               ; preds = %75
  %80 = load ptr, ptr %4, align 8, !tbaa !5
  call void @prvDeleteMessageQueue(ptr noundef %80)
  br label %81

81:                                               ; preds = %79, %75
  %82 = load i32, ptr %3, align 4, !tbaa !9
  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 %82
}

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

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

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

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

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !12, i64 72}
!18 = !{!14, !6, i64 8}
!19 = !{!14, !6, i64 0}
!20 = !{!15, !6, i64 8}
!21 = !{!15, !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] }

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @mq_close(ptr noundef %0) local_unnamed_addr #0 {
  tail call void @prvInitializeQueueList()
  %2 = tail call i64 @xQueueSemaphoreTake(ptr noundef nonnull @xQueueListMutex, i32 noundef -1) #3
  %3 = tail call i64 @prvFindQueueInList(ptr noundef null, ptr noundef null, ptr noundef %0)
  %4 = icmp eq i64 %3, 1
  br i1 %4, label %5, label %23

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 24
  %7 = load i64, ptr %6, align 8, !tbaa !5
  %.not = icmp eq i64 %7, 0
  br i1 %.not, label %.thread, label %8

8:                                                ; preds = %5
  %9 = add i64 %7, -1
  store i64 %9, ptr %6, align 8, !tbaa !5
  %10 = icmp eq i64 %9, 0
  br i1 %10, label %.thread, label %.thread23

.thread:                                          ; preds = %5, %8
  %11 = getelementptr inbounds i8, ptr %0, i64 72
  %12 = load i64, ptr %11, align 8, !tbaa !13
  %13 = icmp eq i64 %12, 1
  br i1 %13, label %14, label %22

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

17:                                               ; preds = %14
  %18 = load ptr, ptr %0, align 8, !tbaa !15
  %.not22 = icmp eq ptr %18, null
  br i1 %.not22, label %25, label %19

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

22:                                               ; preds = %.thread
  store i64 1, ptr %11, align 8, !tbaa !13
  br label %.thread23

23:                                               ; preds = %1
  store i32 9, ptr @FreeRTOS_errno, align 4, !tbaa !18
  br label %.thread23

.thread23:                                        ; preds = %22, %8, %23
  %.017.ph = phi i32 [ -1, %23 ], [ 0, %8 ], [ 0, %22 ]
  %24 = tail call i64 @xQueueGenericSend(ptr noundef nonnull @xQueueListMutex, ptr noundef null, i32 noundef 0, i64 noundef 0) #3
  br label %27

25:                                               ; preds = %19, %17, %14
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %0, i8 0, i64 16, i1 false)
  %26 = tail call i64 @xQueueGenericSend(ptr noundef nonnull @xQueueListMutex, ptr noundef null, i32 noundef 0, i64 noundef 0) #3
  tail call void @prvDeleteMessageQueue(ptr noundef nonnull %0)
  br label %27

27:                                               ; preds = %.thread23, %25
  %.01725 = phi i32 [ %.017.ph, %.thread23 ], [ 0, %25 ]
  ret i32 %.01725
}

; Function Attrs: nounwind uwtable
declare hidden void @prvInitializeQueueList() local_unnamed_addr #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 #0

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

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

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

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


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