; 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.Image = type { ptr, ptr, ptr }
%struct.TileSet = type { i16, i16, 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_drawImage(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) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #2
  %11 = load ptr, ptr %6, align 8, !tbaa !9
  %12 = getelementptr inbounds %struct.Image, ptr %11, i32 0, i32 1
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  %14 = getelementptr inbounds %struct.TileSet, ptr %13, i32 0, i32 1
  %15 = load i16, ptr %14, align 2, !tbaa !15
  store i16 %15, ptr %9, align 2, !tbaa !11
  %16 = load i16, ptr @curTileInd, align 2, !tbaa !11
  %17 = zext i16 %16 to i32
  %18 = load i16, ptr %9, align 2, !tbaa !11
  %19 = zext i16 %18 to i32
  %20 = add nsw i32 %17, %19
  %21 = load i16, ptr @userTileMaxIndex, align 2, !tbaa !11
  %22 = zext i16 %21 to i32
  %23 = icmp sgt i32 %20, %22
  br i1 %23, label %24, label %25

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

25:                                               ; preds = %24, %4
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = load ptr, ptr %6, align 8, !tbaa !9
  %28 = load i16, ptr @curTileInd, align 2, !tbaa !11
  %29 = zext i16 %28 to i32
  %30 = add nsw i32 0, %29
  %31 = trunc i32 %30 to i16
  %32 = load i16, ptr %7, align 2, !tbaa !11
  %33 = load i16, ptr %8, align 2, !tbaa !11
  %34 = call zeroext i8 @VDP_drawImageEx(i32 noundef %26, ptr noundef %27, i16 noundef zeroext %31, i16 noundef zeroext %32, i16 noundef zeroext %33, i8 noundef zeroext 1, i8 noundef zeroext 1)
  %35 = zext i8 %34 to i16
  store i16 %35, ptr %10, align 2, !tbaa !11
  %36 = load i16, ptr %9, align 2, !tbaa !11
  %37 = zext i16 %36 to i32
  %38 = load i16, ptr @curTileInd, align 2, !tbaa !11
  %39 = zext i16 %38 to i32
  %40 = add nsw i32 %39, %37
  %41 = trunc i32 %40 to i16
  store i16 %41, ptr @curTileInd, align 2, !tbaa !11
  %42 = load i16, ptr %10, align 2, !tbaa !11
  %43 = trunc i16 %42 to i8
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #2
  ret i8 %43
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @VDP_drawImageEx(i32 noundef, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i8 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 = { 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, !10, i64 8}
!14 = !{!"", !10, i64 0, !10, i64 8, !10, i64 16}
!15 = !{!16, !12, i64 2}
!16 = !{!"", !12, i64 0, !12, i64 2, !10, i64 8}


; 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_drawImage(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 8
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %6, i64 2
  %8 = load i16, ptr %7, align 2, !tbaa !10
  %9 = load i16, ptr @curTileInd, align 2, !tbaa !13
  %10 = zext i16 %9 to i32
  %11 = zext i16 %8 to i32
  %12 = add nuw nsw i32 %10, %11
  %13 = load i16, ptr @userTileMaxIndex, align 2, !tbaa !13
  %14 = zext i16 %13 to i32
  %15 = icmp ugt i32 %12, %14
  br i1 %15, label %16, label %17

16:                                               ; preds = %4
  store i16 16, ptr @curTileInd, align 2, !tbaa !13
  br label %17

17:                                               ; preds = %16, %4
  %18 = phi i16 [ 16, %16 ], [ %9, %4 ]
  %19 = tail call zeroext i8 @VDP_drawImageEx(i32 noundef %0, ptr noundef nonnull %1, i16 noundef zeroext %18, i16 noundef zeroext %2, i16 noundef zeroext %3, i8 noundef zeroext 1, i8 noundef zeroext 1)
  %20 = load i16, ptr @curTileInd, align 2, !tbaa !13
  %21 = add i16 %20, %8
  store i16 %21, ptr @curTileInd, align 2, !tbaa !13
  ret i8 %19
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @VDP_drawImageEx(i32 noundef, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i8 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" }

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


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