; 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
declare dso_local void @os_task_clear_wait_queue(i8 noundef zeroext) #1

; Function Attrs: nounwind uwtable
declare hidden void @task_ready_set(i8 noundef zeroext) #1

; Function Attrs: nounwind uwtable
define dso_local void @os_task_tick(i8 noundef zeroext %0, i32 noundef %1) #1 {
  %3 = alloca i8, align 1
  %4 = alloca i32, align 4
  %5 = alloca i8, align 1
  %6 = alloca i32, align 4
  store i8 %0, ptr %3, align 1, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #3
  store i8 0, ptr %5, align 1, !tbaa !5
  br label %7

7:                                                ; preds = %94, %2
  %8 = load i8, ptr %5, align 1, !tbaa !5
  %9 = zext i8 %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 %97

13:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %14 = load i8, ptr %5, align 1, !tbaa !5
  %15 = zext i8 %14 to i64
  %16 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %15
  %17 = getelementptr inbounds %struct.tcb, ptr %16, i32 0, i32 1
  %18 = load i32, ptr %17, align 8, !tbaa !10
  store i32 %18, ptr %6, align 4, !tbaa !8
  %19 = load i32, ptr %6, align 4, !tbaa !8
  %20 = icmp eq i32 %19, 2
  br i1 %20, label %24, label %21

21:                                               ; preds = %13
  %22 = load i32, ptr %6, align 4, !tbaa !8
  %23 = icmp eq i32 %22, 4
  br i1 %23, label %24, label %63

24:                                               ; preds = %21, %13
  %25 = load i8, ptr %5, align 1, !tbaa !5
  %26 = zext i8 %25 to i64
  %27 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %26
  %28 = getelementptr inbounds %struct.tcb, ptr %27, i32 0, i32 13
  %29 = load i8, ptr %28, align 16, !tbaa !15
  %30 = zext i8 %29 to i32
  %31 = load i8, ptr %3, align 1, !tbaa !5
  %32 = zext i8 %31 to i32
  %33 = icmp eq i32 %30, %32
  br i1 %33, label %34, label %62

34:                                               ; preds = %24
  %35 = load i8, ptr %5, align 1, !tbaa !5
  %36 = zext i8 %35 to i64
  %37 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %36
  %38 = getelementptr inbounds %struct.tcb, ptr %37, i32 0, i32 4
  %39 = load i32, ptr %38, align 4, !tbaa !16
  %40 = load i32, ptr %4, align 4, !tbaa !8
  %41 = icmp ule i32 %39, %40
  br i1 %41, label %42, label %53

42:                                               ; preds = %34
  %43 = load i8, ptr %5, align 1, !tbaa !5
  %44 = zext i8 %43 to i64
  %45 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %44
  %46 = getelementptr inbounds %struct.tcb, ptr %45, i32 0, i32 4
  store i32 0, ptr %46, align 4, !tbaa !16
  %47 = load i32, ptr %6, align 4, !tbaa !8
  %48 = icmp eq i32 %47, 4
  br i1 %48, label %49, label %51

49:                                               ; preds = %42
  %50 = load i8, ptr %5, align 1, !tbaa !5
  call void @os_task_clear_wait_queue(i8 noundef zeroext %50)
  br label %51

51:                                               ; preds = %49, %42
  %52 = load i8, ptr %5, align 1, !tbaa !5
  call void @task_ready_set(i8 noundef zeroext %52)
  br label %61

53:                                               ; preds = %34
  %54 = load i32, ptr %4, align 4, !tbaa !8
  %55 = load i8, ptr %5, align 1, !tbaa !5
  %56 = zext i8 %55 to i64
  %57 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %56
  %58 = getelementptr inbounds %struct.tcb, ptr %57, i32 0, i32 4
  %59 = load i32, ptr %58, align 4, !tbaa !16
  %60 = sub i32 %59, %54
  store i32 %60, ptr %58, align 4, !tbaa !16
  br label %61

61:                                               ; preds = %53, %51
  br label %62

62:                                               ; preds = %61, %24
  br label %74

63:                                               ; preds = %21
  %64 = load i32, ptr %6, align 4, !tbaa !8
  %65 = icmp eq i32 %64, 1
  br i1 %65, label %66, label %73

66:                                               ; preds = %63
  %67 = load i8, ptr %5, align 1, !tbaa !5
  %68 = zext i8 %67 to i64
  %69 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %68
  %70 = getelementptr inbounds %struct.tcb, ptr %69, i32 0, i32 4
  %71 = load i32, ptr %70, align 4, !tbaa !16
  %72 = add i32 %71, 1
  store i32 %72, ptr %70, align 4, !tbaa !16
  br label %73

73:                                               ; preds = %66, %63
  br label %74

74:                                               ; preds = %73, %62
  %75 = load i8, ptr %3, align 1, !tbaa !5
  %76 = zext i8 %75 to i32
  %77 = icmp eq i32 %76, 0
  br i1 %77, label %78, label %93

78:                                               ; preds = %74
  %79 = load i8, ptr %5, align 1, !tbaa !5
  %80 = zext i8 %79 to i64
  %81 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %80
  %82 = getelementptr inbounds %struct.tcb, ptr %81, i32 0, i32 8
  %83 = load i8, ptr %82, align 1, !tbaa !17
  %84 = zext i8 %83 to i32
  %85 = icmp ne i32 %84, 255
  br i1 %85, label %86, label %92

86:                                               ; preds = %78
  %87 = load i8, ptr %5, align 1, !tbaa !5
  %88 = zext i8 %87 to i64
  %89 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %88
  %90 = getelementptr inbounds %struct.tcb, ptr %89, i32 0, i32 8
  %91 = load i8, ptr %90, align 1, !tbaa !17
  call void @os_msgQ_tick(i8 noundef zeroext %91)
  br label %92

92:                                               ; preds = %86, %78
  br label %93

93:                                               ; preds = %92, %74
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %94

94:                                               ; preds = %93
  %95 = load i8, ptr %5, align 1, !tbaa !5
  %96 = add i8 %95, 1
  store i8 %96, ptr %5, align 1, !tbaa !5
  br label %7, !llvm.loop !18

97:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #3
  ret void
}

declare void @os_msgQ_tick(i8 noundef zeroext) #2

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 = { "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 #3 = { 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 = !{!"int", !6, i64 0}
!10 = !{!11, !9, i64 8}
!11 = !{!"tcb", !12, i64 0, !9, i64 8, !9, i64 12, !13, i64 16, !9, 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 = !{!"short", !6, i64 0}
!14 = !{!"", !6, i64 0}
!15 = !{!11, !6, i64 32}
!16 = !{!11, !9, i64 20}
!17 = !{!11, !6, i64 27}
!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: nounwind uwtable
declare dso_local void @os_task_clear_wait_queue(i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @task_ready_set(i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @os_task_tick(i8 noundef zeroext %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i8, ptr @nTasks, align 1, !tbaa !5
  %.not26 = icmp eq i8 %3, 0
  br i1 %.not26, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %4 = icmp eq i8 %0, 0
  br i1 %4, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %30
  %.027.us = phi i8 [ %31, %30 ], [ 0, %.lr.ph ]
  %5 = zext i8 %.027.us to i64
  %6 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %5
  %7 = getelementptr inbounds i8, ptr %6, i64 8
  %8 = load i32, ptr %7, align 8, !tbaa !8
  %9 = icmp eq i32 %8, 4
  switch i32 %8, label %26 [
    i32 4, label %14
    i32 2, label %14
    i32 1, label %10
  ]

10:                                               ; preds = %.lr.ph.split.us
  %11 = getelementptr inbounds i8, ptr %6, i64 20
  %12 = load i32, ptr %11, align 4, !tbaa !14
  %13 = add i32 %12, 1
  store i32 %13, ptr %11, align 4, !tbaa !14
  br label %26

14:                                               ; preds = %.lr.ph.split.us, %.lr.ph.split.us
  %15 = getelementptr inbounds i8, ptr %6, i64 32
  %16 = load i8, ptr %15, align 16, !tbaa !15
  %17 = icmp eq i8 %16, 0
  br i1 %17, label %18, label %26

18:                                               ; preds = %14
  %19 = getelementptr inbounds i8, ptr %6, i64 20
  %20 = load i32, ptr %19, align 4, !tbaa !14
  %.not24.us = icmp ugt i32 %20, %1
  br i1 %.not24.us, label %24, label %21

21:                                               ; preds = %18
  store i32 0, ptr %19, align 4, !tbaa !14
  br i1 %9, label %22, label %23

22:                                               ; preds = %21
  tail call void @os_task_clear_wait_queue(i8 noundef zeroext %.027.us)
  br label %23

23:                                               ; preds = %22, %21
  tail call void @task_ready_set(i8 noundef zeroext %.027.us)
  br label %26

24:                                               ; preds = %18
  %25 = sub i32 %20, %1
  store i32 %25, ptr %19, align 4, !tbaa !14
  br label %26

26:                                               ; preds = %24, %23, %14, %10, %.lr.ph.split.us
  %27 = getelementptr inbounds i8, ptr %6, i64 27
  %28 = load i8, ptr %27, align 1, !tbaa !16
  %.not25.us = icmp eq i8 %28, -1
  br i1 %.not25.us, label %30, label %29

29:                                               ; preds = %26
  tail call void @os_msgQ_tick(i8 noundef zeroext %28) #2
  br label %30

30:                                               ; preds = %29, %26
  %31 = add i8 %.027.us, 1
  %32 = load i8, ptr @nTasks, align 1, !tbaa !5
  %.not.us = icmp eq i8 %31, %32
  br i1 %.not.us, label %._crit_edge, label %.lr.ph.split.us, !llvm.loop !17

.lr.ph.split:                                     ; preds = %.lr.ph, %55
  %33 = phi i8 [ %56, %55 ], [ %3, %.lr.ph ]
  %.027 = phi i8 [ %57, %55 ], [ 0, %.lr.ph ]
  %34 = zext i8 %.027 to i64
  %35 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %34
  %36 = getelementptr inbounds i8, ptr %35, i64 8
  %37 = load i32, ptr %36, align 8, !tbaa !8
  %38 = icmp eq i32 %37, 4
  switch i32 %37, label %55 [
    i32 4, label %39
    i32 2, label %39
    i32 1, label %51
  ]

39:                                               ; preds = %.lr.ph.split, %.lr.ph.split
  %40 = getelementptr inbounds i8, ptr %35, i64 32
  %41 = load i8, ptr %40, align 16, !tbaa !15
  %42 = icmp eq i8 %41, %0
  br i1 %42, label %43, label %55

43:                                               ; preds = %39
  %44 = getelementptr inbounds i8, ptr %35, i64 20
  %45 = load i32, ptr %44, align 4, !tbaa !14
  %.not24 = icmp ugt i32 %45, %1
  br i1 %.not24, label %49, label %46

46:                                               ; preds = %43
  store i32 0, ptr %44, align 4, !tbaa !14
  br i1 %38, label %47, label %48

47:                                               ; preds = %46
  tail call void @os_task_clear_wait_queue(i8 noundef zeroext %.027)
  br label %48

48:                                               ; preds = %47, %46
  tail call void @task_ready_set(i8 noundef zeroext %.027)
  %.pre = load i8, ptr @nTasks, align 1, !tbaa !5
  br label %55

49:                                               ; preds = %43
  %50 = sub i32 %45, %1
  store i32 %50, ptr %44, align 4, !tbaa !14
  br label %55

51:                                               ; preds = %.lr.ph.split
  %52 = getelementptr inbounds i8, ptr %35, i64 20
  %53 = load i32, ptr %52, align 4, !tbaa !14
  %54 = add i32 %53, 1
  store i32 %54, ptr %52, align 4, !tbaa !14
  br label %55

55:                                               ; preds = %.lr.ph.split, %51, %39, %49, %48
  %56 = phi i8 [ %33, %.lr.ph.split ], [ %33, %51 ], [ %33, %39 ], [ %33, %49 ], [ %.pre, %48 ]
  %57 = add i8 %.027, 1
  %.not = icmp eq i8 %57, %56
  br i1 %.not, label %._crit_edge, label %.lr.ph.split, !llvm.loop !17

._crit_edge:                                      ; preds = %55, %30, %2
  ret void
}

declare void @os_msgQ_tick(i8 noundef zeroext) local_unnamed_addr #1

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" }
attributes #1 = { "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, !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, !11, i64 20}
!15 = !{!9, !6, i64 32}
!16 = !{!9, !6, i64 27}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass