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

@lastTick = external hidden global i64, align 8

; Function Attrs: nounwind uwtable
define dso_local i64 @getSubTickInternal(i16 noundef zeroext %0, i16 noundef zeroext %1, i64 noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  %7 = alloca i64, align 8
  %8 = alloca i16, align 2
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store i16 %0, ptr %5, align 2, !tbaa !5
  store i16 %1, ptr %6, align 2, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #3
  %11 = load i16, ptr %5, align 2, !tbaa !5
  %12 = load i16, ptr %6, align 2, !tbaa !5
  %13 = call zeroext i16 @getAdjustedVCounterInternal(i16 noundef zeroext %11, i16 noundef zeroext %12)
  store i16 %13, ptr %8, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %14 = load i64, ptr %7, align 8, !tbaa !9
  %15 = shl i64 %14, 8
  %16 = load i16, ptr %8, align 2, !tbaa !5
  %17 = zext i16 %16 to i64
  %18 = add i64 %15, %17
  store i64 %18, ptr %9, align 8, !tbaa !9
  %19 = load i64, ptr %9, align 8, !tbaa !9
  %20 = load i64, ptr @lastTick, align 8, !tbaa !9
  %21 = icmp ult i64 %19, %20
  br i1 %21, label %22, label %25

22:                                               ; preds = %3
  %23 = load i64, ptr %9, align 8, !tbaa !9
  %24 = add i64 %23, 256
  store i64 %24, ptr %9, align 8, !tbaa !9
  br label %25

25:                                               ; preds = %22, %3
  %26 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %26, ptr @lastTick, align 8, !tbaa !9
  %27 = load volatile i16, ptr inttoptr (i64 12582916 to ptr), align 2, !tbaa !5
  %28 = zext i16 %27 to i32
  %29 = and i32 %28, 1
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %34

31:                                               ; preds = %25
  %32 = load i64, ptr %9, align 8, !tbaa !9
  %33 = mul i64 %32, 6
  store i64 %33, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %37

34:                                               ; preds = %25
  %35 = load i64, ptr %9, align 8, !tbaa !9
  %36 = mul i64 %35, 5
  store i64 %36, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %37

37:                                               ; preds = %34, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #3
  %38 = load i64, ptr %4, align 8
  ret i64 %38
}

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

declare zeroext i16 @getAdjustedVCounterInternal(i16 noundef zeroext, i16 noundef zeroext) #2

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}


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

@lastTick = external hidden local_unnamed_addr global i64, align 8

; Function Attrs: nounwind uwtable
define dso_local i64 @getSubTickInternal(i16 noundef zeroext %0, i16 noundef zeroext %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = tail call zeroext i16 @getAdjustedVCounterInternal(i16 noundef zeroext %0, i16 noundef zeroext %1) #2
  %5 = shl i64 %2, 8
  %6 = zext i16 %4 to i64
  %7 = add i64 %5, %6
  %8 = load i64, ptr @lastTick, align 8, !tbaa !5
  %9 = icmp ult i64 %7, %8
  %10 = add i64 %7, 256
  %spec.select = select i1 %9, i64 %10, i64 %7
  store i64 %spec.select, ptr @lastTick, align 8, !tbaa !5
  %11 = load volatile i16, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !9
  %12 = and i16 %11, 1
  %.not = icmp eq i16 %12, 0
  %.010.v = select i1 %.not, i64 5, i64 6
  %.010 = mul i64 %.010.v, %spec.select
  ret i64 %.010
}

declare zeroext i16 @getAdjustedVCounterInternal(i16 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #1

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 = !{!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 = !{!"short", !7, i64 0}


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