; 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
@uxCurrentNumberOfTasks = external hidden global i64, align 8
@xSchedulerRunning = external hidden global i64, align 8
@uxSchedulerSuspended = external hidden global i64, align 8
@xSuspendedTaskList = external hidden global %struct.xLIST, 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

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

declare void @vPortExitCritical() #2

; Function Attrs: nounwind uwtable
define dso_local void @vTaskSuspend(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
  call void @vPortEnterCritical()
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = icmp eq ptr %5, null
  br i1 %6, label %7, label %9

7:                                                ; preds = %1
  %8 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  br label %11

9:                                                ; preds = %1
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %9, %7
  %12 = phi ptr [ %8, %7 ], [ %10, %9 ]
  store ptr %12, ptr %3, align 8, !tbaa !5
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %13, i32 0, i32 1
  %15 = call i64 @uxListRemove(ptr noundef %14)
  %16 = icmp eq i64 %15, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %11
  br label %19

18:                                               ; preds = %11
  br label %19

19:                                               ; preds = %18, %17
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %20, i32 0, i32 2
  %22 = getelementptr inbounds %struct.xLIST_ITEM, ptr %21, i32 0, i32 4
  %23 = load ptr, ptr %22, align 8, !tbaa !9
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %29

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

29:                                               ; preds = %19
  br label %30

30:                                               ; preds = %29, %25
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %31, i32 0, i32 1
  call void @vListInsertEnd(ptr noundef @xSuspendedTaskList, ptr noundef %32)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  store i64 0, ptr %4, align 8, !tbaa !14
  br label %33

33:                                               ; preds = %50, %30
  %34 = load i64, ptr %4, align 8, !tbaa !14
  %35 = icmp slt i64 %34, 3
  br i1 %35, label %36, label %53

36:                                               ; preds = %33
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %37, i32 0, i32 12
  %39 = load i64, ptr %4, align 8, !tbaa !14
  %40 = getelementptr inbounds [3 x i8], ptr %38, i64 0, i64 %39
  %41 = load volatile i8, ptr %40, align 1, !tbaa !15
  %42 = zext i8 %41 to i32
  %43 = icmp eq i32 %42, 1
  br i1 %43, label %44, label %49

44:                                               ; preds = %36
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %45, i32 0, i32 12
  %47 = load i64, ptr %4, align 8, !tbaa !14
  %48 = getelementptr inbounds [3 x i8], ptr %46, i64 0, i64 %47
  store volatile i8 0, ptr %48, align 1, !tbaa !15
  br label %49

49:                                               ; preds = %44, %36
  br label %50

50:                                               ; preds = %49
  %51 = load i64, ptr %4, align 8, !tbaa !14
  %52 = add nsw i64 %51, 1
  store i64 %52, ptr %4, align 8, !tbaa !14
  br label %33, !llvm.loop !16

53:                                               ; preds = %33
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @vPortExitCritical()
  %54 = load volatile i64, ptr @xSchedulerRunning, align 8, !tbaa !14
  %55 = icmp ne i64 %54, 0
  br i1 %55, label %56, label %57

56:                                               ; preds = %53
  call void @vPortEnterCritical()
  call void @prvResetNextTaskUnblockTime()
  call void @vPortExitCritical()
  br label %58

57:                                               ; preds = %53
  br label %58

58:                                               ; preds = %57, %56
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %61 = icmp eq ptr %59, %60
  br i1 %61, label %62, label %81

62:                                               ; preds = %58
  %63 = load volatile i64, ptr @xSchedulerRunning, align 8, !tbaa !14
  %64 = icmp ne i64 %63, 0
  br i1 %64, label %65, label %73

65:                                               ; preds = %62
  %66 = load volatile i64, ptr @uxSchedulerSuspended, align 8, !tbaa !14
  %67 = icmp eq i64 %66, 0
  %68 = zext i1 %67 to i32
  %69 = icmp eq i32 %68, 0
  br i1 %69, label %70, label %72

70:                                               ; preds = %65
  call void @vPortRaiseBASEPRI()
  br label %71

71:                                               ; preds = %71, %70
  br label %71

72:                                               ; preds = %65
  store volatile i32 268435456, ptr inttoptr (i64 3758157060 to ptr), align 4, !tbaa !18
  call void asm sideeffect "dsb", "~{memory},~{dirflag},~{fpsr},~{flags}"() #4, !srcloc !19
  call void asm sideeffect "isb", "~{dirflag},~{fpsr},~{flags}"() #4, !srcloc !20
  br label %80

73:                                               ; preds = %62
  %74 = load volatile i64, ptr @xSuspendedTaskList, align 8, !tbaa !21
  %75 = load volatile i64, ptr @uxCurrentNumberOfTasks, align 8, !tbaa !14
  %76 = icmp eq i64 %74, %75
  br i1 %76, label %77, label %78

77:                                               ; preds = %73
  store volatile ptr null, ptr @pxCurrentTCB, align 8, !tbaa !5
  br label %79

78:                                               ; preds = %73
  call void @vTaskSwitchContext()
  br label %79

79:                                               ; preds = %78, %77
  br label %80

80:                                               ; preds = %79, %72
  br label %82

81:                                               ; preds = %58
  br label %82

82:                                               ; preds = %81, %80
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local void @vTaskSwitchContext() #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, !6, i64 80}
!10 = !{!"tskTaskControlBlock", !6, i64 0, !11, i64 8, !11, i64 48, !13, i64 88, !6, i64 96, !7, i64 104, !6, i64 128, !13, i64 136, !13, i64 144, !6, i64 152, !7, i64 160, !7, i64 200, !7, i64 212, !7, i64 215, !7, i64 216, !12, i64 220}
!11 = !{!"xLIST_ITEM", !12, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!12, !12, i64 0}
!19 = !{i64 2148508956}
!20 = !{i64 2148509026}
!21 = !{!22, !13, i64 0}
!22 = !{!"xLIST", !13, i64 0, !6, i64 8, !23, i64 16}
!23 = !{!"xMINI_LIST_ITEM", !12, i64 0, !6, i64 8, !6, i64 16}


; 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
@xSchedulerRunning = external hidden global i64, align 8
@uxSchedulerSuspended = external hidden global i64, align 8
@xSuspendedTaskList = external hidden global %struct.xLIST, 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

; 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 void @vTaskSuspend(ptr noundef %0) local_unnamed_addr #2 {
  tail call void @vPortEnterCritical() #3
  %2 = icmp eq ptr %0, null
  br i1 %2, label %3, label %5

3:                                                ; preds = %1
  %4 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  br label %5

5:                                                ; preds = %1, %3
  %6 = phi ptr [ %4, %3 ], [ %0, %1 ]
  %7 = getelementptr inbounds i8, ptr %6, i64 8
  %8 = tail call i64 @uxListRemove(ptr noundef nonnull %7) #3
  %9 = getelementptr inbounds i8, ptr %6, i64 80
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %.not = icmp eq ptr %10, null
  br i1 %.not, label %14, label %11

11:                                               ; preds = %5
  %12 = getelementptr inbounds i8, ptr %6, i64 48
  %13 = tail call i64 @uxListRemove(ptr noundef nonnull %12) #3
  br label %14

14:                                               ; preds = %5, %11
  tail call void @vListInsertEnd(ptr noundef nonnull @xSuspendedTaskList, ptr noundef nonnull %7) #3
  %15 = getelementptr inbounds i8, ptr %6, i64 212
  %16 = load volatile i8, ptr %15, align 1, !tbaa !14
  %17 = icmp eq i8 %16, 1
  br i1 %17, label %18, label %19

18:                                               ; preds = %14
  store volatile i8 0, ptr %15, align 1, !tbaa !14
  br label %19

19:                                               ; preds = %14, %18
  %20 = getelementptr inbounds i8, ptr %6, i64 213
  %21 = load volatile i8, ptr %20, align 1, !tbaa !14
  %22 = icmp eq i8 %21, 1
  br i1 %22, label %23, label %24

23:                                               ; preds = %19
  store volatile i8 0, ptr %20, align 1, !tbaa !14
  br label %24

24:                                               ; preds = %23, %19
  %25 = getelementptr inbounds i8, ptr %6, i64 214
  %26 = load volatile i8, ptr %25, align 1, !tbaa !14
  %27 = icmp eq i8 %26, 1
  br i1 %27, label %28, label %29

28:                                               ; preds = %24
  store volatile i8 0, ptr %25, align 1, !tbaa !14
  br label %29

29:                                               ; preds = %28, %24
  tail call void @vPortExitCritical() #3
  %30 = load volatile i64, ptr @xSchedulerRunning, align 8, !tbaa !15
  %.not13 = icmp eq i64 %30, 0
  br i1 %.not13, label %32, label %31

31:                                               ; preds = %29
  tail call void @vPortEnterCritical() #3
  tail call void @prvResetNextTaskUnblockTime()
  tail call void @vPortExitCritical() #3
  br label %32

32:                                               ; preds = %29, %31
  %33 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %34 = icmp eq ptr %6, %33
  br i1 %34, label %35, label %48

35:                                               ; preds = %32
  %36 = load volatile i64, ptr @xSchedulerRunning, align 8, !tbaa !15
  %.not14 = icmp eq i64 %36, 0
  br i1 %.not14, label %42, label %37

37:                                               ; preds = %35
  %38 = load volatile i64, ptr @uxSchedulerSuspended, align 8, !tbaa !15
  %.not15 = icmp eq i64 %38, 0
  br i1 %.not15, label %41, label %39

39:                                               ; preds = %37
  tail call void @vPortRaiseBASEPRI()
  br label %40

40:                                               ; preds = %40, %39
  br label %40

41:                                               ; preds = %37
  store volatile i32 268435456, ptr inttoptr (i64 3758157060 to ptr), align 4, !tbaa !16
  tail call void asm sideeffect "dsb", "~{memory},~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !17
  tail call void asm sideeffect "isb", "~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !18
  br label %48

42:                                               ; preds = %35
  %43 = load volatile i64, ptr @xSuspendedTaskList, align 8, !tbaa !19
  %44 = load volatile i64, ptr @uxCurrentNumberOfTasks, align 8, !tbaa !15
  %45 = icmp eq i64 %43, %44
  br i1 %45, label %46, label %47

46:                                               ; preds = %42
  store volatile ptr null, ptr @pxCurrentTCB, align 8, !tbaa !5
  br label %48

47:                                               ; preds = %42
  tail call void @vTaskSwitchContext()
  br label %48

48:                                               ; preds = %32, %41, %47, %46
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local void @vTaskSwitchContext() 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 80}
!10 = !{!"tskTaskControlBlock", !6, i64 0, !11, i64 8, !11, i64 48, !13, i64 88, !6, i64 96, !7, i64 104, !6, i64 128, !13, i64 136, !13, i64 144, !6, i64 152, !7, i64 160, !7, i64 200, !7, i64 212, !7, i64 215, !7, i64 216, !12, i64 220}
!11 = !{!"xLIST_ITEM", !12, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!13, !13, i64 0}
!16 = !{!12, !12, i64 0}
!17 = !{i64 2148508956}
!18 = !{i64 2148509026}
!19 = !{!20, !13, i64 0}
!20 = !{!"xLIST", !13, i64 0, !6, i64 8, !21, i64 16}
!21 = !{!"xMINI_LIST_ITEM", !12, i64 0, !6, i64 8, !6, i64 16}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,SROAPass,SimplifyCFGPass,TailCallElimPass