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

8:                                                ; preds = %52, %1
  %9 = load i8, ptr %4, align 1, !tbaa !5
  %10 = zext i8 %9 to i32
  %11 = load i8, ptr @nTasks, align 1, !tbaa !5
  %12 = zext i8 %11 to i32
  %13 = icmp ne i32 %10, %12
  br i1 %13, label %14, label %55

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

22:                                               ; preds = %14
  %23 = load ptr, ptr %7, align 8, !tbaa !8
  %24 = getelementptr inbounds %struct.tcb, ptr %23, i32 0, i32 7
  %25 = load i8, ptr %24, align 2, !tbaa !15
  %26 = zext i8 %25 to i32
  %27 = load i8, ptr %2, align 1, !tbaa !5
  %28 = zext i8 %27 to i32
  %29 = icmp eq i32 %26, %28
  br label %30

30:                                               ; preds = %22, %14
  %31 = phi i1 [ false, %14 ], [ %29, %22 ]
  %32 = zext i1 %31 to i32
  %33 = trunc i32 %32 to i8
  store i8 %33, ptr %6, align 1, !tbaa !5
  %34 = load i8, ptr %6, align 1, !tbaa !5
  %35 = zext i8 %34 to i32
  %36 = icmp eq i32 %35, 1
  br i1 %36, label %37, label %51

37:                                               ; preds = %30
  %38 = load ptr, ptr %7, align 8, !tbaa !8
  %39 = getelementptr inbounds %struct.tcb, ptr %38, i32 0, i32 6
  %40 = load i8, ptr %39, align 1, !tbaa !16
  %41 = zext i8 %40 to i32
  %42 = load i8, ptr %3, align 1, !tbaa !5
  %43 = zext i8 %42 to i32
  %44 = icmp slt i32 %41, %43
  br i1 %44, label %45, label %50

45:                                               ; preds = %37
  %46 = load ptr, ptr %7, align 8, !tbaa !8
  %47 = getelementptr inbounds %struct.tcb, ptr %46, i32 0, i32 6
  %48 = load i8, ptr %47, align 1, !tbaa !16
  store i8 %48, ptr %3, align 1, !tbaa !5
  %49 = load i8, ptr %4, align 1, !tbaa !5
  store i8 %49, ptr %5, align 1, !tbaa !5
  br label %50

50:                                               ; preds = %45, %37
  br label %51

51:                                               ; preds = %50, %30
  br label %52

52:                                               ; preds = %51
  %53 = load i8, ptr %4, align 1, !tbaa !5
  %54 = add i8 %53, 1
  store i8 %54, ptr %4, align 1, !tbaa !5
  br label %8, !llvm.loop !17

55:                                               ; preds = %8
  %56 = load i8, ptr %5, align 1, !tbaa !5
  %57 = zext i8 %56 to i32
  %58 = icmp ne i32 255, %57
  br i1 %58, label %59, label %64

59:                                               ; preds = %55
  %60 = load i8, ptr %5, align 1, !tbaa !5
  %61 = zext i8 %60 to i64
  %62 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %61
  %63 = getelementptr inbounds %struct.tcb, ptr %62, i32 0, i32 1
  store i32 5, ptr %63, align 8, !tbaa !10
  br label %64

64:                                               ; preds = %59, %55
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 1, 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
  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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !12, i64 8}
!11 = !{!"tcb", !9, i64 0, !12, i64 8, !12, i64 12, !13, i64 16, !12, 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, !9, i64 40}
!12 = !{!"int", !6, i64 0}
!13 = !{!"short", !6, i64 0}
!14 = !{!"", !6, i64 0}
!15 = !{!11, !6, i64 26}
!16 = !{!11, !6, i64 25}
!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.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(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @os_task_release_waiting_task(i8 noundef zeroext %0) local_unnamed_addr #0 {
  %2 = load i8, ptr @nTasks, align 1, !tbaa !5
  %.not17 = icmp eq i8 %2, 0
  br i1 %.not17, label %._crit_edge.thread, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %3 = zext i8 %2 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.critedge
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %.critedge ]
  %.020 = phi i8 [ -1, %.lr.ph.preheader ], [ %.1, %.critedge ]
  %.01318 = phi i8 [ -1, %.lr.ph.preheader ], [ %.114, %.critedge ]
  %4 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %indvars.iv
  %5 = getelementptr inbounds i8, ptr %4, i64 8
  %6 = load i32, ptr %5, align 8, !tbaa !8
  %7 = icmp eq i32 %6, 1
  br i1 %7, label %8, label %.critedge

8:                                                ; preds = %.lr.ph
  %9 = getelementptr inbounds i8, ptr %4, i64 26
  %10 = load i8, ptr %9, align 2, !tbaa !14
  %11 = icmp eq i8 %10, %0
  br i1 %11, label %12, label %.critedge

12:                                               ; preds = %8
  %13 = getelementptr inbounds i8, ptr %4, i64 25
  %14 = load i8, ptr %13, align 1, !tbaa !15
  %15 = icmp ult i8 %14, %.020
  %16 = trunc nuw i64 %indvars.iv to i8
  %spec.select = select i1 %15, i8 %16, i8 %.01318
  %spec.select16 = tail call i8 @llvm.umin.i8(i8 %14, i8 %.020)
  br label %.critedge

.critedge:                                        ; preds = %12, %.lr.ph, %8
  %.114 = phi i8 [ %.01318, %8 ], [ %spec.select, %12 ], [ %.01318, %.lr.ph ]
  %.1 = phi i8 [ %.020, %8 ], [ %spec.select16, %12 ], [ %.020, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %.not = icmp eq i64 %indvars.iv.next, %3
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !16

._crit_edge:                                      ; preds = %.critedge
  %.not15 = icmp eq i8 %.114, -1
  br i1 %.not15, label %._crit_edge.thread, label %17

17:                                               ; preds = %._crit_edge
  %18 = zext i8 %.114 to i64
  %19 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %18, i32 1
  store i32 5, ptr %19, align 8, !tbaa !8
  br label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %1, %17, %._crit_edge
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.umin.i8(i8, i8) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = !{!9, !6, i64 26}
!15 = !{!9, !6, i64 25}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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