; 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

; 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 i64 @uxListRemove(ptr noundef) #2

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

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

12:                                               ; preds = %2
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %13, i32 0, i32 8
  %15 = load i64, ptr %14, align 8, !tbaa !11
  %16 = icmp eq i64 %15, 0
  br i1 %16, label %17, label %19

17:                                               ; preds = %12
  call void @vPortRaiseBASEPRI()
  br label %18

18:                                               ; preds = %18, %17
  br label %18

19:                                               ; preds = %12
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %20, i32 0, i32 7
  %22 = load i64, ptr %21, align 8, !tbaa !15
  %23 = load i64, ptr %4, align 8, !tbaa !9
  %24 = icmp ult i64 %22, %23
  br i1 %24, label %25, label %27

25:                                               ; preds = %19
  %26 = load i64, ptr %4, align 8, !tbaa !9
  store i64 %26, ptr %7, align 8, !tbaa !9
  br label %31

27:                                               ; preds = %19
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %28, i32 0, i32 7
  %30 = load i64, ptr %29, align 8, !tbaa !15
  store i64 %30, ptr %7, align 8, !tbaa !9
  br label %31

31:                                               ; preds = %27, %25
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %32, i32 0, i32 3
  %34 = load i64, ptr %33, align 8, !tbaa !16
  %35 = load i64, ptr %7, align 8, !tbaa !9
  %36 = icmp ne i64 %34, %35
  br i1 %36, label %37, label %111

37:                                               ; preds = %31
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %38, i32 0, i32 8
  %40 = load i64, ptr %39, align 8, !tbaa !11
  %41 = icmp eq i64 %40, 1
  br i1 %41, label %42, label %109

42:                                               ; preds = %37
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %45 = icmp ne ptr %43, %44
  %46 = zext i1 %45 to i32
  %47 = icmp eq i32 %46, 0
  br i1 %47, label %48, label %50

48:                                               ; preds = %42
  call void @vPortRaiseBASEPRI()
  br label %49

49:                                               ; preds = %49, %48
  br label %49

50:                                               ; preds = %42
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %51, i32 0, i32 3
  %53 = load i64, ptr %52, align 8, !tbaa !16
  store i64 %53, ptr %6, align 8, !tbaa !9
  %54 = load i64, ptr %7, align 8, !tbaa !9
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %55, i32 0, i32 3
  store i64 %54, ptr %56, align 8, !tbaa !16
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %57, i32 0, i32 2
  %59 = getelementptr inbounds %struct.xLIST_ITEM, ptr %58, i32 0, i32 0
  %60 = load i32, ptr %59, align 8, !tbaa !17
  %61 = zext i32 %60 to i64
  %62 = and i64 %61, 2147483648
  %63 = icmp eq i64 %62, 0
  br i1 %63, label %64, label %71

64:                                               ; preds = %50
  %65 = load i64, ptr %7, align 8, !tbaa !9
  %66 = trunc i64 %65 to i32
  %67 = sub i32 32, %66
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %68, i32 0, i32 2
  %70 = getelementptr inbounds %struct.xLIST_ITEM, ptr %69, i32 0, i32 0
  store i32 %67, ptr %70, align 8, !tbaa !17
  br label %72

71:                                               ; preds = %50
  br label %72

72:                                               ; preds = %71, %64
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %73, i32 0, i32 1
  %75 = getelementptr inbounds %struct.xLIST_ITEM, ptr %74, i32 0, i32 4
  %76 = load ptr, ptr %75, align 8, !tbaa !18
  %77 = load i64, ptr %6, align 8, !tbaa !9
  %78 = getelementptr inbounds [32 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %77
  %79 = icmp eq ptr %76, %78
  %80 = zext i1 %79 to i64
  %81 = select i1 %79, i64 1, i64 0
  %82 = icmp ne i64 %81, 0
  br i1 %82, label %83, label %107

83:                                               ; preds = %72
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %84, i32 0, i32 1
  %86 = call i64 @uxListRemove(ptr noundef %85)
  %87 = icmp eq i64 %86, 0
  br i1 %87, label %88, label %89

88:                                               ; preds = %83
  br label %90

89:                                               ; preds = %83
  br label %90

90:                                               ; preds = %89, %88
  %91 = load ptr, ptr %5, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %91, i32 0, i32 3
  %93 = load i64, ptr %92, align 8, !tbaa !16
  %94 = load volatile i64, ptr @uxTopReadyPriority, align 8, !tbaa !9
  %95 = icmp ugt i64 %93, %94
  br i1 %95, label %96, label %100

96:                                               ; preds = %90
  %97 = load ptr, ptr %5, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %97, i32 0, i32 3
  %99 = load i64, ptr %98, align 8, !tbaa !16
  store volatile i64 %99, ptr @uxTopReadyPriority, align 8, !tbaa !9
  br label %100

100:                                              ; preds = %96, %90
  %101 = load ptr, ptr %5, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %101, i32 0, i32 3
  %103 = load i64, ptr %102, align 8, !tbaa !16
  %104 = getelementptr inbounds [32 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %103
  %105 = load ptr, ptr %5, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %105, i32 0, i32 1
  call void @vListInsertEnd(ptr noundef %104, ptr noundef %106)
  br label %108

107:                                              ; preds = %72
  br label %108

108:                                              ; preds = %107, %100
  br label %110

109:                                              ; preds = %37
  br label %110

110:                                              ; preds = %109, %108
  br label %112

111:                                              ; preds = %31
  br label %112

112:                                              ; preds = %111, %110
  br label %114

113:                                              ; preds = %2
  br label %114

114:                                              ; preds = %113, %112
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  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
  ret void
}

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, !10, i64 144}
!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, !10, i64 136}
!16 = !{!12, !10, i64 88}
!17 = !{!12, !14, i64 48}
!18 = !{!12, !6, i64 40}


; 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

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

declare i64 @uxListRemove(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local void @vTaskPriorityDisinheritAfterTimeout(ptr noundef %0, i64 noundef %1) local_unnamed_addr #2 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %40, label %3

3:                                                ; preds = %2
  %4 = getelementptr inbounds i8, ptr %0, i64 144
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = icmp eq i64 %5, 0
  br i1 %6, label %7, label %9

7:                                                ; preds = %3
  tail call void @vPortRaiseBASEPRI()
  br label %8

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

9:                                                ; preds = %3
  %10 = getelementptr inbounds i8, ptr %0, i64 136
  %11 = load i64, ptr %10, align 8, !tbaa !13
  %. = tail call i64 @llvm.umax.i64(i64 %11, i64 %1)
  %12 = getelementptr inbounds i8, ptr %0, i64 88
  %13 = load i64, ptr %12, align 8, !tbaa !14
  %.not26 = icmp ne i64 %13, %.
  %14 = icmp eq i64 %5, 1
  %or.cond = and i1 %14, %.not26
  br i1 %or.cond, label %15, label %40

15:                                               ; preds = %9
  %16 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !15
  %.not27 = icmp eq ptr %16, %0
  br i1 %.not27, label %17, label %19

17:                                               ; preds = %15
  tail call void @vPortRaiseBASEPRI()
  br label %18

18:                                               ; preds = %18, %17
  br label %18

19:                                               ; preds = %15
  store i64 %., ptr %12, align 8, !tbaa !14
  %20 = getelementptr inbounds i8, ptr %0, i64 48
  %21 = load i32, ptr %20, align 8, !tbaa !16
  %22 = icmp sgt i32 %21, -1
  br i1 %22, label %23, label %26

23:                                               ; preds = %19
  %24 = trunc i64 %. to i32
  %25 = sub i32 32, %24
  store i32 %25, ptr %20, align 8, !tbaa !16
  br label %26

26:                                               ; preds = %19, %23
  %27 = getelementptr inbounds i8, ptr %0, i64 8
  %28 = getelementptr inbounds i8, ptr %0, i64 40
  %29 = load ptr, ptr %28, align 8, !tbaa !17
  %30 = getelementptr inbounds [32 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %13
  %31 = icmp eq ptr %29, %30
  br i1 %31, label %32, label %40

32:                                               ; preds = %26
  %33 = tail call i64 @uxListRemove(ptr noundef nonnull %27) #4
  %34 = load i64, ptr %12, align 8, !tbaa !14
  %35 = load volatile i64, ptr @uxTopReadyPriority, align 8, !tbaa !18
  %36 = icmp ugt i64 %34, %35
  br i1 %36, label %37, label %38

37:                                               ; preds = %32
  store volatile i64 %34, ptr @uxTopReadyPriority, align 8, !tbaa !18
  br label %38

38:                                               ; preds = %37, %32
  %39 = getelementptr inbounds [32 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %34
  tail call void @vListInsertEnd(ptr noundef nonnull %39, ptr noundef nonnull %27) #4
  br label %40

40:                                               ; preds = %2, %38, %26, %9
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #3

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !12, i64 144}
!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, !12, i64 136}
!14 = !{!6, !12, i64 88}
!15 = !{!7, !7, i64 0}
!16 = !{!6, !11, i64 48}
!17 = !{!6, !7, i64 40}
!18 = !{!12, !12, i64 0}


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