; 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
@uxTaskNumber = external hidden global i64, align 8
@xTasksWaitingTermination = external hidden global %struct.xLIST, align 8
@uxDeletedTasksWaitingCleanUp = external hidden global i64, align 8
@uxCurrentNumberOfTasks = external hidden global i64, align 8
@xSchedulerRunning = external hidden global i64, align 8
@uxSchedulerSuspended = 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 @vTaskDelete(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, 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()
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %5 = icmp eq ptr %4, null
  br i1 %5, label %6, label %8

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

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

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

16:                                               ; preds = %10
  br label %18

17:                                               ; preds = %10
  br label %18

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

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

28:                                               ; preds = %18
  br label %29

29:                                               ; preds = %28, %24
  %30 = load i64, ptr @uxTaskNumber, align 8, !tbaa !14
  %31 = add i64 %30, 1
  store i64 %31, ptr @uxTaskNumber, align 8, !tbaa !14
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %34 = icmp eq ptr %32, %33
  br i1 %34, label %35, label %40

35:                                               ; preds = %29
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %36, i32 0, i32 1
  call void @vListInsertEnd(ptr noundef @xTasksWaitingTermination, ptr noundef %37)
  %38 = load volatile i64, ptr @uxDeletedTasksWaitingCleanUp, align 8, !tbaa !14
  %39 = add i64 %38, 1
  store volatile i64 %39, ptr @uxDeletedTasksWaitingCleanUp, align 8, !tbaa !14
  br label %44

40:                                               ; preds = %29
  %41 = load volatile i64, ptr @uxCurrentNumberOfTasks, align 8, !tbaa !14
  %42 = add i64 %41, -1
  store volatile i64 %42, ptr @uxCurrentNumberOfTasks, align 8, !tbaa !14
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  call void @prvDeleteTCB(ptr noundef %43)
  call void @prvResetNextTaskUnblockTime()
  br label %44

44:                                               ; preds = %40, %35
  call void @vPortExitCritical()
  %45 = load volatile i64, ptr @xSchedulerRunning, align 8, !tbaa !14
  %46 = icmp ne i64 %45, 0
  br i1 %46, label %47, label %61

47:                                               ; preds = %44
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %50 = icmp eq ptr %48, %49
  br i1 %50, label %51, label %59

51:                                               ; preds = %47
  %52 = load volatile i64, ptr @uxSchedulerSuspended, align 8, !tbaa !14
  %53 = icmp eq i64 %52, 0
  %54 = zext i1 %53 to i32
  %55 = icmp eq i32 %54, 0
  br i1 %55, label %56, label %58

56:                                               ; preds = %51
  call void @vPortRaiseBASEPRI()
  br label %57

57:                                               ; preds = %57, %56
  br label %57

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

59:                                               ; preds = %47
  br label %60

60:                                               ; preds = %59, %58
  br label %61

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

declare void @vPortEnterCritical() #3

declare i64 @uxListRemove(ptr noundef) #3

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

; Function Attrs: nounwind uwtable
declare hidden void @prvDeleteTCB(ptr noundef) #2

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

declare void @vPortExitCritical() #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 = !{!"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 = !{!12, !12, i64 0}
!16 = !{i64 2148503016}
!17 = !{i64 2148503086}


; 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
@uxTaskNumber = external hidden local_unnamed_addr global i64, align 8
@xTasksWaitingTermination = external hidden global %struct.xLIST, align 8
@uxDeletedTasksWaitingCleanUp = external hidden global i64, align 8
@uxCurrentNumberOfTasks = external hidden global i64, align 8
@xSchedulerRunning = external hidden global i64, align 8
@uxSchedulerSuspended = 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 @vTaskDelete(ptr noundef %0) local_unnamed_addr #1 {
  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
  %15 = load i64, ptr @uxTaskNumber, align 8, !tbaa !14
  %16 = add i64 %15, 1
  store i64 %16, ptr @uxTaskNumber, align 8, !tbaa !14
  %17 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %18 = icmp eq ptr %6, %17
  br i1 %18, label %19, label %22

19:                                               ; preds = %14
  tail call void @vListInsertEnd(ptr noundef nonnull @xTasksWaitingTermination, ptr noundef nonnull %7) #3
  %20 = load volatile i64, ptr @uxDeletedTasksWaitingCleanUp, align 8, !tbaa !14
  %21 = add i64 %20, 1
  store volatile i64 %21, ptr @uxDeletedTasksWaitingCleanUp, align 8, !tbaa !14
  br label %25

22:                                               ; preds = %14
  %23 = load volatile i64, ptr @uxCurrentNumberOfTasks, align 8, !tbaa !14
  %24 = add i64 %23, -1
  store volatile i64 %24, ptr @uxCurrentNumberOfTasks, align 8, !tbaa !14
  tail call void @prvDeleteTCB(ptr noundef nonnull %6)
  tail call void @prvResetNextTaskUnblockTime()
  br label %25

25:                                               ; preds = %22, %19
  tail call void @vPortExitCritical() #3
  %26 = load volatile i64, ptr @xSchedulerRunning, align 8, !tbaa !14
  %.not10 = icmp eq i64 %26, 0
  br i1 %.not10, label %35, label %27

27:                                               ; preds = %25
  %28 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %29 = icmp eq ptr %6, %28
  br i1 %29, label %30, label %35

30:                                               ; preds = %27
  %31 = load volatile i64, ptr @uxSchedulerSuspended, align 8, !tbaa !14
  %.not11 = icmp eq i64 %31, 0
  br i1 %.not11, label %34, label %32

32:                                               ; preds = %30
  tail call void @vPortRaiseBASEPRI()
  br label %33

33:                                               ; preds = %33, %32
  br label %33

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

35:                                               ; preds = %34, %27, %25
  ret void
}

declare void @vPortEnterCritical() local_unnamed_addr #2

declare i64 @uxListRemove(ptr noundef) local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
declare hidden void @prvDeleteTCB(ptr noundef) local_unnamed_addr #1

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

declare void @vPortExitCritical() 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 = !{!"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 = !{!12, !12, i64 0}
!16 = !{i64 2148503016}
!17 = !{i64 2148503086}


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