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

; 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.VDPSprite = type { i16, %union.anon, %union.anon.1, i16 }
%union.anon = type { %struct.anon }
%struct.anon = type { i16 }
%union.anon.1 = type { i16 }

@vdpSpriteCache = external dso_local global [96 x %struct.VDPSprite], align 16
@allocStack = external hidden global [80 x ptr], align 16
@free = external hidden global ptr, align 8
@highestVDPSpriteIndex = external dso_local global i16, align 2

; 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
define dso_local signext i16 @VDP_refreshHighestAllocatedSpriteIndex() #1 {
  %1 = alloca i16, align 2
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i16, align 2
  call void @llvm.lifetime.start.p0(i64 2, ptr %1) #2
  store i16 -1, ptr %1, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  store ptr getelementptr inbounds ([80 x ptr], ptr @allocStack, i64 0, i64 80), ptr %2, align 8, !tbaa !9
  br label %5

5:                                                ; preds = %25, %0
  %6 = load ptr, ptr %2, align 8, !tbaa !9
  %7 = load ptr, ptr @free, align 8, !tbaa !9
  %8 = icmp ugt ptr %6, %7
  br i1 %8, label %9, label %26

9:                                                ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %10 = load ptr, ptr %2, align 8, !tbaa !9
  %11 = getelementptr inbounds ptr, ptr %10, i32 -1
  store ptr %11, ptr %2, align 8, !tbaa !9
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  store ptr %12, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #2
  %13 = load ptr, ptr %3, align 8, !tbaa !9
  %14 = ptrtoint ptr %13 to i64
  %15 = sub i64 %14, ptrtoint (ptr @vdpSpriteCache to i64)
  %16 = sdiv exact i64 %15, 8
  %17 = trunc i64 %16 to i16
  store i16 %17, ptr %4, align 2, !tbaa !5
  %18 = load i16, ptr %4, align 2, !tbaa !5
  %19 = sext i16 %18 to i32
  %20 = load i16, ptr %1, align 2, !tbaa !5
  %21 = sext i16 %20 to i32
  %22 = icmp sgt i32 %19, %21
  br i1 %22, label %23, label %25

23:                                               ; preds = %9
  %24 = load i16, ptr %4, align 2, !tbaa !5
  store i16 %24, ptr %1, align 2, !tbaa !5
  br label %25

25:                                               ; preds = %23, %9
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  br label %5, !llvm.loop !11

26:                                               ; preds = %5
  %27 = load i16, ptr %1, align 2, !tbaa !5
  store i16 %27, ptr @highestVDPSpriteIndex, align 2, !tbaa !5
  %28 = load i16, ptr %1, align 2, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %1) #2
  ret i16 %28
}

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"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

%struct.VDPSprite = type { i16, %union.anon, %union.anon.1, i16 }
%union.anon = type { %struct.anon }
%struct.anon = type { i16 }
%union.anon.1 = type { i16 }

@vdpSpriteCache = external dso_local global [96 x %struct.VDPSprite], align 16
@allocStack = external hidden global [80 x ptr], align 16
@free = external hidden local_unnamed_addr global ptr, align 8
@highestVDPSpriteIndex = external dso_local local_unnamed_addr global i16, align 2

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local signext i16 @VDP_refreshHighestAllocatedSpriteIndex() local_unnamed_addr #0 {
  %1 = load ptr, ptr @free, align 8, !tbaa !5
  %2 = icmp ult ptr %1, getelementptr inbounds (i8, ptr @allocStack, i64 640)
  br i1 %2, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.09 = phi i16 [ %spec.select, %.lr.ph ], [ -1, %0 ]
  %.078 = phi ptr [ %3, %.lr.ph ], [ getelementptr inbounds (i8, ptr @allocStack, i64 640), %0 ]
  %3 = getelementptr inbounds i8, ptr %.078, i64 -8
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = ptrtoint ptr %4 to i64
  %6 = sub i64 %5, ptrtoint (ptr @vdpSpriteCache to i64)
  %7 = lshr exact i64 %6, 3
  %8 = trunc i64 %7 to i16
  %spec.select = tail call i16 @llvm.smax.i16(i16 %.09, i16 %8)
  %9 = icmp ugt ptr %3, %1
  br i1 %9, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %0
  %.0.lcssa = phi i16 [ -1, %0 ], [ %spec.select, %.lr.ph ]
  store i16 %.0.lcssa, ptr @highestVDPSpriteIndex, align 2, !tbaa !11
  ret i16 %.0.lcssa
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.smax.i16(i16, i16) #1

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) 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 speculatable willreturn memory(none) }

!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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}


; Effective O3 passes: GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass