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

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

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

; 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 void @VDP_drawTextBG(i32 noundef, ptr noundef, i16 noundef zeroext, i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
define dso_local void @VDP_drawTextBGFill(i32 noundef %0, ptr noundef %1, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i16, align 2
  %9 = alloca i16, align 2
  %10 = alloca i16, align 2
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca i16, align 2
  %16 = alloca i16, align 2
  store i32 %0, ptr %6, align 4, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !9
  store i16 %2, ptr %8, align 2, !tbaa !11
  store i16 %3, ptr %9, align 2, !tbaa !11
  store i16 %4, ptr %10, align 2, !tbaa !11
  %17 = load i16, ptr %10, align 2, !tbaa !11
  %18 = zext i16 %17 to i32
  %19 = add nsw i32 %18, 1
  %20 = zext i32 %19 to i64
  %21 = call ptr @llvm.stacksave.p0()
  store ptr %21, ptr %11, align 8
  %22 = alloca i8, i64 %20, align 16
  store i64 %20, ptr %12, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  store ptr %22, ptr %13, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  %23 = load ptr, ptr %7, align 8, !tbaa !9
  store ptr %23, ptr %14, align 8, !tbaa !9
  br label %24

24:                                               ; preds = %31, %5
  %25 = load ptr, ptr %14, align 8, !tbaa !9
  %26 = getelementptr inbounds i8, ptr %25, i32 1
  store ptr %26, ptr %14, align 8, !tbaa !9
  %27 = load i8, ptr %25, align 1, !tbaa !13
  %28 = load ptr, ptr %13, align 8, !tbaa !9
  %29 = getelementptr inbounds i8, ptr %28, i32 1
  store ptr %29, ptr %13, align 8, !tbaa !9
  store i8 %27, ptr %28, align 1, !tbaa !13
  %30 = icmp ne i8 %27, 0
  br i1 %30, label %31, label %32

31:                                               ; preds = %24
  br label %24, !llvm.loop !14

32:                                               ; preds = %24
  %33 = load ptr, ptr %13, align 8, !tbaa !9
  %34 = getelementptr inbounds i8, ptr %33, i32 -1
  store ptr %34, ptr %13, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %15) #3
  %35 = load ptr, ptr %13, align 8, !tbaa !9
  %36 = ptrtoint ptr %35 to i64
  %37 = ptrtoint ptr %22 to i64
  %38 = sub i64 %36, %37
  %39 = trunc i64 %38 to i16
  store i16 %39, ptr %15, align 2, !tbaa !11
  %40 = load i16, ptr %15, align 2, !tbaa !11
  %41 = zext i16 %40 to i32
  %42 = load i16, ptr %10, align 2, !tbaa !11
  %43 = zext i16 %42 to i32
  %44 = icmp slt i32 %41, %43
  br i1 %44, label %45, label %60

45:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 2, ptr %16) #3
  %46 = load i16, ptr %10, align 2, !tbaa !11
  %47 = zext i16 %46 to i32
  %48 = load i16, ptr %15, align 2, !tbaa !11
  %49 = zext i16 %48 to i32
  %50 = sub nsw i32 %47, %49
  %51 = trunc i32 %50 to i16
  store i16 %51, ptr %16, align 2, !tbaa !11
  br label %52

52:                                               ; preds = %56, %45
  %53 = load i16, ptr %16, align 2, !tbaa !11
  %54 = add i16 %53, -1
  store i16 %54, ptr %16, align 2, !tbaa !11
  %55 = icmp ne i16 %53, 0
  br i1 %55, label %56, label %59

56:                                               ; preds = %52
  %57 = load ptr, ptr %13, align 8, !tbaa !9
  %58 = getelementptr inbounds i8, ptr %57, i32 1
  store ptr %58, ptr %13, align 8, !tbaa !9
  store i8 32, ptr %57, align 1, !tbaa !13
  br label %52, !llvm.loop !16

59:                                               ; preds = %52
  call void @llvm.lifetime.end.p0(i64 2, ptr %16) #3
  br label %60

60:                                               ; preds = %59, %32
  %61 = load ptr, ptr %13, align 8, !tbaa !9
  store i8 0, ptr %61, align 1, !tbaa !13
  %62 = load i32, ptr %6, align 4, !tbaa !5
  %63 = load i16, ptr %8, align 2, !tbaa !11
  %64 = load i16, ptr %9, align 2, !tbaa !11
  call void @VDP_drawTextBG(i32 noundef %62, ptr noundef %22, i16 noundef zeroext %63, i16 noundef zeroext %64)
  call void @llvm.lifetime.end.p0(i64 2, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  %65 = load ptr, ptr %11, align 8
  call void @llvm.stackrestore.p0(ptr %65)
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

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 = { nocallback nofree nosync nounwind willreturn }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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

; Function Attrs: nounwind uwtable
declare dso_local void @VDP_drawTextBG(i32 noundef, ptr noundef, i16 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @VDP_drawTextBGFill(i32 noundef %0, ptr nocapture noundef readonly %1, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4) local_unnamed_addr #0 {
  %6 = zext i16 %4 to i32
  %7 = add nuw nsw i32 %6, 1
  %8 = zext nneg i32 %7 to i64
  %9 = alloca i8, i64 %8, align 16
  %scevgep = getelementptr i8, ptr %9, i64 1
  br label %10

10:                                               ; preds = %10, %5
  %indvars.iv26.in = phi i16 [ %indvars.iv26, %10 ], [ %4, %5 ]
  %indvars.iv = phi ptr [ %scevgep25, %10 ], [ %scevgep, %5 ]
  %.017 = phi ptr [ %11, %10 ], [ %1, %5 ]
  %.016 = phi ptr [ %13, %10 ], [ %9, %5 ]
  %indvars.iv26 = add i16 %indvars.iv26.in, -1
  %11 = getelementptr inbounds i8, ptr %.017, i64 1
  %12 = load i8, ptr %.017, align 1, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %.016, i64 1
  store i8 %12, ptr %.016, align 1, !tbaa !5
  %.not = icmp eq i8 %12, 0
  %scevgep25 = getelementptr i8, ptr %indvars.iv, i64 1
  br i1 %.not, label %14, label %10, !llvm.loop !8

14:                                               ; preds = %10
  %15 = ptrtoint ptr %.016 to i64
  %16 = ptrtoint ptr %9 to i64
  %17 = sub i64 %15, %16
  %18 = trunc i64 %17 to i32
  %19 = and i32 %18, 65535
  %20 = icmp uge i32 %19, %6
  %21 = trunc i64 %17 to i16
  %.not2021 = icmp eq i16 %21, %4
  %or.cond = or i1 %20, %.not2021
  br i1 %or.cond, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %14
  %22 = sub i16 %4, %21
  %23 = zext i16 %22 to i64
  call void @llvm.memset.p0.i64(ptr nonnull align 1 %.016, i8 32, i64 %23, i1 false), !tbaa !5
  %24 = zext i16 %indvars.iv26 to i64
  %scevgep27 = getelementptr i8, ptr %indvars.iv, i64 %24
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph.preheader, %14
  %.2 = phi ptr [ %.016, %14 ], [ %scevgep27, %.lr.ph.preheader ]
  store i8 0, ptr %.2, align 1, !tbaa !5
  call void @VDP_drawTextBG(i32 noundef %0, ptr noundef nonnull %9, i16 noundef zeroext %2, i16 noundef zeroext %3)
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!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 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass