; LLVM IR from alekmaul/pvsneslib :: snes-examples/maps/DynamicMap (DynamicMap)

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

@spritemap_len = external dso_local global i16, align 2
@number_of_sprites = external dso_local global i32, align 4
@map32x32 = external dso_local global i8, align 1
@max_scroll_width = external dso_local global i16, align 2
@max_scroll_height = external dso_local global i16, align 2
@spritemap = external dso_local global [8192 x i16], align 16
@sprites_ram = external global i8, align 1
@.str.1 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [18 x i8], align 1
@sprite16 = external global i8, align 1
@.str.3 = external hidden unnamed_addr constant [42 x i8], align 1
@palsprite16 = external global i8, align 1

; Function Attrs: nounwind uwtable
declare dso_local void @copySpriteToRAM_32x32(ptr noundef, i16 noundef zeroext) #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @resetRAM32x32(i16 noundef zeroext) #0

declare void @consoleNocashMessage(ptr noundef, ...) #2

; Function Attrs: nounwind uwtable
define dso_local void @initDemoMap32x32() #0 {
  %1 = alloca i16, align 2
  %2 = alloca i16, align 2
  %3 = alloca i16, align 2
  store i8 -1, ptr @map32x32, align 1, !tbaa !5
  store i16 256, ptr @max_scroll_width, align 2, !tbaa !8
  store i16 288, ptr @max_scroll_height, align 2, !tbaa !8
  call void @setMode(i8 noundef zeroext 3, i8 noundef zeroext 0)
  %4 = load i16, ptr @spritemap_len, align 2, !tbaa !8
  call void @setSpriteMap32x32(ptr noundef @spritemap, i16 noundef zeroext %4)
  call void (...) @initSpriteMap32x32()
  call void @llvm.lifetime.start.p0(i64 2, ptr %1) #3
  %5 = load i32, ptr @number_of_sprites, align 4, !tbaa !10
  %6 = trunc i32 %5 to i16
  %7 = call zeroext i16 @calculateSpritesLength32x32(i16 noundef zeroext %6)
  store i16 %7, ptr %1, align 2, !tbaa !8
  call void (ptr, ...) @consoleNocashMessage(ptr noundef @.str.1, ptr noundef @sprites_ram)
  %8 = load i16, ptr %1, align 2, !tbaa !8
  %9 = zext i16 %8 to i32
  call void (ptr, ...) @consoleNocashMessage(ptr noundef @.str.2, i32 noundef %9)
  %10 = call zeroext i16 @calculateSpriteIndex32x32(i8 noundef zeroext 0)
  call void @resetRAM32x32(i16 noundef zeroext %10)
  call void @llvm.lifetime.start.p0(i64 2, ptr %2) #3
  store i16 1, ptr %2, align 2, !tbaa !8
  br label %11

11:                                               ; preds = %28, %0
  %12 = load i16, ptr %2, align 2, !tbaa !8
  %13 = zext i16 %12 to i32
  %14 = icmp slt i32 %13, 10
  br i1 %14, label %15, label %31

15:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #3
  %16 = load i16, ptr %2, align 2, !tbaa !8
  %17 = trunc i16 %16 to i8
  %18 = call zeroext i16 @calculateSpriteIndex32x32(i8 noundef zeroext %17)
  store i16 %18, ptr %3, align 2, !tbaa !8
  %19 = load i16, ptr %3, align 2, !tbaa !8
  call void @copySpriteToRAM_32x32(ptr noundef @sprite16, i16 noundef zeroext %19)
  %20 = load i16, ptr %2, align 2, !tbaa !8
  %21 = zext i16 %20 to i32
  %22 = load i16, ptr %2, align 2, !tbaa !8
  %23 = trunc i16 %22 to i8
  %24 = call zeroext i16 @element2sprite32x32(i8 noundef zeroext %23)
  %25 = zext i16 %24 to i32
  %26 = load i16, ptr %3, align 2, !tbaa !8
  %27 = zext i16 %26 to i32
  call void (ptr, ...) @consoleNocashMessage(ptr noundef @.str.3, i32 noundef %21, i32 noundef %25, i32 noundef %27)
  call void @llvm.lifetime.end.p0(i64 2, ptr %3) #3
  br label %28

28:                                               ; preds = %15
  %29 = load i16, ptr %2, align 2, !tbaa !8
  %30 = add i16 %29, 1
  store i16 %30, ptr %2, align 2, !tbaa !8
  br label %11, !llvm.loop !12

31:                                               ; preds = %11
  %32 = load i16, ptr %1, align 2, !tbaa !8
  call void @bgInitTileSet(i8 noundef zeroext 0, ptr noundef @sprites_ram, ptr noundef @palsprite16, i8 noundef zeroext 0, i16 noundef zeroext %32, i16 noundef zeroext 32, i16 noundef zeroext 256, i16 noundef zeroext 16384)
  %33 = load i16, ptr @spritemap_len, align 2, !tbaa !8
  call void @bgInitMapSet(i8 noundef zeroext 0, ptr noundef @spritemap, i16 noundef zeroext %33, i8 noundef zeroext 3, i16 noundef zeroext 4096)
  call void @llvm.lifetime.end.p0(i64 2, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %1) #3
  ret void
}

declare void @setMode(i8 noundef zeroext, i8 noundef zeroext) #2

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

declare void @initSpriteMap32x32(...) #2

declare zeroext i16 @calculateSpritesLength32x32(i16 noundef zeroext) #2

declare zeroext i16 @calculateSpriteIndex32x32(i8 noundef zeroext) #2

declare zeroext i16 @element2sprite32x32(i8 noundef zeroext) #2

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

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

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, !9, i64 0}
!9 = !{!"short", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !6, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

@spritemap_len = external dso_local local_unnamed_addr global i16, align 2
@number_of_sprites = external dso_local local_unnamed_addr global i32, align 4
@map32x32 = external dso_local local_unnamed_addr global i8, align 1
@max_scroll_width = external dso_local local_unnamed_addr global i16, align 2
@max_scroll_height = external dso_local local_unnamed_addr global i16, align 2
@spritemap = external dso_local global [8192 x i16], align 16
@sprites_ram = external global i8, align 1
@.str.1 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [18 x i8], align 1
@sprite16 = external global i8, align 1
@.str.3 = external hidden unnamed_addr constant [42 x i8], align 1
@palsprite16 = external global i8, align 1

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

; Function Attrs: nounwind uwtable
declare dso_local void @resetRAM32x32(i16 noundef zeroext) local_unnamed_addr #0

declare void @consoleNocashMessage(ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @initDemoMap32x32() local_unnamed_addr #0 {
  store i8 -1, ptr @map32x32, align 1, !tbaa !5
  store i16 256, ptr @max_scroll_width, align 2, !tbaa !8
  store i16 288, ptr @max_scroll_height, align 2, !tbaa !8
  tail call void @setMode(i8 noundef zeroext 3, i8 noundef zeroext 0) #2
  %1 = load i16, ptr @spritemap_len, align 2, !tbaa !8
  tail call void @setSpriteMap32x32(ptr noundef nonnull @spritemap, i16 noundef zeroext %1) #2
  tail call void (...) @initSpriteMap32x32() #2
  %2 = load i32, ptr @number_of_sprites, align 4, !tbaa !10
  %3 = trunc i32 %2 to i16
  %4 = tail call zeroext i16 @calculateSpritesLength32x32(i16 noundef zeroext %3) #2
  tail call void (ptr, ...) @consoleNocashMessage(ptr noundef nonnull @.str.1, ptr noundef nonnull @sprites_ram) #2
  %5 = zext i16 %4 to i32
  tail call void (ptr, ...) @consoleNocashMessage(ptr noundef nonnull @.str.2, i32 noundef %5) #2
  %6 = tail call zeroext i16 @calculateSpriteIndex32x32(i8 noundef zeroext 0) #2
  tail call void @resetRAM32x32(i16 noundef zeroext %6)
  %7 = tail call zeroext i16 @calculateSpriteIndex32x32(i8 noundef zeroext 1) #2
  tail call void @copySpriteToRAM_32x32(ptr noundef nonnull @sprite16, i16 noundef zeroext %7)
  %8 = tail call zeroext i16 @element2sprite32x32(i8 noundef zeroext 1) #2
  %9 = zext i16 %8 to i32
  %10 = zext i16 %7 to i32
  tail call void (ptr, ...) @consoleNocashMessage(ptr noundef nonnull @.str.3, i32 noundef 1, i32 noundef %9, i32 noundef %10) #2
  %11 = tail call zeroext i16 @calculateSpriteIndex32x32(i8 noundef zeroext 2) #2
  tail call void @copySpriteToRAM_32x32(ptr noundef nonnull @sprite16, i16 noundef zeroext %11)
  %12 = tail call zeroext i16 @element2sprite32x32(i8 noundef zeroext 2) #2
  %13 = zext i16 %12 to i32
  %14 = zext i16 %11 to i32
  tail call void (ptr, ...) @consoleNocashMessage(ptr noundef nonnull @.str.3, i32 noundef 2, i32 noundef %13, i32 noundef %14) #2
  %15 = tail call zeroext i16 @calculateSpriteIndex32x32(i8 noundef zeroext 3) #2
  tail call void @copySpriteToRAM_32x32(ptr noundef nonnull @sprite16, i16 noundef zeroext %15)
  %16 = tail call zeroext i16 @element2sprite32x32(i8 noundef zeroext 3) #2
  %17 = zext i16 %16 to i32
  %18 = zext i16 %15 to i32
  tail call void (ptr, ...) @consoleNocashMessage(ptr noundef nonnull @.str.3, i32 noundef 3, i32 noundef %17, i32 noundef %18) #2
  %19 = tail call zeroext i16 @calculateSpriteIndex32x32(i8 noundef zeroext 4) #2
  tail call void @copySpriteToRAM_32x32(ptr noundef nonnull @sprite16, i16 noundef zeroext %19)
  %20 = tail call zeroext i16 @element2sprite32x32(i8 noundef zeroext 4) #2
  %21 = zext i16 %20 to i32
  %22 = zext i16 %19 to i32
  tail call void (ptr, ...) @consoleNocashMessage(ptr noundef nonnull @.str.3, i32 noundef 4, i32 noundef %21, i32 noundef %22) #2
  %23 = tail call zeroext i16 @calculateSpriteIndex32x32(i8 noundef zeroext 5) #2
  tail call void @copySpriteToRAM_32x32(ptr noundef nonnull @sprite16, i16 noundef zeroext %23)
  %24 = tail call zeroext i16 @element2sprite32x32(i8 noundef zeroext 5) #2
  %25 = zext i16 %24 to i32
  %26 = zext i16 %23 to i32
  tail call void (ptr, ...) @consoleNocashMessage(ptr noundef nonnull @.str.3, i32 noundef 5, i32 noundef %25, i32 noundef %26) #2
  %27 = tail call zeroext i16 @calculateSpriteIndex32x32(i8 noundef zeroext 6) #2
  tail call void @copySpriteToRAM_32x32(ptr noundef nonnull @sprite16, i16 noundef zeroext %27)
  %28 = tail call zeroext i16 @element2sprite32x32(i8 noundef zeroext 6) #2
  %29 = zext i16 %28 to i32
  %30 = zext i16 %27 to i32
  tail call void (ptr, ...) @consoleNocashMessage(ptr noundef nonnull @.str.3, i32 noundef 6, i32 noundef %29, i32 noundef %30) #2
  %31 = tail call zeroext i16 @calculateSpriteIndex32x32(i8 noundef zeroext 7) #2
  tail call void @copySpriteToRAM_32x32(ptr noundef nonnull @sprite16, i16 noundef zeroext %31)
  %32 = tail call zeroext i16 @element2sprite32x32(i8 noundef zeroext 7) #2
  %33 = zext i16 %32 to i32
  %34 = zext i16 %31 to i32
  tail call void (ptr, ...) @consoleNocashMessage(ptr noundef nonnull @.str.3, i32 noundef 7, i32 noundef %33, i32 noundef %34) #2
  %35 = tail call zeroext i16 @calculateSpriteIndex32x32(i8 noundef zeroext 8) #2
  tail call void @copySpriteToRAM_32x32(ptr noundef nonnull @sprite16, i16 noundef zeroext %35)
  %36 = tail call zeroext i16 @element2sprite32x32(i8 noundef zeroext 8) #2
  %37 = zext i16 %36 to i32
  %38 = zext i16 %35 to i32
  tail call void (ptr, ...) @consoleNocashMessage(ptr noundef nonnull @.str.3, i32 noundef 8, i32 noundef %37, i32 noundef %38) #2
  %39 = tail call zeroext i16 @calculateSpriteIndex32x32(i8 noundef zeroext 9) #2
  tail call void @copySpriteToRAM_32x32(ptr noundef nonnull @sprite16, i16 noundef zeroext %39)
  %40 = tail call zeroext i16 @element2sprite32x32(i8 noundef zeroext 9) #2
  %41 = zext i16 %40 to i32
  %42 = zext i16 %39 to i32
  tail call void (ptr, ...) @consoleNocashMessage(ptr noundef nonnull @.str.3, i32 noundef 9, i32 noundef %41, i32 noundef %42) #2
  tail call void @bgInitTileSet(i8 noundef zeroext 0, ptr noundef nonnull @sprites_ram, ptr noundef nonnull @palsprite16, i8 noundef zeroext 0, i16 noundef zeroext %4, i16 noundef zeroext 32, i16 noundef zeroext 256, i16 noundef zeroext 16384) #2
  %43 = load i16, ptr @spritemap_len, align 2, !tbaa !8
  tail call void @bgInitMapSet(i8 noundef zeroext 0, ptr noundef nonnull @spritemap, i16 noundef zeroext %43, i8 noundef zeroext 3, i16 noundef zeroext 4096) #2
  ret void
}

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

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

declare void @initSpriteMap32x32(...) local_unnamed_addr #1

declare zeroext i16 @calculateSpritesLength32x32(i16 noundef zeroext) local_unnamed_addr #1

declare zeroext i16 @calculateSpriteIndex32x32(i8 noundef zeroext) local_unnamed_addr #1

declare zeroext i16 @element2sprite32x32(i8 noundef zeroext) local_unnamed_addr #1

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

declare void @bgInitMapSet(i8 noundef zeroext, ptr noundef, i16 noundef zeroext, i8 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, !9, i64 0}
!9 = !{!"short", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !6, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,SROAPass,SimplifyCFGPass,TailCallElimPass