; LLVM IR from ponylang/ponyc :: src/libponyrt/sched (cpu)

; 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.timespec = type { i64, i64 }

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 @ponyint_cpu_core_pause(i64 noundef %0, i64 noundef %1, i1 noundef zeroext %2) #2 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i8, align 1
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca %struct.timespec, align 8
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !5
  %10 = zext i1 %2 to i8
  store i8 %10, ptr %6, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %11 = load i64, ptr %4, align 8, !tbaa !5
  %12 = load i64, ptr %5, align 8, !tbaa !5
  %13 = call i64 @ponyint_cpu_tick_diff(i64 noundef %11, i64 noundef %12)
  store i64 %13, ptr %7, align 8, !tbaa !5
  %14 = load i64, ptr %7, align 8, !tbaa !5
  %15 = icmp ult i64 %14, 10000000
  br i1 %15, label %16, label %17

16:                                               ; preds = %3
  store i32 1, ptr %8, align 4
  br label %45

17:                                               ; preds = %3
  %18 = load i8, ptr %6, align 1, !tbaa !9, !range !11, !noundef !12
  %19 = trunc i8 %18 to i1
  br i1 %19, label %20, label %44

20:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 16, ptr %9) #4
  call void @llvm.memset.p0.i64(ptr align 8 %9, i8 0, i64 16, i1 false)
  %21 = load i64, ptr %7, align 8, !tbaa !5
  %22 = icmp ugt i64 %21, 10000000000
  br i1 %22, label %23, label %25

23:                                               ; preds = %20
  %24 = getelementptr inbounds %struct.timespec, ptr %9, i32 0, i32 1
  store i64 30000000, ptr %24, align 8, !tbaa !13
  br label %39

25:                                               ; preds = %20
  %26 = load i64, ptr %7, align 8, !tbaa !5
  %27 = icmp ugt i64 %26, 3000000000
  br i1 %27, label %28, label %30

28:                                               ; preds = %25
  %29 = getelementptr inbounds %struct.timespec, ptr %9, i32 0, i32 1
  store i64 10000000, ptr %29, align 8, !tbaa !13
  br label %38

30:                                               ; preds = %25
  %31 = load i64, ptr %7, align 8, !tbaa !5
  %32 = icmp ugt i64 %31, 1000000000
  br i1 %32, label %33, label %35

33:                                               ; preds = %30
  %34 = getelementptr inbounds %struct.timespec, ptr %9, i32 0, i32 1
  store i64 1000000, ptr %34, align 8, !tbaa !13
  br label %37

35:                                               ; preds = %30
  %36 = getelementptr inbounds %struct.timespec, ptr %9, i32 0, i32 1
  store i64 100000, ptr %36, align 8, !tbaa !13
  br label %37

37:                                               ; preds = %35, %33
  br label %38

38:                                               ; preds = %37, %28
  br label %39

39:                                               ; preds = %38, %23
  br label %40

40:                                               ; preds = %39
  br label %41

41:                                               ; preds = %40
  br label %42

42:                                               ; preds = %41
  %43 = call i32 @nanosleep(ptr noundef %9, ptr noundef null)
  call void @llvm.lifetime.end.p0(i64 16, ptr %9) #4
  br label %44

44:                                               ; preds = %42, %17
  store i32 0, ptr %8, align 4
  br label %45

45:                                               ; preds = %44, %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  %46 = load i32, ptr %8, align 4
  switch i32 %46, label %48 [
    i32 0, label %47
    i32 1, label %47
  ]

47:                                               ; preds = %45, %45
  ret void

48:                                               ; preds = %45
  unreachable
}

; Function Attrs: nounwind uwtable
declare dso_local i64 @ponyint_cpu_tick_diff(i64 noundef, i64 noundef) #2

declare i32 @nanosleep(ptr noundef, ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { "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 #4 = { 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 = !{!"_Bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = !{!14, !6, i64 8}
!14 = !{!"timespec", !6, i64 0, !6, i64 8}


; 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.timespec = type { i64, i64 }

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: mustprogress 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 @ponyint_cpu_core_pause(i64 noundef %0, i64 noundef %1, i1 noundef zeroext %2) local_unnamed_addr #2 {
  %4 = alloca %struct.timespec, align 8
  %5 = tail call i64 @ponyint_cpu_tick_diff(i64 noundef %0, i64 noundef %1)
  %6 = icmp ugt i64 %5, 9999999
  %brmerge.not = and i1 %6, %2
  br i1 %brmerge.not, label %7, label %22

7:                                                ; preds = %3
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %4, i8 0, i64 16, i1 false)
  %8 = icmp ugt i64 %5, 10000000000
  br i1 %8, label %9, label %11

9:                                                ; preds = %7
  %10 = getelementptr inbounds i8, ptr %4, i64 8
  store i64 30000000, ptr %10, align 8, !tbaa !5
  br label %20

11:                                               ; preds = %7
  %12 = icmp ugt i64 %5, 3000000000
  br i1 %12, label %13, label %15

13:                                               ; preds = %11
  %14 = getelementptr inbounds i8, ptr %4, i64 8
  store i64 10000000, ptr %14, align 8, !tbaa !5
  br label %20

15:                                               ; preds = %11
  %16 = icmp ugt i64 %5, 1000000000
  %17 = getelementptr inbounds i8, ptr %4, i64 8
  br i1 %16, label %18, label %19

18:                                               ; preds = %15
  store i64 1000000, ptr %17, align 8, !tbaa !5
  br label %20

19:                                               ; preds = %15
  store i64 100000, ptr %17, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %9, %18, %19, %13
  %21 = call i32 @nanosleep(ptr noundef nonnull %4, ptr noundef null) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #4
  br label %22

22:                                               ; preds = %3, %20
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local i64 @ponyint_cpu_tick_diff(i64 noundef, i64 noundef) local_unnamed_addr #2

declare i32 @nanosleep(ptr noundef, ptr noundef) local_unnamed_addr #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { "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 #4 = { 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, !7, i64 8}
!6 = !{!"timespec", !7, i64 0, !7, i64 8}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}


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