; 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
@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

declare void @vPortEnterCritical() #2

declare i64 @uxListRemove(ptr noundef) #2

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

declare void @vPortExitCritical() #2

; Function Attrs: nounwind uwtable
declare dso_local void @vTaskSuspendAll() #3

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @eTaskGetState(ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i64 @xTaskAbortDelay(ptr noundef %0) #3 {
  %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) #4
  %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) #4
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %10

8:                                                ; preds = %1
  call void @vPortRaiseBASEPRI()
  br label %9

9:                                                ; preds = %9, %8
  br label %9

10:                                               ; preds = %1
  call void @vTaskSuspendAll()
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = call i32 @eTaskGetState(ptr noundef %11)
  %13 = icmp eq i32 %12, 2
  br i1 %13, label %14, label %57

14:                                               ; preds = %10
  store i64 1, ptr %4, align 8, !tbaa !9
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %15, i32 0, i32 1
  %17 = call i64 @uxListRemove(ptr noundef %16)
  call void @vPortEnterCritical()
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %18, i32 0, i32 2
  %20 = getelementptr inbounds %struct.xLIST_ITEM, ptr %19, i32 0, i32 4
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  %22 = icmp ne ptr %21, null
  br i1 %22, label %23, label %29

23:                                               ; preds = %14
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %24, i32 0, i32 2
  %26 = call i64 @uxListRemove(ptr noundef %25)
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %27, i32 0, i32 14
  store i8 1, ptr %28, align 8, !tbaa !15
  br label %30

29:                                               ; preds = %14
  br label %30

30:                                               ; preds = %29, %23
  call void @vPortExitCritical()
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %31, i32 0, i32 3
  %33 = load i64, ptr %32, align 8, !tbaa !16
  %34 = load volatile i64, ptr @uxTopReadyPriority, align 8, !tbaa !9
  %35 = icmp ugt i64 %33, %34
  br i1 %35, label %36, label %40

36:                                               ; preds = %30
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %37, i32 0, i32 3
  %39 = load i64, ptr %38, align 8, !tbaa !16
  store volatile i64 %39, ptr @uxTopReadyPriority, align 8, !tbaa !9
  br label %40

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

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

55:                                               ; preds = %40
  br label %56

56:                                               ; preds = %55, %54
  br label %58

57:                                               ; preds = %10
  store i64 0, ptr %4, align 8, !tbaa !9
  br label %58

58:                                               ; preds = %57, %56
  %59 = call i64 @xTaskResumeAll()
  %60 = load i64, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret i64 %60
}

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, !6, i64 80}
!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, !7, i64 216}
!16 = !{!12, !10, i64 88}


; 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
@xYieldPending = external hidden global i64, align 8

; 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

declare void @vPortExitCritical() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @vTaskSuspendAll() local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @eTaskGetState(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local range(i64 0, 2) i64 @xTaskAbortDelay(ptr noundef %0) local_unnamed_addr #2 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %3, label %5

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

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

5:                                                ; preds = %1
  tail call void @vTaskSuspendAll()
  %6 = tail call i32 @eTaskGetState(ptr noundef nonnull %0)
  %7 = icmp eq i32 %6, 2
  br i1 %7, label %8, label %31

8:                                                ; preds = %5
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = tail call i64 @uxListRemove(ptr noundef nonnull %9) #3
  tail call void @vPortEnterCritical() #3
  %11 = getelementptr inbounds i8, ptr %0, i64 80
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %.not = icmp eq ptr %12, null
  br i1 %.not, label %17, label %13

13:                                               ; preds = %8
  %14 = getelementptr inbounds i8, ptr %0, i64 48
  %15 = tail call i64 @uxListRemove(ptr noundef nonnull %14) #3
  %16 = getelementptr inbounds i8, ptr %0, i64 216
  store i8 1, ptr %16, align 8, !tbaa !13
  br label %17

17:                                               ; preds = %8, %13
  tail call void @vPortExitCritical() #3
  %18 = getelementptr inbounds i8, ptr %0, i64 88
  %19 = load i64, ptr %18, align 8, !tbaa !14
  %20 = load volatile i64, ptr @uxTopReadyPriority, align 8, !tbaa !15
  %21 = icmp ugt i64 %19, %20
  br i1 %21, label %22, label %23

22:                                               ; preds = %17
  store volatile i64 %19, ptr @uxTopReadyPriority, align 8, !tbaa !15
  br label %23

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

30:                                               ; preds = %23
  store volatile i64 1, ptr @xYieldPending, align 8, !tbaa !15
  br label %31

31:                                               ; preds = %5, %30, %23
  %.0 = phi i64 [ 1, %30 ], [ 1, %23 ], [ 0, %5 ]
  %32 = tail call i64 @xTaskResumeAll()
  ret i64 %.0
}

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, !7, i64 80}
!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 = !{!6, !8, i64 216}
!14 = !{!6, !12, i64 88}
!15 = !{!12, !12, i64 0}
!16 = !{!7, !7, i64 0}


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