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

; 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 zeroext i16 @VDP_getScreenHeight() #1

; Function Attrs: nounwind uwtable
define dso_local zeroext i16 @getAdjustedVCounterInternal(i16 noundef zeroext %0, i16 noundef zeroext %1) #1 {
  %3 = alloca i16, align 2
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  store i16 %0, ptr %3, align 2, !tbaa !5
  store i16 %1, ptr %4, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #2
  %6 = load i16, ptr %4, align 2, !tbaa !5
  store i16 %6, ptr %5, align 2, !tbaa !5
  %7 = load volatile i16, ptr inttoptr (i64 12582916 to ptr), align 2, !tbaa !5
  %8 = zext i16 %7 to i32
  %9 = and i32 %8, 1
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %11, label %38

11:                                               ; preds = %2
  %12 = load i16, ptr %3, align 2, !tbaa !5
  %13 = zext i16 %12 to i32
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %15, label %24

15:                                               ; preds = %11
  %16 = load i16, ptr %5, align 2, !tbaa !5
  %17 = zext i16 %16 to i32
  %18 = icmp sge i32 %17, 202
  br i1 %18, label %23, label %19

19:                                               ; preds = %15
  %20 = load i16, ptr %5, align 2, !tbaa !5
  %21 = zext i16 %20 to i32
  %22 = icmp sle i32 %21, 10
  br i1 %22, label %23, label %24

23:                                               ; preds = %19, %15
  store i16 8, ptr %5, align 2, !tbaa !5
  br label %37

24:                                               ; preds = %19, %11
  %25 = load i16, ptr %5, align 2, !tbaa !5
  %26 = zext i16 %25 to i32
  %27 = call zeroext i16 @VDP_getScreenHeight()
  %28 = zext i16 %27 to i32
  %29 = icmp sge i32 %26, %28
  br i1 %29, label %30, label %31

30:                                               ; preds = %24
  store i16 8, ptr %5, align 2, !tbaa !5
  br label %36

31:                                               ; preds = %24
  %32 = load i16, ptr %5, align 2, !tbaa !5
  %33 = zext i16 %32 to i32
  %34 = add nsw i32 %33, 16
  %35 = trunc i32 %34 to i16
  store i16 %35, ptr %5, align 2, !tbaa !5
  br label %36

36:                                               ; preds = %31, %30
  br label %37

37:                                               ; preds = %36, %23
  br label %59

38:                                               ; preds = %2
  %39 = load i16, ptr %3, align 2, !tbaa !5
  %40 = zext i16 %39 to i32
  %41 = icmp ne i32 %40, 0
  br i1 %41, label %42, label %47

42:                                               ; preds = %38
  %43 = load i16, ptr %5, align 2, !tbaa !5
  %44 = zext i16 %43 to i32
  %45 = icmp sge i32 %44, 223
  br i1 %45, label %46, label %47

46:                                               ; preds = %42
  store i16 16, ptr %5, align 2, !tbaa !5
  br label %58

47:                                               ; preds = %42, %38
  %48 = load i16, ptr %5, align 2, !tbaa !5
  %49 = zext i16 %48 to i32
  %50 = icmp sge i32 %49, 224
  br i1 %50, label %51, label %52

51:                                               ; preds = %47
  store i16 16, ptr %5, align 2, !tbaa !5
  br label %57

52:                                               ; preds = %47
  %53 = load i16, ptr %5, align 2, !tbaa !5
  %54 = zext i16 %53 to i32
  %55 = add nsw i32 %54, 32
  %56 = trunc i32 %55 to i16
  store i16 %56, ptr %5, align 2, !tbaa !5
  br label %57

57:                                               ; preds = %52, %51
  br label %58

58:                                               ; preds = %57, %46
  br label %59

59:                                               ; preds = %58, %37
  %60 = load i16, ptr %5, align 2, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #2
  ret i16 %60
}

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 = { 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"}


; 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 zeroext i16 @VDP_getScreenHeight() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local zeroext range(i16 16, 15) i16 @getAdjustedVCounterInternal(i16 noundef zeroext %0, i16 noundef zeroext %1) local_unnamed_addr #0 {
  %3 = load volatile i16, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %4 = and i16 %3, 1
  %.not = icmp eq i16 %4, 0
  %5 = icmp ne i16 %0, 0
  br i1 %.not, label %11, label %6

6:                                                ; preds = %2
  %7 = add i16 %1, -202
  %or.cond = icmp ult i16 %7, -191
  %or.cond20 = and i1 %5, %or.cond
  br i1 %or.cond20, label %14, label %8

8:                                                ; preds = %6
  %9 = tail call zeroext i16 @VDP_getScreenHeight()
  %.not19 = icmp ugt i16 %9, %1
  %10 = add i16 %1, 16
  %spec.select = select i1 %.not19, i16 %10, i16 8
  br label %14

11:                                               ; preds = %2
  %12 = icmp ugt i16 %1, 222
  %or.cond5 = and i1 %5, %12
  %13 = icmp ugt i16 %1, 223
  %or.cond21 = or i1 %13, %or.cond5
  %narrow = add nuw nsw i16 %1, 32
  %spec.select22 = select i1 %or.cond21, i16 16, i16 %narrow
  br label %14

14:                                               ; preds = %11, %8, %6
  %.0 = phi i16 [ 8, %6 ], [ %spec.select, %8 ], [ %spec.select22, %11 ]
  ret i16 %.0
}

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

!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"}


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