; LLVM IR from ctlst-tech/catpilot :: os/freertos/core/src (tasks)

; 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.xLIST = type { i64, ptr, %struct.xMINI_LIST_ITEM }
%struct.xMINI_LIST_ITEM = type { i32, ptr, ptr }
%struct.tskTaskControlBlock = type { ptr, %struct.xLIST_ITEM, %struct.xLIST_ITEM, i64, ptr, [24 x i8], ptr, i64, i64, ptr, [5 x ptr], [3 x i32], [3 x i8], i8, i8, i32 }
%struct.xLIST_ITEM = type { i32, ptr, ptr, ptr, ptr }

@pxCurrentTCB = external dso_local global ptr, align 8
@pxReadyTasksLists = external hidden global [32 x %struct.xLIST], align 16
@uxTopReadyPriority = external hidden global i64, 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

declare i64 @uxListRemove(ptr noundef) #1

declare void @vListInsertEnd(ptr noundef, ptr noundef) #1

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

8:                                                ; preds = %1
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %9, i32 0, i32 3
  %11 = load i64, ptr %10, align 8, !tbaa !11
  %12 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %12, i32 0, i32 3
  %14 = load i64, ptr %13, align 8, !tbaa !11
  %15 = icmp ult i64 %11, %14
  br i1 %15, label %16, label %83

16:                                               ; preds = %8
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %17, i32 0, i32 2
  %19 = getelementptr inbounds %struct.xLIST_ITEM, ptr %18, i32 0, i32 0
  %20 = load i32, ptr %19, align 8, !tbaa !15
  %21 = zext i32 %20 to i64
  %22 = and i64 %21, 2147483648
  %23 = icmp eq i64 %22, 0
  br i1 %23, label %24, label %33

24:                                               ; preds = %16
  %25 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %25, i32 0, i32 3
  %27 = load i64, ptr %26, align 8, !tbaa !11
  %28 = trunc i64 %27 to i32
  %29 = sub i32 32, %28
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %30, i32 0, i32 2
  %32 = getelementptr inbounds %struct.xLIST_ITEM, ptr %31, i32 0, i32 0
  store i32 %29, ptr %32, align 8, !tbaa !15
  br label %34

33:                                               ; preds = %16
  br label %34

34:                                               ; preds = %33, %24
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %35, i32 0, i32 1
  %37 = getelementptr inbounds %struct.xLIST_ITEM, ptr %36, i32 0, i32 4
  %38 = load ptr, ptr %37, align 8, !tbaa !16
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %39, i32 0, i32 3
  %41 = load i64, ptr %40, align 8, !tbaa !11
  %42 = getelementptr inbounds [32 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %41
  %43 = icmp eq ptr %38, %42
  %44 = zext i1 %43 to i64
  %45 = select i1 %43, i64 1, i64 0
  %46 = icmp ne i64 %45, 0
  br i1 %46, label %47, label %76

47:                                               ; preds = %34
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %48, i32 0, i32 1
  %50 = call i64 @uxListRemove(ptr noundef %49)
  %51 = icmp eq i64 %50, 0
  br i1 %51, label %52, label %53

52:                                               ; preds = %47
  br label %54

53:                                               ; preds = %47
  br label %54

54:                                               ; preds = %53, %52
  %55 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %55, i32 0, i32 3
  %57 = load i64, ptr %56, align 8, !tbaa !11
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %58, i32 0, i32 3
  store i64 %57, ptr %59, align 8, !tbaa !11
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %60, i32 0, i32 3
  %62 = load i64, ptr %61, align 8, !tbaa !11
  %63 = load volatile i64, ptr @uxTopReadyPriority, align 8, !tbaa !9
  %64 = icmp ugt i64 %62, %63
  br i1 %64, label %65, label %69

65:                                               ; preds = %54
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %66, i32 0, i32 3
  %68 = load i64, ptr %67, align 8, !tbaa !11
  store volatile i64 %68, ptr @uxTopReadyPriority, align 8, !tbaa !9
  br label %69

69:                                               ; preds = %65, %54
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %70, i32 0, i32 3
  %72 = load i64, ptr %71, align 8, !tbaa !11
  %73 = getelementptr inbounds [32 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %72
  %74 = load ptr, ptr %3, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %74, i32 0, i32 1
  call void @vListInsertEnd(ptr noundef %73, ptr noundef %75)
  br label %82

76:                                               ; preds = %34
  %77 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %77, i32 0, i32 3
  %79 = load i64, ptr %78, align 8, !tbaa !11
  %80 = load ptr, ptr %3, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %80, i32 0, i32 3
  store i64 %79, ptr %81, align 8, !tbaa !11
  br label %82

82:                                               ; preds = %76, %69
  store i64 1, ptr %4, align 8, !tbaa !9
  br label %94

83:                                               ; preds = %8
  %84 = load ptr, ptr %3, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %84, i32 0, i32 7
  %86 = load i64, ptr %85, align 8, !tbaa !17
  %87 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %87, i32 0, i32 3
  %89 = load i64, ptr %88, align 8, !tbaa !11
  %90 = icmp ult i64 %86, %89
  br i1 %90, label %91, label %92

91:                                               ; preds = %83
  store i64 1, ptr %4, align 8, !tbaa !9
  br label %93

92:                                               ; preds = %83
  br label %93

93:                                               ; preds = %92, %91
  br label %94

94:                                               ; preds = %93, %82
  br label %96

95:                                               ; preds = %1
  br label %96

96:                                               ; preds = %95, %94
  %97 = load i64, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret i64 %97
}

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 = { 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, !10, i64 88}
!12 = !{!"tskTaskControlBlock", !6, i64 0, !13, i64 8, !13, i64 48, !10, i64 88, !6, i64 96, !7, i64 104, !6, i64 128, !10, i64 136, !10, i64 144, !6, i64 152, !7, i64 160, !7, i64 200, !7, i64 212, !7, i64 215, !7, i64 216, !14, i64 220}
!13 = !{!"xLIST_ITEM", !14, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !14, i64 48}
!16 = !{!12, !6, i64 40}
!17 = !{!12, !10, i64 136}


; 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.xLIST = type { i64, ptr, %struct.xMINI_LIST_ITEM }
%struct.xMINI_LIST_ITEM = type { i32, ptr, ptr }

@pxCurrentTCB = external dso_local global ptr, align 8
@pxReadyTasksLists = external hidden global [32 x %struct.xLIST], align 16
@uxTopReadyPriority = external hidden global i64, align 8

declare i64 @uxListRemove(ptr noundef) local_unnamed_addr #0

declare void @vListInsertEnd(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i64 0, 2) i64 @xTaskPriorityInherit(ptr noundef %0) local_unnamed_addr #1 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %46, label %2

2:                                                ; preds = %1
  %3 = getelementptr inbounds i8, ptr %0, i64 88
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !13
  %6 = getelementptr inbounds i8, ptr %5, i64 88
  %7 = load i64, ptr %6, align 8, !tbaa !5
  %8 = icmp ult i64 %4, %7
  br i1 %8, label %9, label %39

9:                                                ; preds = %2
  %10 = getelementptr inbounds i8, ptr %0, i64 48
  %11 = load i32, ptr %10, align 8, !tbaa !14
  %12 = icmp sgt i32 %11, -1
  br i1 %12, label %13, label %19

13:                                               ; preds = %9
  %14 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !13
  %15 = getelementptr inbounds i8, ptr %14, i64 88
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = trunc i64 %16 to i32
  %18 = sub i32 32, %17
  store i32 %18, ptr %10, align 8, !tbaa !14
  br label %19

19:                                               ; preds = %9, %13
  %20 = getelementptr inbounds i8, ptr %0, i64 8
  %21 = getelementptr inbounds i8, ptr %0, i64 40
  %22 = load ptr, ptr %21, align 8, !tbaa !15
  %23 = getelementptr inbounds [32 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %4
  %24 = icmp eq ptr %22, %23
  br i1 %24, label %25, label %35

25:                                               ; preds = %19
  %26 = tail call i64 @uxListRemove(ptr noundef nonnull %20) #2
  %27 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !13
  %28 = getelementptr inbounds i8, ptr %27, i64 88
  %29 = load i64, ptr %28, align 8, !tbaa !5
  store i64 %29, ptr %3, align 8, !tbaa !5
  %30 = load volatile i64, ptr @uxTopReadyPriority, align 8, !tbaa !16
  %31 = icmp ugt i64 %29, %30
  br i1 %31, label %32, label %33

32:                                               ; preds = %25
  store volatile i64 %29, ptr @uxTopReadyPriority, align 8, !tbaa !16
  br label %33

33:                                               ; preds = %32, %25
  %34 = getelementptr inbounds [32 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %29
  tail call void @vListInsertEnd(ptr noundef nonnull %34, ptr noundef nonnull %20) #2
  br label %46

35:                                               ; preds = %19
  %36 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !13
  %37 = getelementptr inbounds i8, ptr %36, i64 88
  %38 = load i64, ptr %37, align 8, !tbaa !5
  store i64 %38, ptr %3, align 8, !tbaa !5
  br label %46

39:                                               ; preds = %2
  %40 = getelementptr inbounds i8, ptr %0, i64 136
  %41 = load i64, ptr %40, align 8, !tbaa !17
  %42 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !13
  %43 = getelementptr inbounds i8, ptr %42, i64 88
  %44 = load i64, ptr %43, align 8, !tbaa !5
  %45 = icmp ult i64 %41, %44
  %spec.select = zext i1 %45 to i64
  br label %46

46:                                               ; preds = %39, %33, %35, %1
  %.0 = phi i64 [ 0, %1 ], [ 1, %35 ], [ 1, %33 ], [ %spec.select, %39 ]
  ret i64 %.0
}

attributes #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 = { 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 = { 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, !12, i64 88}
!6 = !{!"tskTaskControlBlock", !7, i64 0, !10, i64 8, !10, i64 48, !12, i64 88, !7, i64 96, !8, i64 104, !7, i64 128, !12, i64 136, !12, i64 144, !7, i64 152, !8, i64 160, !8, i64 200, !8, i64 212, !8, i64 215, !8, i64 216, !11, i64 220}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"xLIST_ITEM", !11, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32}
!11 = !{!"int", !8, i64 0}
!12 = !{!"long", !8, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!6, !11, i64 48}
!15 = !{!6, !7, i64 40}
!16 = !{!12, !12, i64 0}
!17 = !{!6, !12, i64 136}


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