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

%struct.TileSet = type { i16, i16, ptr }

@palette_grey = external constant [16 x i16], align 16
@palette_red = external constant [16 x i16], align 16
@palette_green = external constant [16 x i16], align 16
@palette_blue = external constant [16 x i16], align 16
@font_default = external constant %struct.TileSet, align 8
@.str = external hidden unnamed_addr constant [57 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [33 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [24 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [19 x i8], align 1

; 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 dso_local void @VDP_waitDMACompletion() #1

; Function Attrs: noinline nounwind uwtable
define dso_local void @VDP_resetScreen() #2 {
  %1 = alloca i16, align 2
  %2 = alloca i8, align 1
  %3 = alloca [20 x i16], align 16
  call void @llvm.lifetime.start.p0(i64 2, ptr %1) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr %2) #4
  %4 = call zeroext i8 @VDP_isEnable()
  store i8 %4, ptr %2, align 1, !tbaa !5
  call void @VDP_setEnable(i8 noundef zeroext 0)
  call void @DMA_doVRamFill(i16 noundef zeroext 0, i16 noundef zeroext 0, i8 noundef zeroext 0, i16 noundef signext 1)
  call void @VDP_waitDMACompletion()
  store i16 16, ptr %1, align 2, !tbaa !8
  br label %5

5:                                                ; preds = %9, %0
  %6 = load i16, ptr %1, align 2, !tbaa !8
  %7 = add i16 %6, -1
  store i16 %7, ptr %1, align 2, !tbaa !8
  %8 = icmp ne i16 %6, 0
  br i1 %8, label %9, label %21

9:                                                ; preds = %5
  %10 = load i16, ptr %1, align 2, !tbaa !8
  %11 = zext i16 %10 to i32
  %12 = load i16, ptr %1, align 2, !tbaa !8
  %13 = zext i16 %12 to i32
  %14 = shl i32 %13, 4
  %15 = or i32 %11, %14
  %16 = trunc i32 %15 to i8
  %17 = load i16, ptr %1, align 2, !tbaa !8
  %18 = zext i16 %17 to i32
  %19 = add nsw i32 0, %18
  %20 = trunc i32 %19 to i16
  call void @VDP_fillTileData(i8 noundef zeroext %16, i16 noundef zeroext %20, i16 noundef zeroext 1, i8 noundef zeroext 1)
  br label %5, !llvm.loop !10

21:                                               ; preds = %5
  call void @PAL_setPalette(i16 noundef zeroext 0, ptr noundef @palette_grey, i32 noundef 0)
  call void @PAL_setPalette(i16 noundef zeroext 1, ptr noundef @palette_red, i32 noundef 0)
  call void @PAL_setPalette(i16 noundef zeroext 2, ptr noundef @palette_green, i32 noundef 0)
  call void @PAL_setPalette(i16 noundef zeroext 3, ptr noundef @palette_blue, i32 noundef 0)
  call void @VDP_setScrollingMode(i16 noundef zeroext 0, i16 noundef zeroext 0)
  call void @VDP_setHorizontalScroll(i32 noundef 0, i16 noundef signext 0)
  call void @VDP_setHorizontalScroll(i32 noundef 1, i16 noundef signext 0)
  call void @llvm.lifetime.start.p0(i64 40, ptr %3) #4
  %22 = getelementptr inbounds [20 x i16], ptr %3, i64 0, i64 0
  call void @memset(ptr noundef %22, i8 noundef zeroext 0, i16 noundef zeroext 40)
  %23 = getelementptr inbounds [20 x i16], ptr %3, i64 0, i64 0
  call void @VDP_setVerticalScrollTile(i32 noundef 0, i16 noundef zeroext 0, ptr noundef %23, i16 noundef zeroext 20, i32 noundef 0)
  %24 = getelementptr inbounds [20 x i16], ptr %3, i64 0, i64 0
  call void @VDP_setVerticalScrollTile(i32 noundef 1, i16 noundef zeroext 0, ptr noundef %24, i16 noundef zeroext 20, i32 noundef 0)
  call void @llvm.lifetime.end.p0(i64 40, ptr %3) #4
  %25 = call zeroext i16 @VDP_loadFont(ptr noundef @font_default, i32 noundef 1)
  %26 = icmp ne i16 %25, 0
  br i1 %26, label %30, label %27

27:                                               ; preds = %21
  call void @VDP_setEnable(i8 noundef zeroext 1)
  call void @KLog(ptr noundef @.str)
  call void @VDP_drawText(ptr noundef @.str.1, i16 noundef zeroext 0, i16 noundef zeroext 2)
  call void @VDP_drawText(ptr noundef @.str.2, i16 noundef zeroext 2, i16 noundef zeroext 4)
  call void @VDP_drawText(ptr noundef @.str.3, i16 noundef zeroext 4, i16 noundef zeroext 5)
  br label %28

28:                                               ; preds = %29, %27
  br label %29

29:                                               ; preds = %28
  br label %28

30:                                               ; preds = %21
  %31 = load i8, ptr %2, align 1, !tbaa !5
  %32 = icmp ne i8 %31, 0
  br i1 %32, label %33, label %34

33:                                               ; preds = %30
  call void @VDP_setEnable(i8 noundef zeroext 1)
  br label %34

34:                                               ; preds = %33, %30
  call void @llvm.lifetime.end.p0(i64 1, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %1) #4
  ret void
}

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @VDP_isEnable() #1

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

declare void @DMA_doVRamFill(i16 noundef zeroext, i16 noundef zeroext, i8 noundef zeroext, i16 noundef signext) #3

declare void @VDP_fillTileData(i8 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i8 noundef zeroext) #3

declare void @PAL_setPalette(i16 noundef zeroext, ptr noundef, i32 noundef) #3

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

declare void @VDP_setHorizontalScroll(i32 noundef, i16 noundef signext) #3

declare void @memset(ptr noundef, i8 noundef zeroext, i16 noundef zeroext) #3

declare void @VDP_setVerticalScrollTile(i32 noundef, i16 noundef zeroext, ptr noundef, i16 noundef zeroext, i32 noundef) #3

declare zeroext i16 @VDP_loadFont(ptr noundef, i32 noundef) #3

declare void @KLog(ptr noundef) #3

declare void @VDP_drawText(ptr noundef, i16 noundef zeroext, i16 noundef zeroext) #3

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 = { "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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"short", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"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"

%struct.TileSet = type { i16, i16, ptr }

@palette_grey = external constant [16 x i16], align 16
@palette_red = external constant [16 x i16], align 16
@palette_green = external constant [16 x i16], align 16
@palette_blue = external constant [16 x i16], align 16
@font_default = external constant %struct.TileSet, align 8
@.str = external hidden unnamed_addr constant [57 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [33 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [24 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [19 x i8], align 1

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

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

; Function Attrs: noinline nounwind uwtable
define dso_local void @VDP_resetScreen() local_unnamed_addr #2 {
  %1 = alloca [20 x i16], align 16
  %2 = tail call zeroext i8 @VDP_isEnable()
  tail call void @VDP_setEnable(i8 noundef zeroext 0)
  tail call void @DMA_doVRamFill(i16 noundef zeroext 0, i16 noundef zeroext 0, i8 noundef zeroext 0, i16 noundef signext 1) #4
  tail call void @VDP_waitDMACompletion()
  tail call void @VDP_fillTileData(i8 noundef zeroext -1, i16 noundef zeroext 15, i16 noundef zeroext 1, i8 noundef zeroext 1) #4
  tail call void @VDP_fillTileData(i8 noundef zeroext -18, i16 noundef zeroext 14, i16 noundef zeroext 1, i8 noundef zeroext 1) #4
  tail call void @VDP_fillTileData(i8 noundef zeroext -35, i16 noundef zeroext 13, i16 noundef zeroext 1, i8 noundef zeroext 1) #4
  tail call void @VDP_fillTileData(i8 noundef zeroext -52, i16 noundef zeroext 12, i16 noundef zeroext 1, i8 noundef zeroext 1) #4
  tail call void @VDP_fillTileData(i8 noundef zeroext -69, i16 noundef zeroext 11, i16 noundef zeroext 1, i8 noundef zeroext 1) #4
  tail call void @VDP_fillTileData(i8 noundef zeroext -86, i16 noundef zeroext 10, i16 noundef zeroext 1, i8 noundef zeroext 1) #4
  tail call void @VDP_fillTileData(i8 noundef zeroext -103, i16 noundef zeroext 9, i16 noundef zeroext 1, i8 noundef zeroext 1) #4
  tail call void @VDP_fillTileData(i8 noundef zeroext -120, i16 noundef zeroext 8, i16 noundef zeroext 1, i8 noundef zeroext 1) #4
  tail call void @VDP_fillTileData(i8 noundef zeroext 119, i16 noundef zeroext 7, i16 noundef zeroext 1, i8 noundef zeroext 1) #4
  tail call void @VDP_fillTileData(i8 noundef zeroext 102, i16 noundef zeroext 6, i16 noundef zeroext 1, i8 noundef zeroext 1) #4
  tail call void @VDP_fillTileData(i8 noundef zeroext 85, i16 noundef zeroext 5, i16 noundef zeroext 1, i8 noundef zeroext 1) #4
  tail call void @VDP_fillTileData(i8 noundef zeroext 68, i16 noundef zeroext 4, i16 noundef zeroext 1, i8 noundef zeroext 1) #4
  tail call void @VDP_fillTileData(i8 noundef zeroext 51, i16 noundef zeroext 3, i16 noundef zeroext 1, i8 noundef zeroext 1) #4
  tail call void @VDP_fillTileData(i8 noundef zeroext 34, i16 noundef zeroext 2, i16 noundef zeroext 1, i8 noundef zeroext 1) #4
  tail call void @VDP_fillTileData(i8 noundef zeroext 17, i16 noundef zeroext 1, i16 noundef zeroext 1, i8 noundef zeroext 1) #4
  tail call void @VDP_fillTileData(i8 noundef zeroext 0, i16 noundef zeroext 0, i16 noundef zeroext 1, i8 noundef zeroext 1) #4
  tail call void @PAL_setPalette(i16 noundef zeroext 0, ptr noundef nonnull @palette_grey, i32 noundef 0) #4
  tail call void @PAL_setPalette(i16 noundef zeroext 1, ptr noundef nonnull @palette_red, i32 noundef 0) #4
  tail call void @PAL_setPalette(i16 noundef zeroext 2, ptr noundef nonnull @palette_green, i32 noundef 0) #4
  tail call void @PAL_setPalette(i16 noundef zeroext 3, ptr noundef nonnull @palette_blue, i32 noundef 0) #4
  tail call void @VDP_setScrollingMode(i16 noundef zeroext 0, i16 noundef zeroext 0)
  tail call void @VDP_setHorizontalScroll(i32 noundef 0, i16 noundef signext 0) #4
  tail call void @VDP_setHorizontalScroll(i32 noundef 1, i16 noundef signext 0) #4
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %1) #4
  call void @memset(ptr noundef nonnull %1, i8 noundef zeroext 0, i16 noundef zeroext 40) #4
  call void @VDP_setVerticalScrollTile(i32 noundef 0, i16 noundef zeroext 0, ptr noundef nonnull %1, i16 noundef zeroext 20, i32 noundef 0) #4
  call void @VDP_setVerticalScrollTile(i32 noundef 1, i16 noundef zeroext 0, ptr noundef nonnull %1, i16 noundef zeroext 20, i32 noundef 0) #4
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %1) #4
  %3 = call zeroext i16 @VDP_loadFont(ptr noundef nonnull @font_default, i32 noundef 1) #4
  %.not5 = icmp eq i16 %3, 0
  br i1 %.not5, label %4, label %6

4:                                                ; preds = %0
  call void @VDP_setEnable(i8 noundef zeroext 1)
  call void @KLog(ptr noundef nonnull @.str) #4
  call void @VDP_drawText(ptr noundef nonnull @.str.1, i16 noundef zeroext 0, i16 noundef zeroext 2) #4
  call void @VDP_drawText(ptr noundef nonnull @.str.2, i16 noundef zeroext 2, i16 noundef zeroext 4) #4
  call void @VDP_drawText(ptr noundef nonnull @.str.3, i16 noundef zeroext 4, i16 noundef zeroext 5) #4
  br label %5

5:                                                ; preds = %5, %4
  br label %5

6:                                                ; preds = %0
  %.not6 = icmp eq i8 %2, 0
  br i1 %.not6, label %8, label %7

7:                                                ; preds = %6
  call void @VDP_setEnable(i8 noundef zeroext 1)
  br label %8

8:                                                ; preds = %7, %6
  ret void
}

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @VDP_isEnable() local_unnamed_addr #1

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

declare void @DMA_doVRamFill(i16 noundef zeroext, i16 noundef zeroext, i8 noundef zeroext, i16 noundef signext) local_unnamed_addr #3

declare void @VDP_fillTileData(i8 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i8 noundef zeroext) local_unnamed_addr #3

declare void @PAL_setPalette(i16 noundef zeroext, ptr noundef, i32 noundef) local_unnamed_addr #3

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

declare void @VDP_setHorizontalScroll(i32 noundef, i16 noundef signext) local_unnamed_addr #3

declare void @memset(ptr noundef, i8 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #3

declare void @VDP_setVerticalScrollTile(i32 noundef, i16 noundef zeroext, ptr noundef, i16 noundef zeroext, i32 noundef) local_unnamed_addr #3

declare zeroext i16 @VDP_loadFont(ptr noundef, i32 noundef) local_unnamed_addr #3

declare void @KLog(ptr noundef) local_unnamed_addr #3

declare void @VDP_drawText(ptr noundef, i16 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #3

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


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