; 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
@xTasksWaitingTermination = external hidden global %struct.xLIST, align 8
@pxDelayedTaskList = external hidden global ptr, align 8
@pxOverflowDelayedTaskList = external hidden global ptr, 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 void @vPortExitCritical() #2

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

12:                                               ; preds = %1
  call void @vPortRaiseBASEPRI()
  br label %13

13:                                               ; preds = %13, %12
  br label %13

14:                                               ; preds = %1
  %15 = load ptr, ptr %7, align 8, !tbaa !5
  %16 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %17 = icmp eq ptr %15, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %14
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %74

19:                                               ; preds = %14
  call void @vPortEnterCritical()
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %20, i32 0, i32 1
  %22 = getelementptr inbounds %struct.xLIST_ITEM, ptr %21, i32 0, i32 4
  %23 = load ptr, ptr %22, align 8, !tbaa !11
  store ptr %23, ptr %4, align 8, !tbaa !5
  %24 = load volatile ptr, ptr @pxDelayedTaskList, align 8, !tbaa !5
  store ptr %24, ptr %5, align 8, !tbaa !5
  %25 = load volatile ptr, ptr @pxOverflowDelayedTaskList, align 8, !tbaa !5
  store ptr %25, ptr %6, align 8, !tbaa !5
  call void @vPortExitCritical()
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = icmp eq ptr %26, %27
  br i1 %28, label %33, label %29

29:                                               ; preds = %19
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = icmp eq ptr %30, %31
  br i1 %32, label %33, label %34

33:                                               ; preds = %29, %19
  store i32 2, ptr %3, align 4, !tbaa !9
  br label %73

34:                                               ; preds = %29
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = icmp eq ptr %35, @xSuspendedTaskList
  br i1 %36, label %37, label %63

37:                                               ; preds = %34
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %38, i32 0, i32 2
  %40 = getelementptr inbounds %struct.xLIST_ITEM, ptr %39, i32 0, i32 4
  %41 = load ptr, ptr %40, align 8, !tbaa !15
  %42 = icmp eq ptr %41, null
  br i1 %42, label %43, label %61

43:                                               ; preds = %37
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store i32 3, ptr %3, align 4, !tbaa !9
  store i64 0, ptr %8, align 8, !tbaa !16
  br label %44

44:                                               ; preds = %57, %43
  %45 = load i64, ptr %8, align 8, !tbaa !16
  %46 = icmp slt i64 %45, 3
  br i1 %46, label %47, label %60

47:                                               ; preds = %44
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %48, i32 0, i32 12
  %50 = load i64, ptr %8, align 8, !tbaa !16
  %51 = getelementptr inbounds [3 x i8], ptr %49, i64 0, i64 %50
  %52 = load volatile i8, ptr %51, align 1, !tbaa !17
  %53 = zext i8 %52 to i32
  %54 = icmp eq i32 %53, 1
  br i1 %54, label %55, label %56

55:                                               ; preds = %47
  store i32 2, ptr %3, align 4, !tbaa !9
  br label %60

56:                                               ; preds = %47
  br label %57

57:                                               ; preds = %56
  %58 = load i64, ptr %8, align 8, !tbaa !16
  %59 = add nsw i64 %58, 1
  store i64 %59, ptr %8, align 8, !tbaa !16
  br label %44, !llvm.loop !18

60:                                               ; preds = %55, %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %62

61:                                               ; preds = %37
  store i32 2, ptr %3, align 4, !tbaa !9
  br label %62

62:                                               ; preds = %61, %60
  br label %72

63:                                               ; preds = %34
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = icmp eq ptr %64, @xTasksWaitingTermination
  br i1 %65, label %69, label %66

66:                                               ; preds = %63
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = icmp eq ptr %67, null
  br i1 %68, label %69, label %70

69:                                               ; preds = %66, %63
  store i32 4, ptr %3, align 4, !tbaa !9
  br label %71

70:                                               ; preds = %66
  store i32 1, ptr %3, align 4, !tbaa !9
  br label %71

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

72:                                               ; preds = %71, %62
  br label %73

73:                                               ; preds = %72, %33
  br label %74

74:                                               ; preds = %73, %18
  %75 = load i32, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  ret i32 %75
}

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 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 40}
!12 = !{!"tskTaskControlBlock", !6, i64 0, !13, i64 8, !13, i64 48, !14, i64 88, !6, i64 96, !7, i64 104, !6, i64 128, !14, i64 136, !14, i64 144, !6, i64 152, !7, i64 160, !7, i64 200, !7, i64 212, !7, i64 215, !7, i64 216, !10, i64 220}
!13 = !{!"xLIST_ITEM", !10, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!14 = !{!"long", !7, i64 0}
!15 = !{!12, !6, i64 80}
!16 = !{!14, !14, i64 0}
!17 = !{!7, !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; 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
@xTasksWaitingTermination = external hidden global %struct.xLIST, align 8
@pxDelayedTaskList = external hidden global ptr, align 8
@pxOverflowDelayedTaskList = external hidden global ptr, 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 void @vPortExitCritical() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 5) i32 @eTaskGetState(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
  %6 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %7 = icmp eq ptr %6, %0
  br i1 %7, label %.loopexit, label %8

8:                                                ; preds = %5
  tail call void @vPortEnterCritical() #3
  %9 = getelementptr inbounds i8, ptr %0, i64 40
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = load volatile ptr, ptr @pxDelayedTaskList, align 8, !tbaa !5
  %12 = load volatile ptr, ptr @pxOverflowDelayedTaskList, align 8, !tbaa !5
  tail call void @vPortExitCritical() #3
  %13 = icmp eq ptr %10, %11
  %14 = icmp eq ptr %10, %12
  %or.cond20 = select i1 %13, i1 true, i1 %14
  br i1 %or.cond20, label %.loopexit, label %15

15:                                               ; preds = %8
  %16 = icmp eq ptr %10, @xSuspendedTaskList
  br i1 %16, label %17, label %32

17:                                               ; preds = %15
  %18 = getelementptr inbounds i8, ptr %0, i64 80
  %19 = load ptr, ptr %18, align 8, !tbaa !14
  %20 = icmp eq ptr %19, null
  br i1 %20, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %17
  %21 = getelementptr inbounds i8, ptr %0, i64 212
  %22 = load volatile i8, ptr %21, align 1, !tbaa !15
  %23 = icmp eq i8 %22, 1
  br i1 %23, label %.loopexit, label %24

24:                                               ; preds = %.preheader
  %25 = getelementptr inbounds i8, ptr %0, i64 213
  %26 = load volatile i8, ptr %25, align 1, !tbaa !15
  %27 = icmp eq i8 %26, 1
  br i1 %27, label %.loopexit, label %28

28:                                               ; preds = %24
  %29 = getelementptr inbounds i8, ptr %0, i64 214
  %30 = load volatile i8, ptr %29, align 1, !tbaa !15
  %31 = icmp eq i8 %30, 1
  %spec.select = select i1 %31, i32 2, i32 3
  br label %.loopexit

32:                                               ; preds = %15
  %33 = icmp eq ptr %10, @xTasksWaitingTermination
  %34 = icmp eq ptr %10, null
  %or.cond = or i1 %33, %34
  %. = select i1 %or.cond, i32 4, i32 1
  br label %.loopexit

.loopexit:                                        ; preds = %28, %.preheader, %24, %32, %17, %8, %5
  %.1 = phi i32 [ 0, %5 ], [ 2, %8 ], [ 2, %17 ], [ %., %32 ], [ 2, %.preheader ], [ 2, %24 ], [ %spec.select, %28 ]
  ret i32 %.1
}

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 40}
!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 = !{!10, !6, i64 80}
!15 = !{!7, !7, i64 0}


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