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

@.str.10 = external hidden unnamed_addr constant [14 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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @SPR_nextFrame(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i16, align 2
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  call void @checkSpriteValid(ptr noundef %6, ptr noundef @.str.10)
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.Sprite, ptr %7, i32 0, i32 4
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  store ptr %9, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #2
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.Sprite, ptr %10, i32 0, i32 7
  %12 = load i16, ptr %11, align 2, !tbaa !13
  %13 = sext i16 %12 to i32
  %14 = add nsw i32 %13, 1
  %15 = trunc i32 %14 to i16
  store i16 %15, ptr %4, align 2, !tbaa !14
  %16 = load i16, ptr %4, align 2, !tbaa !14
  %17 = zext i16 %16 to i32
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.Animation, ptr %18, i32 0, i32 0
  %20 = load i8, ptr %19, align 8, !tbaa !15
  %21 = zext i8 %20 to i32
  %22 = icmp sge i32 %17, %21
  br i1 %22, label %23, label %42

23:                                               ; preds = %1
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.Sprite, ptr %24, i32 0, i32 0
  %26 = load i16, ptr %25, align 8, !tbaa !17
  %27 = zext i16 %26 to i32
  %28 = and i32 %27, 8192
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %23
  store i32 1, ptr %5, align 4
  br label %45

31:                                               ; preds = %23
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.Animation, ptr %32, i32 0, i32 1
  %34 = load i8, ptr %33, align 1, !tbaa !18
  %35 = zext i8 %34 to i16
  store i16 %35, ptr %4, align 2, !tbaa !14
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.Sprite, ptr %36, i32 0, i32 0
  %38 = load i16, ptr %37, align 8, !tbaa !17
  %39 = zext i16 %38 to i32
  %40 = or i32 %39, 16
  %41 = trunc i32 %40 to i16
  store i16 %41, ptr %37, align 8, !tbaa !17
  br label %42

42:                                               ; preds = %31, %1
  %43 = load ptr, ptr %2, align 8, !tbaa !5
  %44 = load i16, ptr %4, align 2, !tbaa !14
  call void @SPR_setFrame(ptr noundef %43, i16 noundef signext %44)
  store i32 0, ptr %5, align 4
  br label %45

45:                                               ; preds = %42, %30
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  %46 = load i32, ptr %5, align 4
  switch i32 %46, label %48 [
    i32 0, label %47
    i32 1, label %47
  ]

47:                                               ; preds = %45, %45
  ret void

48:                                               ; preds = %45
  unreachable
}

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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 24}
!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 = !{!10, !11, i64 42}
!14 = !{!11, !11, i64 0}
!15 = !{!16, !7, i64 0}
!16 = !{!"", !7, i64 0, !7, i64 1, !6, i64 8}
!17 = !{!10, !11, i64 0}
!18 = !{!16, !7, i64 1}


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

@.str.10 = external hidden unnamed_addr constant [14 x i8], align 1

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

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

; Function Attrs: nounwind uwtable
define dso_local void @SPR_nextFrame(ptr noundef %0) local_unnamed_addr #0 {
  tail call void @checkSpriteValid(ptr noundef %0, ptr noundef nonnull @.str.10)
  %2 = getelementptr inbounds i8, ptr %0, i64 24
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 42
  %5 = load i16, ptr %4, align 2, !tbaa !12
  %6 = add i16 %5, 1
  %7 = load i8, ptr %3, align 8, !tbaa !13
  %8 = zext i8 %7 to i16
  %.not = icmp ult i16 %6, %8
  br i1 %.not, label %17, label %9

9:                                                ; preds = %1
  %10 = load i16, ptr %0, align 8, !tbaa !15
  %11 = and i16 %10, 8192
  %.not9 = icmp eq i16 %11, 0
  br i1 %.not9, label %12, label %18

12:                                               ; preds = %9
  %13 = getelementptr inbounds i8, ptr %3, i64 1
  %14 = load i8, ptr %13, align 1, !tbaa !16
  %15 = zext i8 %14 to i16
  %16 = or i16 %10, 16
  store i16 %16, ptr %0, align 8, !tbaa !15
  br label %17

17:                                               ; preds = %12, %1
  %.0 = phi i16 [ %15, %12 ], [ %6, %1 ]
  tail call void @SPR_setFrame(ptr noundef nonnull %0, i16 noundef signext %.0)
  br label %18

18:                                               ; preds = %9, %17
  ret void
}

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

!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 24}
!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 = !{!6, !7, i64 42}
!13 = !{!14, !8, i64 0}
!14 = !{!"", !8, i64 0, !8, i64 1, !10, i64 8}
!15 = !{!6, !7, i64 0}
!16 = !{!14, !8, i64 1}


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