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

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

39:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #3
  %40 = load ptr, ptr %9, align 8, !tbaa !9
  %41 = call ptr @unpackBitmap(ptr noundef %40, ptr noundef null)
  store ptr %41, ptr %17, align 8, !tbaa !9
  %42 = load ptr, ptr %17, align 8, !tbaa !9
  %43 = icmp eq ptr %42, null
  br i1 %43, label %44, label %45

44:                                               ; preds = %39
  store i8 0, ptr %7, align 1
  store i32 1, ptr %18, align 4
  br label %57

45:                                               ; preds = %39
  %46 = load ptr, ptr %17, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.Bitmap, ptr %46, i32 0, i32 4
  %48 = load ptr, ptr %47, align 8, !tbaa !19
  %49 = load i16, ptr %10, align 2, !tbaa !11
  %50 = zext i16 %49 to i32
  %51 = and i32 %50, 2047
  %52 = trunc i32 %51 to i16
  %53 = load i16, ptr %14, align 2, !tbaa !11
  %54 = load i16, ptr %15, align 2, !tbaa !11
  %55 = load i16, ptr %14, align 2, !tbaa !11
  call void @VDP_loadBMPTileData(ptr noundef %48, i16 noundef zeroext %52, i16 noundef zeroext %53, i16 noundef zeroext %54, i16 noundef zeroext %55)
  %56 = load ptr, ptr %17, align 8, !tbaa !9
  call void @MEM_free(ptr noundef %56)
  store i32 0, ptr %18, align 4
  br label %57

57:                                               ; preds = %45, %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #3
  %58 = load i32, ptr %18, align 4
  switch i32 %58, label %88 [
    i32 0, label %59
  ]

59:                                               ; preds = %57
  br label %71

60:                                               ; preds = %6
  %61 = load ptr, ptr %9, align 8, !tbaa !9
  %62 = getelementptr inbounds %struct.Bitmap, ptr %61, i32 0, i32 4
  %63 = load ptr, ptr %62, align 8, !tbaa !19
  %64 = load i16, ptr %10, align 2, !tbaa !11
  %65 = zext i16 %64 to i32
  %66 = and i32 %65, 2047
  %67 = trunc i32 %66 to i16
  %68 = load i16, ptr %14, align 2, !tbaa !11
  %69 = load i16, ptr %15, align 2, !tbaa !11
  %70 = load i16, ptr %14, align 2, !tbaa !11
  call void @VDP_loadBMPTileData(ptr noundef %63, i16 noundef zeroext %67, i16 noundef zeroext %68, i16 noundef zeroext %69, i16 noundef zeroext %70)
  br label %71

71:                                               ; preds = %60, %59
  %72 = load i32, ptr %8, align 4, !tbaa !5
  %73 = load i16, ptr %10, align 2, !tbaa !11
  %74 = load i16, ptr %11, align 2, !tbaa !11
  %75 = load i16, ptr %12, align 2, !tbaa !11
  %76 = load i16, ptr %14, align 2, !tbaa !11
  %77 = load i16, ptr %15, align 2, !tbaa !11
  call void @VDP_fillTileMapRectInc(i32 noundef %72, i16 noundef zeroext %73, i16 noundef zeroext %74, i16 noundef zeroext %75, i16 noundef zeroext %76, i16 noundef zeroext %77)
  %78 = load i8, ptr %13, align 1, !tbaa !13
  %79 = icmp ne i8 %78, 0
  br i1 %79, label %80, label %87

80:                                               ; preds = %71
  %81 = load i16, ptr %10, align 2, !tbaa !11
  %82 = zext i16 %81 to i32
  %83 = ashr i32 %82, 9
  %84 = and i32 %83, 48
  %85 = trunc i32 %84 to i16
  %86 = load ptr, ptr %16, align 8, !tbaa !9
  call void @PAL_setPaletteColors(i16 noundef zeroext %85, ptr noundef %86, i32 noundef 0)
  br label %87

87:                                               ; preds = %80, %71
  store i8 1, ptr %7, align 1
  store i32 1, ptr %18, align 4
  br label %88

88:                                               ; preds = %87, %57
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %14) #3
  %89 = load i8, ptr %7, align 1
  ret i8 %89
}

declare ptr @unpackBitmap(ptr noundef, ptr noundef) #2

declare void @VDP_loadBMPTileData(ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext) #2

declare void @MEM_free(ptr noundef) #2

declare void @VDP_fillTileMapRectInc(i32 noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext) #2

declare void @PAL_setPaletteColors(i16 noundef zeroext, ptr noundef, 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 = !{!15, !12, i64 2}
!15 = !{!"", !12, i64 0, !12, i64 2, !12, i64 4, !10, i64 8, !10, i64 16}
!16 = !{!15, !12, i64 4}
!17 = !{!15, !10, i64 8}
!18 = !{!15, !12, i64 0}
!19 = !{!15, !10, i64 16}


; 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
define dso_local zeroext range(i8 0, 2) i8 @VDP_drawBitmapEx(i32 noundef %0, ptr noundef %1, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4, i8 noundef zeroext %5) local_unnamed_addr #0 {
  %7 = getelementptr inbounds i8, ptr %1, i64 2
  %8 = load i16, ptr %7, align 2, !tbaa !5
  %9 = lshr i16 %8, 3
  %10 = getelementptr inbounds i8, ptr %1, i64 4
  %11 = load i16, ptr %10, align 4, !tbaa !11
  %12 = lshr i16 %11, 3
  %13 = getelementptr inbounds i8, ptr %1, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !12
  %15 = load i16, ptr %1, align 8, !tbaa !13
  %.not = icmp eq i16 %15, 0
  br i1 %.not, label %22, label %16

16:                                               ; preds = %6
  %17 = tail call ptr @unpackBitmap(ptr noundef nonnull %1, ptr noundef null) #2
  %.not29 = icmp eq ptr %17, null
  br i1 %.not29, label %.critedge, label %18

18:                                               ; preds = %16
  %19 = getelementptr inbounds i8, ptr %17, i64 16
  %20 = load ptr, ptr %19, align 8, !tbaa !14
  %21 = and i16 %2, 2047
  tail call void @VDP_loadBMPTileData(ptr noundef %20, i16 noundef zeroext %21, i16 noundef zeroext %9, i16 noundef zeroext %12, i16 noundef zeroext %9) #2
  tail call void @MEM_free(ptr noundef nonnull %17) #2
  br label %26

22:                                               ; preds = %6
  %23 = getelementptr inbounds i8, ptr %1, i64 16
  %24 = load ptr, ptr %23, align 8, !tbaa !14
  %25 = and i16 %2, 2047
  tail call void @VDP_loadBMPTileData(ptr noundef %24, i16 noundef zeroext %25, i16 noundef zeroext %9, i16 noundef zeroext %12, i16 noundef zeroext %9) #2
  br label %26

26:                                               ; preds = %18, %22
  tail call void @VDP_fillTileMapRectInc(i32 noundef %0, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4, i16 noundef zeroext %9, i16 noundef zeroext %12) #2
  %.not30 = icmp eq i8 %5, 0
  br i1 %.not30, label %.critedge, label %27

27:                                               ; preds = %26
  %28 = lshr i16 %2, 9
  %29 = and i16 %28, 48
  tail call void @PAL_setPaletteColors(i16 noundef zeroext %29, ptr noundef %14, i32 noundef 0) #2
  br label %.critedge

.critedge:                                        ; preds = %16, %26, %27
  %.1 = phi i8 [ 1, %27 ], [ 1, %26 ], [ 0, %16 ]
  ret i8 %.1
}

declare ptr @unpackBitmap(ptr noundef, ptr noundef) local_unnamed_addr #1

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

declare void @MEM_free(ptr noundef) local_unnamed_addr #1

declare void @VDP_fillTileMapRectInc(i32 noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #1

declare void @PAL_setPaletteColors(i16 noundef zeroext, ptr noundef, i32 noundef) local_unnamed_addr #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 = { "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 2}
!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 4}
!12 = !{!6, !10, i64 8}
!13 = !{!6, !7, i64 0}
!14 = !{!6, !10, i64 16}


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