; LLVM IR from asllop/AppOS :: core/task (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.TaskStruct = type { i32, ptr, i32, i32, i32, i32, ptr, ptr, ptr, ptr, ptr, i64, i64 }

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

declare ptr @get_current_task(...) #1

; 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 @core_sleep(i64 noundef %0) #2 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %4 = call ptr (...) @get_current_task()
  store ptr %4, ptr %3, align 8, !tbaa !9
  %5 = load i64, ptr %2, align 8, !tbaa !5
  %6 = icmp ne i64 %5, 0
  br i1 %6, label %7, label %26

7:                                                ; preds = %1
  %8 = call i64 @core_timestamp()
  %9 = load i64, ptr %2, align 8, !tbaa !5
  %10 = add i64 %8, %9
  %11 = load ptr, ptr %3, align 8, !tbaa !9
  %12 = getelementptr inbounds %struct.TaskStruct, ptr %11, i32 0, i32 12
  store i64 %10, ptr %12, align 8, !tbaa !11
  br label %13

13:                                               ; preds = %22, %7
  %14 = load ptr, ptr %3, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.TaskStruct, ptr %14, i32 0, i32 12
  %16 = load i64, ptr %15, align 8, !tbaa !11
  %17 = call i64 @core_timestamp()
  %18 = icmp ugt i64 %16, %17
  br i1 %18, label %19, label %23

19:                                               ; preds = %13
  %20 = call zeroext i1 (...) @get_scheduling()
  br i1 %20, label %21, label %22

21:                                               ; preds = %19
  call void (...) @force_task_scheduling()
  br label %22

22:                                               ; preds = %21, %19
  br label %13, !llvm.loop !15

23:                                               ; preds = %13
  %24 = load ptr, ptr %3, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.TaskStruct, ptr %24, i32 0, i32 12
  store i64 0, ptr %25, align 8, !tbaa !11
  br label %32

26:                                               ; preds = %1
  %27 = load ptr, ptr %3, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.TaskStruct, ptr %27, i32 0, i32 12
  store i64 0, ptr %28, align 8, !tbaa !11
  %29 = call zeroext i1 (...) @get_scheduling()
  br i1 %29, label %30, label %31

30:                                               ; preds = %26
  call void (...) @force_task_scheduling()
  br label %31

31:                                               ; preds = %30, %26
  br label %32

32:                                               ; preds = %31, %23
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret void
}

declare zeroext i1 @get_scheduling(...) #1

declare void @force_task_scheduling(...) #1

; Function Attrs: nounwind uwtable
declare dso_local i64 @core_timestamp() #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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !14, i64 80}
!12 = !{!"TaskStruct", !13, i64 0, !10, i64 8, !13, i64 16, !13, i64 20, !13, i64 24, !13, i64 28, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !6, i64 72, !14, i64 80}
!13 = !{!"int", !7, i64 0}
!14 = !{!"long long", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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"

declare ptr @get_current_task(...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @core_sleep(i64 noundef %0) local_unnamed_addr #1 {
  %2 = tail call ptr (...) @get_current_task() #2
  %.not = icmp eq i64 %0, 0
  br i1 %.not, label %15, label %3

3:                                                ; preds = %1
  %4 = tail call i64 @core_timestamp()
  %5 = add i64 %4, %0
  %6 = getelementptr inbounds i8, ptr %2, i64 80
  store i64 %5, ptr %6, align 8, !tbaa !5
  %7 = tail call i64 @core_timestamp()
  %8 = icmp ugt i64 %5, %7
  br i1 %8, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3, %11
  %9 = tail call zeroext i1 (...) @get_scheduling() #2
  br i1 %9, label %10, label %11

10:                                               ; preds = %.lr.ph
  tail call void (...) @force_task_scheduling() #2
  br label %11

11:                                               ; preds = %10, %.lr.ph
  %12 = load i64, ptr %6, align 8, !tbaa !5
  %13 = tail call i64 @core_timestamp()
  %14 = icmp ugt i64 %12, %13
  br i1 %14, label %.lr.ph, label %._crit_edge, !llvm.loop !13

._crit_edge:                                      ; preds = %11, %3
  store i64 0, ptr %6, align 8, !tbaa !5
  br label %19

15:                                               ; preds = %1
  %16 = getelementptr inbounds i8, ptr %2, i64 80
  store i64 0, ptr %16, align 8, !tbaa !5
  %17 = tail call zeroext i1 (...) @get_scheduling() #2
  br i1 %17, label %18, label %19

18:                                               ; preds = %15
  tail call void (...) @force_task_scheduling() #2
  br label %19

19:                                               ; preds = %15, %18, %._crit_edge
  ret void
}

declare zeroext i1 @get_scheduling(...) local_unnamed_addr #0

declare void @force_task_scheduling(...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i64 @core_timestamp() 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, !12, i64 80}
!6 = !{!"TaskStruct", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 20, !7, i64 24, !7, i64 28, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !11, i64 72, !12, i64 80}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!"long long", !8, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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