; LLVM IR from cocoOS/cocoOS :: src (os_task)

; 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.tcb = type { ptr, i32, i32, i16, i32, i8, i8, i8, i8, i8, i8, i8, i8, i8, %struct.EventQueue_t, ptr }
%struct.EventQueue_t = type { [1 x i8] }

@nTasks = external hidden global i8, align 1
@task_list = external hidden global [1 x %struct.tcb], align 16
@last_running_task = external global i8, align 1

; 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

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @os_task_next_ready_task() #1 {
  %1 = alloca i16, align 2
  %2 = alloca i8, align 1
  %3 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 2, ptr %1) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #2
  %4 = load i8, ptr @last_running_task, align 1, !tbaa !5
  %5 = zext i8 %4 to i32
  %6 = icmp eq i32 255, %5
  br i1 %6, label %7, label %8

7:                                                ; preds = %0
  store i16 0, ptr %1, align 2, !tbaa !8
  br label %20

8:                                                ; preds = %0
  %9 = load i8, ptr @last_running_task, align 1, !tbaa !5
  %10 = zext i8 %9 to i32
  %11 = add nsw i32 %10, 1
  %12 = trunc i32 %11 to i16
  store i16 %12, ptr %1, align 2, !tbaa !8
  %13 = load i16, ptr %1, align 2, !tbaa !8
  %14 = zext i16 %13 to i32
  %15 = load i8, ptr @nTasks, align 1, !tbaa !5
  %16 = zext i8 %15 to i32
  %17 = icmp sge i32 %14, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %8
  store i16 0, ptr %1, align 2, !tbaa !8
  br label %19

19:                                               ; preds = %18, %8
  br label %20

20:                                               ; preds = %19, %7
  store i8 0, ptr %2, align 1, !tbaa !5
  store i8 0, ptr %3, align 1, !tbaa !5
  br label %21

21:                                               ; preds = %41, %20
  %22 = load i16, ptr %1, align 2, !tbaa !8
  %23 = zext i16 %22 to i64
  %24 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %23
  %25 = getelementptr inbounds %struct.tcb, ptr %24, i32 0, i32 1
  %26 = load i32, ptr %25, align 8, !tbaa !10
  %27 = icmp eq i32 5, %26
  br i1 %27, label %28, label %31

28:                                               ; preds = %21
  %29 = load i16, ptr %1, align 2, !tbaa !8
  %30 = trunc i16 %29 to i8
  store i8 %30, ptr @last_running_task, align 1, !tbaa !5
  store i8 1, ptr %2, align 1, !tbaa !5
  br label %48

31:                                               ; preds = %21
  %32 = load i16, ptr %1, align 2, !tbaa !8
  %33 = add i16 %32, 1
  store i16 %33, ptr %1, align 2, !tbaa !8
  %34 = load i16, ptr %1, align 2, !tbaa !8
  %35 = zext i16 %34 to i32
  %36 = load i8, ptr @nTasks, align 1, !tbaa !5
  %37 = zext i8 %36 to i32
  %38 = icmp eq i32 %35, %37
  br i1 %38, label %39, label %40

39:                                               ; preds = %31
  store i16 0, ptr %1, align 2, !tbaa !8
  br label %40

40:                                               ; preds = %39, %31
  br label %41

41:                                               ; preds = %40
  %42 = load i8, ptr %3, align 1, !tbaa !5
  %43 = add i8 %42, 1
  store i8 %43, ptr %3, align 1, !tbaa !5
  %44 = zext i8 %43 to i32
  %45 = load i8, ptr @nTasks, align 1, !tbaa !5
  %46 = zext i8 %45 to i32
  %47 = icmp ne i32 %44, %46
  br i1 %47, label %21, label %48, !llvm.loop !15

48:                                               ; preds = %41, %28
  %49 = load i8, ptr %2, align 1, !tbaa !5
  %50 = icmp ne i8 %49, 0
  br i1 %50, label %52, label %51

51:                                               ; preds = %48
  store i8 -1, ptr @last_running_task, align 1, !tbaa !5
  br label %52

52:                                               ; preds = %51, %48
  %53 = load i8, ptr @last_running_task, align 1, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %1) #2
  ret i8 %53
}

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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"short", !6, i64 0}
!10 = !{!11, !13, i64 8}
!11 = !{!"tcb", !12, i64 0, !13, i64 8, !13, i64 12, !9, i64 16, !13, i64 20, !6, i64 24, !6, i64 25, !6, i64 26, !6, i64 27, !6, i64 28, !6, i64 29, !6, i64 30, !6, i64 31, !6, i64 32, !14, i64 33, !12, i64 40}
!12 = !{!"any pointer", !6, i64 0}
!13 = !{!"int", !6, i64 0}
!14 = !{!"", !6, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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.tcb = type { ptr, i32, i32, i16, i32, i8, i8, i8, i8, i8, i8, i8, i8, i8, %struct.EventQueue_t, ptr }
%struct.EventQueue_t = type { [1 x i8] }

@nTasks = external hidden local_unnamed_addr global i8, align 1
@task_list = external hidden local_unnamed_addr global [1 x %struct.tcb], align 16
@last_running_task = external local_unnamed_addr global i8, align 1

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local zeroext i8 @os_task_next_ready_task() local_unnamed_addr #0 {
  %1 = load i8, ptr @last_running_task, align 1, !tbaa !5
  %2 = icmp eq i8 %1, -1
  %3 = zext i8 %1 to i16
  %4 = add nuw nsw i16 %3, 1
  %5 = load i8, ptr @nTasks, align 1
  %6 = zext i8 %5 to i16
  %.not = icmp ult i16 %4, %6
  %spec.store.select = select i1 %.not, i16 %4, i16 0
  %.09 = select i1 %2, i16 0, i16 %spec.store.select
  br label %7

7:                                                ; preds = %12, %0
  %.1 = phi i16 [ %.09, %0 ], [ %spec.store.select1, %12 ]
  %.0 = phi i8 [ 0, %0 ], [ %15, %12 ]
  %8 = zext i16 %.1 to i64
  %9 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %8, i32 1
  %10 = load i32, ptr %9, align 8, !tbaa !8
  %.not13 = icmp eq i32 %10, 5
  br i1 %.not13, label %.thread, label %12

.thread:                                          ; preds = %7
  %11 = trunc i16 %.1 to i8
  br label %.loopexit

12:                                               ; preds = %7
  %13 = add i16 %.1, 1
  %14 = icmp eq i16 %13, %6
  %spec.store.select1 = select i1 %14, i16 0, i16 %13
  %15 = add i8 %.0, 1
  %.not11 = icmp eq i8 %15, %5
  br i1 %.not11, label %.loopexit, label %7, !llvm.loop !14

.loopexit:                                        ; preds = %12, %.thread
  %storemerge = phi i8 [ %11, %.thread ], [ -1, %12 ]
  store i8 %storemerge, ptr @last_running_task, align 1, !tbaa !5
  ret i8 %storemerge
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !11, i64 8}
!9 = !{!"tcb", !10, i64 0, !11, i64 8, !11, i64 12, !12, i64 16, !11, i64 20, !6, i64 24, !6, i64 25, !6, i64 26, !6, i64 27, !6, i64 28, !6, i64 29, !6, i64 30, !6, i64 31, !6, i64 32, !13, i64 33, !10, i64 40}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!"int", !6, i64 0}
!12 = !{!"short", !6, i64 0}
!13 = !{!"", !6, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass