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

@windowWidth = external global i16, align 2
@planeWidth = external global i16, align 2
@planeHeight = external global i16, align 2
@maps_addr = external global i16, align 2

; 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
define dso_local void @VDP_drawTextEx(i32 noundef %0, ptr noundef %1, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4, i32 noundef %5) #1 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i16, align 2
  %10 = alloca i16, align 2
  %11 = alloca i16, align 2
  %12 = alloca i32, align 4
  %13 = alloca [128 x i16], align 16
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca i16, align 2
  %17 = alloca i16, align 2
  %18 = alloca i16, align 2
  %19 = alloca i16, align 2
  %20 = alloca i32, align 4
  store i32 %0, ptr %7, align 4, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !9
  store i16 %2, ptr %9, align 2, !tbaa !11
  store i16 %3, ptr %10, align 2, !tbaa !11
  store i16 %4, ptr %11, align 2, !tbaa !11
  store i32 %5, ptr %12, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 256, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %16) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %17) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %18) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %19) #3
  %21 = load i32, ptr %7, align 4, !tbaa !5
  %22 = icmp eq i32 %21, 2
  br i1 %22, label %23, label %26

23:                                               ; preds = %6
  %24 = load i16, ptr @windowWidth, align 2, !tbaa !11
  %25 = zext i16 %24 to i32
  br label %29

26:                                               ; preds = %6
  %27 = load i16, ptr @planeWidth, align 2, !tbaa !11
  %28 = zext i16 %27 to i32
  br label %29

29:                                               ; preds = %26, %23
  %30 = phi i32 [ %25, %23 ], [ %28, %26 ]
  %31 = trunc i32 %30 to i16
  store i16 %31, ptr %17, align 2, !tbaa !11
  %32 = load i32, ptr %7, align 4, !tbaa !5
  %33 = icmp eq i32 %32, 2
  br i1 %33, label %34, label %35

34:                                               ; preds = %29
  br label %38

35:                                               ; preds = %29
  %36 = load i16, ptr @planeHeight, align 2, !tbaa !11
  %37 = zext i16 %36 to i32
  br label %38

38:                                               ; preds = %35, %34
  %39 = phi i32 [ 32, %34 ], [ %37, %35 ]
  %40 = trunc i32 %39 to i16
  store i16 %40, ptr %18, align 2, !tbaa !11
  %41 = load i16, ptr %10, align 2, !tbaa !11
  %42 = zext i16 %41 to i32
  %43 = load i16, ptr %17, align 2, !tbaa !11
  %44 = zext i16 %43 to i32
  %45 = icmp sge i32 %42, %44
  br i1 %45, label %52, label %46

46:                                               ; preds = %38
  %47 = load i16, ptr %11, align 2, !tbaa !11
  %48 = zext i16 %47 to i32
  %49 = load i16, ptr %18, align 2, !tbaa !11
  %50 = zext i16 %49 to i32
  %51 = icmp sge i32 %48, %50
  br i1 %51, label %52, label %53

52:                                               ; preds = %46, %38
  store i32 1, ptr %20, align 4
  br label %101

53:                                               ; preds = %46
  %54 = load ptr, ptr %8, align 8, !tbaa !9
  %55 = call zeroext i16 @strlen(ptr noundef %54)
  store i16 %55, ptr %19, align 2, !tbaa !11
  %56 = load i16, ptr %19, align 2, !tbaa !11
  %57 = zext i16 %56 to i32
  %58 = load i16, ptr %17, align 2, !tbaa !11
  %59 = zext i16 %58 to i32
  %60 = load i16, ptr %10, align 2, !tbaa !11
  %61 = zext i16 %60 to i32
  %62 = sub nsw i32 %59, %61
  %63 = icmp sgt i32 %57, %62
  br i1 %63, label %64, label %71

64:                                               ; preds = %53
  %65 = load i16, ptr %17, align 2, !tbaa !11
  %66 = zext i16 %65 to i32
  %67 = load i16, ptr %10, align 2, !tbaa !11
  %68 = zext i16 %67 to i32
  %69 = sub nsw i32 %66, %68
  %70 = trunc i32 %69 to i16
  store i16 %70, ptr %19, align 2, !tbaa !11
  br label %71

71:                                               ; preds = %64, %53
  %72 = load ptr, ptr %8, align 8, !tbaa !9
  store ptr %72, ptr %14, align 8, !tbaa !9
  %73 = getelementptr inbounds [128 x i16], ptr %13, i64 0, i64 0
  store ptr %73, ptr %15, align 8, !tbaa !9
  %74 = load i16, ptr %19, align 2, !tbaa !11
  store i16 %74, ptr %16, align 2, !tbaa !11
  br label %75

75:                                               ; preds = %79, %71
  %76 = load i16, ptr %16, align 2, !tbaa !11
  %77 = add i16 %76, -1
  store i16 %77, ptr %16, align 2, !tbaa !11
  %78 = icmp ne i16 %76, 0
  br i1 %78, label %79, label %93

79:                                               ; preds = %75
  %80 = load i16, ptr @maps_addr, align 2, !tbaa !11
  %81 = zext i16 %80 to i32
  %82 = sdiv i32 %81, 32
  %83 = sub nsw i32 %82, 96
  %84 = load ptr, ptr %14, align 8, !tbaa !9
  %85 = getelementptr inbounds i8, ptr %84, i32 1
  store ptr %85, ptr %14, align 8, !tbaa !9
  %86 = load i8, ptr %84, align 1, !tbaa !13
  %87 = zext i8 %86 to i32
  %88 = sub nsw i32 %87, 32
  %89 = add nsw i32 %83, %88
  %90 = trunc i32 %89 to i16
  %91 = load ptr, ptr %15, align 8, !tbaa !9
  %92 = getelementptr inbounds i16, ptr %91, i32 1
  store ptr %92, ptr %15, align 8, !tbaa !9
  store i16 %90, ptr %91, align 2, !tbaa !11
  br label %75, !llvm.loop !14

93:                                               ; preds = %75
  %94 = load i32, ptr %7, align 4, !tbaa !5
  %95 = getelementptr inbounds [128 x i16], ptr %13, i64 0, i64 0
  %96 = load i16, ptr %9, align 2, !tbaa !11
  %97 = load i16, ptr %11, align 2, !tbaa !11
  %98 = load i16, ptr %10, align 2, !tbaa !11
  %99 = load i16, ptr %19, align 2, !tbaa !11
  %100 = load i32, ptr %12, align 4, !tbaa !5
  call void @VDP_setTileMapDataRowEx(i32 noundef %94, ptr noundef %95, i16 noundef zeroext %96, i16 noundef zeroext %97, i16 noundef zeroext %98, i16 noundef zeroext %99, i32 noundef %100)
  store i32 0, ptr %20, align 4
  br label %101

101:                                              ; preds = %93, %52
  call void @llvm.lifetime.end.p0(i64 2, ptr %19) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %18) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %17) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %16) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 256, ptr %13) #3
  %102 = load i32, ptr %20, align 4
  switch i32 %102, label %104 [
    i32 0, label %103
    i32 1, label %103
  ]

103:                                              ; preds = %101, %101
  ret void

104:                                              ; preds = %101
  unreachable
}

declare zeroext i16 @strlen(ptr noundef) #2

declare void @VDP_setTileMapDataRowEx(i32 noundef, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i32 noundef) #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 = { "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 = !{!"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"}


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

@windowWidth = external local_unnamed_addr global i16, align 2
@planeWidth = external local_unnamed_addr global i16, align 2
@planeHeight = external local_unnamed_addr global i16, align 2
@maps_addr = external local_unnamed_addr global i16, align 2

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

; 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
define dso_local void @VDP_drawTextEx(i32 noundef %0, ptr noundef %1, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4, i32 noundef %5) local_unnamed_addr #1 {
  %7 = alloca [128 x i16], align 16
  call void @llvm.lifetime.start.p0(i64 256, ptr nonnull %7) #3
  %8 = icmp eq i32 %0, 2
  %9 = load i16, ptr @windowWidth, align 2
  %10 = load i16, ptr @planeWidth, align 2
  %.v = select i1 %8, i16 %9, i16 %10
  %11 = zext i16 %3 to i32
  %12 = zext i16 %.v to i32
  %.not = icmp ugt i16 %.v, %3
  br i1 %.not, label %13, label %42

13:                                               ; preds = %6
  %14 = load i16, ptr @planeHeight, align 2
  %15 = zext i16 %14 to i32
  %16 = zext i16 %4 to i32
  %17 = select i1 %8, i32 32, i32 %15
  %.not28 = icmp ugt i32 %17, %16
  br i1 %.not28, label %18, label %42

18:                                               ; preds = %13
  %19 = tail call zeroext i16 @strlen(ptr noundef %1) #3
  %20 = zext i16 %19 to i32
  %21 = sub nsw i32 %12, %11
  %22 = icmp slt i32 %21, %20
  %23 = trunc i32 %21 to i16
  %spec.select = select i1 %22, i16 %23, i16 %19
  %.not2930 = icmp eq i16 %spec.select, 0
  br i1 %.not2930, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %18
  %24 = load i16, ptr @maps_addr, align 2, !tbaa !5
  %25 = lshr i16 %24, 5
  %26 = add nsw i16 %25, -128
  %27 = zext i16 %spec.select to i64
  %min.iters.check = icmp ult i16 %spec.select, 16
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.vec = and i64 %27, 65520
  %ind.end = getelementptr i8, ptr %1, i64 %n.vec
  %28 = shl nuw nsw i64 %n.vec, 1
  %ind.end34 = getelementptr i8, ptr %7, i64 %28
  %.cast = trunc nuw i64 %n.vec to i16
  %ind.end36 = sub i16 %spec.select, %.cast
  %broadcast.splatinsert = insertelement <8 x i16> poison, i16 %26, i64 0
  %broadcast.splat = shufflevector <8 x i16> %broadcast.splatinsert, <8 x i16> poison, <8 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %next.gep = getelementptr i8, ptr %1, i64 %index
  %offset.idx = shl i64 %index, 1
  %next.gep39 = getelementptr i8, ptr %7, i64 %offset.idx
  %29 = getelementptr i8, ptr %next.gep, i64 8
  %wide.load = load <8 x i8>, ptr %next.gep, align 1, !tbaa !9
  %wide.load41 = load <8 x i8>, ptr %29, align 1, !tbaa !9
  %30 = zext <8 x i8> %wide.load to <8 x i16>
  %31 = zext <8 x i8> %wide.load41 to <8 x i16>
  %32 = add nsw <8 x i16> %broadcast.splat, %30
  %33 = add nsw <8 x i16> %broadcast.splat, %31
  %34 = getelementptr i8, ptr %next.gep39, i64 16
  store <8 x i16> %32, ptr %next.gep39, align 16, !tbaa !5
  store <8 x i16> %33, ptr %34, align 16, !tbaa !5
  %index.next = add nuw i64 %index, 16
  %35 = icmp eq i64 %index.next, %n.vec
  br i1 %35, label %middle.block, label %vector.body, !llvm.loop !10

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %27
  br i1 %cmp.n, label %._crit_edge, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %.02233.ph = phi ptr [ %1, %.lr.ph ], [ %ind.end, %middle.block ]
  %.02332.ph = phi ptr [ %7, %.lr.ph ], [ %ind.end34, %middle.block ]
  %.02431.ph = phi i16 [ %spec.select, %.lr.ph ], [ %ind.end36, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %.02233 = phi ptr [ %37, %scalar.ph ], [ %.02233.ph, %scalar.ph.preheader ]
  %.02332 = phi ptr [ %41, %scalar.ph ], [ %.02332.ph, %scalar.ph.preheader ]
  %.02431 = phi i16 [ %36, %scalar.ph ], [ %.02431.ph, %scalar.ph.preheader ]
  %36 = add i16 %.02431, -1
  %37 = getelementptr inbounds i8, ptr %.02233, i64 1
  %38 = load i8, ptr %.02233, align 1, !tbaa !9
  %39 = zext i8 %38 to i16
  %40 = add nsw i16 %26, %39
  %41 = getelementptr inbounds i8, ptr %.02332, i64 2
  store i16 %40, ptr %.02332, align 2, !tbaa !5
  %.not29 = icmp eq i16 %36, 0
  br i1 %.not29, label %._crit_edge, label %scalar.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %scalar.ph, %middle.block, %18
  call void @VDP_setTileMapDataRowEx(i32 noundef %0, ptr noundef nonnull %7, i16 noundef zeroext %2, i16 noundef zeroext %4, i16 noundef zeroext %3, i16 noundef zeroext %spec.select, i32 noundef %5) #3
  br label %42

42:                                               ; preds = %6, %13, %._crit_edge
  call void @llvm.lifetime.end.p0(i64 256, ptr nonnull %7) #3
  ret void
}

declare zeroext i16 @strlen(ptr noundef) local_unnamed_addr #2

declare void @VDP_setTileMapDataRowEx(i32 noundef, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i32 noundef) local_unnamed_addr #2

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 = { "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 = distinct !{!10, !11, !12, !13}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!"llvm.loop.isvectorized", i32 1}
!13 = !{!"llvm.loop.unroll.runtime.disable"}
!14 = distinct !{!14, !11, !13, !12}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass