; 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 }
%struct.Sprite = type { i16, i16, ptr, ptr, ptr, ptr, i16, i16, i16, i16, i16, i16, i16, i64, ptr, ptr }
%struct.SpriteDefinition = type { i16, i16, ptr, i16, ptr, i16, i16 }

@maps_addr = external global i16, align 2
@vram = external hidden global %struct.VRAMRegion, align 8
@spriteVramSize = external dso_local global i16, align 2
@firstSprite = 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

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

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

declare void @VRAM_releaseRegion(ptr noundef) #1

declare void @MEM_pack() #1

declare signext i16 @VRAM_alloc(ptr noundef, i16 noundef zeroext) #1

; Function Attrs: noinline nounwind uwtable
define dso_local void @SPR_defragVRAM() #2 {
  %1 = alloca ptr, align 8
  %2 = alloca i16, align 2
  %3 = alloca i16, align 2
  %4 = alloca i16, align 2
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  call void @VRAM_releaseRegion(ptr noundef @vram)
  call void @MEM_pack()
  %5 = load i16, ptr @maps_addr, align 2, !tbaa !5
  %6 = zext i16 %5 to i32
  %7 = sdiv i32 %6, 32
  %8 = sub nsw i32 %7, 96
  %9 = load i16, ptr @spriteVramSize, align 2, !tbaa !5
  %10 = zext i16 %9 to i32
  %11 = sub nsw i32 %8, %10
  %12 = trunc i32 %11 to i16
  %13 = load i16, ptr @spriteVramSize, align 2, !tbaa !5
  call void @VRAM_createRegion(ptr noundef @vram, i16 noundef zeroext %12, i16 noundef zeroext %13)
  %14 = load ptr, ptr @firstSprite, align 8, !tbaa !9
  store ptr %14, ptr %1, align 8, !tbaa !9
  br label %15

15:                                               ; preds = %66, %0
  %16 = load ptr, ptr %1, align 8, !tbaa !9
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %70

18:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 2, ptr %2) #3
  %19 = load ptr, ptr %1, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.Sprite, ptr %19, i32 0, i32 0
  %21 = load i16, ptr %20, align 8, !tbaa !11
  store i16 %21, ptr %2, align 2, !tbaa !5
  %22 = load i16, ptr %2, align 2, !tbaa !5
  %23 = zext i16 %22 to i32
  %24 = and i32 %23, 2048
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %66

26:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #3
  %27 = load ptr, ptr %1, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.Sprite, ptr %27, i32 0, i32 2
  %29 = load ptr, ptr %28, align 8, !tbaa !14
  %30 = getelementptr inbounds %struct.SpriteDefinition, ptr %29, i32 0, i32 5
  %31 = load i16, ptr %30, align 8, !tbaa !15
  %32 = call signext i16 @VRAM_alloc(ptr noundef @vram, i16 noundef zeroext %31)
  store i16 %32, ptr %3, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #3
  %33 = load ptr, ptr %1, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.Sprite, ptr %33, i32 0, i32 12
  %35 = load i16, ptr %34, align 4, !tbaa !17
  store i16 %35, ptr %4, align 2, !tbaa !5
  %36 = load i16, ptr %4, align 2, !tbaa !5
  %37 = zext i16 %36 to i32
  %38 = and i32 %37, -63489
  %39 = load i16, ptr %3, align 2, !tbaa !5
  %40 = zext i16 %39 to i32
  %41 = icmp ne i32 %38, %40
  br i1 %41, label %42, label %65

42:                                               ; preds = %26
  %43 = load i16, ptr %3, align 2, !tbaa !5
  %44 = zext i16 %43 to i32
  %45 = load i16, ptr %4, align 2, !tbaa !5
  %46 = zext i16 %45 to i32
  %47 = and i32 %46, 63488
  %48 = or i32 %44, %47
  %49 = trunc i32 %48 to i16
  %50 = load ptr, ptr %1, align 8, !tbaa !9
  %51 = getelementptr inbounds %struct.Sprite, ptr %50, i32 0, i32 12
  store i16 %49, ptr %51, align 4, !tbaa !17
  %52 = load i16, ptr %2, align 2, !tbaa !5
  %53 = zext i16 %52 to i32
  %54 = and i32 %53, 1024
  %55 = icmp ne i32 %54, 0
  br i1 %55, label %56, label %61

56:                                               ; preds = %42
  %57 = load i16, ptr %2, align 2, !tbaa !5
  %58 = zext i16 %57 to i32
  %59 = or i32 %58, 4
  %60 = trunc i32 %59 to i16
  store i16 %60, ptr %2, align 2, !tbaa !5
  br label %61

61:                                               ; preds = %56, %42
  %62 = load i16, ptr %2, align 2, !tbaa !5
  %63 = load ptr, ptr %1, align 8, !tbaa !9
  %64 = getelementptr inbounds %struct.Sprite, ptr %63, i32 0, i32 0
  store i16 %62, ptr %64, align 8, !tbaa !11
  br label %65

65:                                               ; preds = %61, %26
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %3) #3
  br label %66

66:                                               ; preds = %65, %18
  %67 = load ptr, ptr %1, align 8, !tbaa !9
  %68 = getelementptr inbounds %struct.Sprite, ptr %67, i32 0, i32 15
  %69 = load ptr, ptr %68, align 8, !tbaa !18
  store ptr %69, ptr %1, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 2, ptr %2) #3
  br label %15, !llvm.loop !19

70:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #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 = !{!"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 = !{!12, !6, i64 0}
!12 = !{!"Sprite", !6, i64 0, !6, i64 2, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !6, i64 40, !6, i64 42, !6, i64 44, !6, i64 46, !6, i64 48, !6, i64 50, !6, i64 52, !13, i64 56, !10, i64 64, !10, i64 72}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !10, i64 8}
!15 = !{!16, !6, i64 32}
!16 = !{!"", !6, i64 0, !6, i64 2, !10, i64 8, !6, i64 16, !10, i64 24, !6, i64 32, !6, i64 34}
!17 = !{!12, !6, i64 52}
!18 = !{!12, !10, i64 72}
!19 = distinct !{!19, !20}
!20 = !{!"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.VRAMRegion = type { i16, i16, ptr, ptr }

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

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

declare void @VRAM_releaseRegion(ptr noundef) local_unnamed_addr #0

declare void @MEM_pack() local_unnamed_addr #0

declare signext i16 @VRAM_alloc(ptr noundef, i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: noinline nounwind uwtable
define dso_local void @SPR_defragVRAM() local_unnamed_addr #1 {
  tail call void @VRAM_releaseRegion(ptr noundef nonnull @vram) #2
  tail call void @MEM_pack() #2
  %1 = load i16, ptr @maps_addr, align 2, !tbaa !5
  %2 = lshr i16 %1, 5
  %3 = add nsw i16 %2, -96
  %4 = load i16, ptr @spriteVramSize, align 2, !tbaa !5
  %5 = sub i16 %3, %4
  tail call void @VRAM_createRegion(ptr noundef nonnull @vram, i16 noundef zeroext %5, i16 noundef zeroext %4) #2
  %.019 = load ptr, ptr @firstSprite, align 8, !tbaa !9
  %.not20 = icmp eq ptr %.019, null
  br i1 %.not20, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %22
  %.021 = phi ptr [ %.0, %22 ], [ %.019, %0 ]
  %6 = load i16, ptr %.021, align 8, !tbaa !11
  %7 = and i16 %6, 2048
  %.not16 = icmp eq i16 %7, 0
  br i1 %.not16, label %22, label %8

8:                                                ; preds = %.lr.ph
  %9 = getelementptr inbounds i8, ptr %.021, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !14
  %11 = getelementptr inbounds i8, ptr %10, i64 32
  %12 = load i16, ptr %11, align 8, !tbaa !15
  %13 = tail call signext i16 @VRAM_alloc(ptr noundef nonnull @vram, i16 noundef zeroext %12) #2
  %14 = getelementptr inbounds i8, ptr %.021, i64 52
  %15 = load i16, ptr %14, align 4, !tbaa !17
  %16 = and i16 %15, 2047
  %.not17 = icmp eq i16 %16, %13
  br i1 %.not17, label %22, label %17

17:                                               ; preds = %8
  %18 = and i16 %15, -2048
  %19 = or i16 %18, %13
  store i16 %19, ptr %14, align 4, !tbaa !17
  %20 = lshr i16 %6, 8
  %21 = and i16 %20, 4
  %spec.select = or i16 %21, %6
  store i16 %spec.select, ptr %.021, align 8, !tbaa !11
  br label %22

22:                                               ; preds = %8, %17, %.lr.ph
  %23 = getelementptr inbounds i8, ptr %.021, i64 72
  %.0 = load ptr, ptr %23, align 8, !tbaa !9
  %.not = icmp eq ptr %.0, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !18

._crit_edge:                                      ; preds = %22, %0
  ret void
}

attributes #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 = { 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 #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 = !{!12, !6, i64 0}
!12 = !{!"Sprite", !6, i64 0, !6, i64 2, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !6, i64 40, !6, i64 42, !6, i64 44, !6, i64 46, !6, i64 48, !6, i64 50, !6, i64 52, !13, i64 56, !10, i64 64, !10, i64 72}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !10, i64 8}
!15 = !{!16, !6, i64 32}
!16 = !{!"", !6, i64 0, !6, i64 2, !10, i64 8, !6, i64 16, !10, i64 24, !6, i64 32, !6, i64 34}
!17 = !{!12, !6, i64 52}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass