; 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

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

declare void @memsetU16(ptr noundef, i16 noundef zeroext, i16 noundef zeroext) #1

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

25:                                               ; preds = %7
  %26 = load i16, ptr @windowWidth, align 2, !tbaa !9
  %27 = zext i16 %26 to i32
  br label %31

28:                                               ; preds = %7
  %29 = load i16, ptr @planeWidth, align 2, !tbaa !9
  %30 = zext i16 %29 to i32
  br label %31

31:                                               ; preds = %28, %25
  %32 = phi i32 [ %27, %25 ], [ %30, %28 ]
  %33 = trunc i32 %32 to i16
  store i16 %33, ptr %18, align 2, !tbaa !9
  %34 = load i32, ptr %8, align 4, !tbaa !5
  %35 = icmp eq i32 %34, 2
  br i1 %35, label %36, label %37

36:                                               ; preds = %31
  br label %40

37:                                               ; preds = %31
  %38 = load i16, ptr @planeHeight, align 2, !tbaa !9
  %39 = zext i16 %38 to i32
  br label %40

40:                                               ; preds = %37, %36
  %41 = phi i32 [ 32, %36 ], [ %39, %37 ]
  %42 = trunc i32 %41 to i16
  store i16 %42, ptr %19, align 2, !tbaa !9
  %43 = load i16, ptr %10, align 2, !tbaa !9
  %44 = zext i16 %43 to i32
  %45 = load i16, ptr %18, align 2, !tbaa !9
  %46 = zext i16 %45 to i32
  %47 = icmp sge i32 %44, %46
  br i1 %47, label %54, label %48

48:                                               ; preds = %40
  %49 = load i16, ptr %11, align 2, !tbaa !9
  %50 = zext i16 %49 to i32
  %51 = load i16, ptr %19, align 2, !tbaa !9
  %52 = zext i16 %51 to i32
  %53 = icmp sge i32 %50, %52
  br i1 %53, label %54, label %55

54:                                               ; preds = %48, %40
  store i32 1, ptr %22, align 4
  br label %113

55:                                               ; preds = %48
  %56 = load i16, ptr %12, align 2, !tbaa !9
  store i16 %56, ptr %20, align 2, !tbaa !9
  %57 = load i16, ptr %20, align 2, !tbaa !9
  %58 = zext i16 %57 to i32
  %59 = load i16, ptr %18, align 2, !tbaa !9
  %60 = zext i16 %59 to i32
  %61 = load i16, ptr %10, align 2, !tbaa !9
  %62 = zext i16 %61 to i32
  %63 = sub nsw i32 %60, %62
  %64 = icmp sgt i32 %58, %63
  br i1 %64, label %65, label %72

65:                                               ; preds = %55
  %66 = load i16, ptr %18, align 2, !tbaa !9
  %67 = zext i16 %66 to i32
  %68 = load i16, ptr %10, align 2, !tbaa !9
  %69 = zext i16 %68 to i32
  %70 = sub nsw i32 %67, %69
  %71 = trunc i32 %70 to i16
  store i16 %71, ptr %20, align 2, !tbaa !9
  br label %72

72:                                               ; preds = %65, %55
  %73 = load i16, ptr %13, align 2, !tbaa !9
  store i16 %73, ptr %21, align 2, !tbaa !9
  %74 = load i16, ptr %21, align 2, !tbaa !9
  %75 = zext i16 %74 to i32
  %76 = load i16, ptr %19, align 2, !tbaa !9
  %77 = zext i16 %76 to i32
  %78 = load i16, ptr %11, align 2, !tbaa !9
  %79 = zext i16 %78 to i32
  %80 = sub nsw i32 %77, %79
  %81 = icmp sgt i32 %75, %80
  br i1 %81, label %82, label %89

82:                                               ; preds = %72
  %83 = load i16, ptr %19, align 2, !tbaa !9
  %84 = zext i16 %83 to i32
  %85 = load i16, ptr %11, align 2, !tbaa !9
  %86 = zext i16 %85 to i32
  %87 = sub nsw i32 %84, %86
  %88 = trunc i32 %87 to i16
  store i16 %88, ptr %21, align 2, !tbaa !9
  br label %89

89:                                               ; preds = %82, %72
  %90 = getelementptr inbounds [128 x i16], ptr %15, i64 0, i64 0
  %91 = load i16, ptr @maps_addr, align 2, !tbaa !9
  %92 = zext i16 %91 to i32
  %93 = sdiv i32 %92, 32
  %94 = sub nsw i32 %93, 96
  %95 = trunc i32 %94 to i16
  %96 = load i16, ptr %20, align 2, !tbaa !9
  call void @memsetU16(ptr noundef %90, i16 noundef zeroext %95, i16 noundef zeroext %96)
  %97 = load i16, ptr %11, align 2, !tbaa !9
  store i16 %97, ptr %17, align 2, !tbaa !9
  %98 = load i16, ptr %21, align 2, !tbaa !9
  store i16 %98, ptr %16, align 2, !tbaa !9
  br label %99

99:                                               ; preds = %103, %89
  %100 = load i16, ptr %16, align 2, !tbaa !9
  %101 = add i16 %100, -1
  store i16 %101, ptr %16, align 2, !tbaa !9
  %102 = icmp ne i16 %100, 0
  br i1 %102, label %103, label %112

103:                                              ; preds = %99
  %104 = load i32, ptr %8, align 4, !tbaa !5
  %105 = getelementptr inbounds [128 x i16], ptr %15, i64 0, i64 0
  %106 = load i16, ptr %9, align 2, !tbaa !9
  %107 = load i16, ptr %17, align 2, !tbaa !9
  %108 = add i16 %107, 1
  store i16 %108, ptr %17, align 2, !tbaa !9
  %109 = load i16, ptr %10, align 2, !tbaa !9
  %110 = load i16, ptr %20, align 2, !tbaa !9
  %111 = load i32, ptr %14, align 4, !tbaa !5
  call void @VDP_setTileMapDataRowEx(i32 noundef %104, ptr noundef %105, i16 noundef zeroext %106, i16 noundef zeroext %107, i16 noundef zeroext %109, i16 noundef zeroext %110, i32 noundef %111)
  br label %99, !llvm.loop !11

112:                                              ; preds = %99
  store i32 0, ptr %22, align 4
  br label %113

113:                                              ; preds = %112, %54
  call void @llvm.lifetime.end.p0(i64 2, ptr %21) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %20) #3
  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 256, ptr %15) #3
  %114 = load i32, ptr %22, align 4
  switch i32 %114, label %116 [
    i32 0, label %115
    i32 1, label %115
  ]

115:                                              ; preds = %113, %113
  ret void

116:                                              ; preds = %113
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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

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 #1

declare void @memsetU16(ptr noundef, i16 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @VDP_clearTextAreaEx(i32 noundef %0, i16 noundef zeroext %1, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4, i16 noundef zeroext %5, i32 noundef %6) local_unnamed_addr #2 {
  %8 = alloca [128 x i16], align 16
  call void @llvm.lifetime.start.p0(i64 256, ptr nonnull %8) #3
  %9 = icmp eq i32 %0, 2
  %10 = load i16, ptr @windowWidth, align 2
  %11 = load i16, ptr @planeWidth, align 2
  %.v = select i1 %9, i16 %10, i16 %11
  %12 = zext i16 %2 to i32
  %13 = zext i16 %.v to i32
  %.not = icmp ugt i16 %.v, %2
  br i1 %.not, label %14, label %.loopexit

14:                                               ; preds = %7
  %15 = load i16, ptr @planeHeight, align 2
  %16 = zext i16 %15 to i32
  %17 = zext i16 %3 to i32
  %18 = select i1 %9, i32 32, i32 %16
  %.not37 = icmp ugt i32 %18, %17
  br i1 %.not37, label %19, label %.loopexit

19:                                               ; preds = %14
  %20 = zext i16 %4 to i32
  %21 = sub nsw i32 %13, %12
  %22 = icmp slt i32 %21, %20
  %23 = trunc i32 %21 to i16
  %spec.select = select i1 %22, i16 %23, i16 %4
  %24 = zext i16 %5 to i32
  %25 = sub nsw i32 %18, %17
  %26 = icmp slt i32 %25, %24
  %27 = trunc i32 %25 to i16
  %.0 = select i1 %26, i16 %27, i16 %5
  %28 = load i16, ptr @maps_addr, align 2, !tbaa !5
  %29 = lshr i16 %28, 5
  %30 = add nsw i16 %29, -96
  call void @memsetU16(ptr noundef nonnull %8, i16 noundef zeroext %30, i16 noundef zeroext %spec.select) #3
  %.not3839 = icmp eq i16 %.0, 0
  br i1 %.not3839, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %19, %.lr.ph
  %.02941 = phi i16 [ %31, %.lr.ph ], [ %.0, %19 ]
  %.03040 = phi i16 [ %32, %.lr.ph ], [ %3, %19 ]
  %31 = add i16 %.02941, -1
  %32 = add i16 %.03040, 1
  call void @VDP_setTileMapDataRowEx(i32 noundef %0, ptr noundef nonnull %8, i16 noundef zeroext %1, i16 noundef zeroext %.03040, i16 noundef zeroext %2, i16 noundef zeroext %spec.select, i32 noundef %6) #3
  %.not38 = icmp eq i16 %31, 0
  br i1 %.not38, label %.loopexit, label %.lr.ph, !llvm.loop !9

.loopexit:                                        ; preds = %.lr.ph, %19, %7, %14
  call void @llvm.lifetime.end.p0(i64 256, ptr nonnull %8) #3
  ret void
}

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass