; 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 }
%struct.SCB_t = type { i32, i32, i32, i32, i32, i32, [12 x i8], i32, i32, i32, i32, i32, i32, i32, [2 x i32], i32, i32, [4 x i32], [5 x i32], [5 x i32], i32 }

@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
define dso_local void @STOS_Schedule() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  %3 = load ptr, ptr @stos_ker, align 8, !tbaa !5
  store ptr %3, ptr %1, align 8, !tbaa !12
  %4 = load ptr, ptr %1, align 8, !tbaa !12
  %5 = icmp eq ptr %4, null
  br i1 %5, label %6, label %7

6:                                                ; preds = %0
  store i32 1, ptr %2, align 4
  br label %40

7:                                                ; preds = %0
  %8 = load ptr, ptr getelementptr inbounds (%struct.stos_kernel, ptr @stos_ker, i32 0, i32 3), align 8, !tbaa !13
  %9 = getelementptr inbounds %struct.stos_tcb, ptr %8, i32 0, i32 2
  %10 = load i32, ptr %9, align 8, !tbaa !14
  %11 = icmp eq i32 %10, 1
  br i1 %11, label %12, label %21

12:                                               ; preds = %7
  %13 = load ptr, ptr %1, align 8, !tbaa !12
  call void @STOS_RemoveTask(ptr noundef %13)
  %14 = load ptr, ptr %1, align 8, !tbaa !12
  store ptr %14, ptr getelementptr inbounds (%struct.stos_kernel, ptr @stos_ker, i32 0, i32 2), align 8, !tbaa !15
  %15 = load ptr, ptr getelementptr inbounds (%struct.stos_kernel, ptr @stos_ker, i32 0, i32 2), align 8, !tbaa !15
  %16 = getelementptr inbounds %struct.stos_tcb, ptr %15, i32 0, i32 2
  store i32 2, ptr %16, align 8, !tbaa !14
  %17 = load volatile i32, ptr getelementptr inbounds (%struct.SCB_t, ptr inttoptr (i64 3758157056 to ptr), i32 0, i32 1), align 4, !tbaa !16
  %18 = zext i32 %17 to i64
  %19 = or i64 %18, 268435456
  %20 = trunc i64 %19 to i32
  store volatile i32 %20, ptr getelementptr inbounds (%struct.SCB_t, ptr inttoptr (i64 3758157056 to ptr), i32 0, i32 1), align 4, !tbaa !16
  store i32 1, ptr %2, align 4
  br label %40

21:                                               ; preds = %7
  %22 = load ptr, ptr %1, align 8, !tbaa !12
  %23 = getelementptr inbounds %struct.stos_tcb, ptr %22, i32 0, i32 3
  %24 = load i32, ptr %23, align 4, !tbaa !18
  %25 = load ptr, ptr getelementptr inbounds (%struct.stos_kernel, ptr @stos_ker, i32 0, i32 3), align 8, !tbaa !13
  %26 = getelementptr inbounds %struct.stos_tcb, ptr %25, i32 0, i32 3
  %27 = load i32, ptr %26, align 4, !tbaa !18
  %28 = icmp ult i32 %24, %27
  br i1 %28, label %29, label %30

29:                                               ; preds = %21
  store i32 1, ptr %2, align 4
  br label %40

30:                                               ; preds = %21
  %31 = load ptr, ptr %1, align 8, !tbaa !12
  call void @STOS_RemoveTask(ptr noundef %31)
  %32 = load ptr, ptr %1, align 8, !tbaa !12
  store ptr %32, ptr getelementptr inbounds (%struct.stos_kernel, ptr @stos_ker, i32 0, i32 2), align 8, !tbaa !15
  %33 = load ptr, ptr getelementptr inbounds (%struct.stos_kernel, ptr @stos_ker, i32 0, i32 2), align 8, !tbaa !15
  %34 = getelementptr inbounds %struct.stos_tcb, ptr %33, i32 0, i32 2
  store i32 2, ptr %34, align 8, !tbaa !14
  %35 = load ptr, ptr getelementptr inbounds (%struct.stos_kernel, ptr @stos_ker, i32 0, i32 3), align 8, !tbaa !13
  call void @STOS_AddTask(ptr noundef %35, i32 noundef 0)
  %36 = load volatile i32, ptr getelementptr inbounds (%struct.SCB_t, ptr inttoptr (i64 3758157056 to ptr), i32 0, i32 1), align 4, !tbaa !16
  %37 = zext i32 %36 to i64
  %38 = or i64 %37, 268435456
  %39 = trunc i64 %38 to i32
  store volatile i32 %39, ptr getelementptr inbounds (%struct.SCB_t, ptr inttoptr (i64 3758157056 to ptr), i32 0, i32 1), align 4, !tbaa !16
  store i32 0, ptr %2, align 4
  br label %40

40:                                               ; preds = %30, %29, %12, %6
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  %41 = load i32, ptr %2, align 4
  switch i32 %41, label %43 [
    i32 0, label %42
    i32 1, label %42
  ]

42:                                               ; preds = %40, %40
  ret void

43:                                               ; preds = %40
  unreachable
}

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 0}
!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 = !{!7, !7, i64 0}
!13 = !{!6, !7, i64 24}
!14 = !{!10, !11, i64 16}
!15 = !{!6, !7, i64 16}
!16 = !{!17, !11, i64 4}
!17 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !8, i64 24, !11, i64 36, !11, i64 40, !11, i64 44, !11, i64 48, !11, i64 52, !11, i64 56, !11, i64 60, !8, i64 64, !11, i64 72, !11, i64 76, !8, i64 80, !8, i64 96, !8, i64 116, !11, i64 136}
!18 = !{!10, !11, i64 20}


; 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
define dso_local void @STOS_Schedule() local_unnamed_addr #0 {
  %1 = load ptr, ptr @stos_ker, align 8, !tbaa !5
  %2 = icmp eq ptr %1, null
  br i1 %2, label %21, label %3

3:                                                ; preds = %0
  %4 = load ptr, ptr getelementptr inbounds (i8, ptr @stos_ker, i64 24), align 8, !tbaa !12
  %5 = getelementptr inbounds i8, ptr %4, i64 16
  %6 = load i32, ptr %5, align 8, !tbaa !13
  %7 = icmp eq i32 %6, 1
  br i1 %7, label %8, label %10

8:                                                ; preds = %3
  tail call void @STOS_RemoveTask(ptr noundef nonnull %1)
  store ptr %1, ptr getelementptr inbounds (i8, ptr @stos_ker, i64 16), align 8, !tbaa !14
  %9 = getelementptr inbounds i8, ptr %1, i64 16
  store i32 2, ptr %9, align 8, !tbaa !13
  br label %.sink.split

10:                                               ; preds = %3
  %11 = getelementptr inbounds i8, ptr %1, i64 20
  %12 = load i32, ptr %11, align 4, !tbaa !15
  %13 = getelementptr inbounds i8, ptr %4, i64 20
  %14 = load i32, ptr %13, align 4, !tbaa !15
  %15 = icmp ult i32 %12, %14
  br i1 %15, label %21, label %16

16:                                               ; preds = %10
  tail call void @STOS_RemoveTask(ptr noundef nonnull %1)
  store ptr %1, ptr getelementptr inbounds (i8, ptr @stos_ker, i64 16), align 8, !tbaa !14
  %17 = getelementptr inbounds i8, ptr %1, i64 16
  store i32 2, ptr %17, align 8, !tbaa !13
  %18 = load ptr, ptr getelementptr inbounds (i8, ptr @stos_ker, i64 24), align 8, !tbaa !12
  tail call void @STOS_AddTask(ptr noundef %18, i32 noundef 0)
  br label %.sink.split

.sink.split:                                      ; preds = %8, %16
  %19 = load volatile i32, ptr inttoptr (i64 3758157060 to ptr), align 4, !tbaa !16
  %20 = or i32 %19, 268435456
  store volatile i32 %20, ptr inttoptr (i64 3758157060 to ptr), align 4, !tbaa !16
  br label %21

21:                                               ; preds = %.sink.split, %10, %0
  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 0}
!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 = !{!6, !7, i64 24}
!13 = !{!10, !11, i64 16}
!14 = !{!6, !7, i64 16}
!15 = !{!10, !11, i64 20}
!16 = !{!17, !11, i64 4}
!17 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !8, i64 24, !11, i64 36, !11, i64 40, !11, i64 44, !11, i64 48, !11, i64 52, !11, i64 56, !11, i64 60, !8, i64 64, !11, i64 72, !11, i64 76, !8, i64 80, !8, i64 96, !8, i64 116, !11, i64 136}


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