; 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.xTIME_OUT = type { i64, i32 }
%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
@xTickCount = external hidden global i32, section "privileged_data", align 4
@xNumOfOverflows = 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

declare void @vPortEnterCritical() #1

declare void @vPortExitCritical() #1

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

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

10:                                               ; preds = %2
  call void asm sideeffect " cpsid i ", "~{memory},~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !9
  br label %11

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

12:                                               ; preds = %2
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %15, label %17

15:                                               ; preds = %12
  call void asm sideeffect " cpsid i ", "~{memory},~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !10
  br label %16

16:                                               ; preds = %16, %15
  br label %16

17:                                               ; preds = %12
  call void @vPortEnterCritical()
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %18 = load volatile i32, ptr @xTickCount, align 4, !tbaa !11
  store i32 %18, ptr %6, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %19 = load i32, ptr %6, align 4, !tbaa !11
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.xTIME_OUT, ptr %20, i32 0, i32 1
  %22 = load i32, ptr %21, align 8, !tbaa !13
  %23 = sub i32 %19, %22
  store i32 %23, ptr %7, align 4, !tbaa !11
  %24 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %24, i32 0, i32 14
  %26 = load i8, ptr %25, align 2, !tbaa !16
  %27 = zext i8 %26 to i32
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %32

29:                                               ; preds = %17
  %30 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.tskTaskControlBlock, ptr %30, i32 0, i32 14
  store i8 0, ptr %31, align 2, !tbaa !16
  store i64 1, ptr %5, align 8, !tbaa !20
  br label %67

32:                                               ; preds = %17
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = load i32, ptr %33, align 4, !tbaa !11
  %35 = icmp eq i32 %34, -1
  br i1 %35, label %36, label %37

36:                                               ; preds = %32
  store i64 0, ptr %5, align 8, !tbaa !20
  br label %66

37:                                               ; preds = %32
  %38 = load volatile i64, ptr @xNumOfOverflows, align 8, !tbaa !20
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.xTIME_OUT, ptr %39, i32 0, i32 0
  %41 = load i64, ptr %40, align 8, !tbaa !21
  %42 = icmp ne i64 %38, %41
  br i1 %42, label %43, label %51

43:                                               ; preds = %37
  %44 = load i32, ptr %6, align 4, !tbaa !11
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.xTIME_OUT, ptr %45, i32 0, i32 1
  %47 = load i32, ptr %46, align 8, !tbaa !13
  %48 = icmp uge i32 %44, %47
  br i1 %48, label %49, label %51

49:                                               ; preds = %43
  store i64 1, ptr %5, align 8, !tbaa !20
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  store i32 0, ptr %50, align 4, !tbaa !11
  br label %65

51:                                               ; preds = %43, %37
  %52 = load i32, ptr %7, align 4, !tbaa !11
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = load i32, ptr %53, align 4, !tbaa !11
  %55 = icmp ult i32 %52, %54
  br i1 %55, label %56, label %62

56:                                               ; preds = %51
  %57 = load i32, ptr %7, align 4, !tbaa !11
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = load i32, ptr %58, align 4, !tbaa !11
  %60 = sub i32 %59, %57
  store i32 %60, ptr %58, align 4, !tbaa !11
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  call void @vTaskInternalSetTimeOutState(ptr noundef %61)
  store i64 0, ptr %5, align 8, !tbaa !20
  br label %64

62:                                               ; preds = %51
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  store i32 0, ptr %63, align 4, !tbaa !11
  store i64 1, ptr %5, align 8, !tbaa !20
  br label %64

64:                                               ; preds = %62, %56
  br label %65

65:                                               ; preds = %64, %49
  br label %66

66:                                               ; preds = %65, %36
  br label %67

67:                                               ; preds = %66, %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @vPortExitCritical()
  %68 = load i64, ptr %5, align 8, !tbaa !20
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret i64 %68
}

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

@pxCurrentTCB = external dso_local global ptr, section "privileged_data", align 8
@xTickCount = external hidden global i32, section "privileged_data", align 4
@xNumOfOverflows = external hidden global i64, section "privileged_data", align 8

declare void @vPortEnterCritical() local_unnamed_addr #0

declare void @vPortExitCritical() local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local range(i64 0, 2) i64 @xTaskCheckForTimeOut(ptr noundef %0, ptr noundef %1) local_unnamed_addr #1 section "privileged_functions" {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %4, label %6

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

5:                                                ; preds = %5, %4
  br label %5

6:                                                ; preds = %2
  %7 = icmp eq ptr %1, null
  br i1 %7, label %8, label %10

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

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

10:                                               ; preds = %6
  tail call void @vPortEnterCritical() #2
  %11 = load volatile i32, ptr @xTickCount, align 4, !tbaa !7
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  %13 = load i32, ptr %12, align 8, !tbaa !11
  %14 = sub i32 %11, %13
  %15 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !14
  %16 = getelementptr inbounds i8, ptr %15, i64 198
  %17 = load i8, ptr %16, align 2, !tbaa !16
  %.not = icmp eq i8 %17, 0
  br i1 %.not, label %21, label %18

18:                                               ; preds = %10
  %19 = load volatile ptr, ptr @pxCurrentTCB, align 8, !tbaa !14
  %20 = getelementptr inbounds i8, ptr %19, i64 198
  store i8 0, ptr %20, align 2, !tbaa !16
  br label %33

21:                                               ; preds = %10
  %22 = load i32, ptr %1, align 4, !tbaa !7
  %23 = icmp eq i32 %22, -1
  br i1 %23, label %33, label %24

24:                                               ; preds = %21
  %25 = load volatile i64, ptr @xNumOfOverflows, align 8, !tbaa !20
  %26 = load i64, ptr %0, align 8, !tbaa !21
  %.not19 = icmp eq i64 %25, %26
  %.not20 = icmp ult i32 %11, %13
  %or.cond = select i1 %.not19, i1 true, i1 %.not20
  br i1 %or.cond, label %28, label %27

27:                                               ; preds = %24
  store i32 0, ptr %1, align 4, !tbaa !7
  br label %33

28:                                               ; preds = %24
  %29 = icmp ult i32 %14, %22
  br i1 %29, label %30, label %32

30:                                               ; preds = %28
  %31 = sub i32 %22, %14
  store i32 %31, ptr %1, align 4, !tbaa !7
  tail call void @vTaskInternalSetTimeOutState(ptr noundef nonnull %0)
  br label %33

32:                                               ; preds = %28
  store i32 0, ptr %1, align 4, !tbaa !7
  br label %33

33:                                               ; preds = %21, %30, %32, %27, %18
  %.0 = phi i64 [ 1, %18 ], [ 1, %27 ], [ 0, %30 ], [ 1, %32 ], [ 0, %21 ]
  tail call void @vPortExitCritical() #2
  ret i64 %.0
}

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 = !{i64 2148481830}
!6 = !{i64 2148481971}
!7 = !{!8, !8, i64 0}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!12, !8, i64 8}
!12 = !{!"xTIME_OUT", !13, i64 0, !8, i64 8}
!13 = !{!"long", !9, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"any pointer", !9, i64 0}
!16 = !{!17, !9, i64 198}
!17 = !{!"tskTaskControlBlock", !15, i64 0, !18, i64 8, !19, i64 48, !19, i64 88, !13, i64 128, !15, i64 136, !9, i64 144, !13, i64 160, !13, i64 168, !13, i64 176, !13, i64 184, !9, i64 192, !9, i64 196, !9, i64 197, !9, i64 198}
!18 = !{!"MPU_SETTINGS", !8, i64 0, !9, i64 4}
!19 = !{!"xLIST_ITEM", !8, i64 0, !15, i64 8, !15, i64 16, !15, i64 24, !15, i64 32}
!20 = !{!13, !13, i64 0}
!21 = !{!12, !13, i64 0}


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