; 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

; 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_highest_prio_ready_task() #1 {
  %1 = alloca i16, align 2
  %2 = alloca ptr, align 8
  %3 = alloca i8, align 1
  %4 = alloca i8, align 1
  %5 = alloca i32, align 4
  %6 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 2, ptr %1) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #2
  store i8 -1, ptr %3, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #2
  store i8 -1, ptr %4, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #2
  store i16 0, ptr %1, align 2, !tbaa !8
  br label %7

7:                                                ; preds = %37, %0
  %8 = load i16, ptr %1, align 2, !tbaa !8
  %9 = zext i16 %8 to i32
  %10 = load i8, ptr @nTasks, align 1, !tbaa !5
  %11 = zext i8 %10 to i32
  %12 = icmp ne i32 %9, %11
  br i1 %12, label %13, label %40

13:                                               ; preds = %7
  %14 = load i16, ptr %1, align 2, !tbaa !8
  %15 = zext i16 %14 to i64
  %16 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %15
  store ptr %16, ptr %2, align 8, !tbaa !10
  %17 = load ptr, ptr %2, align 8, !tbaa !10
  %18 = getelementptr inbounds %struct.tcb, ptr %17, i32 0, i32 6
  %19 = load i8, ptr %18, align 1, !tbaa !12
  store i8 %19, ptr %6, align 1, !tbaa !5
  %20 = load ptr, ptr %2, align 8, !tbaa !10
  %21 = getelementptr inbounds %struct.tcb, ptr %20, i32 0, i32 1
  %22 = load i32, ptr %21, align 8, !tbaa !16
  store i32 %22, ptr %5, align 4, !tbaa !17
  %23 = load i32, ptr %5, align 4, !tbaa !17
  %24 = icmp eq i32 5, %23
  br i1 %24, label %25, label %36

25:                                               ; preds = %13
  %26 = load i8, ptr %6, align 1, !tbaa !5
  %27 = zext i8 %26 to i32
  %28 = load i8, ptr %4, align 1, !tbaa !5
  %29 = zext i8 %28 to i32
  %30 = icmp slt i32 %27, %29
  br i1 %30, label %31, label %35

31:                                               ; preds = %25
  %32 = load i8, ptr %6, align 1, !tbaa !5
  store i8 %32, ptr %4, align 1, !tbaa !5
  %33 = load i16, ptr %1, align 2, !tbaa !8
  %34 = trunc i16 %33 to i8
  store i8 %34, ptr %3, align 1, !tbaa !5
  br label %35

35:                                               ; preds = %31, %25
  br label %36

36:                                               ; preds = %35, %13
  br label %37

37:                                               ; preds = %36
  %38 = load i16, ptr %1, align 2, !tbaa !8
  %39 = add i16 %38, 1
  store i16 %39, ptr %1, align 2, !tbaa !8
  br label %7, !llvm.loop !18

40:                                               ; preds = %7
  %41 = load i8, ptr %3, align 1, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %1) #2
  ret i8 %41
}

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, !11, i64 0}
!11 = !{!"any pointer", !6, i64 0}
!12 = !{!13, !6, i64 25}
!13 = !{!"tcb", !11, i64 0, !14, i64 8, !14, i64 12, !9, i64 16, !14, 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, !15, i64 33, !11, i64 40}
!14 = !{!"int", !6, i64 0}
!15 = !{!"", !6, i64 0}
!16 = !{!13, !14, i64 8}
!17 = !{!14, !14, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"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

; Function Attrs: nofree norecurse nosync nounwind memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local zeroext i8 @os_task_highest_prio_ready_task() local_unnamed_addr #0 {
  %1 = load i8, ptr @nTasks, align 1, !tbaa !5
  %.not13 = icmp eq i8 %1, 0
  br i1 %.not13, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %0
  %2 = zext i8 %1 to i64
  %xtraiter = and i64 %2, 1
  %3 = icmp eq i8 %1, 1
  br i1 %3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %2, 254
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %.01015 = phi i8 [ -1, %.lr.ph.preheader.new ], [ %.1.1, %.lr.ph ]
  %.01114 = phi i8 [ -1, %.lr.ph.preheader.new ], [ %.112.1, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %4 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %indvars.iv
  %5 = getelementptr inbounds i8, ptr %4, i64 25
  %6 = load i8, ptr %5, align 1, !tbaa !8
  %7 = getelementptr inbounds i8, ptr %4, i64 8
  %8 = load i32, ptr %7, align 8, !tbaa !14
  %9 = icmp eq i32 %8, 5
  %10 = icmp ult i8 %6, %.01114
  %or.cond = select i1 %9, i1 %10, i1 false
  %11 = trunc i64 %indvars.iv to i8
  %.112 = select i1 %or.cond, i8 %6, i8 %.01114
  %.1 = select i1 %or.cond, i8 %11, i8 %.01015
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %12 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %indvars.iv.next
  %13 = getelementptr inbounds i8, ptr %12, i64 25
  %14 = load i8, ptr %13, align 1, !tbaa !8
  %15 = getelementptr inbounds i8, ptr %12, i64 8
  %16 = load i32, ptr %15, align 8, !tbaa !14
  %17 = icmp eq i32 %16, 5
  %18 = icmp ult i8 %14, %.112
  %or.cond.1 = select i1 %17, i1 %18, i1 false
  %19 = trunc i64 %indvars.iv.next to i8
  %.112.1 = select i1 %or.cond.1, i8 %14, i8 %.112
  %.1.1 = select i1 %or.cond.1, i8 %19, i8 %.1
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !15

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.1.lcssa.ph = phi i8 [ poison, %.lr.ph.preheader ], [ %.1.1, %.lr.ph ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.1, %.lr.ph ]
  %.01015.unr = phi i8 [ -1, %.lr.ph.preheader ], [ %.1.1, %.lr.ph ]
  %.01114.unr = phi i8 [ -1, %.lr.ph.preheader ], [ %.112.1, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %20 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %indvars.iv.unr
  %21 = getelementptr inbounds i8, ptr %20, i64 25
  %22 = load i8, ptr %21, align 1, !tbaa !8
  %23 = getelementptr inbounds i8, ptr %20, i64 8
  %24 = load i32, ptr %23, align 8, !tbaa !14
  %25 = icmp eq i32 %24, 5
  %26 = icmp ult i8 %22, %.01114.unr
  %or.cond.epil = select i1 %25, i1 %26, i1 false
  %27 = trunc i64 %indvars.iv.unr to i8
  %.1.epil = select i1 %or.cond.epil, i8 %27, i8 %.01015.unr
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %0
  %.010.lcssa = phi i8 [ -1, %0 ], [ %.1.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.1.epil, %.lr.ph.epil ]
  ret i8 %.010.lcssa
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, 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, !6, i64 25}
!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 = !{!9, !11, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass