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

@window_addr = external dso_local global i16, align 2
@bga_addr = external dso_local global i16, align 2
@bgb_addr = external dso_local global i16, align 2
@slist_addr = external dso_local global i16, align 2
@hscrl_addr = external dso_local global i16, align 2
@screenWidth = external dso_local global i16, align 2
@screenHeight = external dso_local global i16, align 2
@planeWidth = external dso_local global i16, align 2
@planeHeight = external dso_local global i16, align 2
@windowWidth = external dso_local global i16, align 2
@planeWidthSft = external dso_local global i16, align 2
@planeHeightSft = external dso_local global i16, align 2
@windowWidthSft = external dso_local global i16, align 2
@lastVCnt = external dso_local global i16, align 2
@regValues = external hidden global [19 x i8], align 16
@maps_addr = external dso_local global i16, align 2
@curTileInd = external global i16, align 2

; Function Attrs: noinline nounwind uwtable
define dso_local void @VDP_init() #0 {
  %1 = alloca ptr, align 8
  %2 = alloca i16, align 2
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #4
  call void @llvm.lifetime.start.p0(i64 2, ptr %2) #4
  call void @VDP_waitDMACompletion()
  store i16 -12288, ptr @window_addr, align 2, !tbaa !5
  store i16 -8192, ptr @bga_addr, align 2, !tbaa !5
  store i16 -16384, ptr @bgb_addr, align 2, !tbaa !5
  store i16 -3072, ptr @slist_addr, align 2, !tbaa !5
  store i16 -4096, ptr @hscrl_addr, align 2, !tbaa !5
  store i16 320, ptr @screenWidth, align 2, !tbaa !5
  store i16 224, ptr @screenHeight, align 2, !tbaa !5
  store i16 64, ptr @planeWidth, align 2, !tbaa !5
  store i16 32, ptr @planeHeight, align 2, !tbaa !5
  store i16 64, ptr @windowWidth, align 2, !tbaa !5
  store i16 6, ptr @planeWidthSft, align 2, !tbaa !5
  store i16 5, ptr @planeHeightSft, align 2, !tbaa !5
  store i16 6, ptr @windowWidthSft, align 2, !tbaa !5
  store i16 0, ptr @lastVCnt, align 2, !tbaa !5
  store i8 4, ptr @regValues, align 16, !tbaa !9
  store i8 116, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 1), align 1, !tbaa !9
  %3 = load i16, ptr @bga_addr, align 2, !tbaa !5
  %4 = zext i16 %3 to i32
  %5 = sdiv i32 %4, 1024
  %6 = trunc i32 %5 to i8
  store i8 %6, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 2), align 2, !tbaa !9
  %7 = load i16, ptr @window_addr, align 2, !tbaa !5
  %8 = zext i16 %7 to i32
  %9 = sdiv i32 %8, 1024
  %10 = trunc i32 %9 to i8
  store i8 %10, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 3), align 1, !tbaa !9
  %11 = load i16, ptr @bgb_addr, align 2, !tbaa !5
  %12 = zext i16 %11 to i32
  %13 = sdiv i32 %12, 8192
  %14 = trunc i32 %13 to i8
  store i8 %14, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 4), align 4, !tbaa !9
  %15 = load i16, ptr @slist_addr, align 2, !tbaa !5
  %16 = zext i16 %15 to i32
  %17 = sdiv i32 %16, 512
  %18 = trunc i32 %17 to i8
  store i8 %18, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 5), align 1, !tbaa !9
  store i8 0, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 6), align 2, !tbaa !9
  store i8 0, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 7), align 1, !tbaa !9
  store i8 0, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 8), align 8, !tbaa !9
  store i8 0, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 9), align 1, !tbaa !9
  store i8 1, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 10), align 2, !tbaa !9
  store i8 0, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 11), align 1, !tbaa !9
  store i8 -127, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 12), align 4, !tbaa !9
  %19 = load i16, ptr @hscrl_addr, align 2, !tbaa !5
  %20 = zext i16 %19 to i32
  %21 = sdiv i32 %20, 1024
  %22 = trunc i32 %21 to i8
  store i8 %22, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 13), align 1, !tbaa !9
  store i8 0, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 14), align 2, !tbaa !9
  store i8 2, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 15), align 1, !tbaa !9
  store i8 1, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 16), align 16, !tbaa !9
  store i8 0, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 17), align 1, !tbaa !9
  store i8 0, ptr getelementptr inbounds ([19 x i8], ptr @regValues, i64 0, i64 18), align 2, !tbaa !9
  store ptr inttoptr (i64 12582916 to ptr), ptr %1, align 8, !tbaa !10
  store i16 0, ptr %2, align 2, !tbaa !5
  br label %23

23:                                               ; preds = %40, %0
  %24 = load i16, ptr %2, align 2, !tbaa !5
  %25 = zext i16 %24 to i32
  %26 = icmp slt i32 %25, 19
  br i1 %26, label %27, label %43

27:                                               ; preds = %23
  %28 = load i16, ptr %2, align 2, !tbaa !5
  %29 = zext i16 %28 to i32
  %30 = shl i32 %29, 8
  %31 = or i32 32768, %30
  %32 = load i16, ptr %2, align 2, !tbaa !5
  %33 = zext i16 %32 to i64
  %34 = getelementptr inbounds [19 x i8], ptr @regValues, i64 0, i64 %33
  %35 = load i8, ptr %34, align 1, !tbaa !9
  %36 = zext i8 %35 to i32
  %37 = or i32 %31, %36
  %38 = trunc i32 %37 to i16
  %39 = load ptr, ptr %1, align 8, !tbaa !10
  store volatile i16 %38, ptr %39, align 2, !tbaa !5
  br label %40

40:                                               ; preds = %27
  %41 = load i16, ptr %2, align 2, !tbaa !5
  %42 = add i16 %41, 1
  store i16 %42, ptr %2, align 2, !tbaa !5
  br label %23, !llvm.loop !12

43:                                               ; preds = %23
  store i16 0, ptr @maps_addr, align 2, !tbaa !5
  call void @updateMapsAddress(i8 noundef zeroext 1)
  call void @VDP_resetScreen()
  call void @VDP_resetSprites()
  call void @VDP_setTextPlane(i32 noundef 0)
  call void @VDP_setTextPalette(i16 noundef zeroext 0)
  call void @VDP_setTextPriority(i16 noundef zeroext 1)
  store i16 16, ptr @curTileInd, align 2, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 2, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #4
  ret void
}

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

; Function Attrs: nounwind uwtable
declare dso_local void @VDP_waitDMACompletion() #2

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

; Function Attrs: noinline nounwind uwtable
declare dso_local void @VDP_resetScreen() #0

declare void @VDP_resetSprites() #3

declare void @VDP_setTextPlane(i32 noundef) #3

declare void @VDP_setTextPalette(i16 noundef zeroext) #3

declare void @VDP_setTextPriority(i16 noundef zeroext) #3

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 = { "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 #4 = { 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}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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

@window_addr = external dso_local local_unnamed_addr global i16, align 2
@bga_addr = external dso_local local_unnamed_addr global i16, align 2
@bgb_addr = external dso_local local_unnamed_addr global i16, align 2
@slist_addr = external dso_local local_unnamed_addr global i16, align 2
@hscrl_addr = external dso_local local_unnamed_addr global i16, align 2
@screenWidth = external dso_local local_unnamed_addr global i16, align 2
@screenHeight = external dso_local local_unnamed_addr global i16, align 2
@planeWidth = external dso_local local_unnamed_addr global i16, align 2
@planeHeight = external dso_local local_unnamed_addr global i16, align 2
@windowWidth = 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
@windowWidthSft = external dso_local local_unnamed_addr global i16, align 2
@lastVCnt = external dso_local local_unnamed_addr global i16, align 2
@regValues = external hidden local_unnamed_addr global [19 x i8], align 16
@maps_addr = external dso_local local_unnamed_addr global i16, align 2
@curTileInd = external local_unnamed_addr global i16, align 2

; Function Attrs: noinline nounwind uwtable
define dso_local void @VDP_init() local_unnamed_addr #0 {
  tail call void @VDP_waitDMACompletion()
  store i16 -12288, ptr @window_addr, align 2, !tbaa !5
  store i16 -8192, ptr @bga_addr, align 2, !tbaa !5
  store i16 -16384, ptr @bgb_addr, align 2, !tbaa !5
  store i16 -3072, ptr @slist_addr, align 2, !tbaa !5
  store i16 -4096, ptr @hscrl_addr, align 2, !tbaa !5
  store i16 320, ptr @screenWidth, align 2, !tbaa !5
  store i16 224, ptr @screenHeight, align 2, !tbaa !5
  store i16 64, ptr @planeWidth, align 2, !tbaa !5
  store i16 32, ptr @planeHeight, align 2, !tbaa !5
  store i16 64, ptr @windowWidth, align 2, !tbaa !5
  store i16 6, ptr @planeWidthSft, align 2, !tbaa !5
  store i16 5, ptr @planeHeightSft, align 2, !tbaa !5
  store i16 6, ptr @windowWidthSft, align 2, !tbaa !5
  store i16 0, ptr @lastVCnt, align 2, !tbaa !5
  store <4 x i8> <i8 4, i8 116, i8 56, i8 52>, ptr @regValues, align 16, !tbaa !9
  store i8 6, ptr getelementptr inbounds (i8, ptr @regValues, i64 4), align 4, !tbaa !9
  store i8 122, ptr getelementptr inbounds (i8, ptr @regValues, i64 5), align 1, !tbaa !9
  store i32 0, ptr getelementptr inbounds (i8, ptr @regValues, i64 6), align 2
  store <8 x i8> <i8 1, i8 0, i8 -127, i8 60, i8 0, i8 2, i8 1, i8 0>, ptr getelementptr inbounds (i8, ptr @regValues, i64 10), align 2, !tbaa !9
  store i8 0, ptr getelementptr inbounds (i8, ptr @regValues, i64 18), align 2, !tbaa !9
  store volatile i16 -32764, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %1 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 1), align 1, !tbaa !9
  %2 = zext i8 %1 to i16
  %3 = or disjoint i16 %2, -32512
  store volatile i16 %3, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %4 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 2), align 2, !tbaa !9
  %5 = zext i8 %4 to i16
  %6 = or disjoint i16 %5, -32256
  store volatile i16 %6, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %7 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 3), align 1, !tbaa !9
  %8 = zext i8 %7 to i16
  %9 = or disjoint i16 %8, -32000
  store volatile i16 %9, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %10 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 4), align 4, !tbaa !9
  %11 = zext i8 %10 to i16
  %12 = or disjoint i16 %11, -31744
  store volatile i16 %12, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %13 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 5), align 1, !tbaa !9
  %14 = zext i8 %13 to i16
  %15 = or disjoint i16 %14, -31488
  store volatile i16 %15, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %16 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 6), align 2, !tbaa !9
  %17 = zext i8 %16 to i16
  %18 = or disjoint i16 %17, -31232
  store volatile i16 %18, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %19 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 7), align 1, !tbaa !9
  %20 = zext i8 %19 to i16
  %21 = or disjoint i16 %20, -30976
  store volatile i16 %21, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %22 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 8), align 8, !tbaa !9
  %23 = zext i8 %22 to i16
  %24 = or disjoint i16 %23, -30720
  store volatile i16 %24, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %25 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 9), align 1, !tbaa !9
  %26 = zext i8 %25 to i16
  %27 = or disjoint i16 %26, -30464
  store volatile i16 %27, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %28 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 10), align 2, !tbaa !9
  %29 = zext i8 %28 to i16
  %30 = or disjoint i16 %29, -30208
  store volatile i16 %30, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %31 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 11), align 1, !tbaa !9
  %32 = zext i8 %31 to i16
  %33 = or disjoint i16 %32, -29952
  store volatile i16 %33, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %34 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 12), align 4, !tbaa !9
  %35 = zext i8 %34 to i16
  %36 = or disjoint i16 %35, -29696
  store volatile i16 %36, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %37 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 13), align 1, !tbaa !9
  %38 = zext i8 %37 to i16
  %39 = or disjoint i16 %38, -29440
  store volatile i16 %39, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %40 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 14), align 2, !tbaa !9
  %41 = zext i8 %40 to i16
  %42 = or disjoint i16 %41, -29184
  store volatile i16 %42, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %43 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 15), align 1, !tbaa !9
  %44 = zext i8 %43 to i16
  %45 = or disjoint i16 %44, -28928
  store volatile i16 %45, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %46 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 16), align 16, !tbaa !9
  %47 = zext i8 %46 to i16
  %48 = or disjoint i16 %47, -28672
  store volatile i16 %48, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %49 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 17), align 1, !tbaa !9
  %50 = zext i8 %49 to i16
  %51 = or disjoint i16 %50, -28416
  store volatile i16 %51, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  %52 = load i8, ptr getelementptr inbounds (i8, ptr @regValues, i64 18), align 2, !tbaa !9
  %53 = zext i8 %52 to i16
  %54 = or disjoint i16 %53, -28160
  store volatile i16 %54, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !5
  store i16 0, ptr @maps_addr, align 2, !tbaa !5
  tail call void @updateMapsAddress(i8 noundef zeroext 1)
  tail call void @VDP_resetScreen()
  tail call void @VDP_resetSprites() #3
  tail call void @VDP_setTextPlane(i32 noundef 0) #3
  tail call void @VDP_setTextPalette(i16 noundef zeroext 0) #3
  tail call void @VDP_setTextPriority(i16 noundef zeroext 1) #3
  store i16 16, ptr @curTileInd, align 2, !tbaa !5
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local void @VDP_waitDMACompletion() local_unnamed_addr #1

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

; Function Attrs: noinline nounwind uwtable
declare dso_local void @VDP_resetScreen() local_unnamed_addr #0

declare void @VDP_resetSprites() local_unnamed_addr #2

declare void @VDP_setTextPlane(i32 noundef) local_unnamed_addr #2

declare void @VDP_setTextPalette(i16 noundef zeroext) local_unnamed_addr #2

declare void @VDP_setTextPriority(i16 noundef zeroext) local_unnamed_addr #2

attributes #0 = { 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 #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 = { "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}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,MemCpyOptPass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass