; 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
@allocStack = external hidden global [80 x ptr], align 16
@free = external hidden global ptr, align 8
@highestVDPSpriteIndex = external dso_local global i16, align 2
@lastAllocatedVDPSprite = external dso_local 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 signext i16 @VDP_allocateSprites(i16 noundef zeroext %0) #1 {
  %2 = alloca i16, align 2
  %3 = alloca i16, align 2
  %4 = alloca ptr, align 8
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  %7 = alloca i16, align 2
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i16, align 2
  store i16 %0, ptr %3, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #2
  %11 = load ptr, ptr @free, align 8, !tbaa !9
  %12 = ptrtoint ptr %11 to i64
  %13 = sub i64 %12, ptrtoint (ptr @allocStack to i64)
  %14 = sdiv exact i64 %13, 8
  %15 = load i16, ptr %3, align 2, !tbaa !5
  %16 = zext i16 %15 to i64
  %17 = icmp slt i64 %14, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %1
  store i16 -1, ptr %2, align 2
  store i32 1, ptr %8, align 4
  br label %74

19:                                               ; preds = %1
  %20 = load ptr, ptr @free, align 8, !tbaa !9
  %21 = getelementptr inbounds ptr, ptr %20, i32 -1
  store ptr %21, ptr @free, align 8, !tbaa !9
  %22 = load ptr, ptr %21, align 8, !tbaa !9
  store ptr %22, ptr %4, align 8, !tbaa !9
  %23 = load ptr, ptr %4, align 8, !tbaa !9
  %24 = ptrtoint ptr %23 to i64
  %25 = sub i64 %24, ptrtoint (ptr @vdpSpriteCache to i64)
  %26 = sdiv exact i64 %25, 8
  %27 = trunc i64 %26 to i16
  store i16 %27, ptr %6, align 2, !tbaa !5
  %28 = load i16, ptr %6, align 2, !tbaa !5
  store i16 %28, ptr %7, align 2, !tbaa !5
  %29 = load i16, ptr %3, align 2, !tbaa !5
  %30 = zext i16 %29 to i32
  %31 = sub nsw i32 %30, 1
  %32 = trunc i32 %31 to i16
  store i16 %32, ptr %5, align 2, !tbaa !5
  br label %33

33:                                               ; preds = %53, %19
  %34 = load i16, ptr %5, align 2, !tbaa !5
  %35 = add i16 %34, -1
  store i16 %35, ptr %5, align 2, !tbaa !5
  %36 = icmp ne i16 %34, 0
  br i1 %36, label %37, label %60

37:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %38 = load ptr, ptr @free, align 8, !tbaa !9
  %39 = getelementptr inbounds ptr, ptr %38, i32 -1
  store ptr %39, ptr @free, align 8, !tbaa !9
  %40 = load ptr, ptr %39, align 8, !tbaa !9
  store ptr %40, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #2
  %41 = load ptr, ptr %9, align 8, !tbaa !9
  %42 = ptrtoint ptr %41 to i64
  %43 = sub i64 %42, ptrtoint (ptr @vdpSpriteCache to i64)
  %44 = sdiv exact i64 %43, 8
  %45 = trunc i64 %44 to i16
  store i16 %45, ptr %10, align 2, !tbaa !5
  %46 = load i16, ptr %10, align 2, !tbaa !5
  %47 = sext i16 %46 to i32
  %48 = load i16, ptr %7, align 2, !tbaa !5
  %49 = sext i16 %48 to i32
  %50 = icmp sgt i32 %47, %49
  br i1 %50, label %51, label %53

51:                                               ; preds = %37
  %52 = load i16, ptr %10, align 2, !tbaa !5
  store i16 %52, ptr %7, align 2, !tbaa !5
  br label %53

53:                                               ; preds = %51, %37
  %54 = load i16, ptr %10, align 2, !tbaa !5
  %55 = trunc i16 %54 to i8
  %56 = load ptr, ptr %4, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.VDPSprite, ptr %56, i32 0, i32 1
  %58 = getelementptr inbounds %struct.anon.0, ptr %57, i32 0, i32 1
  store i8 %55, ptr %58, align 1, !tbaa !11
  %59 = load ptr, ptr %9, align 8, !tbaa !9
  store ptr %59, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  br label %33, !llvm.loop !12

60:                                               ; preds = %33
  %61 = load i16, ptr %7, align 2, !tbaa !5
  %62 = sext i16 %61 to i32
  %63 = load i16, ptr @highestVDPSpriteIndex, align 2, !tbaa !5
  %64 = sext i16 %63 to i32
  %65 = icmp sgt i32 %62, %64
  br i1 %65, label %66, label %68

66:                                               ; preds = %60
  %67 = load i16, ptr %7, align 2, !tbaa !5
  store i16 %67, ptr @highestVDPSpriteIndex, align 2, !tbaa !5
  br label %68

68:                                               ; preds = %66, %60
  %69 = load ptr, ptr %4, align 8, !tbaa !9
  %70 = getelementptr inbounds %struct.VDPSprite, ptr %69, i32 0, i32 1
  %71 = getelementptr inbounds %struct.anon.0, ptr %70, i32 0, i32 1
  store i8 0, ptr %71, align 1, !tbaa !11
  %72 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %72, ptr @lastAllocatedVDPSprite, align 8, !tbaa !9
  %73 = load i16, ptr %6, align 2, !tbaa !5
  store i16 %73, ptr %2, align 2
  store i32 1, ptr %8, align 4
  br label %74

74:                                               ; preds = %68, %18
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %75 = load i16, ptr %2, align 2
  ret i16 %75
}

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
@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
@lastAllocatedVDPSprite = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local signext i16 @VDP_allocateSprites(i16 noundef zeroext %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @free, align 8, !tbaa !5
  %3 = ptrtoint ptr %2 to i64
  %4 = sub i64 %3, ptrtoint (ptr @allocStack to i64)
  %5 = ashr exact i64 %4, 3
  %6 = zext i16 %0 to i64
  %7 = icmp slt i64 %5, %6
  br i1 %7, label %48, label %8

8:                                                ; preds = %1
  %9 = getelementptr inbounds i8, ptr %2, i64 -8
  store ptr %9, ptr @free, align 8, !tbaa !5
  %10 = load ptr, ptr %9, align 8, !tbaa !5
  %11 = ptrtoint ptr %10 to i64
  %12 = sub i64 %11, ptrtoint (ptr @vdpSpriteCache to i64)
  %13 = lshr exact i64 %12, 3
  %14 = trunc i64 %13 to i16
  %.01920 = add i16 %0, -1
  %.not21 = icmp eq i16 %.01920, 0
  br i1 %.not21, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %8
  %xtraiter = and i16 %.01920, 1
  %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
  %15 = load ptr, ptr @free, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %15, i64 -8
  store ptr %16, ptr @free, align 8, !tbaa !5
  %17 = load ptr, ptr %16, align 8, !tbaa !5
  %18 = ptrtoint ptr %17 to i64
  %19 = sub i64 %18, ptrtoint (ptr @vdpSpriteCache to i64)
  %20 = lshr exact i64 %19, 3
  %21 = trunc i64 %20 to i16
  %spec.select.prol = tail call i16 @llvm.smax.i16(i16 %14, i16 %21)
  %22 = trunc i64 %20 to i8
  %23 = getelementptr inbounds i8, ptr %10, i64 3
  store i8 %22, ptr %23, align 1, !tbaa !9
  %.019.prol = add i16 %0, -2
  br label %.lr.ph.prol.loopexit

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.lcssa.unr = phi ptr [ poison, %.lr.ph.preheader ], [ %17, %.lr.ph.prol ]
  %spec.select.lcssa.unr = phi i16 [ poison, %.lr.ph.preheader ], [ %spec.select.prol, %.lr.ph.prol ]
  %.01924.unr = phi i16 [ %.01920, %.lr.ph.preheader ], [ %.019.prol, %.lr.ph.prol ]
  %.01723.unr = phi ptr [ %10, %.lr.ph.preheader ], [ %17, %.lr.ph.prol ]
  %.01822.unr = phi i16 [ %14, %.lr.ph.preheader ], [ %spec.select.prol, %.lr.ph.prol ]
  %24 = icmp eq i16 %0, 2
  br i1 %24, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.01924 = phi i16 [ %.019.1, %.lr.ph ], [ %.01924.unr, %.lr.ph.prol.loopexit ]
  %.01723 = phi ptr [ %36, %.lr.ph ], [ %.01723.unr, %.lr.ph.prol.loopexit ]
  %.01822 = phi i16 [ %spec.select.1, %.lr.ph ], [ %.01822.unr, %.lr.ph.prol.loopexit ]
  %25 = load ptr, ptr @free, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %25, i64 -8
  store ptr %26, ptr @free, align 8, !tbaa !5
  %27 = load ptr, ptr %26, align 8, !tbaa !5
  %28 = ptrtoint ptr %27 to i64
  %29 = sub i64 %28, ptrtoint (ptr @vdpSpriteCache to i64)
  %30 = lshr exact i64 %29, 3
  %31 = trunc i64 %30 to i16
  %spec.select = tail call i16 @llvm.smax.i16(i16 %.01822, i16 %31)
  %32 = trunc i64 %30 to i8
  %33 = getelementptr inbounds i8, ptr %.01723, i64 3
  store i8 %32, ptr %33, align 1, !tbaa !9
  %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
  %36 = load ptr, ptr %35, align 8, !tbaa !5
  %37 = ptrtoint ptr %36 to i64
  %38 = sub i64 %37, ptrtoint (ptr @vdpSpriteCache to i64)
  %39 = lshr exact i64 %38, 3
  %40 = trunc i64 %39 to i16
  %spec.select.1 = tail call i16 @llvm.smax.i16(i16 %spec.select, i16 %40)
  %41 = trunc i64 %39 to i8
  %42 = getelementptr inbounds i8, ptr %27, i64 3
  store i8 %41, ptr %42, align 1, !tbaa !9
  %.019.1 = add i16 %.01924, -2
  %.not.1 = icmp eq i16 %.019.1, 0
  br i1 %.not.1, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %8
  %.018.lcssa = phi i16 [ %14, %8 ], [ %spec.select.lcssa.unr, %.lr.ph.prol.loopexit ], [ %spec.select.1, %.lr.ph ]
  %.017.lcssa = phi ptr [ %10, %8 ], [ %.lcssa.unr, %.lr.ph.prol.loopexit ], [ %36, %.lr.ph ]
  %43 = load i16, ptr @highestVDPSpriteIndex, align 2, !tbaa !12
  %44 = icmp sgt i16 %.018.lcssa, %43
  br i1 %44, label %45, label %46

45:                                               ; preds = %._crit_edge
  store i16 %.018.lcssa, ptr @highestVDPSpriteIndex, align 2, !tbaa !12
  br label %46

46:                                               ; preds = %45, %._crit_edge
  %47 = getelementptr inbounds i8, ptr %.017.lcssa, i64 3
  store i8 0, ptr %47, align 1, !tbaa !9
  store ptr %.017.lcssa, ptr @lastAllocatedVDPSprite, align 8, !tbaa !5
  br label %48

48:                                               ; preds = %1, %46
  %.0 = phi i16 [ %14, %46 ], [ -1, %1 ]
  ret i16 %.0
}

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


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