; 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

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

14:                                               ; preds = %18, %2
  %15 = load i16, ptr %7, align 2, !tbaa !5
  %16 = add i16 %15, -1
  store i16 %16, ptr %7, align 2, !tbaa !5
  %17 = icmp ne i16 %15, 0
  br i1 %17, label %18, label %27

18:                                               ; preds = %14
  %19 = load i8, ptr %6, align 1, !tbaa !11
  %20 = add i8 %19, 1
  store i8 %20, ptr %6, align 1, !tbaa !11
  %21 = load i8, ptr %6, align 1, !tbaa !11
  %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
  store i8 %21, ptr %24, align 1, !tbaa !11
  %25 = load ptr, ptr %5, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.VDPSprite, ptr %25, i32 1
  store ptr %26, ptr %5, align 8, !tbaa !9
  br label %14, !llvm.loop !12

27:                                               ; preds = %14
  %28 = load ptr, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret ptr %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 = !{!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

; Function Attrs: nofree norecurse nosync nounwind memory(write, inaccessiblemem: none) uwtable
define dso_local nonnull ptr @VDP_linkSprites(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
  %.not10 = icmp eq i16 %1, 0
  br i1 %.not10, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %5 = trunc i16 %0 to i8
  %xtraiter = and i16 %1, 7
  %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
  %.013.prol = phi i16 [ %6, %.lr.ph.prol ], [ %1, %.lr.ph.preheader ]
  %.0812.prol = phi i8 [ %7, %.lr.ph.prol ], [ %5, %.lr.ph.preheader ]
  %.0911.prol = phi ptr [ %9, %.lr.ph.prol ], [ %4, %.lr.ph.preheader ]
  %prol.iter = phi i16 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %6 = add i16 %.013.prol, -1
  %7 = add i8 %.0812.prol, 1
  %8 = getelementptr inbounds i8, ptr %.0911.prol, i64 3
  store i8 %7, ptr %8, align 1, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %.0911.prol, i64 8
  %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 !8

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.lcssa.unr = phi ptr [ poison, %.lr.ph.preheader ], [ %9, %.lr.ph.prol ]
  %.013.unr = phi i16 [ %1, %.lr.ph.preheader ], [ %6, %.lr.ph.prol ]
  %.0812.unr = phi i8 [ %5, %.lr.ph.preheader ], [ %7, %.lr.ph.prol ]
  %.0911.unr = phi ptr [ %4, %.lr.ph.preheader ], [ %9, %.lr.ph.prol ]
  %10 = icmp ult i16 %1, 8
  br i1 %10, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.013 = phi i16 [ %25, %.lr.ph ], [ %.013.unr, %.lr.ph.prol.loopexit ]
  %.0812 = phi i8 [ %26, %.lr.ph ], [ %.0812.unr, %.lr.ph.prol.loopexit ]
  %.0911 = phi ptr [ %28, %.lr.ph ], [ %.0911.unr, %.lr.ph.prol.loopexit ]
  %11 = add i8 %.0812, 1
  %12 = getelementptr inbounds i8, ptr %.0911, i64 3
  store i8 %11, ptr %12, align 1, !tbaa !5
  %13 = add i8 %.0812, 2
  %14 = getelementptr inbounds i8, ptr %.0911, i64 11
  store i8 %13, ptr %14, align 1, !tbaa !5
  %15 = add i8 %.0812, 3
  %16 = getelementptr inbounds i8, ptr %.0911, i64 19
  store i8 %15, ptr %16, align 1, !tbaa !5
  %17 = add i8 %.0812, 4
  %18 = getelementptr inbounds i8, ptr %.0911, i64 27
  store i8 %17, ptr %18, align 1, !tbaa !5
  %19 = add i8 %.0812, 5
  %20 = getelementptr inbounds i8, ptr %.0911, i64 35
  store i8 %19, ptr %20, align 1, !tbaa !5
  %21 = add i8 %.0812, 6
  %22 = getelementptr inbounds i8, ptr %.0911, i64 43
  store i8 %21, ptr %22, align 1, !tbaa !5
  %23 = add i8 %.0812, 7
  %24 = getelementptr inbounds i8, ptr %.0911, i64 51
  store i8 %23, ptr %24, align 1, !tbaa !5
  %25 = add i16 %.013, -8
  %26 = add i8 %.0812, 8
  %27 = getelementptr inbounds i8, ptr %.0911, i64 59
  store i8 %26, ptr %27, align 1, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %.0911, i64 64
  %.not.7 = icmp eq i16 %25, 0
  br i1 %.not.7, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %2
  %.09.lcssa = phi ptr [ %4, %2 ], [ %.lcssa.unr, %.lr.ph.prol.loopexit ], [ %28, %.lr.ph ]
  ret ptr %.09.lcssa
}

attributes #0 = { nofree norecurse nosync nounwind memory(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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.unroll.disable"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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