; 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 }
%struct.anon.0 = type { i8, i8 }

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

; 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 void @VDP_releaseSprites(i16 noundef zeroext %0, i16 noundef zeroext %1) #1 {
  %3 = alloca i16, align 2
  %4 = alloca i16, align 2
  %5 = alloca ptr, align 8
  %6 = 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 8, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #2
  %7 = load i16, ptr %3, align 2, !tbaa !5
  %8 = zext i16 %7 to i64
  %9 = getelementptr inbounds [96 x %struct.VDPSprite], ptr @vdpSpriteCache, i64 0, i64 %8
  store ptr %9, ptr %5, align 8, !tbaa !9
  %10 = load ptr, ptr %5, align 8, !tbaa !9
  %11 = load ptr, ptr @free, align 8, !tbaa !9
  %12 = getelementptr inbounds ptr, ptr %11, i32 1
  store ptr %12, ptr @free, align 8, !tbaa !9
  store ptr %10, ptr %11, align 8, !tbaa !9
  %13 = load i16, ptr %4, align 2, !tbaa !5
  %14 = zext i16 %13 to i32
  %15 = sub nsw i32 %14, 1
  %16 = trunc i32 %15 to i16
  store i16 %16, ptr %6, align 2, !tbaa !5
  br label %17

17:                                               ; preds = %21, %2
  %18 = load i16, ptr %6, align 2, !tbaa !5
  %19 = add i16 %18, -1
  store i16 %19, ptr %6, align 2, !tbaa !5
  %20 = icmp ne i16 %18, 0
  br i1 %20, label %21, label %31

21:                                               ; preds = %17
  %22 = load ptr, ptr %5, align 8, !tbaa !9
  %23 = getelementptr inbounds %struct.VDPSprite, ptr %22, i32 0, i32 1
  %24 = getelementptr inbounds %struct.anon.0, ptr %23, i32 0, i32 1
  %25 = load i8, ptr %24, align 1, !tbaa !11
  %26 = zext i8 %25 to i64
  %27 = getelementptr inbounds [96 x %struct.VDPSprite], ptr @vdpSpriteCache, i64 0, i64 %26
  store ptr %27, ptr %5, align 8, !tbaa !9
  %28 = load ptr, ptr %5, align 8, !tbaa !9
  %29 = load ptr, ptr @free, align 8, !tbaa !9
  %30 = getelementptr inbounds ptr, ptr %29, i32 1
  store ptr %30, ptr @free, align 8, !tbaa !9
  store ptr %28, ptr %29, align 8, !tbaa !9
  br label %17, !llvm.loop !12

31:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  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 = { 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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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
@free = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: write, inaccessiblemem: none) uwtable
define dso_local void @VDP_releaseSprites(i16 noundef zeroext %0, i16 noundef zeroext %1) local_unnamed_addr #0 {
  %3 = zext i16 %0 to i64
  %4 = getelementptr inbounds [96 x %struct.VDPSprite], ptr @vdpSpriteCache, i64 0, i64 %3
  %5 = load ptr, ptr @free, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 8
  store ptr %6, ptr @free, align 8, !tbaa !5
  store ptr %4, ptr %5, align 8, !tbaa !5
  %.06 = add i16 %1, -1
  %.not7 = icmp eq i16 %.06, 0
  br i1 %.not7, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %7 = add i16 %1, -2
  %xtraiter = and i16 %.06, 3
  %lcmp.mod.not = icmp eq i16 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader, %.lr.ph.prol
  %.08.prol = phi i16 [ %.0.prol, %.lr.ph.prol ], [ %.06, %.lr.ph.preheader ]
  %8 = phi i64 [ %10, %.lr.ph.prol ], [ %3, %.lr.ph.preheader ]
  %prol.iter = phi i16 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %gep.prol = getelementptr inbounds [96 x %struct.VDPSprite], ptr getelementptr inbounds (i8, ptr @vdpSpriteCache, i64 1), i64 0, i64 %8, i32 1
  %9 = load i8, ptr %gep.prol, align 1, !tbaa !9
  %10 = zext i8 %9 to i64
  %11 = getelementptr inbounds [96 x %struct.VDPSprite], ptr @vdpSpriteCache, i64 0, i64 %10
  %12 = load ptr, ptr @free, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %12, i64 8
  store ptr %13, ptr @free, align 8, !tbaa !5
  store ptr %11, ptr %12, align 8, !tbaa !5
  %.0.prol = add i16 %.08.prol, -1
  %prol.iter.next = add i16 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i16 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !10

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.08.unr = phi i16 [ %.06, %.lr.ph.preheader ], [ %.0.prol, %.lr.ph.prol ]
  %.unr = phi i64 [ %3, %.lr.ph.preheader ], [ %10, %.lr.ph.prol ]
  %14 = icmp ult i16 %7, 3
  br i1 %14, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.08 = phi i16 [ %.0.3, %.lr.ph ], [ %.08.unr, %.lr.ph.prol.loopexit ]
  %15 = phi i64 [ %32, %.lr.ph ], [ %.unr, %.lr.ph.prol.loopexit ]
  %gep = getelementptr inbounds [96 x %struct.VDPSprite], ptr getelementptr inbounds (i8, ptr @vdpSpriteCache, i64 1), i64 0, i64 %15, i32 1
  %16 = load i8, ptr %gep, align 1, !tbaa !9
  %17 = zext i8 %16 to i64
  %18 = getelementptr inbounds [96 x %struct.VDPSprite], ptr @vdpSpriteCache, i64 0, i64 %17
  %19 = load ptr, ptr @free, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %19, i64 8
  store ptr %20, ptr @free, align 8, !tbaa !5
  store ptr %18, ptr %19, align 8, !tbaa !5
  %gep.1 = getelementptr inbounds [96 x %struct.VDPSprite], ptr getelementptr inbounds (i8, ptr @vdpSpriteCache, i64 1), i64 0, i64 %17, i32 1
  %21 = load i8, ptr %gep.1, align 1, !tbaa !9
  %22 = zext i8 %21 to i64
  %23 = getelementptr inbounds [96 x %struct.VDPSprite], ptr @vdpSpriteCache, i64 0, i64 %22
  %24 = load ptr, ptr @free, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %24, i64 8
  store ptr %25, ptr @free, align 8, !tbaa !5
  store ptr %23, ptr %24, align 8, !tbaa !5
  %gep.2 = getelementptr inbounds [96 x %struct.VDPSprite], ptr getelementptr inbounds (i8, ptr @vdpSpriteCache, i64 1), i64 0, i64 %22, i32 1
  %26 = load i8, ptr %gep.2, align 1, !tbaa !9
  %27 = zext i8 %26 to i64
  %28 = getelementptr inbounds [96 x %struct.VDPSprite], ptr @vdpSpriteCache, i64 0, i64 %27
  %29 = load ptr, ptr @free, align 8, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %29, i64 8
  store ptr %30, ptr @free, align 8, !tbaa !5
  store ptr %28, ptr %29, align 8, !tbaa !5
  %gep.3 = getelementptr inbounds [96 x %struct.VDPSprite], ptr getelementptr inbounds (i8, ptr @vdpSpriteCache, i64 1), i64 0, i64 %27, i32 1
  %31 = load i8, ptr %gep.3, align 1, !tbaa !9
  %32 = zext i8 %31 to i64
  %33 = getelementptr inbounds [96 x %struct.VDPSprite], ptr @vdpSpriteCache, i64 0, i64 %32
  %34 = load ptr, ptr @free, align 8, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %34, i64 8
  store ptr %35, ptr @free, align 8, !tbaa !5
  store ptr %33, ptr %34, align 8, !tbaa !5
  %.0.3 = add i16 %.08, -4
  %.not.3 = icmp eq i16 %.0.3, 0
  br i1 %.not.3, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %2
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, argmem: write, 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" }

!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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.unroll.disable"}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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