; 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
@FreeRTOS_errno = external dso_local global i32, align 4
@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

; 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

; Function Attrs: nounwind uwtable
define dso_local void @vTaskSwitchContext() #2 {
  %1 = alloca i64, align 8
  %2 = alloca ptr, align 8
  %3 = load volatile i64, ptr @uxSchedulerSuspended, align 8, !tbaa !5
  %4 = icmp ne i64 %3, 0
  br i1 %4, label %5, label %6

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

6:                                                ; preds = %0
  store volatile i64 0, ptr @xYieldPending, align 8, !tbaa !5
  %7 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !9
  %8 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %7, i32 0, i32 0
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !9
  %11 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %10, i32 0, i32 4
  %12 = load ptr, ptr %11, align 8, !tbaa !15
  %13 = icmp ule ptr %9, %12
  br i1 %13, label %14, label %19

14:                                               ; preds = %6
  %15 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !9
  %16 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %16, i32 0, i32 5
  %18 = getelementptr inbounds [24 x i8], ptr %17, i64 0, i64 0
  call void @vApplicationStackOverflowHook(ptr noundef %15, ptr noundef %18)
  br label %19

19:                                               ; preds = %14, %6
  %20 = load i32, ptr @FreeRTOS_errno, align 4, !tbaa !16
  %21 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !9
  %22 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %21, i32 0, i32 15
  store i32 %20, ptr %22, align 4, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #4
  %23 = load volatile i64, ptr @uxTopReadyPriority, align 8, !tbaa !5
  store i64 %23, ptr %1, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %38, %19
  %25 = load i64, ptr %1, align 8, !tbaa !5
  %26 = getelementptr inbounds [32 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %25
  %27 = getelementptr inbounds %struct.xLIST, ptr %26, i32 0, i32 0
  %28 = load volatile i64, ptr %27, align 8, !tbaa !18
  %29 = icmp eq i64 %28, 0
  %30 = zext i1 %29 to i64
  %31 = select i1 %29, i64 1, i64 0
  %32 = icmp ne i64 %31, 0
  br i1 %32, label %33, label %41

33:                                               ; preds = %24
  %34 = load i64, ptr %1, align 8, !tbaa !5
  %35 = icmp eq i64 %34, 0
  br i1 %35, label %36, label %38

36:                                               ; preds = %33
  call void @vPortRaiseBASEPRI()
  br label %37

37:                                               ; preds = %37, %36
  br label %37

38:                                               ; preds = %33
  %39 = load i64, ptr %1, align 8, !tbaa !5
  %40 = add i64 %39, -1
  store i64 %40, ptr %1, align 8, !tbaa !5
  br label %24, !llvm.loop !21

41:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  %42 = load i64, ptr %1, align 8, !tbaa !5
  %43 = getelementptr inbounds [32 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %42
  store ptr %43, ptr %2, align 8, !tbaa !9
  %44 = load ptr, ptr %2, align 8, !tbaa !9
  %45 = getelementptr inbounds %struct.xLIST, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !23
  %47 = getelementptr inbounds %struct.xLIST_ITEM, ptr %46, i32 0, i32 1
  %48 = load ptr, ptr %47, align 8, !tbaa !24
  %49 = load ptr, ptr %2, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.xLIST, ptr %49, i32 0, i32 1
  store ptr %48, ptr %50, align 8, !tbaa !23
  %51 = load ptr, ptr %2, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.xLIST, ptr %51, i32 0, i32 1
  %53 = load ptr, ptr %52, align 8, !tbaa !23
  %54 = load ptr, ptr %2, align 8, !tbaa !9
  %55 = getelementptr inbounds %struct.xLIST, ptr %54, i32 0, i32 2
  %56 = icmp eq ptr %53, %55
  br i1 %56, label %57, label %65

57:                                               ; preds = %41
  %58 = load ptr, ptr %2, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.xLIST, ptr %58, i32 0, i32 1
  %60 = load ptr, ptr %59, align 8, !tbaa !23
  %61 = getelementptr inbounds %struct.xLIST_ITEM, ptr %60, i32 0, i32 1
  %62 = load ptr, ptr %61, align 8, !tbaa !24
  %63 = load ptr, ptr %2, align 8, !tbaa !9
  %64 = getelementptr inbounds %struct.xLIST, ptr %63, i32 0, i32 1
  store ptr %62, ptr %64, align 8, !tbaa !23
  br label %65

65:                                               ; preds = %57, %41
  %66 = load ptr, ptr %2, align 8, !tbaa !9
  %67 = getelementptr inbounds %struct.xLIST, ptr %66, i32 0, i32 1
  %68 = load ptr, ptr %67, align 8, !tbaa !23
  %69 = getelementptr inbounds %struct.xLIST_ITEM, ptr %68, i32 0, i32 3
  %70 = load ptr, ptr %69, align 8, !tbaa !25
  store volatile ptr %70, ptr @pxCurrentTCB, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  %71 = load i64, ptr %1, align 8, !tbaa !5
  store volatile i64 %71, ptr @uxTopReadyPriority, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #4
  %72 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !9
  %73 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %72, i32 0, i32 15
  %74 = load i32, ptr %73, align 4, !tbaa !17
  store i32 %74, ptr @FreeRTOS_errno, align 4, !tbaa !16
  br label %75

75:                                               ; preds = %65, %5
  ret void
}

declare void @vApplicationStackOverflowHook(ptr noundef, ptr noundef) #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 = { 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 = { "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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"tskTaskControlBlock", !10, i64 0, !13, i64 8, !13, i64 48, !6, i64 88, !10, i64 96, !7, i64 104, !10, i64 128, !6, i64 136, !6, i64 144, !10, 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, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !10, i64 96}
!16 = !{!14, !14, i64 0}
!17 = !{!12, !14, i64 220}
!18 = !{!19, !6, i64 0}
!19 = !{!"xLIST", !6, i64 0, !10, i64 8, !20, i64 16}
!20 = !{!"xMINI_LIST_ITEM", !14, i64 0, !10, i64 8, !10, i64 16}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!19, !10, i64 8}
!24 = !{!13, !10, i64 8}
!25 = !{!13, !10, i64 24}


; 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
@FreeRTOS_errno = external dso_local local_unnamed_addr global i32, align 4
@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

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

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

2:                                                ; preds = %0
  store volatile i64 1, ptr @xYieldPending, align 8, !tbaa !5
  br label %45

3:                                                ; preds = %0
  store volatile i64 0, ptr @xYieldPending, align 8, !tbaa !5
  %4 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !9
  %5 = load ptr, ptr %4, align 8, !tbaa !11
  %6 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !9
  %7 = getelementptr inbounds i8, ptr %6, i64 96
  %8 = load ptr, ptr %7, align 8, !tbaa !15
  %.not12 = icmp ugt ptr %5, %8
  br i1 %.not12, label %13, label %9

9:                                                ; preds = %3
  %10 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !9
  %11 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !9
  %12 = getelementptr inbounds i8, ptr %11, i64 104
  tail call void @vApplicationStackOverflowHook(ptr noundef %10, ptr noundef nonnull %12) #3
  br label %13

13:                                               ; preds = %9, %3
  %14 = load i32, ptr @FreeRTOS_errno, align 4, !tbaa !16
  %15 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !9
  %16 = getelementptr inbounds i8, ptr %15, i64 220
  store i32 %14, ptr %16, align 4, !tbaa !17
  %17 = load volatile i64, ptr @uxTopReadyPriority, align 8, !tbaa !5
  %18 = getelementptr inbounds [32 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %17
  %19 = load volatile i64, ptr %18, align 8, !tbaa !18
  %20 = icmp eq i64 %19, 0
  br i1 %20, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %13, %24
  %.015 = phi i64 [ %25, %24 ], [ %17, %13 ]
  %21 = icmp eq i64 %.015, 0
  br i1 %21, label %22, label %24

22:                                               ; preds = %.lr.ph
  tail call void @vPortRaiseBASEPRI()
  br label %23

23:                                               ; preds = %23, %22
  br label %23

24:                                               ; preds = %.lr.ph
  %25 = add i64 %.015, -1
  %26 = getelementptr inbounds [32 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %25
  %27 = load volatile i64, ptr %26, align 8, !tbaa !18
  %28 = icmp eq i64 %27, 0
  br i1 %28, label %.lr.ph, label %._crit_edge, !llvm.loop !21

._crit_edge:                                      ; preds = %24, %13
  %.0.lcssa = phi i64 [ %17, %13 ], [ %25, %24 ]
  %.lcssa = phi ptr [ %18, %13 ], [ %26, %24 ]
  %29 = getelementptr inbounds i8, ptr %.lcssa, i64 8
  %30 = load ptr, ptr %29, align 8, !tbaa !23
  %31 = getelementptr inbounds i8, ptr %30, i64 8
  %32 = load ptr, ptr %31, align 8, !tbaa !24
  store ptr %32, ptr %29, align 8, !tbaa !23
  %33 = getelementptr inbounds i8, ptr %.lcssa, i64 16
  %34 = icmp eq ptr %32, %33
  br i1 %34, label %35, label %38

35:                                               ; preds = %._crit_edge
  %36 = getelementptr inbounds i8, ptr %32, i64 8
  %37 = load ptr, ptr %36, align 8, !tbaa !24
  store ptr %37, ptr %29, align 8, !tbaa !23
  br label %38

38:                                               ; preds = %35, %._crit_edge
  %39 = phi ptr [ %37, %35 ], [ %32, %._crit_edge ]
  %40 = getelementptr inbounds i8, ptr %39, i64 24
  %41 = load ptr, ptr %40, align 8, !tbaa !25
  store volatile ptr %41, ptr @pxCurrentTCB, align 8, !tbaa !9
  store volatile i64 %.0.lcssa, ptr @uxTopReadyPriority, align 8, !tbaa !5
  %42 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !9
  %43 = getelementptr inbounds i8, ptr %42, i64 220
  %44 = load i32, ptr %43, align 4, !tbaa !17
  store i32 %44, ptr @FreeRTOS_errno, align 4, !tbaa !16
  br label %45

45:                                               ; preds = %38, %2
  ret void
}

declare void @vApplicationStackOverflowHook(ptr noundef, ptr noundef) 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 = { 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 }

!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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"tskTaskControlBlock", !10, i64 0, !13, i64 8, !13, i64 48, !6, i64 88, !10, i64 96, !7, i64 104, !10, i64 128, !6, i64 136, !6, i64 144, !10, 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, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !10, i64 96}
!16 = !{!14, !14, i64 0}
!17 = !{!12, !14, i64 220}
!18 = !{!19, !6, i64 0}
!19 = !{!"xLIST", !6, i64 0, !10, i64 8, !20, i64 16}
!20 = !{!"xMINI_LIST_ITEM", !14, i64 0, !10, i64 8, !10, i64 16}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!19, !10, i64 8}
!24 = !{!13, !10, i64 8}
!25 = !{!13, !10, i64 24}


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