; LLVM IR from alekmaul/pvsneslib :: snes-examples/graphics/Effects/Waves (Waves)

; 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
@palette = external global i8, align 1
@patterns_end = external global i8, align 1
@palette_end = external global i8, align 1
@map = external global i8, align 1
@map_end = external global i8, align 1
@pada = external dso_local global i8, align 1
@padb = 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 @bgInitTileSet(i8 noundef zeroext 0, 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 trunc (i64 sub (i64 ptrtoint (ptr @palette_end to i64), i64 ptrtoint (ptr @palette to i64)) to i16), i16 noundef zeroext 16, i16 noundef zeroext 16384)
  call void @bgInitMapSet(i8 noundef zeroext 0, 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 0, i16 noundef zeroext 0)
  call void @setMode(i8 noundef zeroext 1, i8 noundef zeroext 0)
  call void @bgSetDisable(i8 noundef zeroext 1)
  call void @bgSetDisable(i8 noundef zeroext 2)
  call void @setScreenOn()
  call void @setModeHdmaWaves(i8 noundef zeroext 0)
  store i8 0, ptr @pada, align 1, !tbaa !5
  store i8 0, ptr @padb, align 1, !tbaa !5
  br label %2

2:                                                ; preds = %21, %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 = zext i16 %4 to i32
  %6 = and i32 %5, 128
  %7 = icmp ne i32 %6, 0
  br i1 %7, label %8, label %13

8:                                                ; preds = %2
  %9 = load i8, ptr @pada, align 1, !tbaa !5
  %10 = icmp ne i8 %9, 0
  br i1 %10, label %12, label %11

11:                                               ; preds = %8
  store i8 1, ptr @pada, align 1, !tbaa !5
  call void @setModeHdmaReset(i8 noundef zeroext 0)
  br label %12

12:                                               ; preds = %11, %8
  br label %14

13:                                               ; preds = %2
  store i8 0, ptr @pada, align 1, !tbaa !5
  br label %14

14:                                               ; preds = %13, %12
  %15 = load i16, ptr @pad0, align 2, !tbaa !8
  %16 = zext i16 %15 to i32
  %17 = and i32 %16, 32768
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %19, label %20

19:                                               ; preds = %14
  store i8 1, ptr @padb, align 1, !tbaa !5
  call void @setModeHdmaWaves(i8 noundef zeroext 0)
  br label %21

20:                                               ; preds = %14
  store i8 0, ptr @padb, align 1, !tbaa !5
  br label %21

21:                                               ; preds = %20, %19
  call void @setModeHdmaWavesMove()
  call void @WaitForVBlank()
  br label %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) #1

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

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

declare void @bgSetDisable(i8 noundef zeroext) #1

declare void @setScreenOn() #1

declare void @setModeHdmaWaves(i8 noundef zeroext) #1

declare void @setModeHdmaReset(i8 noundef zeroext) #1

declare void @setModeHdmaWavesMove() #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
@palette = external global i8, align 1
@patterns_end = external global i8, align 1
@palette_end = external global i8, align 1
@map = external global i8, align 1
@map_end = external global i8, align 1
@pada = external dso_local local_unnamed_addr global i8, align 1
@padb = 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 @bgInitTileSet(i8 noundef zeroext 0, 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 trunc (i64 sub (i64 ptrtoint (ptr @palette_end to i64), i64 ptrtoint (ptr @palette to i64)) to i16), i16 noundef zeroext 16, i16 noundef zeroext 16384) #2
  tail call void @bgInitMapSet(i8 noundef zeroext 0, 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 0, i16 noundef zeroext 0) #2
  tail call void @setMode(i8 noundef zeroext 1, i8 noundef zeroext 0) #2
  tail call void @bgSetDisable(i8 noundef zeroext 1) #2
  tail call void @bgSetDisable(i8 noundef zeroext 2) #2
  tail call void @setScreenOn() #2
  tail call void @setModeHdmaWaves(i8 noundef zeroext 0) #2
  store i8 0, ptr @pada, align 1, !tbaa !5
  store i8 0, ptr @padb, align 1, !tbaa !5
  br label %1

1:                                                ; preds = %11, %0
  %2 = load i16, ptr @pad_keys, align 2, !tbaa !8
  store i16 %2, ptr @pad0, align 2, !tbaa !8
  %3 = and i16 %2, 128
  %.not = icmp eq i16 %3, 0
  br i1 %.not, label %7, label %4

4:                                                ; preds = %1
  %5 = load i8, ptr @pada, align 1, !tbaa !5
  %.not1 = icmp eq i8 %5, 0
  br i1 %.not1, label %6, label %thread-pre-split

6:                                                ; preds = %4
  store i8 1, ptr @pada, align 1, !tbaa !5
  tail call void @setModeHdmaReset(i8 noundef zeroext 0) #2
  %.pr.pre = load i16, ptr @pad0, align 2, !tbaa !8
  br label %thread-pre-split

7:                                                ; preds = %1
  store i8 0, ptr @pada, align 1, !tbaa !5
  br label %thread-pre-split

thread-pre-split:                                 ; preds = %4, %6, %7
  %8 = phi i16 [ %2, %7 ], [ %.pr.pre, %6 ], [ %2, %4 ]
  %.not2 = icmp sgt i16 %8, -1
  br i1 %.not2, label %10, label %9

9:                                                ; preds = %thread-pre-split
  store i8 1, ptr @padb, align 1, !tbaa !5
  tail call void @setModeHdmaWaves(i8 noundef zeroext 0) #2
  br label %11

10:                                               ; preds = %thread-pre-split
  store i8 0, ptr @padb, align 1, !tbaa !5
  br label %11

11:                                               ; preds = %10, %9
  tail call void @setModeHdmaWavesMove() #2
  tail call void @WaitForVBlank() #2
  br label %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 @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 @setModeHdmaWaves(i8 noundef zeroext) local_unnamed_addr #1

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

declare void @setModeHdmaWavesMove() 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,GVNPass,GlobalOptPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass