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

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

@patterns = external global i8, align 1
@map = external global i8, align 1
@palette = external global i8, align 1
@patterns_end = external global i8, align 1
@angle = external dso_local global i8, align 1
@pad_keys = external global [5 x i16], align 2
@pad0 = external dso_local global i16, align 2

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @bgInitMapTileSet7(ptr noundef @patterns, ptr noundef @map, ptr noundef @palette, i16 noundef zeroext trunc (i64 sub (i64 ptrtoint (ptr @patterns_end to i64), i64 ptrtoint (ptr @patterns to i64)) to i16), i16 noundef zeroext 0)
  call void @setMode7(i8 noundef zeroext 0)
  call void @setScreenOn()
  store i8 0, ptr @angle, align 1, !tbaa !5
  br label %2

2:                                                ; preds = %18, %0
  %3 = load i16, ptr @pad_keys, align 2, !tbaa !8
  store i16 %3, ptr @pad0, align 2, !tbaa !8
  %4 = load i16, ptr @pad0, align 2, !tbaa !8
  %5 = icmp ne i16 %4, 0
  br i1 %5, label %6, label %18

6:                                                ; preds = %2
  %7 = load i16, ptr @pad0, align 2, !tbaa !8
  %8 = zext i16 %7 to i32
  switch i32 %8, label %17 [
    i32 128, label %9
    i32 32768, label %13
  ]

9:                                                ; preds = %6
  %10 = load i8, ptr @angle, align 1, !tbaa !5
  %11 = add i8 %10, 1
  store i8 %11, ptr @angle, align 1, !tbaa !5
  %12 = load i8, ptr @angle, align 1, !tbaa !5
  call void @setMode7Rot(i8 noundef zeroext %12)
  br label %17

13:                                               ; preds = %6
  %14 = load i8, ptr @angle, align 1, !tbaa !5
  %15 = add i8 %14, -1
  store i8 %15, ptr @angle, align 1, !tbaa !5
  %16 = load i8, ptr @angle, align 1, !tbaa !5
  call void @setMode7Rot(i8 noundef zeroext %16)
  br label %17

17:                                               ; preds = %13, %9, %6
  br label %18

18:                                               ; preds = %17, %2
  call void @WaitForVBlank()
  br label %2
}

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

declare void @setMode7(i8 noundef zeroext) #1

declare void @setScreenOn() #1

declare void @setMode7Rot(i8 noundef zeroext) #1

declare void @WaitForVBlank() #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" }

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


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

@patterns = external global i8, align 1
@map = external global i8, align 1
@palette = external global i8, align 1
@patterns_end = external global i8, align 1
@angle = external dso_local local_unnamed_addr global i8, align 1
@pad_keys = external local_unnamed_addr global [5 x i16], align 2
@pad0 = external dso_local local_unnamed_addr global i16, align 2

; Function Attrs: noreturn nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  tail call void @bgInitMapTileSet7(ptr noundef nonnull @patterns, ptr noundef nonnull @map, ptr noundef nonnull @palette, i16 noundef zeroext trunc (i64 sub (i64 ptrtoint (ptr @patterns_end to i64), i64 ptrtoint (ptr @patterns to i64)) to i16), i16 noundef zeroext 0) #2
  tail call void @setMode7(i8 noundef zeroext 0) #2
  tail call void @setScreenOn() #2
  store i8 0, ptr @angle, align 1, !tbaa !5
  br label %1

1:                                                ; preds = %6, %0
  %2 = load i16, ptr @pad_keys, align 2, !tbaa !8
  store i16 %2, ptr @pad0, align 2, !tbaa !8
  switch i16 %2, label %6 [
    i16 -32768, label %3
    i16 128, label %.sink.split
  ]

3:                                                ; preds = %1
  br label %.sink.split

.sink.split:                                      ; preds = %1, %3
  %.sink3 = phi i8 [ -1, %3 ], [ 1, %1 ]
  %4 = load i8, ptr @angle, align 1, !tbaa !5
  %5 = add i8 %4, %.sink3
  store i8 %5, ptr @angle, align 1, !tbaa !5
  tail call void @setMode7Rot(i8 noundef zeroext %5) #2
  br label %6

6:                                                ; preds = %.sink.split, %1
  tail call void @WaitForVBlank() #2
  br label %1
}

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

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

declare void @setScreenOn() local_unnamed_addr #1

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

declare void @WaitForVBlank() local_unnamed_addr #1

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 = { 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}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass