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

; 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

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

; Function Attrs: nounwind uwtable
define dso_local void @os_task_wait_event(i8 noundef zeroext %0, i8 noundef zeroext %1, i8 noundef zeroext %2, i32 noundef %3) #2 {
  %5 = alloca i8, align 1
  %6 = alloca i8, align 1
  %7 = alloca i8, align 1
  %8 = alloca i32, align 4
  %9 = alloca i8, align 1
  %10 = alloca i8, align 1
  %11 = alloca ptr, align 8
  store i8 %0, ptr %5, align 1, !tbaa !5
  store i8 %1, ptr %6, align 1, !tbaa !5
  store i8 %2, ptr %7, align 1, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %12 = load i8, ptr %5, align 1, !tbaa !5
  %13 = zext i8 %12 to i32
  %14 = load i8, ptr @nTasks, align 1, !tbaa !5
  %15 = zext i8 %14 to i32
  %16 = icmp slt i32 %13, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %4
  call void @os_on_assert(ptr noundef @.str, i16 noundef zeroext 529, ptr noundef @.str.5)
  br label %18

18:                                               ; preds = %17, %4
  %19 = load i8, ptr %5, align 1, !tbaa !5
  %20 = zext i8 %19 to i64
  %21 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %20
  store ptr %21, ptr %11, align 8, !tbaa !10
  %22 = load i8, ptr %6, align 1, !tbaa !5
  %23 = zext i8 %22 to i32
  %24 = sdiv i32 %23, 8
  %25 = trunc i32 %24 to i8
  store i8 %25, ptr %9, align 1, !tbaa !5
  %26 = load i8, ptr %6, align 1, !tbaa !5
  %27 = zext i8 %26 to i32
  %28 = and i32 %27, 7
  %29 = trunc i32 %28 to i8
  store i8 %29, ptr %10, align 1, !tbaa !5
  %30 = load i8, ptr %10, align 1, !tbaa !5
  %31 = zext i8 %30 to i32
  %32 = shl i32 1, %31
  %33 = load ptr, ptr %11, align 8, !tbaa !10
  %34 = getelementptr inbounds %struct.tcb, ptr %33, i32 0, i32 14
  %35 = getelementptr inbounds %struct.EventQueue_t, ptr %34, i32 0, i32 0
  %36 = load i8, ptr %9, align 1, !tbaa !5
  %37 = zext i8 %36 to i64
  %38 = getelementptr inbounds [1 x i8], ptr %35, i64 0, i64 %37
  %39 = load i8, ptr %38, align 1, !tbaa !5
  %40 = zext i8 %39 to i32
  %41 = or i32 %40, %32
  %42 = trunc i32 %41 to i8
  store i8 %42, ptr %38, align 1, !tbaa !5
  %43 = load i8, ptr %7, align 1, !tbaa !5
  %44 = load ptr, ptr %11, align 8, !tbaa !10
  %45 = getelementptr inbounds %struct.tcb, ptr %44, i32 0, i32 12
  store i8 %43, ptr %45, align 1, !tbaa !12
  %46 = load i32, ptr %8, align 4, !tbaa !8
  %47 = icmp ne i32 %46, 0
  br i1 %47, label %48, label %55

48:                                               ; preds = %18
  %49 = load ptr, ptr %11, align 8, !tbaa !10
  %50 = getelementptr inbounds %struct.tcb, ptr %49, i32 0, i32 13
  store i8 0, ptr %50, align 8, !tbaa !16
  %51 = load i32, ptr %8, align 4, !tbaa !8
  %52 = load ptr, ptr %11, align 8, !tbaa !10
  %53 = getelementptr inbounds %struct.tcb, ptr %52, i32 0, i32 4
  store i32 %51, ptr %53, align 4, !tbaa !17
  %54 = load ptr, ptr %11, align 8, !tbaa !10
  call void @task_waiting_event_timeout_set(ptr noundef %54)
  br label %57

55:                                               ; preds = %18
  %56 = load ptr, ptr %11, align 8, !tbaa !10
  call void @task_waiting_event_set(ptr noundef %56)
  br label %57

57:                                               ; preds = %55, %48
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #3
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @task_waiting_event_timeout_set(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @task_waiting_event_set(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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, !11, i64 0}
!11 = !{!"any pointer", !6, i64 0}
!12 = !{!13, !6, i64 31}
!13 = !{!"tcb", !11, i64 0, !9, i64 8, !9, i64 12, !14, 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, !15, i64 33, !11, i64 40}
!14 = !{!"short", !6, i64 0}
!15 = !{!"", !6, i64 0}
!16 = !{!13, !6, i64 32}
!17 = !{!13, !9, i64 20}


; 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 global [1 x %struct.tcb], align 16
@.str = external hidden unnamed_addr constant [61 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [13 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_wait_event(i8 noundef zeroext %0, i8 noundef zeroext %1, i8 noundef zeroext %2, i32 noundef %3) local_unnamed_addr #1 {
  %5 = load i8, ptr @nTasks, align 1, !tbaa !5
  %6 = icmp ugt i8 %5, %0
  br i1 %6, label %8, label %7

7:                                                ; preds = %4
  tail call void @os_on_assert(ptr noundef nonnull @.str, i16 noundef zeroext 529, ptr noundef nonnull @.str.5) #2
  br label %8

8:                                                ; preds = %7, %4
  %9 = zext i8 %0 to i64
  %10 = getelementptr inbounds [1 x %struct.tcb], ptr @task_list, i64 0, i64 %9
  %11 = lshr i8 %1, 3
  %12 = and i8 %1, 7
  %13 = shl nuw i8 1, %12
  %14 = getelementptr inbounds i8, ptr %10, i64 33
  %15 = zext nneg i8 %11 to i64
  %16 = getelementptr inbounds [1 x i8], ptr %14, i64 0, i64 %15
  %17 = load i8, ptr %16, align 1, !tbaa !5
  %18 = or i8 %17, %13
  store i8 %18, ptr %16, align 1, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %10, i64 31
  store i8 %2, ptr %19, align 1, !tbaa !8
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %23, label %20

20:                                               ; preds = %8
  %21 = getelementptr inbounds i8, ptr %10, i64 32
  store i8 0, ptr %21, align 16, !tbaa !14
  %22 = getelementptr inbounds i8, ptr %10, i64 20
  store i32 %3, ptr %22, align 4, !tbaa !15
  tail call void @task_waiting_event_timeout_set(ptr noundef nonnull %10)
  br label %24

23:                                               ; preds = %8
  tail call void @task_waiting_event_set(ptr noundef nonnull %10)
  br label %24

24:                                               ; preds = %23, %20
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @task_waiting_event_timeout_set(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @task_waiting_event_set(ptr noundef) local_unnamed_addr #1

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, !6, i64 31}
!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 32}
!15 = !{!9, !11, i64 20}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,TailCallElimPass