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

; 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.VRAMRegion = type { i16, i16, ptr, ptr }

@spritesPool = external dso_local global ptr, align 8
@maps_addr = external global i16, align 2
@vram = external hidden global %struct.VRAMRegion, align 8
@spriteVramSize = external dso_local global i16, align 2
@usedVDPSprite = external hidden global i16, align 2

; Function Attrs: noinline nounwind uwtable
define dso_local void @SPR_initEx(i16 noundef zeroext %0) #0 {
  %2 = alloca i16, align 2
  %3 = alloca i16, align 2
  %4 = alloca i16, align 2
  store i16 %0, ptr %2, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #4
  call void @SPR_end()
  %5 = call ptr @POOL_create(i16 noundef zeroext 80, i16 noundef zeroext 80)
  store ptr %5, ptr @spritesPool, align 8, !tbaa !9
  %6 = load i16, ptr %2, align 2, !tbaa !5
  %7 = zext i16 %6 to i32
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %9, label %12

9:                                                ; preds = %1
  %10 = load i16, ptr %2, align 2, !tbaa !5
  %11 = zext i16 %10 to i32
  br label %13

12:                                               ; preds = %1
  br label %13

13:                                               ; preds = %12, %9
  %14 = phi i32 [ %11, %9 ], [ 420, %12 ]
  %15 = trunc i32 %14 to i16
  store i16 %15, ptr %4, align 2, !tbaa !5
  %16 = load i16, ptr @maps_addr, align 2, !tbaa !5
  %17 = zext i16 %16 to i32
  %18 = sdiv i32 %17, 32
  %19 = sub nsw i32 %18, 96
  %20 = load i16, ptr %4, align 2, !tbaa !5
  %21 = zext i16 %20 to i32
  %22 = sub nsw i32 %19, %21
  %23 = trunc i32 %22 to i16
  store i16 %23, ptr %3, align 2, !tbaa !5
  %24 = load i16, ptr %3, align 2, !tbaa !5
  %25 = load i16, ptr %4, align 2, !tbaa !5
  call void @VRAM_createRegion(ptr noundef @vram, i16 noundef zeroext %24, i16 noundef zeroext %25)
  %26 = load i16, ptr %4, align 2, !tbaa !5
  store i16 %26, ptr @spriteVramSize, align 2, !tbaa !5
  call void (...) @updateUserTileMaxIndex()
  store i16 0, ptr @usedVDPSprite, align 2, !tbaa !5
  call void @SPR_reset()
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %3) #4
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: noinline nounwind uwtable
declare dso_local void @SPR_end() #0

declare ptr @POOL_create(i16 noundef zeroext, i16 noundef zeroext) #2

declare void @VRAM_createRegion(ptr noundef, i16 noundef zeroext, i16 noundef zeroext) #2

declare void @updateUserTileMaxIndex(...) #2

; Function Attrs: nounwind uwtable
declare dso_local void @SPR_reset() #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { noinline 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 #3 = { 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 #4 = { 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}


; 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.VRAMRegion = type { i16, i16, ptr, ptr }

@spritesPool = external dso_local local_unnamed_addr global ptr, align 8
@maps_addr = external local_unnamed_addr global i16, align 2
@vram = external hidden global %struct.VRAMRegion, align 8
@spriteVramSize = external dso_local local_unnamed_addr global i16, align 2
@usedVDPSprite = external hidden local_unnamed_addr global i16, align 2

; Function Attrs: noinline nounwind uwtable
define dso_local void @SPR_initEx(i16 noundef zeroext %0) local_unnamed_addr #0 {
  tail call void @SPR_end()
  %2 = tail call ptr @POOL_create(i16 noundef zeroext 80, i16 noundef zeroext 80) #3
  store ptr %2, ptr @spritesPool, align 8, !tbaa !5
  %.not = icmp eq i16 %0, 0
  %narrow = select i1 %.not, i16 420, i16 %0
  %3 = load i16, ptr @maps_addr, align 2, !tbaa !9
  %4 = lshr i16 %3, 5
  %reass.sub = sub i16 %4, %narrow
  %5 = add i16 %reass.sub, -96
  tail call void @VRAM_createRegion(ptr noundef nonnull @vram, i16 noundef zeroext %5, i16 noundef zeroext %narrow) #3
  store i16 %narrow, ptr @spriteVramSize, align 2, !tbaa !9
  tail call void (...) @updateUserTileMaxIndex() #3
  store i16 0, ptr @usedVDPSprite, align 2, !tbaa !9
  tail call void @SPR_reset()
  ret void
}

; Function Attrs: noinline nounwind uwtable
declare dso_local void @SPR_end() local_unnamed_addr #0

declare ptr @POOL_create(i16 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #1

declare void @VRAM_createRegion(ptr noundef, i16 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #1

declare void @updateUserTileMaxIndex(...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @SPR_reset() local_unnamed_addr #2

attributes #0 = { noinline 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 #1 = { "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 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 #3 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}


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