; LLVM IR from HANDS-FREE/OpenRE :: 3_OS/STM32F1/RTOS/uCOS-II/CORE (os_core)

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

@OSIdleCtr = external dso_local global i32, align 4
@OSIdleCtrMax = external dso_local global i32, align 4
@OSStatRdy = external dso_local global i8, align 1
@OSCPUUsage = external dso_local global i8, align 1
@OSIdleCtrRun = external dso_local global i32, align 4

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

declare i32 @OS_CPU_SR_Save() #1

declare void @OS_CPU_SR_Restore(i32 noundef) #1

declare void @OSTimeDly(i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @OS_TaskStat(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !9
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %4, ptr %2, align 8, !tbaa !5
  br label %5

5:                                                ; preds = %9, %1
  %6 = load i8, ptr @OSStatRdy, align 1, !tbaa !11
  %7 = zext i8 %6 to i32
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %9, label %10

9:                                                ; preds = %5
  call void @OSTimeDly(i32 noundef 40)
  br label %5, !llvm.loop !12

10:                                               ; preds = %5
  %11 = load i32, ptr @OSIdleCtrMax, align 4, !tbaa !9
  %12 = zext i32 %11 to i64
  %13 = udiv i64 %12, 100
  %14 = trunc i64 %13 to i32
  store i32 %14, ptr @OSIdleCtrMax, align 4, !tbaa !9
  %15 = load i32, ptr @OSIdleCtrMax, align 4, !tbaa !9
  %16 = zext i32 %15 to i64
  %17 = icmp eq i64 %16, 0
  br i1 %17, label %18, label %20

18:                                               ; preds = %10
  store i8 0, ptr @OSCPUUsage, align 1, !tbaa !11
  %19 = call zeroext i8 @OSTaskSuspend(i8 noundef zeroext -1)
  br label %20

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

21:                                               ; preds = %21, %20
  %22 = call i32 @OS_CPU_SR_Save()
  store i32 %22, ptr %3, align 4, !tbaa !9
  %23 = load volatile i32, ptr @OSIdleCtr, align 4, !tbaa !9
  store i32 %23, ptr @OSIdleCtrRun, align 4, !tbaa !9
  store volatile i32 0, ptr @OSIdleCtr, align 4, !tbaa !9
  %24 = load i32, ptr %3, align 4, !tbaa !9
  call void @OS_CPU_SR_Restore(i32 noundef %24)
  %25 = load i32, ptr @OSIdleCtrRun, align 4, !tbaa !9
  %26 = load i32, ptr @OSIdleCtrMax, align 4, !tbaa !9
  %27 = udiv i32 %25, %26
  %28 = zext i32 %27 to i64
  %29 = sub i64 100, %28
  %30 = trunc i64 %29 to i8
  store i8 %30, ptr @OSCPUUsage, align 1, !tbaa !11
  call void @OSTaskStatHook()
  call void @OS_TaskStatStkChk()
  call void @OSTimeDly(i32 noundef 20)
  br label %21
}

declare zeroext i8 @OSTaskSuspend(i8 noundef zeroext) #1

declare void @OSTaskStatHook() #1

; Function Attrs: nounwind uwtable
declare dso_local void @OS_TaskStatStkChk() #2

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


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

@OSIdleCtr = external dso_local global i32, align 4
@OSIdleCtrMax = external dso_local local_unnamed_addr global i32, align 4
@OSStatRdy = external dso_local local_unnamed_addr global i8, align 1
@OSCPUUsage = external dso_local local_unnamed_addr global i8, align 1
@OSIdleCtrRun = external dso_local local_unnamed_addr global i32, align 4

declare i32 @OS_CPU_SR_Save() local_unnamed_addr #0

declare void @OS_CPU_SR_Restore(i32 noundef) local_unnamed_addr #0

declare void @OSTimeDly(i32 noundef) local_unnamed_addr #0

; Function Attrs: noreturn nounwind uwtable
define dso_local void @OS_TaskStat(ptr nocapture noundef readnone %0) local_unnamed_addr #1 {
  %2 = load i8, ptr @OSStatRdy, align 1, !tbaa !5
  %3 = icmp eq i8 %2, 0
  br i1 %3, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1, %.lr.ph
  tail call void @OSTimeDly(i32 noundef 40) #3
  %4 = load i8, ptr @OSStatRdy, align 1, !tbaa !5
  %5 = icmp eq i8 %4, 0
  br i1 %5, label %.lr.ph, label %._crit_edge, !llvm.loop !8

._crit_edge:                                      ; preds = %.lr.ph, %1
  %6 = load i32, ptr @OSIdleCtrMax, align 4, !tbaa !10
  %7 = udiv i32 %6, 100
  store i32 %7, ptr @OSIdleCtrMax, align 4, !tbaa !10
  %8 = icmp ult i32 %6, 100
  br i1 %8, label %9, label %.preheader

9:                                                ; preds = %._crit_edge
  store i8 0, ptr @OSCPUUsage, align 1, !tbaa !5
  %10 = tail call zeroext i8 @OSTaskSuspend(i8 noundef zeroext -1) #3
  br label %.preheader

.preheader:                                       ; preds = %9, %._crit_edge
  br label %11

11:                                               ; preds = %.preheader, %11
  %12 = tail call i32 @OS_CPU_SR_Save() #3
  %13 = load volatile i32, ptr @OSIdleCtr, align 4, !tbaa !10
  store i32 %13, ptr @OSIdleCtrRun, align 4, !tbaa !10
  store volatile i32 0, ptr @OSIdleCtr, align 4, !tbaa !10
  tail call void @OS_CPU_SR_Restore(i32 noundef %12) #3
  %14 = load i32, ptr @OSIdleCtrRun, align 4, !tbaa !10
  %15 = load i32, ptr @OSIdleCtrMax, align 4, !tbaa !10
  %16 = udiv i32 %14, %15
  %17 = trunc i32 %16 to i8
  %18 = sub i8 100, %17
  store i8 %18, ptr @OSCPUUsage, align 1, !tbaa !5
  tail call void @OSTaskStatHook() #3
  tail call void @OS_TaskStatStkChk()
  tail call void @OSTimeDly(i32 noundef 20) #3
  br label %11
}

declare zeroext i8 @OSTaskSuspend(i8 noundef zeroext) local_unnamed_addr #0

declare void @OSTaskStatHook() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @OS_TaskStatStkChk() local_unnamed_addr #2

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 = { noreturn 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 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !6, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass