; LLVM IR from alekmaul/pvsneslib :: snes-examples/graphics/Backgrounds/Mode1Scroll (Mode1Scroll)

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

@snesfont = external global i8, align 1
@snespal = external global i8, align 1
@patterns = external global i8, align 1
@palette = external global i8, align 1
@patterns_end = external global i8, align 1
@map = external global i8, align 1
@map_end = external global i8, align 1
@pad_keys = external global [5 x i16], align 2
@.str = external hidden unnamed_addr constant [14 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i16, align 2
  %3 = alloca i16, align 2
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 2, ptr %2) #3
  store i16 0, ptr %2, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #3
  store i16 0, ptr %3, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #3
  call void @consoleSetTextMapPtr(i16 noundef zeroext 26624)
  call void @consoleSetTextGfxPtr(i16 noundef zeroext 12288)
  call void @consoleSetTextOffset(i16 noundef zeroext 256)
  call void @consoleInitText(i8 noundef zeroext 0, i8 noundef zeroext 32, ptr noundef @snesfont, ptr noundef @snespal)
  call void @bgInitTileSet(i8 noundef zeroext 1, ptr noundef @patterns, ptr noundef @palette, i8 noundef zeroext 0, i16 noundef zeroext trunc (i64 sub (i64 ptrtoint (ptr @patterns_end to i64), i64 ptrtoint (ptr @patterns to i64)) to i16), i16 noundef zeroext 32, i16 noundef zeroext 16, i16 noundef zeroext 16384)
  call void @bgInitMapSet(i8 noundef zeroext 1, ptr noundef @map, i16 noundef zeroext trunc (i64 sub (i64 ptrtoint (ptr @map_end to i64), i64 ptrtoint (ptr @map to i64)) to i16), i8 noundef zeroext 3, i16 noundef zeroext 4096)
  call void @bgSetGfxPtr(i8 noundef zeroext 0, i16 noundef zeroext 8192)
  call void @bgSetMapPtr(i8 noundef zeroext 0, i16 noundef zeroext 26624, i8 noundef zeroext 0)
  call void @setMode(i8 noundef zeroext 1, i8 noundef zeroext 0)
  call void @bgSetDisable(i8 noundef zeroext 2)
  call void @setScreenOn()
  br label %6

6:                                                ; preds = %37, %0
  br label %7

7:                                                ; preds = %6
  store i16 0, ptr %5, align 2, !tbaa !5
  %8 = load i16, ptr @pad_keys, align 2, !tbaa !5
  store i16 %8, ptr %4, align 2, !tbaa !5
  %9 = load i16, ptr %4, align 2, !tbaa !5
  %10 = zext i16 %9 to i32
  switch i32 %10, label %31 [
    i32 256, label %11
    i32 512, label %16
    i32 1024, label %21
    i32 2048, label %26
  ]

11:                                               ; preds = %7
  %12 = load i16, ptr %2, align 2, !tbaa !5
  %13 = zext i16 %12 to i32
  %14 = add nsw i32 %13, 2
  %15 = trunc i32 %14 to i16
  store i16 %15, ptr %2, align 2, !tbaa !5
  store i16 1, ptr %5, align 2, !tbaa !5
  br label %31

16:                                               ; preds = %7
  %17 = load i16, ptr %2, align 2, !tbaa !5
  %18 = zext i16 %17 to i32
  %19 = sub nsw i32 %18, 2
  %20 = trunc i32 %19 to i16
  store i16 %20, ptr %2, align 2, !tbaa !5
  store i16 1, ptr %5, align 2, !tbaa !5
  br label %31

21:                                               ; preds = %7
  %22 = load i16, ptr %3, align 2, !tbaa !5
  %23 = zext i16 %22 to i32
  %24 = add nsw i32 %23, 2
  %25 = trunc i32 %24 to i16
  store i16 %25, ptr %3, align 2, !tbaa !5
  store i16 1, ptr %5, align 2, !tbaa !5
  br label %31

26:                                               ; preds = %7
  %27 = load i16, ptr %3, align 2, !tbaa !5
  %28 = zext i16 %27 to i32
  %29 = sub nsw i32 %28, 2
  %30 = trunc i32 %29 to i16
  store i16 %30, ptr %3, align 2, !tbaa !5
  store i16 1, ptr %5, align 2, !tbaa !5
  br label %31

31:                                               ; preds = %26, %21, %16, %11, %7
  %32 = load i16, ptr %5, align 2, !tbaa !5
  %33 = icmp ne i16 %32, 0
  br i1 %33, label %34, label %37

34:                                               ; preds = %31
  %35 = load i16, ptr %2, align 2, !tbaa !5
  %36 = load i16, ptr %3, align 2, !tbaa !5
  call void @bgSetScroll(i8 noundef zeroext 1, i16 noundef zeroext %35, i16 noundef zeroext %36)
  br label %37

37:                                               ; preds = %34, %31
  %38 = load i16, ptr %2, align 2, !tbaa !5
  %39 = zext i16 %38 to i32
  %40 = load i16, ptr %3, align 2, !tbaa !5
  %41 = zext i16 %40 to i32
  call void (i16, i16, ptr, ...) @consoleDrawText(i16 noundef zeroext 0, i16 noundef zeroext 0, ptr noundef @.str, i32 noundef %39, i32 noundef %41)
  call void @WaitForVBlank()
  br label %6
}

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

declare void @consoleSetTextMapPtr(i16 noundef zeroext) #2

declare void @consoleSetTextGfxPtr(i16 noundef zeroext) #2

declare void @consoleSetTextOffset(i16 noundef zeroext) #2

declare void @consoleInitText(i8 noundef zeroext, i8 noundef zeroext, ptr noundef, ptr noundef) #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

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

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

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

declare void @bgSetDisable(i8 noundef zeroext) #2

declare void @setScreenOn() #2

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

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

declare void @WaitForVBlank() #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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


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

@snesfont = external global i8, align 1
@snespal = external global i8, align 1
@patterns = external global i8, align 1
@palette = external global i8, align 1
@patterns_end = external global i8, align 1
@map = external global i8, align 1
@map_end = external global i8, align 1
@pad_keys = external local_unnamed_addr global [5 x i16], align 2
@.str = external hidden unnamed_addr constant [14 x i8], align 1

; Function Attrs: noreturn nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  tail call void @consoleSetTextMapPtr(i16 noundef zeroext 26624) #3
  tail call void @consoleSetTextGfxPtr(i16 noundef zeroext 12288) #3
  tail call void @consoleSetTextOffset(i16 noundef zeroext 256) #3
  tail call void @consoleInitText(i8 noundef zeroext 0, i8 noundef zeroext 32, ptr noundef nonnull @snesfont, ptr noundef nonnull @snespal) #3
  tail call void @bgInitTileSet(i8 noundef zeroext 1, ptr noundef nonnull @patterns, ptr noundef nonnull @palette, i8 noundef zeroext 0, i16 noundef zeroext trunc (i64 sub (i64 ptrtoint (ptr @patterns_end to i64), i64 ptrtoint (ptr @patterns to i64)) to i16), i16 noundef zeroext 32, i16 noundef zeroext 16, i16 noundef zeroext 16384) #3
  tail call void @bgInitMapSet(i8 noundef zeroext 1, ptr noundef nonnull @map, i16 noundef zeroext trunc (i64 sub (i64 ptrtoint (ptr @map_end to i64), i64 ptrtoint (ptr @map to i64)) to i16), i8 noundef zeroext 3, i16 noundef zeroext 4096) #3
  tail call void @bgSetGfxPtr(i8 noundef zeroext 0, i16 noundef zeroext 8192) #3
  tail call void @bgSetMapPtr(i8 noundef zeroext 0, i16 noundef zeroext 26624, i8 noundef zeroext 0) #3
  tail call void @setMode(i8 noundef zeroext 1, i8 noundef zeroext 0) #3
  tail call void @bgSetDisable(i8 noundef zeroext 2) #3
  tail call void @setScreenOn() #3
  br label %1

1:                                                ; preds = %15, %0
  %.011 = phi i16 [ 0, %0 ], [ %.11217, %15 ]
  %.010 = phi i16 [ 0, %0 ], [ %.119, %15 ]
  %2 = load i16, ptr @pad_keys, align 2, !tbaa !5
  %3 = zext i16 %2 to i32
  %4 = add nsw i32 %3, -256
  %5 = tail call i32 @llvm.fshl.i32(i32 %4, i32 %4, i32 24)
  switch i32 %5, label %15 [
    i32 0, label %6
    i32 1, label %8
    i32 3, label %10
    i32 7, label %12
  ]

6:                                                ; preds = %1
  %7 = add i16 %.010, 2
  br label %14

8:                                                ; preds = %1
  %9 = add i16 %.010, -2
  br label %14

10:                                               ; preds = %1
  %11 = add i16 %.011, 2
  br label %14

12:                                               ; preds = %1
  %13 = add i16 %.011, -2
  br label %14

14:                                               ; preds = %12, %10, %8, %6
  %.112.ph = phi i16 [ %.011, %6 ], [ %.011, %8 ], [ %11, %10 ], [ %13, %12 ]
  %.1.ph = phi i16 [ %7, %6 ], [ %9, %8 ], [ %.010, %10 ], [ %.010, %12 ]
  tail call void @bgSetScroll(i8 noundef zeroext 1, i16 noundef zeroext %.1.ph, i16 noundef zeroext %.112.ph) #3
  br label %15

15:                                               ; preds = %1, %14
  %.119 = phi i16 [ %.1.ph, %14 ], [ %.010, %1 ]
  %.11217 = phi i16 [ %.112.ph, %14 ], [ %.011, %1 ]
  %16 = zext i16 %.119 to i32
  %17 = zext i16 %.11217 to i32
  tail call void (i16, i16, ptr, ...) @consoleDrawText(i16 noundef zeroext 0, i16 noundef zeroext 0, ptr noundef nonnull @.str, i32 noundef %16, i32 noundef %17) #3
  tail call void @WaitForVBlank() #3
  br label %1
}

declare void @consoleSetTextMapPtr(i16 noundef zeroext) local_unnamed_addr #1

declare void @consoleSetTextGfxPtr(i16 noundef zeroext) local_unnamed_addr #1

declare void @consoleSetTextOffset(i16 noundef zeroext) local_unnamed_addr #1

declare void @consoleInitText(i8 noundef zeroext, i8 noundef zeroext, ptr noundef, ptr noundef) 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

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

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

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

declare void @bgSetDisable(i8 noundef zeroext) local_unnamed_addr #1

declare void @setScreenOn() local_unnamed_addr #1

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

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

declare void @WaitForVBlank() local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.fshl.i32(i32, i32, i32) #2

attributes #0 = { noreturn 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass