; 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.xTASK_STATUS = type { ptr, ptr, i64, i32, i64, i64, i32, ptr, i16 }

@xTasksWaitingTermination = external hidden global %struct.xLIST, section "privileged_data", align 8
@uxCurrentNumberOfTasks = external hidden global i64, section "privileged_data", align 8
@pxDelayedTaskList = external hidden global ptr, section "privileged_data", align 8
@pxOverflowDelayedTaskList = external hidden global ptr, section "privileged_data", align 8
@xSuspendedTaskList = external hidden global %struct.xLIST, section "privileged_data", align 8
@pxReadyTasksLists = external hidden global [5 x %struct.xLIST], section "privileged_data", align 16

; 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
declare dso_local void @vTaskSuspendAll() #1 section "privileged_functions"

; Function Attrs: nounwind uwtable
declare dso_local i64 @xTaskResumeAll() #1 section "privileged_functions"

; Function Attrs: nounwind uwtable
define dso_local i64 @uxTaskGetSystemState(ptr noundef %0, i64 noundef %1, ptr noundef %2) #1 section "privileged_functions" {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store i64 0, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  store i64 5, ptr %8, align 8, !tbaa !9
  call void @vTaskSuspendAll()
  %9 = load i64, ptr %5, align 8, !tbaa !9
  %10 = load volatile i64, ptr @uxCurrentNumberOfTasks, align 8, !tbaa !9
  %11 = icmp uge i64 %9, %10
  br i1 %11, label %12, label %59

12:                                               ; preds = %3
  br label %13

13:                                               ; preds = %24, %12
  %14 = load i64, ptr %8, align 8, !tbaa !9
  %15 = add i64 %14, -1
  store i64 %15, ptr %8, align 8, !tbaa !9
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = load i64, ptr %7, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.xTASK_STATUS, ptr %16, i64 %17
  %19 = load i64, ptr %8, align 8, !tbaa !9
  %20 = getelementptr inbounds [5 x %struct.xLIST], ptr @pxReadyTasksLists, i64 0, i64 %19
  %21 = call i64 @prvListTasksWithinSingleList(ptr noundef %18, ptr noundef %20, i32 noundef 1)
  %22 = load i64, ptr %7, align 8, !tbaa !9
  %23 = add i64 %22, %21
  store i64 %23, ptr %7, align 8, !tbaa !9
  br label %24

24:                                               ; preds = %13
  %25 = load i64, ptr %8, align 8, !tbaa !9
  %26 = icmp ugt i64 %25, 0
  br i1 %26, label %13, label %27, !llvm.loop !11

27:                                               ; preds = %24
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.xTASK_STATUS, ptr %28, i64 %29
  %31 = load volatile ptr, ptr @pxDelayedTaskList, align 8, !tbaa !5
  %32 = call i64 @prvListTasksWithinSingleList(ptr noundef %30, ptr noundef %31, i32 noundef 2)
  %33 = load i64, ptr %7, align 8, !tbaa !9
  %34 = add i64 %33, %32
  store i64 %34, ptr %7, align 8, !tbaa !9
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = load i64, ptr %7, align 8, !tbaa !9
  %37 = getelementptr inbounds %struct.xTASK_STATUS, ptr %35, i64 %36
  %38 = load volatile ptr, ptr @pxOverflowDelayedTaskList, align 8, !tbaa !5
  %39 = call i64 @prvListTasksWithinSingleList(ptr noundef %37, ptr noundef %38, i32 noundef 2)
  %40 = load i64, ptr %7, align 8, !tbaa !9
  %41 = add i64 %40, %39
  store i64 %41, ptr %7, align 8, !tbaa !9
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = load i64, ptr %7, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.xTASK_STATUS, ptr %42, i64 %43
  %45 = call i64 @prvListTasksWithinSingleList(ptr noundef %44, ptr noundef @xTasksWaitingTermination, i32 noundef 4)
  %46 = load i64, ptr %7, align 8, !tbaa !9
  %47 = add i64 %46, %45
  store i64 %47, ptr %7, align 8, !tbaa !9
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = load i64, ptr %7, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.xTASK_STATUS, ptr %48, i64 %49
  %51 = call i64 @prvListTasksWithinSingleList(ptr noundef %50, ptr noundef @xSuspendedTaskList, i32 noundef 3)
  %52 = load i64, ptr %7, align 8, !tbaa !9
  %53 = add i64 %52, %51
  store i64 %53, ptr %7, align 8, !tbaa !9
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = icmp ne ptr %54, null
  br i1 %55, label %56, label %58

56:                                               ; preds = %27
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 0, ptr %57, align 4, !tbaa !13
  br label %58

58:                                               ; preds = %56, %27
  br label %60

59:                                               ; preds = %3
  br label %60

60:                                               ; preds = %59, %58
  %61 = call i64 @xTaskResumeAll()
  %62 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret i64 %62
}

; Function Attrs: nounwind uwtable
declare hidden i64 @prvListTasksWithinSingleList(ptr noundef, ptr noundef, i32 noundef) #1 section "privileged_functions"

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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}


; 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 }
%struct.xTASK_STATUS = type { ptr, ptr, i64, i32, i64, i64, i32, ptr, i16 }

@xTasksWaitingTermination = external hidden global %struct.xLIST, section "privileged_data", align 8
@uxCurrentNumberOfTasks = external hidden global i64, section "privileged_data", align 8
@pxDelayedTaskList = external hidden global ptr, section "privileged_data", align 8
@pxOverflowDelayedTaskList = external hidden global ptr, section "privileged_data", align 8
@xSuspendedTaskList = external hidden global %struct.xLIST, section "privileged_data", align 8
@pxReadyTasksLists = external hidden global [5 x %struct.xLIST], section "privileged_data", align 16

; Function Attrs: nounwind uwtable
declare dso_local void @vTaskSuspendAll() local_unnamed_addr #0 section "privileged_functions"

; Function Attrs: nounwind uwtable
declare dso_local i64 @xTaskResumeAll() local_unnamed_addr #0 section "privileged_functions"

; Function Attrs: nounwind uwtable
define dso_local i64 @uxTaskGetSystemState(ptr noundef %0, i64 noundef %1, ptr noundef writeonly %2) local_unnamed_addr #0 section "privileged_functions" {
  tail call void @vTaskSuspendAll()
  %4 = load volatile i64, ptr @uxCurrentNumberOfTasks, align 8, !tbaa !5
  %.not = icmp ugt i64 %4, %1
  br i1 %.not, label %33, label %.preheader.preheader

.preheader.preheader:                             ; preds = %3
  %5 = tail call i64 @prvListTasksWithinSingleList(ptr noundef %0, ptr noundef nonnull getelementptr inbounds (i8, ptr @pxReadyTasksLists, i64 160), i32 noundef 1)
  %6 = getelementptr inbounds %struct.xTASK_STATUS, ptr %0, i64 %5
  %7 = tail call i64 @prvListTasksWithinSingleList(ptr noundef %6, ptr noundef nonnull getelementptr inbounds (i8, ptr @pxReadyTasksLists, i64 120), i32 noundef 1)
  %8 = add i64 %7, %5
  %9 = getelementptr inbounds %struct.xTASK_STATUS, ptr %0, i64 %8
  %10 = tail call i64 @prvListTasksWithinSingleList(ptr noundef %9, ptr noundef nonnull getelementptr inbounds (i8, ptr @pxReadyTasksLists, i64 80), i32 noundef 1)
  %11 = add i64 %10, %8
  %12 = getelementptr inbounds %struct.xTASK_STATUS, ptr %0, i64 %11
  %13 = tail call i64 @prvListTasksWithinSingleList(ptr noundef %12, ptr noundef nonnull getelementptr inbounds (i8, ptr @pxReadyTasksLists, i64 40), i32 noundef 1)
  %14 = add i64 %13, %11
  %15 = getelementptr inbounds %struct.xTASK_STATUS, ptr %0, i64 %14
  %16 = tail call i64 @prvListTasksWithinSingleList(ptr noundef %15, ptr noundef nonnull @pxReadyTasksLists, i32 noundef 1)
  %17 = add i64 %16, %14
  %18 = getelementptr inbounds %struct.xTASK_STATUS, ptr %0, i64 %17
  %19 = load volatile ptr, ptr @pxDelayedTaskList, align 8, !tbaa !9
  %20 = tail call i64 @prvListTasksWithinSingleList(ptr noundef %18, ptr noundef %19, i32 noundef 2)
  %21 = add i64 %20, %17
  %22 = getelementptr inbounds %struct.xTASK_STATUS, ptr %0, i64 %21
  %23 = load volatile ptr, ptr @pxOverflowDelayedTaskList, align 8, !tbaa !9
  %24 = tail call i64 @prvListTasksWithinSingleList(ptr noundef %22, ptr noundef %23, i32 noundef 2)
  %25 = add i64 %24, %21
  %26 = getelementptr inbounds %struct.xTASK_STATUS, ptr %0, i64 %25
  %27 = tail call i64 @prvListTasksWithinSingleList(ptr noundef %26, ptr noundef nonnull @xTasksWaitingTermination, i32 noundef 4)
  %28 = add i64 %25, %27
  %29 = getelementptr inbounds %struct.xTASK_STATUS, ptr %0, i64 %28
  %30 = tail call i64 @prvListTasksWithinSingleList(ptr noundef %29, ptr noundef nonnull @xSuspendedTaskList, i32 noundef 3)
  %31 = add i64 %28, %30
  %.not24 = icmp eq ptr %2, null
  br i1 %.not24, label %33, label %32

32:                                               ; preds = %.preheader.preheader
  store i32 0, ptr %2, align 4, !tbaa !11
  br label %33

33:                                               ; preds = %3, %.preheader.preheader, %32
  %.1 = phi i64 [ %31, %32 ], [ %31, %.preheader.preheader ], [ 0, %3 ]
  %34 = tail call i64 @xTaskResumeAll()
  ret i64 %.1
}

; Function Attrs: nounwind uwtable
declare hidden i64 @prvListTasksWithinSingleList(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0 section "privileged_functions"

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" }

!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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}


; Effective O3 passes: GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LoopSimplifyPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass