; LLVM IR from Stephane-D/SGDK :: src (timer)

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

; 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 i64 @getTick() #1

; Function Attrs: nounwind uwtable
declare dso_local void @waitSubTick(i64 noundef) #1

declare zeroext i16 @SYS_getInterruptMaskLevel() #2

declare zeroext i8 @SYS_doVBlankProcess() #2

; Function Attrs: nounwind uwtable
define dso_local void @waitTick(i64 noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  %7 = call zeroext i16 @SYS_getInterruptMaskLevel()
  %8 = zext i16 %7 to i32
  %9 = icmp sge i32 %8, 6
  br i1 %9, label %10, label %18

10:                                               ; preds = %1
  %11 = load i64, ptr %2, align 8, !tbaa !5
  %12 = icmp uge i64 %11, 16777215
  br i1 %12, label %13, label %14

13:                                               ; preds = %10
  call void @waitSubTick(i64 noundef 4294967295)
  br label %17

14:                                               ; preds = %10
  %15 = load i64, ptr %2, align 8, !tbaa !5
  %16 = mul i64 %15, 256
  call void @waitSubTick(i64 noundef %16)
  br label %17

17:                                               ; preds = %14, %13
  br label %43

18:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %19 = call i64 @getTick()
  store i64 %19, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %20 = load i64, ptr %3, align 8, !tbaa !5
  %21 = load i64, ptr %2, align 8, !tbaa !5
  %22 = add i64 %20, %21
  store i64 %22, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %23 = load i64, ptr %4, align 8, !tbaa !5
  %24 = load i64, ptr %3, align 8, !tbaa !5
  %25 = icmp ult i64 %23, %24
  br i1 %25, label %26, label %27

26:                                               ; preds = %18
  store i64 4294967295, ptr %4, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %26, %18
  br label %28

28:                                               ; preds = %38, %27
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %29 = call i64 @getTick()
  store i64 %29, ptr %5, align 8, !tbaa !5
  %30 = load i64, ptr %4, align 8, !tbaa !5
  %31 = load i64, ptr %5, align 8, !tbaa !5
  %32 = sub i64 %30, %31
  store i64 %32, ptr %6, align 8, !tbaa !5
  %33 = load i64, ptr %6, align 8, !tbaa !5
  %34 = icmp sge i64 %33, 5
  br i1 %34, label %35, label %37

35:                                               ; preds = %28
  %36 = call zeroext i8 @SYS_doVBlankProcess()
  br label %37

37:                                               ; preds = %35, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %38

38:                                               ; preds = %37
  %39 = load i64, ptr %5, align 8, !tbaa !5
  %40 = load i64, ptr %4, align 8, !tbaa !5
  %41 = icmp ult i64 %39, %40
  br i1 %41, label %28, label %42, !llvm.loop !9

42:                                               ; preds = %38
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %43

43:                                               ; preds = %42, %17
  ret void
}

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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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"

; Function Attrs: nounwind uwtable
declare dso_local i64 @getTick() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @waitSubTick(i64 noundef) local_unnamed_addr #0

declare zeroext i16 @SYS_getInterruptMaskLevel() local_unnamed_addr #1

declare zeroext i8 @SYS_doVBlankProcess() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @waitTick(i64 noundef %0) local_unnamed_addr #0 {
  %2 = tail call zeroext i16 @SYS_getInterruptMaskLevel() #2
  %3 = icmp ugt i16 %2, 5
  br i1 %3, label %.loopexit.sink.split, label %6

.loopexit.sink.split:                             ; preds = %1
  %4 = icmp ugt i64 %0, 16777214
  %5 = shl nuw nsw i64 %0, 8
  %.sink = select i1 %4, i64 4294967295, i64 %5
  tail call void @waitSubTick(i64 noundef %.sink)
  br label %.loopexit

6:                                                ; preds = %1
  %7 = tail call i64 @getTick()
  %8 = add i64 %7, %0
  %9 = icmp ult i64 %8, %7
  %spec.store.select = select i1 %9, i64 4294967295, i64 %8
  br label %10

10:                                               ; preds = %16, %6
  %11 = tail call i64 @getTick()
  %12 = sub i64 %spec.store.select, %11
  %13 = icmp sgt i64 %12, 4
  br i1 %13, label %14, label %16

14:                                               ; preds = %10
  %15 = tail call zeroext i8 @SYS_doVBlankProcess() #2
  br label %16

16:                                               ; preds = %14, %10
  %17 = icmp ult i64 %11, %spec.store.select
  br i1 %17, label %10, label %.loopexit, !llvm.loop !5

.loopexit:                                        ; preds = %16, %.loopexit.sink.split
  ret void
}

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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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