; 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_signal_event(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 i8, align 1
  %8 = alloca i32, align 4
  store i8 %0, ptr %2, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #2
  %9 = load i8, ptr %2, align 1, !tbaa !5
  %10 = zext i8 %9 to i32
  %11 = sdiv i32 %10, 8
  %12 = trunc i32 %11 to i8
  store i8 %12, ptr %4, align 1, !tbaa !5
  %13 = load i8, ptr %2, align 1, !tbaa !5
  %14 = zext i8 %13 to i32
  %15 = and i32 %14, 7
  %16 = trunc i32 %15 to i8
  store i8 %16, ptr %5, align 1, !tbaa !5
  store i8 0, ptr %3, align 1, !tbaa !5
  br label %17

17:                                               ; preds = %92, %1
  %18 = load i8, ptr %3, align 1, !tbaa !5
  %19 = zext i8 %18 to i32
  %20 = load i8, ptr @nTasks, align 1, !tbaa !5
  %21 = zext i8 %20 to i32
  %22 = icmp ne i32 %19, %21
  br i1 %22, label %23, label %95

23:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %24 = load i8, ptr %3, align 1, !tbaa !5
  %25 = zext i8 %24 to i64
  %26 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %25
  %27 = getelementptr inbounds %struct.tcb, ptr %26, i32 0, i32 1
  %28 = load i32, ptr %27, align 8, !tbaa !8
  store i32 %28, ptr %8, align 4, !tbaa !14
  store i8 0, ptr %7, align 1, !tbaa !5
  %29 = load i32, ptr %8, align 4, !tbaa !14
  %30 = icmp eq i32 %29, 3
  br i1 %30, label %34, label %31

31:                                               ; preds = %23
  %32 = load i32, ptr %8, align 4, !tbaa !14
  %33 = icmp eq i32 %32, 4
  br i1 %33, label %34, label %35

34:                                               ; preds = %31, %23
  store i8 1, ptr %7, align 1, !tbaa !5
  br label %35

35:                                               ; preds = %34, %31
  %36 = load i8, ptr %3, align 1, !tbaa !5
  %37 = zext i8 %36 to i64
  %38 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %37
  %39 = getelementptr inbounds %struct.tcb, ptr %38, i32 0, i32 14
  %40 = getelementptr inbounds %struct.EventQueue_t, ptr %39, i32 0, i32 0
  %41 = load i8, ptr %4, align 1, !tbaa !5
  %42 = zext i8 %41 to i64
  %43 = getelementptr inbounds [1 x i8], ptr %40, i64 0, i64 %42
  %44 = load i8, ptr %43, align 1, !tbaa !5
  %45 = zext i8 %44 to i32
  %46 = load i8, ptr %5, align 1, !tbaa !5
  %47 = zext i8 %46 to i32
  %48 = shl i32 1, %47
  %49 = and i32 %45, %48
  %50 = trunc i32 %49 to i8
  store i8 %50, ptr %6, align 1, !tbaa !5
  %51 = load i8, ptr %6, align 1, !tbaa !5
  %52 = zext i8 %51 to i32
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %54, label %91

54:                                               ; preds = %35
  %55 = load i8, ptr %7, align 1, !tbaa !5
  %56 = zext i8 %55 to i32
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %58, label %91

58:                                               ; preds = %54
  %59 = load i8, ptr %5, align 1, !tbaa !5
  %60 = zext i8 %59 to i32
  %61 = shl i32 1, %60
  %62 = xor i32 %61, -1
  %63 = load i8, ptr %3, align 1, !tbaa !5
  %64 = zext i8 %63 to i64
  %65 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %64
  %66 = getelementptr inbounds %struct.tcb, ptr %65, i32 0, i32 14
  %67 = getelementptr inbounds %struct.EventQueue_t, ptr %66, i32 0, i32 0
  %68 = load i8, ptr %4, align 1, !tbaa !5
  %69 = zext i8 %68 to i64
  %70 = getelementptr inbounds [1 x i8], ptr %67, i64 0, i64 %69
  %71 = load i8, ptr %70, align 1, !tbaa !5
  %72 = zext i8 %71 to i32
  %73 = and i32 %72, %62
  %74 = trunc i32 %73 to i8
  store i8 %74, ptr %70, align 1, !tbaa !5
  %75 = load i8, ptr %3, align 1, !tbaa !5
  %76 = zext i8 %75 to i64
  %77 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %76
  %78 = getelementptr inbounds %struct.tcb, ptr %77, i32 0, i32 12
  %79 = load i8, ptr %78, align 1, !tbaa !15
  %80 = zext i8 %79 to i32
  %81 = icmp ne i32 %80, 0
  br i1 %81, label %87, label %82

82:                                               ; preds = %58
  %83 = load i8, ptr %3, align 1, !tbaa !5
  %84 = call zeroext i8 @os_task_wait_queue_empty(i8 noundef zeroext %83)
  %85 = zext i8 %84 to i32
  %86 = icmp ne i32 %85, 0
  br i1 %86, label %87, label %90

87:                                               ; preds = %82, %58
  %88 = load i8, ptr %3, align 1, !tbaa !5
  call void @os_task_clear_wait_queue(i8 noundef zeroext %88)
  %89 = load i8, ptr %3, align 1, !tbaa !5
  call void @task_ready_set(i8 noundef zeroext %89)
  br label %90

90:                                               ; preds = %87, %82
  br label %91

91:                                               ; preds = %90, %54, %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #2
  br label %92

92:                                               ; preds = %91
  %93 = load i8, ptr %3, align 1, !tbaa !5
  %94 = add i8 %93, 1
  store i8 %94, ptr %3, align 1, !tbaa !5
  br label %17, !llvm.loop !16

95:                                               ; preds = %17
  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
}

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

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, !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 = !{!11, !11, i64 0}
!15 = !{!9, !6, i64 31}
!16 = distinct !{!16, !17}
!17 = !{!"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_signal_event(i8 noundef zeroext %0) local_unnamed_addr #0 {
  %2 = load i8, ptr @nTasks, align 1, !tbaa !5
  %.not25 = icmp eq i8 %2, 0
  br i1 %.not25, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %3 = and i8 %0, 7
  %4 = lshr i8 %0, 3
  %5 = zext nneg i8 %4 to i64
  %6 = shl nuw i8 1, %3
  %7 = xor i8 %6, -1
  br label %8

8:                                                ; preds = %.lr.ph, %26
  %.026 = phi i8 [ 0, %.lr.ph ], [ %27, %26 ]
  %9 = zext i8 %.026 to i64
  %10 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %9
  %11 = getelementptr inbounds i8, ptr %10, i64 8
  %12 = load i32, ptr %11, align 8, !tbaa !8
  %13 = add i32 %12, -3
  %or.cond = icmp ult i32 %13, 2
  %14 = getelementptr inbounds i8, ptr %10, i64 33
  %15 = getelementptr inbounds [1 x i8], ptr %14, i64 0, i64 %5
  %16 = load i8, ptr %15, align 1, !tbaa !5
  %17 = and i8 %16, %6
  %18 = icmp ne i8 %17, 0
  %or.cond4 = select i1 %18, i1 %or.cond, i1 false
  br i1 %or.cond4, label %19, label %26

19:                                               ; preds = %8
  %20 = and i8 %16, %7
  store i8 %20, ptr %15, align 1, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %10, i64 31
  %22 = load i8, ptr %21, align 1, !tbaa !14
  %.not23 = icmp eq i8 %22, 0
  br i1 %.not23, label %23, label %25

23:                                               ; preds = %19
  %24 = tail call zeroext i8 @os_task_wait_queue_empty(i8 noundef zeroext %.026)
  %.not24 = icmp eq i8 %24, 0
  br i1 %.not24, label %26, label %25

25:                                               ; preds = %23, %19
  tail call void @os_task_clear_wait_queue(i8 noundef zeroext %.026)
  tail call void @task_ready_set(i8 noundef zeroext %.026)
  br label %26

26:                                               ; preds = %23, %25, %8
  %27 = add i8 %.026, 1
  %28 = load i8, ptr @nTasks, align 1, !tbaa !5
  %.not = icmp eq i8 %27, %28
  br i1 %.not, label %._crit_edge, label %8, !llvm.loop !15

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

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

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" }

!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 31}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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