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

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

@tileset = external global i8, align 1
@tilesetpal = external global i8, align 1
@tilesetend = external global i8, align 1
@mapkungfu = external global i8, align 1
@tilesetdef = external global i8, align 1
@tilesetatt = external global i8, align 1
@mapscx = external dso_local global i16, align 2
@keyr = external dso_local global i8, align 1
@keyl = 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 @tileset, ptr noundef @tilesetpal, i8 noundef zeroext 0, i16 noundef zeroext trunc (i64 sub (i64 ptrtoint (ptr @tilesetend to i64), i64 ptrtoint (ptr @tileset to i64)) to i16), i16 noundef zeroext 96, i16 noundef zeroext 16, i16 noundef zeroext 8192)
  call void @bgSetMapPtr(i8 noundef zeroext 0, i16 noundef zeroext 26624, i8 noundef zeroext 1)
  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 @mapLoad(ptr noundef @mapkungfu, ptr noundef @tilesetdef, ptr noundef @tilesetatt)
  store i16 128, ptr @mapscx, align 2, !tbaa !5
  store i8 0, ptr @keyr, align 1, !tbaa !9
  store i8 0, ptr @keyl, align 1, !tbaa !9
  br label %2

2:                                                ; preds = %29, %0
  call void @mapUpdate()
  %3 = load i16, ptr @pad_keys, align 2, !tbaa !5
  store i16 %3, ptr @pad0, align 2, !tbaa !5
  %4 = load i16, ptr @pad0, align 2, !tbaa !5
  %5 = zext i16 %4 to i32
  %6 = and i32 %5, 512
  %7 = icmp ne i32 %6, 0
  br i1 %7, label %8, label %16

8:                                                ; preds = %2
  %9 = load i16, ptr @mapscx, align 2, !tbaa !5
  %10 = sext i16 %9 to i32
  %11 = icmp sgt i32 %10, 128
  br i1 %11, label %12, label %15

12:                                               ; preds = %8
  %13 = load i16, ptr @mapscx, align 2, !tbaa !5
  %14 = add i16 %13, -1
  store i16 %14, ptr @mapscx, align 2, !tbaa !5
  br label %15

15:                                               ; preds = %12, %8
  br label %16

16:                                               ; preds = %15, %2
  %17 = load i16, ptr @pad0, align 2, !tbaa !5
  %18 = zext i16 %17 to i32
  %19 = and i32 %18, 256
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %21, label %29

21:                                               ; preds = %16
  %22 = load i16, ptr @mapscx, align 2, !tbaa !5
  %23 = sext i16 %22 to i32
  %24 = icmp slt i32 %23, 1664
  br i1 %24, label %25, label %28

25:                                               ; preds = %21
  %26 = load i16, ptr @mapscx, align 2, !tbaa !5
  %27 = add i16 %26, 1
  store i16 %27, ptr @mapscx, align 2, !tbaa !5
  br label %28

28:                                               ; preds = %25, %21
  br label %29

29:                                               ; preds = %28, %16
  %30 = load i16, ptr @mapscx, align 2, !tbaa !5
  call void @mapUpdateCamera(i16 noundef zeroext %30, i16 noundef zeroext 0)
  call void @WaitForVBlank()
  call void @mapVblank()
  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 @bgSetMapPtr(i8 noundef zeroext, i16 noundef zeroext, i8 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 @mapLoad(ptr noundef, ptr noundef, ptr noundef) #1

declare void @mapUpdate() #1

declare void @mapUpdateCamera(i16 noundef zeroext, i16 noundef zeroext) #1

declare void @WaitForVBlank() #1

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

@tileset = external global i8, align 1
@tilesetpal = external global i8, align 1
@tilesetend = external global i8, align 1
@mapkungfu = external global i8, align 1
@tilesetdef = external global i8, align 1
@tilesetatt = external global i8, align 1
@mapscx = external dso_local local_unnamed_addr global i16, align 2
@keyr = external dso_local local_unnamed_addr global i8, align 1
@keyl = 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 @tileset, ptr noundef nonnull @tilesetpal, i8 noundef zeroext 0, i16 noundef zeroext trunc (i64 sub (i64 ptrtoint (ptr @tilesetend to i64), i64 ptrtoint (ptr @tileset to i64)) to i16), i16 noundef zeroext 96, i16 noundef zeroext 16, i16 noundef zeroext 8192) #2
  tail call void @bgSetMapPtr(i8 noundef zeroext 0, i16 noundef zeroext 26624, i8 noundef zeroext 1) #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 @mapLoad(ptr noundef nonnull @mapkungfu, ptr noundef nonnull @tilesetdef, ptr noundef nonnull @tilesetatt) #2
  store i16 128, ptr @mapscx, align 2, !tbaa !5
  store i8 0, ptr @keyr, align 1, !tbaa !9
  store i8 0, ptr @keyl, align 1, !tbaa !9
  br label %1

1:                                                ; preds = %16, %0
  tail call void @mapUpdate() #2
  %2 = load i16, ptr @pad_keys, align 2, !tbaa !5
  store i16 %2, ptr @pad0, align 2, !tbaa !5
  %3 = and i16 %2, 512
  %4 = icmp ne i16 %3, 0
  %5 = load i16, ptr @mapscx, align 2
  %6 = icmp sgt i16 %5, 128
  %or.cond = select i1 %4, i1 %6, i1 false
  %7 = sext i1 %or.cond to i16
  %8 = add nsw i16 %5, %7
  %9 = and i16 %2, 256
  %10 = icmp ne i16 %9, 0
  %11 = icmp slt i16 %8, 1664
  %or.cond5 = select i1 %10, i1 %11, i1 false
  %12 = zext i1 %or.cond5 to i16
  %13 = add nsw i16 %8, %12
  %14 = or i1 %or.cond, %or.cond5
  br i1 %14, label %15, label %16

15:                                               ; preds = %1
  store i16 %13, ptr @mapscx, align 2, !tbaa !5
  br label %16

16:                                               ; preds = %1, %15
  tail call void @mapUpdateCamera(i16 noundef zeroext %13, i16 noundef zeroext 0) #2
  tail call void @WaitForVBlank() #2
  tail call void @mapVblank() #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 @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 @mapLoad(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @mapUpdate() local_unnamed_addr #1

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

declare void @WaitForVBlank() local_unnamed_addr #1

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


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