; LLVM IR from alekmaul/pvsneslib :: snes-examples/graphics/Sprites/DynamicSprite (DynamicSprite)

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

@vblank_flag = external global i8, align 1
@spr_queue = external dso_local global i8, align 1
@spr_mutex = external dso_local global i8, align 1
@sprqueue = external dso_local global [16 x %struct.spritequeue], align 16

; Function Attrs: nounwind uwtable
define dso_local void @myconsoleVblank() #0 {
  %1 = alloca ptr, align 8
  %2 = alloca i16, align 2
  %3 = load i8, ptr @vblank_flag, align 1, !tbaa !5
  %4 = icmp ne i8 %3, 0
  br i1 %4, label %5, label %42

5:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %2) #3
  %6 = load i8, ptr @spr_queue, align 1, !tbaa !5
  %7 = zext i8 %6 to i32
  %8 = icmp ne i32 %7, 255
  br i1 %8, label %9, label %41

9:                                                ; preds = %5
  %10 = load i8, ptr @spr_mutex, align 1, !tbaa !5
  %11 = zext i8 %10 to i32
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %13, label %40

13:                                               ; preds = %9
  br label %14

14:                                               ; preds = %18, %13
  %15 = load i8, ptr @spr_queue, align 1, !tbaa !5
  %16 = zext i8 %15 to i32
  %17 = icmp ne i32 %16, 255
  br i1 %17, label %18, label %39

18:                                               ; preds = %14
  %19 = load i8, ptr @spr_queue, align 1, !tbaa !5
  %20 = zext i8 %19 to i64
  %21 = getelementptr inbounds [16 x %struct.spritequeue], ptr @sprqueue, i64 0, i64 %20
  %22 = getelementptr inbounds %struct.spritequeue, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 16, !tbaa !8
  store ptr %23, ptr %1, align 8, !tbaa !12
  %24 = load i8, ptr @spr_queue, align 1, !tbaa !5
  %25 = zext i8 %24 to i64
  %26 = getelementptr inbounds [16 x %struct.spritequeue], ptr @sprqueue, i64 0, i64 %25
  %27 = getelementptr inbounds %struct.spritequeue, ptr %26, i32 0, i32 1
  %28 = load i16, ptr %27, align 8, !tbaa !13
  store i16 %28, ptr %2, align 2, !tbaa !14
  %29 = load ptr, ptr %1, align 8, !tbaa !12
  %30 = load i16, ptr %2, align 2, !tbaa !14
  call void @dmaCopyVram(ptr noundef %29, i16 noundef zeroext %30, i16 noundef zeroext 64)
  %31 = load ptr, ptr %1, align 8, !tbaa !12
  %32 = getelementptr inbounds i8, ptr %31, i64 512
  %33 = load i16, ptr %2, align 2, !tbaa !14
  %34 = zext i16 %33 to i32
  %35 = add nsw i32 %34, 256
  %36 = trunc i32 %35 to i16
  call void @dmaCopyVram(ptr noundef %32, i16 noundef zeroext %36, i16 noundef zeroext 64)
  %37 = load i8, ptr @spr_queue, align 1, !tbaa !5
  %38 = add i8 %37, -1
  store i8 %38, ptr @spr_queue, align 1, !tbaa !5
  br label %14, !llvm.loop !15

39:                                               ; preds = %14
  br label %40

40:                                               ; preds = %39, %9
  br label %41

41:                                               ; preds = %40, %5
  call void @llvm.lifetime.end.p0(i64 2, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  br label %42

42:                                               ; preds = %41, %0
  ret void
}

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { 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 }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !10, i64 0}
!9 = !{!"", !10, i64 0, !11, i64 8}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!"short", !6, i64 0}
!12 = !{!10, !10, i64 0}
!13 = !{!9, !11, i64 8}
!14 = !{!11, !11, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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.spritequeue = type { ptr, i16 }

@vblank_flag = external local_unnamed_addr global i8, align 1
@spr_queue = external dso_local local_unnamed_addr global i8, align 1
@spr_mutex = external dso_local local_unnamed_addr global i8, align 1
@sprqueue = external dso_local local_unnamed_addr global [16 x %struct.spritequeue], align 16

; Function Attrs: nounwind uwtable
define dso_local void @myconsoleVblank() local_unnamed_addr #0 {
  %1 = load i8, ptr @vblank_flag, align 1, !tbaa !5
  %.not = icmp eq i8 %1, 0
  br i1 %.not, label %.loopexit, label %2

2:                                                ; preds = %0
  %3 = load i8, ptr @spr_queue, align 1
  %4 = icmp ne i8 %3, -1
  %5 = load i8, ptr @spr_mutex, align 1
  %6 = icmp eq i8 %5, 0
  %or.cond = select i1 %4, i1 %6, i1 false
  br i1 %or.cond, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %2, %.preheader
  %7 = phi i8 [ %16, %.preheader ], [ %3, %2 ]
  %8 = zext i8 %7 to i64
  %9 = getelementptr inbounds [16 x %struct.spritequeue], ptr @sprqueue, i64 0, i64 %8
  %10 = load ptr, ptr %9, align 16, !tbaa !8
  %11 = getelementptr inbounds i8, ptr %9, i64 8
  %12 = load i16, ptr %11, align 8, !tbaa !12
  tail call void @dmaCopyVram(ptr noundef %10, i16 noundef zeroext %12, i16 noundef zeroext 64) #2
  %13 = getelementptr inbounds i8, ptr %10, i64 512
  %14 = add i16 %12, 256
  tail call void @dmaCopyVram(ptr noundef nonnull %13, i16 noundef zeroext %14, i16 noundef zeroext 64) #2
  %15 = load i8, ptr @spr_queue, align 1, !tbaa !5
  %16 = add i8 %15, -1
  store i8 %16, ptr @spr_queue, align 1, !tbaa !5
  %.old4.not = icmp eq i8 %15, 0
  br i1 %.old4.not, label %.loopexit, label %.preheader

.loopexit:                                        ; preds = %.preheader, %2, %0
  ret void
}

declare void @dmaCopyVram(ptr noundef, i16 noundef zeroext, i16 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !10, i64 0}
!9 = !{!"", !10, i64 0, !11, i64 8}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!"short", !6, i64 0}
!12 = !{!9, !11, i64 8}


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