; 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.xLIST_ITEM = type { i32, ptr, ptr, 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 }

@pxCurrentTCB = external dso_local global ptr, align 8
@uxCurrentNumberOfTasks = external hidden global i64, align 8
@uxSchedulerSuspended = external hidden global i64, align 8
@pxReadyTasksLists = external hidden global [32 x %struct.xLIST], align 16
@uxTopReadyPriority = external hidden global i64, align 8
@xYieldPending = external hidden global i64, align 8
@xPendingReadyList = external hidden global %struct.xLIST, align 8
@xPendedTicks = external hidden 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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @vPortRaiseBASEPRI() #1

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

declare void @vPortEnterCritical() #2

declare i64 @uxListRemove(ptr noundef) #2

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

; Function Attrs: nounwind uwtable
declare hidden void @prvResetNextTaskUnblockTime() #3

declare void @vPortExitCritical() #2

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

6:                                                ; preds = %0
  call void @vPortRaiseBASEPRI()
  br label %7

7:                                                ; preds = %7, %6
  br label %7

8:                                                ; preds = %0
  call void @vPortEnterCritical()
  %9 = load volatile i64, ptr @uxSchedulerSuspended, align 8, !tbaa !9
  %10 = add i64 %9, -1
  store volatile i64 %10, ptr @uxSchedulerSuspended, align 8, !tbaa !9
  %11 = load volatile i64, ptr @uxSchedulerSuspended, align 8, !tbaa !9
  %12 = icmp eq i64 %11, 0
  br i1 %12, label %13, label %88

13:                                               ; preds = %8
  %14 = load volatile i64, ptr @uxCurrentNumberOfTasks, align 8, !tbaa !9
  %15 = icmp ugt i64 %14, 0
  br i1 %15, label %16, label %87

16:                                               ; preds = %13
  br label %17

17:                                               ; preds = %58, %16
  %18 = load volatile i64, ptr @xPendingReadyList, align 8, !tbaa !11
  %19 = icmp eq i64 %18, 0
  %20 = zext i1 %19 to i64
  %21 = select i1 %19, i64 1, i64 0
  %22 = icmp eq i64 %21, 0
  br i1 %22, label %23, label %59

23:                                               ; preds = %17
  %24 = load ptr, ptr getelementptr inbounds (%struct.xMINI_LIST_ITEM, ptr getelementptr inbounds (%struct.xLIST, ptr @xPendingReadyList, i32 0, i32 2), i32 0, i32 1), align 8, !tbaa !15
  %25 = getelementptr inbounds %struct.xLIST_ITEM, ptr %24, i32 0, i32 3
  %26 = load ptr, ptr %25, align 8, !tbaa !16
  store ptr %26, ptr %1, align 8, !tbaa !5
  %27 = load ptr, ptr %1, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %27, i32 0, i32 2
  %29 = call i64 @uxListRemove(ptr noundef %28)
  %30 = load ptr, ptr %1, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %30, i32 0, i32 1
  %32 = call i64 @uxListRemove(ptr noundef %31)
  %33 = load ptr, ptr %1, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %33, i32 0, i32 3
  %35 = load i64, ptr %34, align 8, !tbaa !18
  %36 = load volatile i64, ptr @uxTopReadyPriority, align 8, !tbaa !9
  %37 = icmp ugt i64 %35, %36
  br i1 %37, label %38, label %42

38:                                               ; preds = %23
  %39 = load ptr, ptr %1, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %39, i32 0, i32 3
  %41 = load i64, ptr %40, align 8, !tbaa !18
  store volatile i64 %41, ptr @uxTopReadyPriority, align 8, !tbaa !9
  br label %42

42:                                               ; preds = %38, %23
  %43 = load ptr, ptr %1, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %43, i32 0, i32 3
  %45 = load i64, ptr %44, align 8, !tbaa !18
  %46 = getelementptr inbounds [32 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %45
  %47 = load ptr, ptr %1, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %47, i32 0, i32 1
  call void @vListInsertEnd(ptr noundef %46, ptr noundef %48)
  %49 = load ptr, ptr %1, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %49, i32 0, i32 3
  %51 = load i64, ptr %50, align 8, !tbaa !18
  %52 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %52, i32 0, i32 3
  %54 = load i64, ptr %53, align 8, !tbaa !18
  %55 = icmp uge i64 %51, %54
  br i1 %55, label %56, label %57

56:                                               ; preds = %42
  store volatile i64 1, ptr @xYieldPending, align 8, !tbaa !9
  br label %58

57:                                               ; preds = %42
  br label %58

58:                                               ; preds = %57, %56
  br label %17, !llvm.loop !20

59:                                               ; preds = %17
  %60 = load ptr, ptr %1, align 8, !tbaa !5
  %61 = icmp ne ptr %60, null
  br i1 %61, label %62, label %63

62:                                               ; preds = %59
  call void @prvResetNextTaskUnblockTime()
  br label %63

63:                                               ; preds = %62, %59
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  %64 = load volatile i32, ptr @xPendedTicks, align 4, !tbaa !22
  store i32 %64, ptr %3, align 4, !tbaa !22
  %65 = load i32, ptr %3, align 4, !tbaa !22
  %66 = icmp ugt i32 %65, 0
  br i1 %66, label %67, label %80

67:                                               ; preds = %63
  br label %68

68:                                               ; preds = %76, %67
  %69 = call i64 @xTaskIncrementTick()
  %70 = icmp ne i64 %69, 0
  br i1 %70, label %71, label %72

71:                                               ; preds = %68
  store volatile i64 1, ptr @xYieldPending, align 8, !tbaa !9
  br label %73

72:                                               ; preds = %68
  br label %73

73:                                               ; preds = %72, %71
  %74 = load i32, ptr %3, align 4, !tbaa !22
  %75 = add i32 %74, -1
  store i32 %75, ptr %3, align 4, !tbaa !22
  br label %76

76:                                               ; preds = %73
  %77 = load i32, ptr %3, align 4, !tbaa !22
  %78 = icmp ugt i32 %77, 0
  br i1 %78, label %68, label %79, !llvm.loop !23

79:                                               ; preds = %76
  store volatile i32 0, ptr @xPendedTicks, align 4, !tbaa !22
  br label %81

80:                                               ; preds = %63
  br label %81

81:                                               ; preds = %80, %79
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  %82 = load volatile i64, ptr @xYieldPending, align 8, !tbaa !9
  %83 = icmp ne i64 %82, 0
  br i1 %83, label %84, label %85

84:                                               ; preds = %81
  store i64 1, ptr %2, align 8, !tbaa !9
  store volatile i32 268435456, ptr inttoptr (i64 3758157060 to ptr), align 4, !tbaa !22
  call void asm sideeffect "dsb", "~{memory},~{dirflag},~{fpsr},~{flags}"() #4, !srcloc !24
  call void asm sideeffect "isb", "~{dirflag},~{fpsr},~{flags}"() #4, !srcloc !25
  br label %86

85:                                               ; preds = %81
  br label %86

86:                                               ; preds = %85, %84
  br label %87

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

88:                                               ; preds = %8
  br label %89

89:                                               ; preds = %88, %87
  call void @vPortExitCritical()
  %90 = load i64, ptr %2, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #4
  ret i64 %90
}

; Function Attrs: nounwind uwtable
declare dso_local i64 @xTaskIncrementTick() #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { alwaysinline 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 = { 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 #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, !10, i64 0}
!12 = !{!"xLIST", !10, i64 0, !6, i64 8, !13, i64 16}
!13 = !{!"xMINI_LIST_ITEM", !14, i64 0, !6, i64 8, !6, i64 16}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !6, i64 24}
!16 = !{!17, !6, i64 24}
!17 = !{!"xLIST_ITEM", !14, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!18 = !{!19, !10, i64 88}
!19 = !{!"tskTaskControlBlock", !6, i64 0, !17, i64 8, !17, 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}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!14, !14, i64 0}
!23 = distinct !{!23, !21}
!24 = !{i64 2148514765}
!25 = !{i64 2148514835}


; 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
@uxCurrentNumberOfTasks = external hidden global i64, align 8
@uxSchedulerSuspended = external hidden global i64, align 8
@pxReadyTasksLists = external hidden global [32 x %struct.xLIST], align 16
@uxTopReadyPriority = external hidden global i64, align 8
@xYieldPending = external hidden global i64, align 8
@xPendingReadyList = external hidden global %struct.xLIST, align 8
@xPendedTicks = external hidden global i32, align 4

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @vPortRaiseBASEPRI() local_unnamed_addr #0

declare void @vPortEnterCritical() local_unnamed_addr #1

declare i64 @uxListRemove(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
declare hidden void @prvResetNextTaskUnblockTime() local_unnamed_addr #2

declare void @vPortExitCritical() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i64 0, 2) i64 @xTaskResumeAll() local_unnamed_addr #2 {
  %1 = load volatile i64, ptr @uxSchedulerSuspended, align 8, !tbaa !5
  %2 = icmp eq i64 %1, 0
  br i1 %2, label %3, label %5

3:                                                ; preds = %0
  tail call void @vPortRaiseBASEPRI()
  br label %4

4:                                                ; preds = %4, %3
  br label %4

5:                                                ; preds = %0
  tail call void @vPortEnterCritical() #3
  %6 = load volatile i64, ptr @uxSchedulerSuspended, align 8, !tbaa !5
  %7 = add i64 %6, -1
  store volatile i64 %7, ptr @uxSchedulerSuspended, align 8, !tbaa !5
  %8 = load volatile i64, ptr @uxSchedulerSuspended, align 8, !tbaa !5
  %9 = icmp eq i64 %8, 0
  br i1 %9, label %10, label %43

10:                                               ; preds = %5
  %11 = load volatile i64, ptr @uxCurrentNumberOfTasks, align 8, !tbaa !5
  %.not = icmp eq i64 %11, 0
  br i1 %.not, label %43, label %.preheader22

.preheader22:                                     ; preds = %10
  %12 = load volatile i64, ptr @xPendingReadyList, align 8, !tbaa !9
  %.not1523 = icmp eq i64 %12, 0
  br i1 %.not1523, label %._crit_edge.thread, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader22, %32
  %13 = load ptr, ptr getelementptr inbounds (i8, ptr @xPendingReadyList, i64 24), align 8, !tbaa !14
  %14 = getelementptr inbounds i8, ptr %13, i64 24
  %15 = load ptr, ptr %14, align 8, !tbaa !15
  %16 = getelementptr inbounds i8, ptr %15, i64 48
  %17 = tail call i64 @uxListRemove(ptr noundef nonnull %16) #3
  %18 = getelementptr inbounds i8, ptr %15, i64 8
  %19 = tail call i64 @uxListRemove(ptr noundef nonnull %18) #3
  %20 = getelementptr inbounds i8, ptr %15, i64 88
  %21 = load i64, ptr %20, align 8, !tbaa !17
  %22 = load volatile i64, ptr @uxTopReadyPriority, align 8, !tbaa !5
  %23 = icmp ugt i64 %21, %22
  br i1 %23, label %24, label %25

24:                                               ; preds = %.lr.ph
  store volatile i64 %21, ptr @uxTopReadyPriority, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %24, %.lr.ph
  %26 = getelementptr inbounds [32 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %21
  tail call void @vListInsertEnd(ptr noundef nonnull %26, ptr noundef nonnull %18) #3
  %27 = load i64, ptr %20, align 8, !tbaa !17
  %28 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !19
  %29 = getelementptr inbounds i8, ptr %28, i64 88
  %30 = load i64, ptr %29, align 8, !tbaa !17
  %.not21 = icmp ult i64 %27, %30
  br i1 %.not21, label %32, label %31

31:                                               ; preds = %25
  store volatile i64 1, ptr @xYieldPending, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %25, %31
  %33 = load volatile i64, ptr @xPendingReadyList, align 8, !tbaa !9
  %.not15 = icmp eq i64 %33, 0
  br i1 %.not15, label %._crit_edge, label %.lr.ph, !llvm.loop !20

._crit_edge:                                      ; preds = %32
  tail call void @prvResetNextTaskUnblockTime()
  br label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %.preheader22, %._crit_edge
  %34 = load volatile i32, ptr @xPendedTicks, align 4, !tbaa !22
  %.not17 = icmp eq i32 %34, 0
  br i1 %.not17, label %40, label %.preheader

.preheader:                                       ; preds = %._crit_edge.thread, %37
  %.0 = phi i32 [ %38, %37 ], [ %34, %._crit_edge.thread ]
  %35 = tail call i64 @xTaskIncrementTick()
  %.not18 = icmp eq i64 %35, 0
  br i1 %.not18, label %37, label %36

36:                                               ; preds = %.preheader
  store volatile i64 1, ptr @xYieldPending, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %.preheader, %36
  %38 = add i32 %.0, -1
  %.not19 = icmp eq i32 %38, 0
  br i1 %.not19, label %39, label %.preheader, !llvm.loop !23

39:                                               ; preds = %37
  store volatile i32 0, ptr @xPendedTicks, align 4, !tbaa !22
  br label %40

40:                                               ; preds = %._crit_edge.thread, %39
  %41 = load volatile i64, ptr @xYieldPending, align 8, !tbaa !5
  %.not20 = icmp eq i64 %41, 0
  br i1 %.not20, label %43, label %42

42:                                               ; preds = %40
  store volatile i32 268435456, ptr inttoptr (i64 3758157060 to ptr), align 4, !tbaa !22
  tail call void asm sideeffect "dsb", "~{memory},~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !24
  tail call void asm sideeffect "isb", "~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !25
  br label %43

43:                                               ; preds = %5, %10, %40, %42
  %.011 = phi i64 [ 1, %42 ], [ 0, %40 ], [ 0, %10 ], [ 0, %5 ]
  tail call void @vPortExitCritical() #3
  ret i64 %.011
}

; Function Attrs: nounwind uwtable
declare dso_local i64 @xTaskIncrementTick() local_unnamed_addr #2

attributes #0 = { alwaysinline 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 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"xLIST", !6, i64 0, !11, i64 8, !12, i64 16}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"xMINI_LIST_ITEM", !13, i64 0, !11, i64 8, !11, i64 16}
!13 = !{!"int", !7, i64 0}
!14 = !{!10, !11, i64 24}
!15 = !{!16, !11, i64 24}
!16 = !{!"xLIST_ITEM", !13, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32}
!17 = !{!18, !6, i64 88}
!18 = !{!"tskTaskControlBlock", !11, i64 0, !16, i64 8, !16, i64 48, !6, i64 88, !11, i64 96, !7, i64 104, !11, i64 128, !6, i64 136, !6, i64 144, !11, i64 152, !7, i64 160, !7, i64 200, !7, i64 212, !7, i64 215, !7, i64 216, !13, i64 220}
!19 = !{!11, !11, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!13, !13, i64 0}
!23 = distinct !{!23, !21}
!24 = !{i64 2148514765}
!25 = !{i64 2148514835}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass