; LLVM IR from Stephane-D/SGDK :: sample/fx/h-int/wobble/src (helpers)

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

@spriteOffsets = external hidden global [80 x %struct.Vect2D_s16], align 16
@vdpSpriteIndexNext = external hidden global i16, align 2

; 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 void @addSprite(i16 noundef signext %0, i16 noundef signext %1, i16 noundef zeroext %2) #1 {
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  %7 = alloca %struct.Vect2D_s16, align 2
  store i16 %0, ptr %4, align 2, !tbaa !5
  store i16 %1, ptr %5, align 2, !tbaa !5
  store i16 %2, ptr %6, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %8 = getelementptr inbounds %struct.Vect2D_s16, ptr %7, i32 0, i32 0
  %9 = load i16, ptr %4, align 2, !tbaa !5
  store i16 %9, ptr %8, align 2, !tbaa !9
  %10 = getelementptr inbounds %struct.Vect2D_s16, ptr %7, i32 0, i32 1
  %11 = load i16, ptr %5, align 2, !tbaa !5
  store i16 %11, ptr %10, align 2, !tbaa !11
  %12 = load i16, ptr @vdpSpriteIndexNext, align 2, !tbaa !5
  %13 = zext i16 %12 to i64
  %14 = getelementptr inbounds [80 x %struct.Vect2D_s16], ptr @spriteOffsets, i64 0, i64 %13
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %14, ptr align 2 %7, i64 4, i1 false), !tbaa.struct !12
  %15 = load i16, ptr @vdpSpriteIndexNext, align 2, !tbaa !5
  %16 = load i16, ptr %6, align 2, !tbaa !5
  call void @VDP_setSpriteFull(i16 noundef zeroext %15, i16 noundef signext 320, i16 noundef signext 0, i8 noundef zeroext 15, i16 noundef zeroext %16, i8 noundef zeroext 0)
  %17 = load i16, ptr @vdpSpriteIndexNext, align 2, !tbaa !5
  %18 = zext i16 %17 to i32
  %19 = icmp sgt i32 %18, 0
  br i1 %19, label %20, label %27

20:                                               ; preds = %3
  %21 = load i16, ptr @vdpSpriteIndexNext, align 2, !tbaa !5
  %22 = zext i16 %21 to i32
  %23 = sub nsw i32 %22, 1
  %24 = trunc i32 %23 to i16
  %25 = load i16, ptr @vdpSpriteIndexNext, align 2, !tbaa !5
  %26 = trunc i16 %25 to i8
  call void @VDP_setSpriteLink(i16 noundef zeroext %24, i8 noundef zeroext %26)
  br label %27

27:                                               ; preds = %20, %3
  %28 = load i16, ptr @vdpSpriteIndexNext, align 2, !tbaa !5
  %29 = add i16 %28, 1
  store i16 %29, ptr @vdpSpriteIndexNext, align 2, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

declare void @VDP_setSpriteFull(i16 noundef zeroext, i16 noundef signext, i16 noundef signext, i8 noundef zeroext, i16 noundef zeroext, i8 noundef zeroext) #3

declare void @VDP_setSpriteLink(i16 noundef zeroext, i8 noundef zeroext) #3

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "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, !6, i64 0}
!10 = !{!"", !6, i64 0, !6, i64 2}
!11 = !{!10, !6, i64 2}
!12 = !{i64 0, i64 2, !5, i64 2, i64 2, !5}


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

@spriteOffsets = external hidden local_unnamed_addr global [80 x %struct.Vect2D_s16], align 16
@vdpSpriteIndexNext = external hidden local_unnamed_addr global i16, align 2

; Function Attrs: nounwind uwtable
define dso_local void @addSprite(i16 noundef signext %0, i16 noundef signext %1, i16 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = load i16, ptr @vdpSpriteIndexNext, align 2, !tbaa !5
  %5 = zext i16 %4 to i64
  %6 = getelementptr inbounds [80 x %struct.Vect2D_s16], ptr @spriteOffsets, i64 0, i64 %5
  store i16 %0, ptr %6, align 4, !tbaa !5
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %6, i64 2
  store i16 %1, ptr %.sroa.4.0..sroa_idx, align 2, !tbaa !5
  tail call void @VDP_setSpriteFull(i16 noundef zeroext %4, i16 noundef signext 320, i16 noundef signext 0, i8 noundef zeroext 15, i16 noundef zeroext %2, i8 noundef zeroext 0) #2
  %7 = load i16, ptr @vdpSpriteIndexNext, align 2, !tbaa !5
  %.not = icmp eq i16 %7, 0
  br i1 %.not, label %12, label %8

8:                                                ; preds = %3
  %9 = add i16 %7, -1
  %10 = trunc i16 %7 to i8
  tail call void @VDP_setSpriteLink(i16 noundef zeroext %9, i8 noundef zeroext %10) #2
  %.pre = load i16, ptr @vdpSpriteIndexNext, align 2, !tbaa !5
  %11 = add i16 %.pre, 1
  br label %12

12:                                               ; preds = %8, %3
  %13 = phi i16 [ %11, %8 ], [ 1, %3 ]
  store i16 %13, ptr @vdpSpriteIndexNext, align 2, !tbaa !5
  ret void
}

declare void @VDP_setSpriteFull(i16 noundef zeroext, i16 noundef signext, i16 noundef signext, i8 noundef zeroext, i16 noundef zeroext, i8 noundef zeroext) local_unnamed_addr #1

declare void @VDP_setSpriteLink(i16 noundef zeroext, i8 noundef zeroext) local_unnamed_addr #1

attributes #0 = { 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 }

!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"}


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