; LLVM IR from CactiLab/Sherloc-Cortex-M-CFVD :: Sherloc_runtime/freertos (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.xLIST_ITEM = type { i32, ptr, ptr, ptr, ptr }

@pxCurrentTCB = external dso_local global ptr, section "privileged_data", align 8
@uxSchedulerSuspended = external hidden global i64, section "privileged_data", align 8
@pxReadyTasksLists = external hidden global [5 x %struct.xLIST], section "privileged_data", align 16
@uxTopReadyPriority = external hidden global i64, section "privileged_data", align 8
@xYieldPending = external hidden global i64, section "privileged_data", 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: 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 @vTaskSwitchContext() #1 section "privileged_functions" {
  %1 = alloca i64, align 8
  %2 = alloca ptr, align 8
  %3 = load volatile i64, ptr @uxSchedulerSuspended, align 8, !tbaa !5
  %4 = icmp ne i64 %3, 0
  br i1 %4, label %5, label %6

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

6:                                                ; preds = %0
  store volatile i64 0, ptr @xYieldPending, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  %7 = load volatile i64, ptr @uxTopReadyPriority, align 8, !tbaa !5
  store i64 %7, ptr %1, align 8, !tbaa !5
  br label %8

8:                                                ; preds = %22, %6
  %9 = load i64, ptr %1, align 8, !tbaa !5
  %10 = getelementptr inbounds [5 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %9
  %11 = getelementptr inbounds %struct.xLIST, ptr %10, i32 0, i32 0
  %12 = load volatile i64, ptr %11, align 8, !tbaa !9
  %13 = icmp eq i64 %12, 0
  %14 = zext i1 %13 to i64
  %15 = select i1 %13, i64 1, i64 0
  %16 = icmp ne i64 %15, 0
  br i1 %16, label %17, label %25

17:                                               ; preds = %8
  %18 = load i64, ptr %1, align 8, !tbaa !5
  %19 = icmp eq i64 %18, 0
  br i1 %19, label %20, label %22

20:                                               ; preds = %17
  call void asm sideeffect " cpsid i ", "~{memory},~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !14
  br label %21

21:                                               ; preds = %21, %20
  br label %21

22:                                               ; preds = %17
  %23 = load i64, ptr %1, align 8, !tbaa !5
  %24 = add i64 %23, -1
  store i64 %24, ptr %1, align 8, !tbaa !5
  br label %8, !llvm.loop !15

25:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  %26 = load i64, ptr %1, align 8, !tbaa !5
  %27 = getelementptr inbounds [5 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %26
  store ptr %27, ptr %2, align 8, !tbaa !17
  %28 = load ptr, ptr %2, align 8, !tbaa !17
  %29 = getelementptr inbounds %struct.xLIST, ptr %28, i32 0, i32 1
  %30 = load ptr, ptr %29, align 8, !tbaa !18
  %31 = getelementptr inbounds %struct.xLIST_ITEM, ptr %30, i32 0, i32 1
  %32 = load ptr, ptr %31, align 8, !tbaa !19
  %33 = load ptr, ptr %2, align 8, !tbaa !17
  %34 = getelementptr inbounds %struct.xLIST, ptr %33, i32 0, i32 1
  store ptr %32, ptr %34, align 8, !tbaa !18
  %35 = load ptr, ptr %2, align 8, !tbaa !17
  %36 = getelementptr inbounds %struct.xLIST, ptr %35, i32 0, i32 1
  %37 = load ptr, ptr %36, align 8, !tbaa !18
  %38 = load ptr, ptr %2, align 8, !tbaa !17
  %39 = getelementptr inbounds %struct.xLIST, ptr %38, i32 0, i32 2
  %40 = icmp eq ptr %37, %39
  br i1 %40, label %41, label %49

41:                                               ; preds = %25
  %42 = load ptr, ptr %2, align 8, !tbaa !17
  %43 = getelementptr inbounds %struct.xLIST, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !18
  %45 = getelementptr inbounds %struct.xLIST_ITEM, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !19
  %47 = load ptr, ptr %2, align 8, !tbaa !17
  %48 = getelementptr inbounds %struct.xLIST, ptr %47, i32 0, i32 1
  store ptr %46, ptr %48, align 8, !tbaa !18
  br label %49

49:                                               ; preds = %41, %25
  %50 = load ptr, ptr %2, align 8, !tbaa !17
  %51 = getelementptr inbounds %struct.xLIST, ptr %50, i32 0, i32 1
  %52 = load ptr, ptr %51, align 8, !tbaa !18
  %53 = getelementptr inbounds %struct.xLIST_ITEM, ptr %52, i32 0, i32 3
  %54 = load ptr, ptr %53, align 8, !tbaa !21
  store volatile ptr %54, ptr @pxCurrentTCB, align 8, !tbaa !17
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  %55 = load i64, ptr %1, align 8, !tbaa !5
  store volatile i64 %55, ptr @uxTopReadyPriority, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  br label %56

56:                                               ; preds = %49, %5
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"xLIST", !6, i64 0, !11, i64 8, !12, i64 16}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"xMINI_LIST_ITEM", !13, i64 0, !11, i64 8, !11, i64 16}
!13 = !{!"int", !7, i64 0}
!14 = !{i64 2148461855}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!11, !11, i64 0}
!18 = !{!10, !11, i64 8}
!19 = !{!20, !11, i64 8}
!20 = !{!"xLIST_ITEM", !13, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32}
!21 = !{!20, !11, i64 24}


; 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, section "privileged_data", align 8
@uxSchedulerSuspended = external hidden global i64, section "privileged_data", align 8
@pxReadyTasksLists = external hidden global [5 x %struct.xLIST], section "privileged_data", align 16
@uxTopReadyPriority = external hidden global i64, section "privileged_data", align 8
@xYieldPending = external hidden global i64, section "privileged_data", align 8

; Function Attrs: nounwind uwtable
define dso_local void @vTaskSwitchContext() local_unnamed_addr #0 section "privileged_functions" {
  %1 = load volatile i64, ptr @uxSchedulerSuspended, align 8, !tbaa !5
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %3, label %2

2:                                                ; preds = %0
  store volatile i64 1, ptr @xYieldPending, align 8, !tbaa !5
  br label %29

3:                                                ; preds = %0
  store volatile i64 0, ptr @xYieldPending, align 8, !tbaa !5
  %4 = load volatile i64, ptr @uxTopReadyPriority, align 8, !tbaa !5
  %5 = getelementptr inbounds [5 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %4
  %6 = load volatile i64, ptr %5, align 8, !tbaa !9
  %7 = icmp eq i64 %6, 0
  br i1 %7, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3, %11
  %.014 = phi i64 [ %12, %11 ], [ %4, %3 ]
  %8 = icmp eq i64 %.014, 0
  br i1 %8, label %9, label %11

9:                                                ; preds = %.lr.ph
  tail call void asm sideeffect " cpsid i ", "~{memory},~{dirflag},~{fpsr},~{flags}"() #1, !srcloc !14
  br label %10

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

11:                                               ; preds = %.lr.ph
  %12 = add i64 %.014, -1
  %13 = getelementptr inbounds [5 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %12
  %14 = load volatile i64, ptr %13, align 8, !tbaa !9
  %15 = icmp eq i64 %14, 0
  br i1 %15, label %.lr.ph, label %._crit_edge, !llvm.loop !15

._crit_edge:                                      ; preds = %11, %3
  %.0.lcssa = phi i64 [ %4, %3 ], [ %12, %11 ]
  %.lcssa = phi ptr [ %5, %3 ], [ %13, %11 ]
  %16 = getelementptr inbounds i8, ptr %.lcssa, i64 8
  %17 = load ptr, ptr %16, align 8, !tbaa !17
  %18 = getelementptr inbounds i8, ptr %17, i64 8
  %19 = load ptr, ptr %18, align 8, !tbaa !18
  store ptr %19, ptr %16, align 8, !tbaa !17
  %20 = getelementptr inbounds i8, ptr %.lcssa, i64 16
  %21 = icmp eq ptr %19, %20
  br i1 %21, label %22, label %25

22:                                               ; preds = %._crit_edge
  %23 = getelementptr inbounds i8, ptr %19, i64 8
  %24 = load ptr, ptr %23, align 8, !tbaa !18
  store ptr %24, ptr %16, align 8, !tbaa !17
  br label %25

25:                                               ; preds = %22, %._crit_edge
  %26 = phi ptr [ %24, %22 ], [ %19, %._crit_edge ]
  %27 = getelementptr inbounds i8, ptr %26, i64 24
  %28 = load ptr, ptr %27, align 8, !tbaa !20
  store volatile ptr %28, ptr @pxCurrentTCB, align 8, !tbaa !21
  store volatile i64 %.0.lcssa, ptr @uxTopReadyPriority, align 8, !tbaa !5
  br label %29

29:                                               ; preds = %25, %2
  ret void
}

attributes #0 = { 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 }

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"xLIST", !6, i64 0, !11, i64 8, !12, i64 16}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"xMINI_LIST_ITEM", !13, i64 0, !11, i64 8, !11, i64 16}
!13 = !{!"int", !7, i64 0}
!14 = !{i64 2148461855}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!10, !11, i64 8}
!18 = !{!19, !11, i64 8}
!19 = !{!"xLIST_ITEM", !13, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32}
!20 = !{!19, !11, i64 24}
!21 = !{!11, !11, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass