; LLVM IR from Stephane-D/SGDK :: src (vdp)

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

@planeWidth = external dso_local global i16, align 2
@planeHeight = external dso_local global i16, align 2
@planeWidthSft = external dso_local global i16, align 2
@planeHeightSft = external dso_local global i16, align 2
@regValues = external hidden global [19 x i8], align 16

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

; Function Attrs: nounwind uwtable
declare hidden void @updateMapsAddress(i8 noundef zeroext) #1

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

; Function Attrs: noinline nounwind uwtable
define dso_local void @VDP_setPlaneSize(i16 noundef zeroext %0, i16 noundef zeroext %1, i8 noundef zeroext %2) #2 {
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  %6 = alloca i8, align 1
  %7 = alloca ptr, align 8
  %8 = alloca i16, align 2
  store i16 %0, ptr %4, align 2, !tbaa !5
  store i16 %1, ptr %5, align 2, !tbaa !5
  store i8 %2, ptr %6, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #3
  store i16 0, ptr %8, align 2, !tbaa !5
  %9 = load i16, ptr %4, align 2, !tbaa !5
  %10 = zext i16 %9 to i32
  %11 = and i32 %10, 128
  %12 = icmp ne i32 %11, 0
  br i1 %12, label %13, label %18

13:                                               ; preds = %3
  store i16 128, ptr @planeWidth, align 2, !tbaa !5
  store i16 7, ptr @planeWidthSft, align 2, !tbaa !5
  %14 = load i16, ptr %8, align 2, !tbaa !5
  %15 = zext i16 %14 to i32
  %16 = or i32 %15, 3
  %17 = trunc i32 %16 to i16
  store i16 %17, ptr %8, align 2, !tbaa !5
  store i16 32, ptr @planeHeight, align 2, !tbaa !5
  store i16 5, ptr @planeHeightSft, align 2, !tbaa !5
  br label %63

18:                                               ; preds = %3
  %19 = load i16, ptr %4, align 2, !tbaa !5
  %20 = zext i16 %19 to i32
  %21 = and i32 %20, 64
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %39

23:                                               ; preds = %18
  store i16 64, ptr @planeWidth, align 2, !tbaa !5
  store i16 6, ptr @planeWidthSft, align 2, !tbaa !5
  %24 = load i16, ptr %8, align 2, !tbaa !5
  %25 = zext i16 %24 to i32
  %26 = or i32 %25, 1
  %27 = trunc i32 %26 to i16
  store i16 %27, ptr %8, align 2, !tbaa !5
  %28 = load i16, ptr %5, align 2, !tbaa !5
  %29 = zext i16 %28 to i32
  %30 = and i32 %29, 64
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %37

32:                                               ; preds = %23
  store i16 64, ptr @planeHeight, align 2, !tbaa !5
  store i16 6, ptr @planeHeightSft, align 2, !tbaa !5
  %33 = load i16, ptr %8, align 2, !tbaa !5
  %34 = zext i16 %33 to i32
  %35 = or i32 %34, 16
  %36 = trunc i32 %35 to i16
  store i16 %36, ptr %8, align 2, !tbaa !5
  br label %38

37:                                               ; preds = %23
  store i16 32, ptr @planeHeight, align 2, !tbaa !5
  store i16 5, ptr @planeHeightSft, align 2, !tbaa !5
  br label %38

38:                                               ; preds = %37, %32
  br label %62

39:                                               ; preds = %18
  store i16 32, ptr @planeWidth, align 2, !tbaa !5
  store i16 5, ptr @planeWidthSft, align 2, !tbaa !5
  %40 = load i16, ptr %5, align 2, !tbaa !5
  %41 = zext i16 %40 to i32
  %42 = and i32 %41, 128
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %49

44:                                               ; preds = %39
  store i16 128, ptr @planeHeight, align 2, !tbaa !5
  store i16 7, ptr @planeHeightSft, align 2, !tbaa !5
  %45 = load i16, ptr %8, align 2, !tbaa !5
  %46 = zext i16 %45 to i32
  %47 = or i32 %46, 48
  %48 = trunc i32 %47 to i16
  store i16 %48, ptr %8, align 2, !tbaa !5
  br label %61

49:                                               ; preds = %39
  %50 = load i16, ptr %5, align 2, !tbaa !5
  %51 = zext i16 %50 to i32
  %52 = and i32 %51, 64
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %54, label %59

54:                                               ; preds = %49
  store i16 64, ptr @planeHeight, align 2, !tbaa !5
  store i16 6, ptr @planeHeightSft, align 2, !tbaa !5
  %55 = load i16, ptr %8, align 2, !tbaa !5
  %56 = zext i16 %55 to i32
  %57 = or i32 %56, 16
  %58 = trunc i32 %57 to i16
  store i16 %58, ptr %8, align 2, !tbaa !5
  br label %60

59:                                               ; preds = %49
  store i16 32, ptr @planeHeight, align 2, !tbaa !5
  store i16 5, ptr @planeHeightSft, align 2, !tbaa !5
  br label %60

60:                                               ; preds = %59, %54
  br label %61

61:                                               ; preds = %60, %44
  br label %62

62:                                               ; preds = %61, %38
  br label %63

63:                                               ; preds = %62, %13
  %64 = load i16, ptr %8, align 2, !tbaa !5
  %65 = trunc i16 %64 to i8
  store i8 %65, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 16), align 16, !tbaa !9
  store ptr inttoptr (i64 12582916 to ptr), ptr %7, align 8, !tbaa !10
  %66 = load i8, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 16), align 16, !tbaa !9
  %67 = zext i8 %66 to i32
  %68 = or i32 36864, %67
  %69 = trunc i32 %68 to i16
  %70 = load ptr, ptr %7, align 8, !tbaa !10
  store volatile i16 %69, ptr %70, align 2, !tbaa !5
  %71 = load i8, ptr %6, align 1, !tbaa !9
  %72 = icmp ne i8 %71, 0
  br i1 %72, label %73, label %83

73:                                               ; preds = %63
  %74 = load i16, ptr @planeWidthSft, align 2, !tbaa !5
  %75 = zext i16 %74 to i32
  %76 = load i16, ptr @planeHeightSft, align 2, !tbaa !5
  %77 = zext i16 %76 to i32
  %78 = add nsw i32 %75, %77
  switch i32 %78, label %81 [
    i32 10, label %79
    i32 11, label %80
  ]

79:                                               ; preds = %73
  call void @VDP_setBGBAddress(i16 noundef zeroext -16384)
  call void @VDP_setWindowAddress(i16 noundef zeroext -14336)
  call void @VDP_setBGAAddress(i16 noundef zeroext -8192)
  call void @VDP_setSpriteListAddress(i16 noundef zeroext -6144)
  call void @VDP_setHScrollTableAddress(i16 noundef zeroext -5120)
  br label %82

80:                                               ; preds = %73
  call void @VDP_setBGBAddress(i16 noundef zeroext -16384)
  call void @VDP_setWindowAddress(i16 noundef zeroext -12288)
  call void @VDP_setBGAAddress(i16 noundef zeroext -8192)
  call void @VDP_setHScrollTableAddress(i16 noundef zeroext -4096)
  call void @VDP_setSpriteListAddress(i16 noundef zeroext -3072)
  br label %82

81:                                               ; preds = %73
  call void @VDP_setSpriteListAddress(i16 noundef zeroext -21504)
  call void @VDP_setHScrollTableAddress(i16 noundef zeroext -22528)
  call void @VDP_setWindowAddress(i16 noundef zeroext -20480)
  call void @VDP_setBGBAddress(i16 noundef zeroext -16384)
  call void @VDP_setBGAAddress(i16 noundef zeroext -8192)
  br label %82

82:                                               ; preds = %81, %80, %79
  call void @updateMapsAddress(i8 noundef zeroext 0)
  br label %83

83:                                               ; preds = %82, %63
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local void @VDP_setBGBAddress(i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
declare dso_local void @VDP_setWindowAddress(i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
declare dso_local void @VDP_setBGAAddress(i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
declare dso_local void @VDP_setSpriteListAddress(i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
declare dso_local void @VDP_setHScrollTableAddress(i16 noundef zeroext) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { noinline 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 #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"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !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"

@planeWidth = external dso_local local_unnamed_addr global i16, align 2
@planeHeight = external dso_local local_unnamed_addr global i16, align 2
@planeWidthSft = external dso_local local_unnamed_addr global i16, align 2
@planeHeightSft = external dso_local local_unnamed_addr global i16, align 2
@regValues = external hidden local_unnamed_addr global [19 x i8], align 16

; Function Attrs: nounwind uwtable
declare hidden void @updateMapsAddress(i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: noinline nounwind uwtable
define dso_local void @VDP_setPlaneSize(i16 noundef zeroext %0, i16 noundef zeroext %1, i8 noundef zeroext %2) local_unnamed_addr #1 {
  %4 = zext i16 %0 to i32
  %5 = and i32 %4, 128
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %7, label %6

6:                                                ; preds = %3
  store i16 128, ptr @planeWidth, align 2, !tbaa !5
  store i16 7, ptr @planeWidthSft, align 2, !tbaa !5
  br label %17

7:                                                ; preds = %3
  %8 = and i32 %4, 64
  %.not12 = icmp eq i32 %8, 0
  br i1 %.not12, label %11, label %9

9:                                                ; preds = %7
  store i16 64, ptr @planeWidth, align 2, !tbaa !5
  store i16 6, ptr @planeWidthSft, align 2, !tbaa !5
  %10 = and i16 %1, 64
  %.not15 = icmp eq i16 %10, 0
  %. = select i1 %.not15, i16 32, i16 64
  %.18 = select i1 %.not15, i16 5, i16 6
  %.19 = select i1 %.not15, i8 1, i8 17
  br label %17

11:                                               ; preds = %7
  store i16 32, ptr @planeWidth, align 2, !tbaa !5
  store i16 5, ptr @planeWidthSft, align 2, !tbaa !5
  %12 = zext i16 %1 to i32
  %13 = and i32 %12, 128
  %.not13 = icmp eq i32 %13, 0
  br i1 %.not13, label %14, label %17

14:                                               ; preds = %11
  %15 = and i32 %12, 64
  %.not14 = icmp eq i32 %15, 0
  %.20 = select i1 %.not14, i16 32, i16 64
  %.21 = select i1 %.not14, i16 5, i16 6
  %16 = lshr exact i32 %15, 2
  %.22 = trunc nuw nsw i32 %16 to i8
  br label %17

17:                                               ; preds = %14, %11, %9, %6
  %.sink17 = phi i16 [ 32, %6 ], [ %., %9 ], [ 128, %11 ], [ %.20, %14 ]
  %.sink = phi i16 [ 5, %6 ], [ %.18, %9 ], [ 7, %11 ], [ %.21, %14 ]
  %.0 = phi i8 [ 3, %6 ], [ %.19, %9 ], [ 48, %11 ], [ %.22, %14 ]
  store i16 %.sink17, ptr @planeHeight, align 2, !tbaa !5
  store i16 %.sink, ptr @planeHeightSft, align 2, !tbaa !5
  store i8 %.0, ptr getelementptr inbounds (i8, ptr @regValues, i64 16), align 16, !tbaa !9
  %18 = zext nneg i8 %.0 to i16
  %19 = or disjoint i16 %18, -28672
  store volatile i16 %19, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %.not16 = icmp eq i8 %2, 0
  br i1 %.not16, label %30, label %20

20:                                               ; preds = %17
  %21 = load i16, ptr @planeWidthSft, align 2, !tbaa !5
  %22 = zext i16 %21 to i32
  %23 = load i16, ptr @planeHeightSft, align 2, !tbaa !5
  %24 = zext i16 %23 to i32
  %25 = add nuw nsw i32 %24, %22
  switch i32 %25, label %28 [
    i32 10, label %26
    i32 11, label %27
  ]

26:                                               ; preds = %20
  tail call void @VDP_setBGBAddress(i16 noundef zeroext -16384)
  tail call void @VDP_setWindowAddress(i16 noundef zeroext -14336)
  tail call void @VDP_setBGAAddress(i16 noundef zeroext -8192)
  tail call void @VDP_setSpriteListAddress(i16 noundef zeroext -6144)
  tail call void @VDP_setHScrollTableAddress(i16 noundef zeroext -5120)
  br label %29

27:                                               ; preds = %20
  tail call void @VDP_setBGBAddress(i16 noundef zeroext -16384)
  tail call void @VDP_setWindowAddress(i16 noundef zeroext -12288)
  tail call void @VDP_setBGAAddress(i16 noundef zeroext -8192)
  tail call void @VDP_setHScrollTableAddress(i16 noundef zeroext -4096)
  tail call void @VDP_setSpriteListAddress(i16 noundef zeroext -3072)
  br label %29

28:                                               ; preds = %20
  tail call void @VDP_setSpriteListAddress(i16 noundef zeroext -21504)
  tail call void @VDP_setHScrollTableAddress(i16 noundef zeroext -22528)
  tail call void @VDP_setWindowAddress(i16 noundef zeroext -20480)
  tail call void @VDP_setBGBAddress(i16 noundef zeroext -16384)
  tail call void @VDP_setBGAAddress(i16 noundef zeroext -8192)
  br label %29

29:                                               ; preds = %28, %27, %26
  tail call void @updateMapsAddress(i8 noundef zeroext 0)
  br label %30

30:                                               ; preds = %29, %17
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local void @VDP_setBGBAddress(i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @VDP_setWindowAddress(i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @VDP_setBGAAddress(i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @VDP_setSpriteListAddress(i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @VDP_setHScrollTableAddress(i16 noundef zeroext) local_unnamed_addr #0

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

!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: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass