; 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.tskTaskControlBlock = type { ptr, %struct.MPU_SETTINGS, %struct.xLIST_ITEM, %struct.xLIST_ITEM, i64, ptr, [10 x i8], i64, i64, i64, i64, [1 x i32], [1 x i8], i8, i8 }
%struct.MPU_SETTINGS = type { i32, [4 x %struct.MPURegionSettings] }
%struct.MPURegionSettings = type { i32, i32 }
%struct.xLIST_ITEM = type { i32, ptr, ptr, ptr, ptr }

@pxCurrentTCB = external dso_local global ptr, section "privileged_data", align 8
@uxCurrentNumberOfTasks = external hidden global i64, section "privileged_data", align 8
@xSchedulerRunning = external hidden global i64, section "privileged_data", align 8
@uxSchedulerSuspended = external hidden global i64, section "privileged_data", align 8
@xSuspendedTaskList = external hidden global %struct.xLIST, 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

declare void @vPortEnterCritical() #1

declare i64 @uxListRemove(ptr noundef) #1 section "privileged_functions"

declare void @vListInsertEnd(ptr noundef, ptr noundef) #1 section "privileged_functions"

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

declare void @vPortExitCritical() #1

declare void @vPortYield() #1

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

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

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

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

17:                                               ; preds = %11
  br label %19

18:                                               ; preds = %11
  br label %19

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

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

29:                                               ; preds = %19
  br label %30

30:                                               ; preds = %29, %25
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %31, i32 0, i32 2
  call void @vListInsertEnd(ptr noundef @xSuspendedTaskList, ptr noundef %32)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store i64 0, ptr %4, align 8, !tbaa !15
  br label %33

33:                                               ; preds = %50, %30
  %34 = load i64, ptr %4, align 8, !tbaa !15
  %35 = icmp slt i64 %34, 1
  br i1 %35, label %36, label %53

36:                                               ; preds = %33
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %37, i32 0, i32 12
  %39 = load i64, ptr %4, align 8, !tbaa !15
  %40 = getelementptr inbounds [1 x i8], ptr %38, i64 0, i64 %39
  %41 = load volatile i8, ptr %40, align 1, !tbaa !16
  %42 = zext i8 %41 to i32
  %43 = icmp eq i32 %42, 1
  br i1 %43, label %44, label %49

44:                                               ; preds = %36
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %45, i32 0, i32 12
  %47 = load i64, ptr %4, align 8, !tbaa !15
  %48 = getelementptr inbounds [1 x i8], ptr %46, i64 0, i64 %47
  store volatile i8 0, ptr %48, align 1, !tbaa !16
  br label %49

49:                                               ; preds = %44, %36
  br label %50

50:                                               ; preds = %49
  %51 = load i64, ptr %4, align 8, !tbaa !15
  %52 = add nsw i64 %51, 1
  store i64 %52, ptr %4, align 8, !tbaa !15
  br label %33, !llvm.loop !17

53:                                               ; preds = %33
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @vPortExitCritical()
  %54 = load volatile i64, ptr @xSchedulerRunning, align 8, !tbaa !15
  %55 = icmp ne i64 %54, 0
  br i1 %55, label %56, label %57

56:                                               ; preds = %53
  call void @vPortEnterCritical()
  call void @prvResetNextTaskUnblockTime()
  call void @vPortExitCritical()
  br label %58

57:                                               ; preds = %53
  br label %58

58:                                               ; preds = %57, %56
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %61 = icmp eq ptr %59, %60
  br i1 %61, label %62, label %81

62:                                               ; preds = %58
  %63 = load volatile i64, ptr @xSchedulerRunning, align 8, !tbaa !15
  %64 = icmp ne i64 %63, 0
  br i1 %64, label %65, label %73

65:                                               ; preds = %62
  %66 = load volatile i64, ptr @uxSchedulerSuspended, align 8, !tbaa !15
  %67 = icmp eq i64 %66, 0
  %68 = zext i1 %67 to i32
  %69 = icmp eq i32 %68, 0
  br i1 %69, label %70, label %72

70:                                               ; preds = %65
  call void asm sideeffect " cpsid i ", "~{memory},~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !19
  br label %71

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

72:                                               ; preds = %65
  call void @vPortYield()
  br label %80

73:                                               ; preds = %62
  %74 = load volatile i64, ptr @xSuspendedTaskList, align 8, !tbaa !20
  %75 = load volatile i64, ptr @uxCurrentNumberOfTasks, align 8, !tbaa !15
  %76 = icmp eq i64 %74, %75
  br i1 %76, label %77, label %78

77:                                               ; preds = %73
  store volatile ptr null, ptr @pxCurrentTCB, align 8, !tbaa !5
  br label %79

78:                                               ; preds = %73
  call void @vTaskSwitchContext()
  br label %79

79:                                               ; preds = %78, %77
  br label %80

80:                                               ; preds = %79, %72
  br label %82

81:                                               ; preds = %58
  br label %82

82:                                               ; preds = %81, %80
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local void @vTaskSwitchContext() #2 section "privileged_functions"

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 120}
!10 = !{!"tskTaskControlBlock", !6, i64 0, !11, i64 8, !13, i64 48, !13, i64 88, !14, i64 128, !6, i64 136, !7, i64 144, !14, i64 160, !14, i64 168, !14, i64 176, !14, i64 184, !7, i64 192, !7, i64 196, !7, i64 197, !7, i64 198}
!11 = !{!"MPU_SETTINGS", !12, i64 0, !7, i64 4}
!12 = !{!"int", !7, i64 0}
!13 = !{!"xLIST_ITEM", !12, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!14 = !{!"long", !7, i64 0}
!15 = !{!14, !14, i64 0}
!16 = !{!7, !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{i64 2148433239}
!20 = !{!21, !14, i64 0}
!21 = !{!"xLIST", !14, i64 0, !6, i64 8, !22, i64 16}
!22 = !{!"xMINI_LIST_ITEM", !12, i64 0, !6, i64 8, !6, i64 16}


; 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
@uxCurrentNumberOfTasks = external hidden global i64, section "privileged_data", align 8
@xSchedulerRunning = external hidden global i64, section "privileged_data", align 8
@uxSchedulerSuspended = external hidden global i64, section "privileged_data", align 8
@xSuspendedTaskList = external hidden global %struct.xLIST, section "privileged_data", align 8

declare void @vPortEnterCritical() local_unnamed_addr #0

declare i64 @uxListRemove(ptr noundef) local_unnamed_addr #0 section "privileged_functions"

declare void @vListInsertEnd(ptr noundef, ptr noundef) local_unnamed_addr #0 section "privileged_functions"

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

declare void @vPortExitCritical() local_unnamed_addr #0

declare void @vPortYield() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @vTaskSuspend(ptr noundef %0) local_unnamed_addr #1 section "privileged_functions" {
  tail call void @vPortEnterCritical() #2
  %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 48
  %8 = tail call i64 @uxListRemove(ptr noundef nonnull %7) #2
  %9 = getelementptr inbounds i8, ptr %6, i64 120
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %.not = icmp eq ptr %10, null
  br i1 %.not, label %.critedge, label %11

11:                                               ; preds = %5
  %12 = getelementptr inbounds i8, ptr %6, i64 88
  %13 = tail call i64 @uxListRemove(ptr noundef nonnull %12) #2
  br label %.critedge

.critedge:                                        ; preds = %5, %11
  tail call void @vListInsertEnd(ptr noundef nonnull @xSuspendedTaskList, ptr noundef nonnull %7) #2
  %14 = getelementptr inbounds i8, ptr %6, i64 196
  %15 = load volatile i8, ptr %14, align 1, !tbaa !15
  %16 = icmp eq i8 %15, 1
  br i1 %16, label %17, label %18

17:                                               ; preds = %.critedge
  store volatile i8 0, ptr %14, align 1, !tbaa !15
  br label %18

18:                                               ; preds = %17, %.critedge
  tail call void @vPortExitCritical() #2
  %19 = load volatile i64, ptr @xSchedulerRunning, align 8, !tbaa !16
  %.not13 = icmp eq i64 %19, 0
  br i1 %.not13, label %21, label %20

20:                                               ; preds = %18
  tail call void @vPortEnterCritical() #2
  tail call void @prvResetNextTaskUnblockTime()
  tail call void @vPortExitCritical() #2
  br label %21

21:                                               ; preds = %18, %20
  %22 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %23 = icmp eq ptr %6, %22
  br i1 %23, label %24, label %37

24:                                               ; preds = %21
  %25 = load volatile i64, ptr @xSchedulerRunning, align 8, !tbaa !16
  %.not14 = icmp eq i64 %25, 0
  br i1 %.not14, label %31, label %26

26:                                               ; preds = %24
  %27 = load volatile i64, ptr @uxSchedulerSuspended, align 8, !tbaa !16
  %.not15 = icmp eq i64 %27, 0
  br i1 %.not15, label %30, label %28

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

29:                                               ; preds = %29, %28
  br label %29

30:                                               ; preds = %26
  tail call void @vPortYield() #2
  br label %37

31:                                               ; preds = %24
  %32 = load volatile i64, ptr @xSuspendedTaskList, align 8, !tbaa !18
  %33 = load volatile i64, ptr @uxCurrentNumberOfTasks, align 8, !tbaa !16
  %34 = icmp eq i64 %32, %33
  br i1 %34, label %35, label %36

35:                                               ; preds = %31
  store volatile ptr null, ptr @pxCurrentTCB, align 8, !tbaa !5
  br label %37

36:                                               ; preds = %31
  tail call void @vTaskSwitchContext()
  br label %37

37:                                               ; preds = %21, %30, %36, %35
  ret void
}

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

attributes #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 = { 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 120}
!10 = !{!"tskTaskControlBlock", !6, i64 0, !11, i64 8, !13, i64 48, !13, i64 88, !14, i64 128, !6, i64 136, !7, i64 144, !14, i64 160, !14, i64 168, !14, i64 176, !14, i64 184, !7, i64 192, !7, i64 196, !7, i64 197, !7, i64 198}
!11 = !{!"MPU_SETTINGS", !12, i64 0, !7, i64 4}
!12 = !{!"int", !7, i64 0}
!13 = !{!"xLIST_ITEM", !12, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!14 = !{!"long", !7, i64 0}
!15 = !{!7, !7, i64 0}
!16 = !{!14, !14, i64 0}
!17 = !{i64 2148433239}
!18 = !{!19, !14, i64 0}
!19 = !{!"xLIST", !14, i64 0, !6, i64 8, !20, i64 16}
!20 = !{!"xMINI_LIST_ITEM", !12, i64 0, !6, i64 8, !6, i64 16}


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