; 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 }

@vram = external hidden global %struct.VRAMRegion, align 8
@usedVDPSprite = external hidden global i16, align 2
@.str = external hidden unnamed_addr constant [18 x i8], align 1

; 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

declare zeroext i16 @VDP_getScreenWidth() #1

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

; Function Attrs: nounwind uwtable
declare dso_local void @SPR_setAnimAndFrame(ptr noundef, i16 noundef signext, i16 noundef signext) #2

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

; Function Attrs: noinline nounwind uwtable
define dso_local zeroext i8 @SPR_setDefinition(ptr noundef %0, ptr noundef %1) #3 {
  %3 = alloca i8, align 1
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i16, align 2
  %7 = alloca i32, align 4
  %8 = alloca i16, align 2
  %9 = alloca i16, align 2
  %10 = alloca i16, align 2
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  call void @checkSpriteValid(ptr noundef %11, ptr noundef @.str)
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.Sprite, ptr %12, i32 0, i32 2
  %14 = load ptr, ptr %13, align 8, !tbaa !9
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = icmp eq ptr %14, %15
  br i1 %16, label %17, label %18

17:                                               ; preds = %2
  store i8 1, ptr %3, align 1
  br label %136

18:                                               ; preds = %2
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.Sprite, ptr %19, i32 0, i32 2
  %21 = load ptr, ptr %20, align 8, !tbaa !9
  %22 = getelementptr inbounds %struct.SpriteDefinition, ptr %21, i32 0, i32 6
  %23 = load i16, ptr %22, align 2, !tbaa !13
  %24 = zext i16 %23 to i32
  %25 = load i16, ptr @usedVDPSprite, align 2, !tbaa !15
  %26 = sext i16 %25 to i32
  %27 = sub nsw i32 %26, %24
  %28 = trunc i32 %27 to i16
  store i16 %28, ptr @usedVDPSprite, align 2, !tbaa !15
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.SpriteDefinition, ptr %29, i32 0, i32 6
  %31 = load i16, ptr %30, align 2, !tbaa !13
  %32 = zext i16 %31 to i32
  %33 = load i16, ptr @usedVDPSprite, align 2, !tbaa !15
  %34 = sext i16 %33 to i32
  %35 = add nsw i32 %34, %32
  %36 = trunc i32 %35 to i16
  store i16 %36, ptr @usedVDPSprite, align 2, !tbaa !15
  %37 = load i16, ptr @usedVDPSprite, align 2, !tbaa !15
  %38 = sext i16 %37 to i32
  %39 = and i32 %38, 32768
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %41, label %75

41:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #4
  %42 = load i16, ptr @usedVDPSprite, align 2, !tbaa !15
  %43 = sext i16 %42 to i32
  %44 = and i32 %43, -32769
  %45 = trunc i32 %44 to i16
  store i16 %45, ptr %6, align 2, !tbaa !15
  %46 = load i16, ptr %6, align 2, !tbaa !15
  %47 = zext i16 %46 to i32
  %48 = call zeroext i16 @VDP_getScreenWidth()
  %49 = zext i16 %48 to i32
  %50 = ashr i32 %49, 2
  %51 = icmp sge i32 %47, %50
  br i1 %51, label %52, label %71

52:                                               ; preds = %41
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.SpriteDefinition, ptr %53, i32 0, i32 6
  %55 = load i16, ptr %54, align 2, !tbaa !13
  %56 = zext i16 %55 to i32
  %57 = load i16, ptr @usedVDPSprite, align 2, !tbaa !15
  %58 = sext i16 %57 to i32
  %59 = sub nsw i32 %58, %56
  %60 = trunc i32 %59 to i16
  store i16 %60, ptr @usedVDPSprite, align 2, !tbaa !15
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.Sprite, ptr %61, i32 0, i32 2
  %63 = load ptr, ptr %62, align 8, !tbaa !9
  %64 = getelementptr inbounds %struct.SpriteDefinition, ptr %63, i32 0, i32 6
  %65 = load i16, ptr %64, align 2, !tbaa !13
  %66 = zext i16 %65 to i32
  %67 = load i16, ptr @usedVDPSprite, align 2, !tbaa !15
  %68 = sext i16 %67 to i32
  %69 = add nsw i32 %68, %66
  %70 = trunc i32 %69 to i16
  store i16 %70, ptr @usedVDPSprite, align 2, !tbaa !15
  store i8 0, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %72

71:                                               ; preds = %41
  store i32 0, ptr %7, align 4
  br label %72

72:                                               ; preds = %71, %52
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #4
  %73 = load i32, ptr %7, align 4
  switch i32 %73, label %138 [
    i32 0, label %74
    i32 1, label %136
  ]

74:                                               ; preds = %72
  br label %75

75:                                               ; preds = %74, %18
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #4
  %76 = load ptr, ptr %4, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.Sprite, ptr %76, i32 0, i32 0
  %78 = load i16, ptr %77, align 8, !tbaa !16
  store i16 %78, ptr %8, align 2, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #4
  %79 = load ptr, ptr %5, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.SpriteDefinition, ptr %79, i32 0, i32 5
  %81 = load i16, ptr %80, align 8, !tbaa !17
  store i16 %81, ptr %9, align 2, !tbaa !15
  %82 = load i16, ptr %8, align 2, !tbaa !15
  %83 = zext i16 %82 to i32
  %84 = and i32 %83, 2048
  %85 = icmp ne i32 %84, 0
  br i1 %85, label %86, label %124

86:                                               ; preds = %75
  %87 = load ptr, ptr %4, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.Sprite, ptr %87, i32 0, i32 2
  %89 = load ptr, ptr %88, align 8, !tbaa !9
  %90 = getelementptr inbounds %struct.SpriteDefinition, ptr %89, i32 0, i32 5
  %91 = load i16, ptr %90, align 8, !tbaa !17
  %92 = zext i16 %91 to i32
  %93 = load i16, ptr %9, align 2, !tbaa !15
  %94 = zext i16 %93 to i32
  %95 = icmp ne i32 %92, %94
  br i1 %95, label %96, label %124

96:                                               ; preds = %86
  %97 = load ptr, ptr %4, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.Sprite, ptr %97, i32 0, i32 12
  %99 = load i16, ptr %98, align 4, !tbaa !18
  %100 = zext i16 %99 to i32
  %101 = and i32 %100, 2047
  %102 = trunc i32 %101 to i16
  call void @VRAM_free(ptr noundef @vram, i16 noundef zeroext %102)
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #4
  %103 = load i16, ptr %9, align 2, !tbaa !15
  %104 = call signext i16 @VRAM_alloc(ptr noundef @vram, i16 noundef zeroext %103)
  store i16 %104, ptr %10, align 2, !tbaa !15
  %105 = load i16, ptr %10, align 2, !tbaa !15
  %106 = sext i16 %105 to i32
  %107 = icmp slt i32 %106, 0
  br i1 %107, label %108, label %109

108:                                              ; preds = %96
  store i8 0, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %121

109:                                              ; preds = %96
  %110 = load ptr, ptr %4, align 8, !tbaa !5
  %111 = getelementptr inbounds %struct.Sprite, ptr %110, i32 0, i32 12
  %112 = load i16, ptr %111, align 4, !tbaa !18
  %113 = zext i16 %112 to i32
  %114 = and i32 %113, 63488
  %115 = load i16, ptr %10, align 2, !tbaa !15
  %116 = sext i16 %115 to i32
  %117 = or i32 %114, %116
  %118 = trunc i32 %117 to i16
  %119 = load ptr, ptr %4, align 8, !tbaa !5
  %120 = getelementptr inbounds %struct.Sprite, ptr %119, i32 0, i32 12
  store i16 %118, ptr %120, align 4, !tbaa !18
  store i32 0, ptr %7, align 4
  br label %121

121:                                              ; preds = %109, %108
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #4
  %122 = load i32, ptr %7, align 4
  switch i32 %122, label %135 [
    i32 0, label %123
  ]

123:                                              ; preds = %121
  br label %124

124:                                              ; preds = %123, %86, %75
  %125 = load ptr, ptr %5, align 8, !tbaa !5
  %126 = load ptr, ptr %4, align 8, !tbaa !5
  %127 = getelementptr inbounds %struct.Sprite, ptr %126, i32 0, i32 2
  store ptr %125, ptr %127, align 8, !tbaa !9
  %128 = load ptr, ptr %4, align 8, !tbaa !5
  %129 = getelementptr inbounds %struct.Sprite, ptr %128, i32 0, i32 5
  store ptr null, ptr %129, align 8, !tbaa !19
  %130 = load ptr, ptr %4, align 8, !tbaa !5
  %131 = getelementptr inbounds %struct.Sprite, ptr %130, i32 0, i32 6
  store i16 -1, ptr %131, align 8, !tbaa !20
  %132 = load ptr, ptr %4, align 8, !tbaa !5
  %133 = getelementptr inbounds %struct.Sprite, ptr %132, i32 0, i32 7
  store i16 -1, ptr %133, align 2, !tbaa !21
  %134 = load ptr, ptr %4, align 8, !tbaa !5
  call void @SPR_setAnimAndFrame(ptr noundef %134, i16 noundef signext 0, i16 noundef signext 0)
  store i8 1, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %135

135:                                              ; preds = %124, %121
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #4
  br label %136

136:                                              ; preds = %135, %72, %17
  %137 = load i8, ptr %3, align 1
  ret i8 %137

138:                                              ; preds = %72
  unreachable
}

; Function Attrs: nounwind uwtable
declare hidden void @checkSpriteValid(ptr noundef, ptr noundef) #2

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 = { 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 = { 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"Sprite", !11, i64 0, !11, i64 2, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !11, i64 40, !11, i64 42, !11, i64 44, !11, i64 46, !11, i64 48, !11, i64 50, !11, i64 52, !12, i64 56, !6, i64 64, !6, i64 72}
!11 = !{!"short", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !11, i64 34}
!14 = !{!"", !11, i64 0, !11, i64 2, !6, i64 8, !11, i64 16, !6, i64 24, !11, i64 32, !11, i64 34}
!15 = !{!11, !11, i64 0}
!16 = !{!10, !11, i64 0}
!17 = !{!14, !11, i64 32}
!18 = !{!10, !11, i64 52}
!19 = !{!10, !6, i64 32}
!20 = !{!10, !11, i64 40}
!21 = !{!10, !11, i64 42}


; 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 }

@vram = external hidden global %struct.VRAMRegion, align 8
@usedVDPSprite = external hidden local_unnamed_addr global i16, align 2
@.str = external hidden unnamed_addr constant [18 x i8], align 1

declare zeroext i16 @VDP_getScreenWidth() local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local void @SPR_setAnimAndFrame(ptr noundef, i16 noundef signext, i16 noundef signext) local_unnamed_addr #1

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

; Function Attrs: noinline nounwind uwtable
define dso_local zeroext range(i8 0, 2) i8 @SPR_setDefinition(ptr noundef %0, ptr noundef %1) local_unnamed_addr #2 {
  tail call void @checkSpriteValid(ptr noundef %0, ptr noundef nonnull @.str)
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = icmp eq ptr %4, %1
  br i1 %5, label %.critedge, label %6

6:                                                ; preds = %2
  %7 = getelementptr inbounds i8, ptr %4, i64 34
  %8 = load i16, ptr %7, align 2, !tbaa !12
  %9 = load i16, ptr @usedVDPSprite, align 2, !tbaa !14
  %10 = sub i16 %9, %8
  %11 = getelementptr inbounds i8, ptr %1, i64 34
  %12 = load i16, ptr %11, align 2, !tbaa !12
  %13 = add i16 %12, %10
  store i16 %13, ptr @usedVDPSprite, align 2, !tbaa !14
  %.not = icmp sgt i16 %13, -1
  br i1 %.not, label %.thread, label %14

14:                                               ; preds = %6
  %15 = and i16 %13, 32767
  %16 = tail call zeroext i16 @VDP_getScreenWidth() #3
  %17 = lshr i16 %16, 2
  %.not31.not = icmp ult i16 %15, %17
  br i1 %.not31.not, label %.thread, label %18

18:                                               ; preds = %14
  %19 = load i16, ptr %11, align 2, !tbaa !12
  %20 = load i16, ptr @usedVDPSprite, align 2, !tbaa !14
  %21 = sub i16 %20, %19
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %22, i64 34
  %24 = load i16, ptr %23, align 2, !tbaa !12
  %25 = add i16 %24, %21
  store i16 %25, ptr @usedVDPSprite, align 2, !tbaa !14
  br label %.critedge

.thread:                                          ; preds = %14, %6
  %26 = load i16, ptr %0, align 8, !tbaa !15
  %27 = getelementptr inbounds i8, ptr %1, i64 32
  %28 = load i16, ptr %27, align 8, !tbaa !16
  %29 = and i16 %26, 2048
  %.not32 = icmp eq i16 %29, 0
  br i1 %.not32, label %44, label %30

30:                                               ; preds = %.thread
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %31, i64 32
  %33 = load i16, ptr %32, align 8, !tbaa !16
  %.not33 = icmp eq i16 %33, %28
  br i1 %.not33, label %44, label %34

34:                                               ; preds = %30
  %35 = getelementptr inbounds i8, ptr %0, i64 52
  %36 = load i16, ptr %35, align 4, !tbaa !17
  %37 = and i16 %36, 2047
  tail call void @VRAM_free(ptr noundef nonnull @vram, i16 noundef zeroext %37) #3
  %38 = tail call signext i16 @VRAM_alloc(ptr noundef nonnull @vram, i16 noundef zeroext %28) #3
  %39 = icmp sgt i16 %38, -1
  br i1 %39, label %40, label %.critedge

40:                                               ; preds = %34
  %41 = load i16, ptr %35, align 4, !tbaa !17
  %42 = and i16 %41, -2048
  %43 = or i16 %42, %38
  store i16 %43, ptr %35, align 4, !tbaa !17
  br label %44

44:                                               ; preds = %40, %30, %.thread
  store ptr %1, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds i8, ptr %0, i64 32
  store ptr null, ptr %45, align 8, !tbaa !18
  %46 = getelementptr inbounds i8, ptr %0, i64 40
  store i16 -1, ptr %46, align 8, !tbaa !19
  %47 = getelementptr inbounds i8, ptr %0, i64 42
  store i16 -1, ptr %47, align 2, !tbaa !20
  tail call void @SPR_setAnimAndFrame(ptr noundef nonnull %0, i16 noundef signext 0, i16 noundef signext 0)
  br label %.critedge

.critedge:                                        ; preds = %18, %44, %34, %2
  %.4 = phi i8 [ 0, %18 ], [ 1, %2 ], [ 1, %44 ], [ 0, %34 ]
  ret i8 %.4
}

; Function Attrs: nounwind uwtable
declare hidden void @checkSpriteValid(ptr noundef, ptr noundef) local_unnamed_addr #1

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 = { 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 = { 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, !10, i64 8}
!6 = !{!"Sprite", !7, i64 0, !7, i64 2, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !7, i64 40, !7, i64 42, !7, i64 44, !7, i64 46, !7, i64 48, !7, i64 50, !7, i64 52, !11, i64 56, !10, i64 64, !10, i64 72}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!13, !7, i64 34}
!13 = !{!"", !7, i64 0, !7, i64 2, !10, i64 8, !7, i64 16, !10, i64 24, !7, i64 32, !7, i64 34}
!14 = !{!7, !7, i64 0}
!15 = !{!6, !7, i64 0}
!16 = !{!13, !7, i64 32}
!17 = !{!6, !7, i64 52}
!18 = !{!6, !10, i64 32}
!19 = !{!6, !7, i64 40}
!20 = !{!6, !7, i64 42}


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