; LLVM IR from alekmaul/pvsneslib :: snes-examples/graphics/Sprites/AnimatedSprite (AnimatedSprite)

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

%struct.Monster = type { i16, i16, i16, i16, i8, i8 }

@sprTiles = external dso_local constant [9 x i8], align 1
@monster = external dso_local global %struct.Monster, align 2
@gfxpsrite = external global i8, align 1
@gfxpsrite_end = external global i8, align 1
@palsprite = external global i8, align 1
@palsprite_end = external 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 @oamInitGfxSet(ptr noundef @gfxpsrite, i16 noundef zeroext trunc (i64 sub (i64 ptrtoint (ptr @gfxpsrite_end to i64), i64 ptrtoint (ptr @gfxpsrite to i64)) to i16), ptr noundef @palsprite, i16 noundef zeroext trunc (i64 sub (i64 ptrtoint (ptr @palsprite_end to i64), i64 ptrtoint (ptr @palsprite to i64)) to i16), i8 noundef zeroext 0, i16 noundef zeroext 0, i8 noundef zeroext 96)
  %2 = load i16, ptr @monster, align 2, !tbaa !5
  %3 = load i16, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 1), align 2, !tbaa !10
  call void @oamSet(i16 noundef zeroext 0, i16 noundef zeroext %2, i16 noundef zeroext %3, i8 noundef zeroext 0, i8 noundef zeroext 0, i8 noundef zeroext 0, i16 noundef zeroext 0, i8 noundef zeroext 0)
  call void @oamSetEx(i16 noundef zeroext 0, i8 noundef zeroext 0, i8 noundef zeroext 0)
  call void @oamSetVisible(i16 noundef zeroext 0, i8 noundef zeroext 0)
  call void @setMode(i8 noundef zeroext 1, i8 noundef zeroext 0)
  call void @bgSetDisable(i8 noundef zeroext 0)
  call void @bgSetDisable(i8 noundef zeroext 1)
  call void @bgSetDisable(i8 noundef zeroext 2)
  call void @setScreenOn()
  br label %4

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

8:                                                ; preds = %4
  %9 = load i16, ptr @pad0, align 2, !tbaa !11
  %10 = zext i16 %9 to i32
  %11 = and i32 %10, 2048
  %12 = icmp ne i32 %11, 0
  br i1 %12, label %13, label %21

13:                                               ; preds = %8
  %14 = load i16, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 1), align 2, !tbaa !10
  %15 = sext i16 %14 to i32
  %16 = icmp sge i32 %15, -16
  br i1 %16, label %17, label %20

17:                                               ; preds = %13
  %18 = load i16, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 1), align 2, !tbaa !10
  %19 = add i16 %18, -1
  store i16 %19, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 1), align 2, !tbaa !10
  br label %20

20:                                               ; preds = %17, %13
  store i8 1, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 4), align 2, !tbaa !12
  store i8 0, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 5), align 1, !tbaa !13
  br label %21

21:                                               ; preds = %20, %8
  %22 = load i16, ptr @pad0, align 2, !tbaa !11
  %23 = zext i16 %22 to i32
  %24 = and i32 %23, 512
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %34

26:                                               ; preds = %21
  %27 = load i16, ptr @monster, align 2, !tbaa !5
  %28 = sext i16 %27 to i32
  %29 = icmp sge i32 %28, -16
  br i1 %29, label %30, label %33

30:                                               ; preds = %26
  %31 = load i16, ptr @monster, align 2, !tbaa !5
  %32 = add i16 %31, -1
  store i16 %32, ptr @monster, align 2, !tbaa !5
  br label %33

33:                                               ; preds = %30, %26
  store i8 2, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 4), align 2, !tbaa !12
  store i8 1, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 5), align 1, !tbaa !13
  br label %34

34:                                               ; preds = %33, %21
  %35 = load i16, ptr @pad0, align 2, !tbaa !11
  %36 = zext i16 %35 to i32
  %37 = and i32 %36, 256
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %39, label %47

39:                                               ; preds = %34
  %40 = load i16, ptr @monster, align 2, !tbaa !5
  %41 = sext i16 %40 to i32
  %42 = icmp sle i32 %41, 256
  br i1 %42, label %43, label %46

43:                                               ; preds = %39
  %44 = load i16, ptr @monster, align 2, !tbaa !5
  %45 = add i16 %44, 1
  store i16 %45, ptr @monster, align 2, !tbaa !5
  br label %46

46:                                               ; preds = %43, %39
  store i8 2, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 4), align 2, !tbaa !12
  store i8 0, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 5), align 1, !tbaa !13
  br label %47

47:                                               ; preds = %46, %34
  %48 = load i16, ptr @pad0, align 2, !tbaa !11
  %49 = zext i16 %48 to i32
  %50 = and i32 %49, 1024
  %51 = icmp ne i32 %50, 0
  br i1 %51, label %52, label %60

52:                                               ; preds = %47
  %53 = load i16, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 1), align 2, !tbaa !10
  %54 = sext i16 %53 to i32
  %55 = icmp sle i32 %54, 224
  br i1 %55, label %56, label %59

56:                                               ; preds = %52
  %57 = load i16, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 1), align 2, !tbaa !10
  %58 = add i16 %57, 1
  store i16 %58, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 1), align 2, !tbaa !10
  br label %59

59:                                               ; preds = %56, %52
  store i8 0, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 4), align 2, !tbaa !12
  store i8 0, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 5), align 1, !tbaa !13
  br label %60

60:                                               ; preds = %59, %47
  %61 = load i16, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 3), align 2, !tbaa !14
  %62 = add i16 %61, 1
  store i16 %62, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 3), align 2, !tbaa !14
  %63 = load i16, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 3), align 2, !tbaa !14
  %64 = zext i16 %63 to i32
  %65 = icmp sge i32 %64, 3
  br i1 %65, label %66, label %67

66:                                               ; preds = %60
  store i16 0, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 3), align 2, !tbaa !14
  br label %67

67:                                               ; preds = %66, %60
  br label %68

68:                                               ; preds = %67, %4
  %69 = load i16, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 3), align 2, !tbaa !14
  %70 = zext i16 %69 to i32
  %71 = load i8, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 4), align 2, !tbaa !12
  %72 = zext i8 %71 to i32
  %73 = mul nsw i32 %72, 3
  %74 = add nsw i32 %70, %73
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [9 x i8], ptr @sprTiles, i64 0, i64 %75
  %77 = load i8, ptr %76, align 1, !tbaa !15
  %78 = sext i8 %77 to i16
  store i16 %78, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 2), align 2, !tbaa !16
  %79 = load i16, ptr @monster, align 2, !tbaa !5
  %80 = load i16, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 1), align 2, !tbaa !10
  %81 = load i8, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 5), align 1, !tbaa !13
  %82 = load i16, ptr getelementptr inbounds (%struct.Monster, ptr @monster, i32 0, i32 2), align 2, !tbaa !16
  call void @oamSet(i16 noundef zeroext 0, i16 noundef zeroext %79, i16 noundef zeroext %80, i8 noundef zeroext 3, i8 noundef zeroext %81, i8 noundef zeroext 0, i16 noundef zeroext %82, i8 noundef zeroext 0)
  call void @WaitForVBlank()
  br label %4
}

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

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

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

declare void @oamSetVisible(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 @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, !7, i64 0}
!6 = !{!"", !7, i64 0, !7, i64 2, !7, i64 4, !7, i64 6, !8, i64 8, !8, i64 9}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 2}
!11 = !{!7, !7, i64 0}
!12 = !{!6, !8, i64 8}
!13 = !{!6, !8, i64 9}
!14 = !{!6, !7, i64 6}
!15 = !{!8, !8, i64 0}
!16 = !{!6, !7, i64 4}


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

%struct.Monster = type { i16, i16, i16, i16, i8, i8 }

@sprTiles = external dso_local local_unnamed_addr constant [9 x i8], align 1
@monster = external dso_local local_unnamed_addr global %struct.Monster, align 2
@gfxpsrite = external global i8, align 1
@gfxpsrite_end = external global i8, align 1
@palsprite = external global i8, align 1
@palsprite_end = external 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 @oamInitGfxSet(ptr noundef nonnull @gfxpsrite, i16 noundef zeroext trunc (i64 sub (i64 ptrtoint (ptr @gfxpsrite_end to i64), i64 ptrtoint (ptr @gfxpsrite to i64)) to i16), ptr noundef nonnull @palsprite, i16 noundef zeroext trunc (i64 sub (i64 ptrtoint (ptr @palsprite_end to i64), i64 ptrtoint (ptr @palsprite to i64)) to i16), i8 noundef zeroext 0, i16 noundef zeroext 0, i8 noundef zeroext 96) #2
  %1 = load i16, ptr @monster, align 2, !tbaa !5
  %2 = load i16, ptr getelementptr inbounds (i8, ptr @monster, i64 2), align 2, !tbaa !10
  tail call void @oamSet(i16 noundef zeroext 0, i16 noundef zeroext %1, i16 noundef zeroext %2, i8 noundef zeroext 0, i8 noundef zeroext 0, i8 noundef zeroext 0, i16 noundef zeroext 0, i8 noundef zeroext 0) #2
  tail call void @oamSetEx(i16 noundef zeroext 0, i8 noundef zeroext 0, i8 noundef zeroext 0) #2
  tail call void @oamSetVisible(i16 noundef zeroext 0, i8 noundef zeroext 0) #2
  tail call void @setMode(i8 noundef zeroext 1, i8 noundef zeroext 0) #2
  tail call void @bgSetDisable(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
  br label %3

3:                                                ; preds = %41, %0
  %4 = load i16, ptr @pad_keys, align 2, !tbaa !11
  store i16 %4, ptr @pad0, align 2, !tbaa !11
  %.not = icmp eq i16 %4, 0
  br i1 %.not, label %._crit_edge, label %5

._crit_edge:                                      ; preds = %3
  %.pre = load i16, ptr getelementptr inbounds (i8, ptr @monster, i64 6), align 2, !tbaa !12
  br label %41

5:                                                ; preds = %3
  %6 = and i16 %4, 2048
  %.not1 = icmp eq i16 %6, 0
  br i1 %.not1, label %13, label %7

7:                                                ; preds = %5
  %8 = load i16, ptr getelementptr inbounds (i8, ptr @monster, i64 2), align 2, !tbaa !10
  %9 = icmp sgt i16 %8, -17
  br i1 %9, label %10, label %12

10:                                               ; preds = %7
  %11 = add nsw i16 %8, -1
  store i16 %11, ptr getelementptr inbounds (i8, ptr @monster, i64 2), align 2, !tbaa !10
  br label %12

12:                                               ; preds = %10, %7
  store i8 1, ptr getelementptr inbounds (i8, ptr @monster, i64 8), align 2, !tbaa !13
  store i8 0, ptr getelementptr inbounds (i8, ptr @monster, i64 9), align 1, !tbaa !14
  br label %13

13:                                               ; preds = %12, %5
  %14 = and i16 %4, 512
  %.not2 = icmp eq i16 %14, 0
  br i1 %.not2, label %21, label %15

15:                                               ; preds = %13
  %16 = load i16, ptr @monster, align 2, !tbaa !5
  %17 = icmp sgt i16 %16, -17
  br i1 %17, label %18, label %20

18:                                               ; preds = %15
  %19 = add nsw i16 %16, -1
  store i16 %19, ptr @monster, align 2, !tbaa !5
  br label %20

20:                                               ; preds = %18, %15
  store i8 2, ptr getelementptr inbounds (i8, ptr @monster, i64 8), align 2, !tbaa !13
  store i8 1, ptr getelementptr inbounds (i8, ptr @monster, i64 9), align 1, !tbaa !14
  br label %21

21:                                               ; preds = %20, %13
  %22 = and i16 %4, 256
  %.not3 = icmp eq i16 %22, 0
  br i1 %.not3, label %29, label %23

23:                                               ; preds = %21
  %24 = load i16, ptr @monster, align 2, !tbaa !5
  %25 = icmp slt i16 %24, 257
  br i1 %25, label %26, label %28

26:                                               ; preds = %23
  %27 = add nsw i16 %24, 1
  store i16 %27, ptr @monster, align 2, !tbaa !5
  br label %28

28:                                               ; preds = %26, %23
  store i8 2, ptr getelementptr inbounds (i8, ptr @monster, i64 8), align 2, !tbaa !13
  store i8 0, ptr getelementptr inbounds (i8, ptr @monster, i64 9), align 1, !tbaa !14
  br label %29

29:                                               ; preds = %28, %21
  %30 = and i16 %4, 1024
  %.not4 = icmp eq i16 %30, 0
  br i1 %.not4, label %37, label %31

31:                                               ; preds = %29
  %32 = load i16, ptr getelementptr inbounds (i8, ptr @monster, i64 2), align 2, !tbaa !10
  %33 = icmp slt i16 %32, 225
  br i1 %33, label %34, label %36

34:                                               ; preds = %31
  %35 = add nsw i16 %32, 1
  store i16 %35, ptr getelementptr inbounds (i8, ptr @monster, i64 2), align 2, !tbaa !10
  br label %36

36:                                               ; preds = %34, %31
  store i8 0, ptr getelementptr inbounds (i8, ptr @monster, i64 8), align 2, !tbaa !13
  store i8 0, ptr getelementptr inbounds (i8, ptr @monster, i64 9), align 1, !tbaa !14
  br label %37

37:                                               ; preds = %36, %29
  %38 = load i16, ptr getelementptr inbounds (i8, ptr @monster, i64 6), align 2, !tbaa !12
  %39 = add i16 %38, 1
  %40 = icmp ugt i16 %39, 2
  %spec.store.select = select i1 %40, i16 0, i16 %39
  store i16 %spec.store.select, ptr getelementptr inbounds (i8, ptr @monster, i64 6), align 2
  br label %41

41:                                               ; preds = %._crit_edge, %37
  %42 = phi i16 [ %.pre, %._crit_edge ], [ %spec.store.select, %37 ]
  %43 = zext i16 %42 to i64
  %44 = load i8, ptr getelementptr inbounds (i8, ptr @monster, i64 8), align 2, !tbaa !13
  %45 = zext i8 %44 to i64
  %46 = mul nuw nsw i64 %45, 3
  %47 = add nuw nsw i64 %46, %43
  %48 = getelementptr inbounds [9 x i8], ptr @sprTiles, i64 0, i64 %47
  %49 = load i8, ptr %48, align 1, !tbaa !15
  %50 = sext i8 %49 to i16
  store i16 %50, ptr getelementptr inbounds (i8, ptr @monster, i64 4), align 2, !tbaa !16
  %51 = load i16, ptr @monster, align 2, !tbaa !5
  %52 = load i16, ptr getelementptr inbounds (i8, ptr @monster, i64 2), align 2, !tbaa !10
  %53 = load i8, ptr getelementptr inbounds (i8, ptr @monster, i64 9), align 1, !tbaa !14
  tail call void @oamSet(i16 noundef zeroext 0, i16 noundef zeroext %51, i16 noundef zeroext %52, i8 noundef zeroext 3, i8 noundef zeroext %53, i8 noundef zeroext 0, i16 noundef zeroext %50, i8 noundef zeroext 0) #2
  tail call void @WaitForVBlank() #2
  br label %3
}

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

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

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

declare void @oamSetVisible(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 @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, !7, i64 0}
!6 = !{!"", !7, i64 0, !7, i64 2, !7, i64 4, !7, i64 6, !8, i64 8, !8, i64 9}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 2}
!11 = !{!7, !7, i64 0}
!12 = !{!6, !7, i64 6}
!13 = !{!6, !8, i64 8}
!14 = !{!6, !8, i64 9}
!15 = !{!8, !8, i64 0}
!16 = !{!6, !7, i64 4}


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