; 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.TileMap = type { i16, i16, i16, 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

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @VDP_drawImageEx(i32 noundef %0, ptr noundef %1, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4, i8 noundef zeroext %5, i8 noundef zeroext %6) #2 {
  %8 = alloca i8, align 1
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i16, align 2
  %12 = alloca i16, align 2
  %13 = alloca i16, align 2
  %14 = alloca i8, align 1
  %15 = alloca i8, align 1
  %16 = alloca ptr, align 8
  %17 = alloca i32, align 4
  %18 = alloca ptr, align 8
  store i32 %0, ptr %9, align 4, !tbaa !5
  store ptr %1, ptr %10, align 8, !tbaa !9
  store i16 %2, ptr %11, align 2, !tbaa !11
  store i16 %3, ptr %12, align 2, !tbaa !11
  store i16 %4, ptr %13, align 2, !tbaa !11
  store i8 %5, ptr %14, align 1, !tbaa !13
  store i8 %6, ptr %15, align 1, !tbaa !13
  %19 = load ptr, ptr %10, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.Image, ptr %19, i32 0, i32 1
  %21 = load ptr, ptr %20, align 8, !tbaa !14
  %22 = load i16, ptr %11, align 2, !tbaa !11
  %23 = zext i16 %22 to i32
  %24 = and i32 %23, 2047
  %25 = trunc i32 %24 to i16
  %26 = load i8, ptr %15, align 1, !tbaa !13
  %27 = zext i8 %26 to i32
  %28 = icmp ne i32 %27, 0
  %29 = zext i1 %28 to i64
  %30 = select i1 %28, i32 1, i32 0
  %31 = call zeroext i16 @VDP_loadTileSet(ptr noundef %21, i16 noundef zeroext %25, i32 noundef %30)
  %32 = icmp ne i16 %31, 0
  br i1 %32, label %34, label %33

33:                                               ; preds = %7
  store i8 0, ptr %8, align 1
  br label %67

34:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #3
  %35 = load ptr, ptr %10, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.Image, ptr %35, i32 0, i32 2
  %37 = load ptr, ptr %36, align 8, !tbaa !16
  store ptr %37, ptr %16, align 8, !tbaa !9
  %38 = load i32, ptr %9, align 4, !tbaa !5
  %39 = load ptr, ptr %16, align 8, !tbaa !9
  %40 = load i16, ptr %11, align 2, !tbaa !11
  %41 = load i16, ptr %12, align 2, !tbaa !11
  %42 = load i16, ptr %13, align 2, !tbaa !11
  %43 = load ptr, ptr %16, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.TileMap, ptr %43, i32 0, i32 1
  %45 = load i16, ptr %44, align 2, !tbaa !17
  %46 = load ptr, ptr %16, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.TileMap, ptr %46, i32 0, i32 2
  %48 = load i16, ptr %47, align 4, !tbaa !19
  %49 = call zeroext i8 @VDP_setTileMapEx(i32 noundef %38, ptr noundef %39, i16 noundef zeroext %40, i16 noundef zeroext %41, i16 noundef zeroext %42, i16 noundef zeroext 0, i16 noundef zeroext 0, i16 noundef zeroext %45, i16 noundef zeroext %48, i32 noundef 0)
  %50 = icmp ne i8 %49, 0
  br i1 %50, label %52, label %51

51:                                               ; preds = %34
  store i8 0, ptr %8, align 1
  store i32 1, ptr %17, align 4
  br label %66

52:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #3
  %53 = load ptr, ptr %10, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.Image, ptr %53, i32 0, i32 0
  %55 = load ptr, ptr %54, align 8, !tbaa !20
  store ptr %55, ptr %18, align 8, !tbaa !9
  %56 = load i8, ptr %14, align 1, !tbaa !13
  %57 = icmp ne i8 %56, 0
  br i1 %57, label %58, label %65

58:                                               ; preds = %52
  %59 = load i16, ptr %11, align 2, !tbaa !11
  %60 = zext i16 %59 to i32
  %61 = ashr i32 %60, 9
  %62 = and i32 %61, 48
  %63 = trunc i32 %62 to i16
  %64 = load ptr, ptr %18, align 8, !tbaa !9
  call void @PAL_setPaletteColors(i16 noundef zeroext %63, ptr noundef %64, i32 noundef 0)
  br label %65

65:                                               ; preds = %58, %52
  store i8 1, ptr %8, align 1
  store i32 1, ptr %17, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #3
  br label %66

66:                                               ; preds = %65, %51
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #3
  br label %67

67:                                               ; preds = %66, %33
  %68 = load i8, ptr %8, align 1
  ret i8 %68
}

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

declare zeroext i8 @VDP_setTileMapEx(i32 noundef, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i32 noundef) #1

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 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !10, i64 8}
!15 = !{!"", !10, i64 0, !10, i64 8, !10, i64 16}
!16 = !{!15, !10, i64 16}
!17 = !{!18, !12, i64 2}
!18 = !{!"", !12, i64 0, !12, i64 2, !12, i64 4, !10, i64 8}
!19 = !{!18, !12, i64 4}
!20 = !{!15, !10, i64 0}


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

declare void @PAL_setPaletteColors(i16 noundef zeroext, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local zeroext range(i8 0, 2) i8 @VDP_drawImageEx(i32 noundef %0, ptr nocapture noundef readonly %1, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4, i8 noundef zeroext %5, i8 noundef zeroext %6) local_unnamed_addr #1 {
  %8 = getelementptr inbounds i8, ptr %1, i64 8
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  %10 = and i16 %2, 2047
  %.not = icmp ne i8 %6, 0
  %11 = zext i1 %.not to i32
  %12 = tail call zeroext i16 @VDP_loadTileSet(ptr noundef %9, i16 noundef zeroext %10, i32 noundef %11) #2
  %.not15 = icmp eq i16 %12, 0
  br i1 %.not15, label %26, label %13

13:                                               ; preds = %7
  %14 = getelementptr inbounds i8, ptr %1, i64 16
  %15 = load ptr, ptr %14, align 8, !tbaa !10
  %16 = getelementptr inbounds i8, ptr %15, i64 2
  %17 = load i16, ptr %16, align 2, !tbaa !11
  %18 = getelementptr inbounds i8, ptr %15, i64 4
  %19 = load i16, ptr %18, align 4, !tbaa !14
  %20 = tail call zeroext i8 @VDP_setTileMapEx(i32 noundef %0, ptr noundef %15, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4, i16 noundef zeroext 0, i16 noundef zeroext 0, i16 noundef zeroext %17, i16 noundef zeroext %19, i32 noundef 0) #2
  %.not16 = icmp eq i8 %20, 0
  br i1 %.not16, label %26, label %21

21:                                               ; preds = %13
  %.not17 = icmp eq i8 %5, 0
  br i1 %.not17, label %26, label %22

22:                                               ; preds = %21
  %23 = load ptr, ptr %1, align 8, !tbaa !15
  %24 = lshr i16 %2, 9
  %25 = and i16 %24, 48
  tail call void @PAL_setPaletteColors(i16 noundef zeroext %25, ptr noundef %23, i32 noundef 0) #2
  br label %26

26:                                               ; preds = %13, %22, %21, %7
  %.1 = phi i8 [ 0, %7 ], [ 0, %13 ], [ 1, %22 ], [ 1, %21 ]
  ret i8 %.1
}

declare zeroext i16 @VDP_loadTileSet(ptr noundef, i16 noundef zeroext, i32 noundef) local_unnamed_addr #0

declare zeroext i8 @VDP_setTileMapEx(i32 noundef, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i32 noundef) local_unnamed_addr #0

attributes #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 = { 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, !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 = !{!6, !7, i64 16}
!11 = !{!12, !13, i64 2}
!12 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !7, i64 8}
!13 = !{!"short", !8, i64 0}
!14 = !{!12, !13, i64 4}
!15 = !{!6, !7, i64 0}


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