; 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
@.str = external hidden unnamed_addr constant [61 x i8], align 1
@running_tid = external global i8, align 1
@.str.7 = external hidden unnamed_addr constant [21 x i8], align 1

declare void @os_on_assert(ptr noundef, i16 noundef zeroext, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @os_task_run() #1 {
  %1 = load i8, ptr @running_tid, align 1, !tbaa !5
  %2 = zext i8 %1 to i32
  %3 = load i8, ptr @nTasks, align 1, !tbaa !5
  %4 = zext i8 %3 to i32
  %5 = icmp slt i32 %2, %4
  br i1 %5, label %7, label %6

6:                                                ; preds = %0
  call void @os_on_assert(ptr noundef @.str, i16 noundef zeroext 624, ptr noundef @.str.7)
  br label %7

7:                                                ; preds = %6, %0
  %8 = load i8, ptr @running_tid, align 1, !tbaa !5
  %9 = zext i8 %8 to i64
  %10 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %9
  %11 = getelementptr inbounds %struct.tcb, ptr %10, i32 0, i32 0
  %12 = load ptr, ptr %11, align 16, !tbaa !8
  call void %12()
  ret void
}

attributes #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 = { 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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !10, i64 0}
!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}


; 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
@.str = external hidden unnamed_addr constant [61 x i8], align 1
@running_tid = external local_unnamed_addr global i8, align 1
@.str.7 = external hidden unnamed_addr constant [21 x i8], align 1

declare void @os_on_assert(ptr noundef, i16 noundef zeroext, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @os_task_run() local_unnamed_addr #1 {
  %1 = load i8, ptr @running_tid, align 1, !tbaa !5
  %2 = load i8, ptr @nTasks, align 1, !tbaa !5
  %3 = icmp ult i8 %1, %2
  br i1 %3, label %5, label %4

4:                                                ; preds = %0
  tail call void @os_on_assert(ptr noundef nonnull @.str, i16 noundef zeroext 624, ptr noundef nonnull @.str.7) #2
  %.pre = load i8, ptr @running_tid, align 1, !tbaa !5
  br label %5

5:                                                ; preds = %4, %0
  %6 = phi i8 [ %.pre, %4 ], [ %1, %0 ]
  %7 = zext i8 %6 to i64
  %8 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %7
  %9 = load ptr, ptr %8, align 16, !tbaa !8
  tail call void %9() #2
  ret void
}

attributes #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 = { 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, !10, i64 0}
!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}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InstCombinePass,TailCallElimPass