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

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

@curTileInd = external dso_local global i16, align 2
@userTileMaxIndex = 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 zeroext i8 @VDP_drawBitmap(i32 noundef %0, ptr noundef %1, i16 noundef zeroext %2, i16 noundef zeroext %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i16, align 2
  %8 = alloca i16, align 2
  %9 = alloca i16, align 2
  %10 = alloca i16, align 2
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i16 %2, ptr %7, align 2, !tbaa !11
  store i16 %3, ptr %8, align 2, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #3
  %11 = load ptr, ptr %6, align 8, !tbaa !9
  %12 = getelementptr inbounds %struct.Bitmap, ptr %11, i32 0, i32 2
  %13 = load i16, ptr %12, align 4, !tbaa !13
  %14 = zext i16 %13 to i32
  %15 = add nsw i32 %14, 7
  %16 = ashr i32 %15, 3
  %17 = trunc i32 %16 to i16
  %18 = load ptr, ptr %6, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.Bitmap, ptr %18, i32 0, i32 1
  %20 = load i16, ptr %19, align 2, !tbaa !15
  %21 = zext i16 %20 to i32
  %22 = add nsw i32 %21, 7
  %23 = ashr i32 %22, 3
  %24 = trunc i32 %23 to i16
  %25 = call i64 @mulu(i16 noundef zeroext %17, i16 noundef zeroext %24)
  %26 = trunc i64 %25 to i16
  store i16 %26, ptr %9, align 2, !tbaa !11
  %27 = load i16, ptr @curTileInd, align 2, !tbaa !11
  %28 = zext i16 %27 to i32
  %29 = load i16, ptr %9, align 2, !tbaa !11
  %30 = zext i16 %29 to i32
  %31 = add nsw i32 %28, %30
  %32 = load i16, ptr @userTileMaxIndex, align 2, !tbaa !11
  %33 = zext i16 %32 to i32
  %34 = icmp sgt i32 %31, %33
  br i1 %34, label %35, label %36

35:                                               ; preds = %4
  store i16 16, ptr @curTileInd, align 2, !tbaa !11
  br label %36

36:                                               ; preds = %35, %4
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = load ptr, ptr %6, align 8, !tbaa !9
  %39 = load i16, ptr @curTileInd, align 2, !tbaa !11
  %40 = zext i16 %39 to i32
  %41 = add nsw i32 0, %40
  %42 = trunc i32 %41 to i16
  %43 = load i16, ptr %7, align 2, !tbaa !11
  %44 = load i16, ptr %8, align 2, !tbaa !11
  %45 = call zeroext i8 @VDP_drawBitmapEx(i32 noundef %37, ptr noundef %38, i16 noundef zeroext %42, i16 noundef zeroext %43, i16 noundef zeroext %44, i8 noundef zeroext 1)
  %46 = zext i8 %45 to i16
  store i16 %46, ptr %10, align 2, !tbaa !11
  %47 = load i16, ptr %9, align 2, !tbaa !11
  %48 = zext i16 %47 to i32
  %49 = load i16, ptr @curTileInd, align 2, !tbaa !11
  %50 = zext i16 %49 to i32
  %51 = add nsw i32 %50, %48
  %52 = trunc i32 %51 to i16
  store i16 %52, ptr @curTileInd, align 2, !tbaa !11
  %53 = load i16, ptr %10, align 2, !tbaa !11
  %54 = trunc i16 %53 to i8
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #3
  ret i8 %54
}

declare i64 @mulu(i16 noundef zeroext, i16 noundef zeroext) #2

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @VDP_drawBitmapEx(i32 noundef, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i8 noundef zeroext) #1

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 = !{!14, !12, i64 4}
!14 = !{!"", !12, i64 0, !12, i64 2, !12, i64 4, !10, i64 8, !10, i64 16}
!15 = !{!14, !12, i64 2}


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

@curTileInd = external dso_local local_unnamed_addr global i16, align 2
@userTileMaxIndex = external local_unnamed_addr global i16, align 2

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @VDP_drawBitmap(i32 noundef %0, ptr noundef %1, i16 noundef zeroext %2, i16 noundef zeroext %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %1, i64 4
  %6 = load i16, ptr %5, align 4, !tbaa !5
  %7 = zext i16 %6 to i32
  %8 = add nuw nsw i32 %7, 7
  %9 = lshr i32 %8, 3
  %10 = trunc nuw nsw i32 %9 to i16
  %11 = getelementptr inbounds i8, ptr %1, i64 2
  %12 = load i16, ptr %11, align 2, !tbaa !11
  %13 = zext i16 %12 to i32
  %14 = add nuw nsw i32 %13, 7
  %15 = lshr i32 %14, 3
  %16 = trunc nuw nsw i32 %15 to i16
  %17 = tail call i64 @mulu(i16 noundef zeroext %10, i16 noundef zeroext %16) #2
  %18 = trunc i64 %17 to i32
  %19 = load i16, ptr @curTileInd, align 2, !tbaa !12
  %20 = zext i16 %19 to i32
  %21 = and i32 %18, 65535
  %22 = add nuw nsw i32 %21, %20
  %23 = load i16, ptr @userTileMaxIndex, align 2, !tbaa !12
  %24 = zext i16 %23 to i32
  %25 = icmp ugt i32 %22, %24
  br i1 %25, label %26, label %27

26:                                               ; preds = %4
  store i16 16, ptr @curTileInd, align 2, !tbaa !12
  br label %27

27:                                               ; preds = %26, %4
  %28 = phi i16 [ 16, %26 ], [ %19, %4 ]
  %29 = tail call zeroext i8 @VDP_drawBitmapEx(i32 noundef %0, ptr noundef nonnull %1, i16 noundef zeroext %28, i16 noundef zeroext %2, i16 noundef zeroext %3, i8 noundef zeroext 1)
  %30 = load i16, ptr @curTileInd, align 2, !tbaa !12
  %31 = trunc i64 %17 to i16
  %32 = add i16 %30, %31
  store i16 %32, ptr @curTileInd, align 2, !tbaa !12
  ret i8 %29
}

declare i64 @mulu(i16 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #1

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

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

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,TailCallElimPass