; LLVM IR from af-jk/rtos :: src (stos)

; 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.stos_kernel = type { ptr, ptr, ptr, ptr, %struct.stos_tcb }
%struct.stos_tcb = type { ptr, ptr, i32, i32, i32, i32, ptr, ptr }

@stos_ker = external hidden global %struct.stos_kernel, 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: nounwind uwtable
declare dso_local void @STOS_AddTask(ptr noundef, i32 noundef) #1

; 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 @STOS_RemoveTask(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @STOS_Schedule() #1

; Function Attrs: nounwind uwtable
define dso_local void @sys_tick_handler() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = load ptr, ptr getelementptr inbounds (%struct.stos_kernel, ptr @stos_ker, i32 0, i32 3), align 8, !tbaa !5
  %4 = getelementptr inbounds %struct.stos_tcb, ptr %3, i32 0, i32 5
  %5 = load i32, ptr %4, align 4, !tbaa !12
  %6 = icmp ugt i32 %5, 0
  br i1 %6, label %7, label %12

7:                                                ; preds = %0
  %8 = load ptr, ptr getelementptr inbounds (%struct.stos_kernel, ptr @stos_ker, i32 0, i32 3), align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.stos_tcb, ptr %8, i32 0, i32 5
  %10 = load i32, ptr %9, align 4, !tbaa !12
  %11 = add i32 %10, -1
  store i32 %11, ptr %9, align 4, !tbaa !12
  br label %12

12:                                               ; preds = %7, %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  %13 = load ptr, ptr getelementptr inbounds (%struct.stos_kernel, ptr @stos_ker, i32 0, i32 1), align 8, !tbaa !13
  store ptr %13, ptr %1, align 8, !tbaa !14
  br label %14

14:                                               ; preds = %33, %12
  %15 = load ptr, ptr %1, align 8, !tbaa !14
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %37

17:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  store ptr %1, ptr %2, align 8, !tbaa !14
  %18 = load ptr, ptr %2, align 8, !tbaa !14
  %19 = load ptr, ptr %18, align 8, !tbaa !14
  %20 = getelementptr inbounds %struct.stos_tcb, ptr %19, i32 0, i32 4
  %21 = load i32, ptr %20, align 8, !tbaa !15
  %22 = add i32 %21, -1
  store i32 %22, ptr %20, align 8, !tbaa !15
  %23 = load ptr, ptr %2, align 8, !tbaa !14
  %24 = load ptr, ptr %23, align 8, !tbaa !14
  %25 = getelementptr inbounds %struct.stos_tcb, ptr %24, i32 0, i32 4
  %26 = load i32, ptr %25, align 8, !tbaa !15
  %27 = icmp eq i32 %26, 0
  br i1 %27, label %28, label %33

28:                                               ; preds = %17
  %29 = load ptr, ptr %2, align 8, !tbaa !14
  %30 = load ptr, ptr %29, align 8, !tbaa !14
  call void @STOS_RemoveTask(ptr noundef %30)
  %31 = load ptr, ptr %2, align 8, !tbaa !14
  %32 = load ptr, ptr %31, align 8, !tbaa !14
  call void @STOS_AddTask(ptr noundef %32, i32 noundef 0)
  br label %33

33:                                               ; preds = %28, %17
  %34 = load ptr, ptr %1, align 8, !tbaa !14
  %35 = getelementptr inbounds %struct.stos_tcb, ptr %34, i32 0, i32 6
  %36 = load ptr, ptr %35, align 8, !tbaa !16
  store ptr %36, ptr %1, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  br label %14, !llvm.loop !17

37:                                               ; preds = %14
  call void @STOS_Schedule()
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  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, !7, i64 24}
!6 = !{!"stos_kernel", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"stos_tcb", !7, i64 0, !7, i64 8, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !7, i64 32, !7, i64 40}
!11 = !{!"int", !8, i64 0}
!12 = !{!10, !11, i64 28}
!13 = !{!6, !7, i64 8}
!14 = !{!7, !7, i64 0}
!15 = !{!10, !11, i64 24}
!16 = !{!10, !7, i64 32}
!17 = distinct !{!17, !18}
!18 = !{!"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"

%struct.stos_kernel = type { ptr, ptr, ptr, ptr, %struct.stos_tcb }
%struct.stos_tcb = type { ptr, ptr, i32, i32, i32, i32, ptr, ptr }

@stos_ker = external hidden local_unnamed_addr global %struct.stos_kernel, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @STOS_AddTask(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @STOS_RemoveTask(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @STOS_Schedule() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @sys_tick_handler() local_unnamed_addr #0 {
  %1 = load ptr, ptr getelementptr inbounds (i8, ptr @stos_ker, i64 24), align 8, !tbaa !5
  %2 = getelementptr inbounds i8, ptr %1, i64 28
  %3 = load i32, ptr %2, align 4, !tbaa !12
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %6, label %4

4:                                                ; preds = %0
  %5 = add i32 %3, -1
  store i32 %5, ptr %2, align 4, !tbaa !12
  br label %6

6:                                                ; preds = %4, %0
  %.011 = load ptr, ptr getelementptr inbounds (i8, ptr @stos_ker, i64 8), align 8, !tbaa !13
  %.not1012 = icmp eq ptr %.011, null
  br i1 %.not1012, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %6, %12
  %.013 = phi ptr [ %.0, %12 ], [ %.011, %6 ]
  %7 = getelementptr inbounds i8, ptr %.013, i64 24
  %8 = load i32, ptr %7, align 8, !tbaa !14
  %9 = add i32 %8, -1
  store i32 %9, ptr %7, align 8, !tbaa !14
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %12

11:                                               ; preds = %.lr.ph
  tail call void @STOS_RemoveTask(ptr noundef nonnull %.013)
  tail call void @STOS_AddTask(ptr noundef nonnull %.013, i32 noundef 0)
  br label %12

12:                                               ; preds = %11, %.lr.ph
  %13 = getelementptr inbounds i8, ptr %.013, i64 32
  %.0 = load ptr, ptr %13, align 8, !tbaa !13
  %.not10 = icmp eq ptr %.0, null
  br i1 %.not10, label %._crit_edge, label %.lr.ph, !llvm.loop !15

._crit_edge:                                      ; preds = %12, %6
  tail call void @STOS_Schedule()
  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" }

!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, !7, i64 24}
!6 = !{!"stos_kernel", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"stos_tcb", !7, i64 0, !7, i64 8, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !7, i64 32, !7, i64 40}
!11 = !{!"int", !8, i64 0}
!12 = !{!10, !11, i64 28}
!13 = !{!7, !7, i64 0}
!14 = !{!10, !11, i64 24}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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