; LLVM IR from Stephane-D/SGDK :: src (vdp_tile)

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

declare void @MEM_free(ptr noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

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

; Function Attrs: nounwind uwtable
declare dso_local void @VDP_setTileMapDataRectEx(i32 noundef, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i32 noundef) #2

declare ptr @unpackTileMap(ptr noundef, ptr noundef) #1

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

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

43:                                               ; preds = %38
  store i8 0, ptr %11, align 1
  store i32 1, ptr %24, align 4
  br label %61

44:                                               ; preds = %38
  %45 = load i32, ptr %12, align 4, !tbaa !5
  %46 = load ptr, ptr %23, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.TileMap, ptr %46, i32 0, i32 3
  %48 = load ptr, ptr %47, align 8, !tbaa !18
  %49 = load i64, ptr %22, align 8, !tbaa !15
  %50 = getelementptr inbounds i16, ptr %48, i64 %49
  %51 = load i16, ptr %14, align 2, !tbaa !11
  %52 = load i16, ptr %15, align 2, !tbaa !11
  %53 = load i16, ptr %16, align 2, !tbaa !11
  %54 = load i16, ptr %19, align 2, !tbaa !11
  %55 = load i16, ptr %20, align 2, !tbaa !11
  %56 = load ptr, ptr %23, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.TileMap, ptr %56, i32 0, i32 1
  %58 = load i16, ptr %57, align 2, !tbaa !13
  %59 = load i32, ptr %21, align 4, !tbaa !5
  call void @VDP_setTileMapDataRectEx(i32 noundef %45, ptr noundef %50, i16 noundef zeroext %51, i16 noundef zeroext %52, i16 noundef zeroext %53, i16 noundef zeroext %54, i16 noundef zeroext %55, i16 noundef zeroext %58, i32 noundef %59)
  %60 = load ptr, ptr %23, align 8, !tbaa !9
  call void @MEM_free(ptr noundef %60)
  store i32 0, ptr %24, align 4
  br label %61

61:                                               ; preds = %44, %43
  call void @llvm.lifetime.end.p0(i64 8, ptr %23) #3
  %62 = load i32, ptr %24, align 4
  switch i32 %62, label %81 [
    i32 0, label %63
  ]

63:                                               ; preds = %61
  br label %80

64:                                               ; preds = %10
  %65 = load i32, ptr %12, align 4, !tbaa !5
  %66 = load ptr, ptr %13, align 8, !tbaa !9
  %67 = getelementptr inbounds %struct.TileMap, ptr %66, i32 0, i32 3
  %68 = load ptr, ptr %67, align 8, !tbaa !18
  %69 = load i64, ptr %22, align 8, !tbaa !15
  %70 = getelementptr inbounds i16, ptr %68, i64 %69
  %71 = load i16, ptr %14, align 2, !tbaa !11
  %72 = load i16, ptr %15, align 2, !tbaa !11
  %73 = load i16, ptr %16, align 2, !tbaa !11
  %74 = load i16, ptr %19, align 2, !tbaa !11
  %75 = load i16, ptr %20, align 2, !tbaa !11
  %76 = load ptr, ptr %13, align 8, !tbaa !9
  %77 = getelementptr inbounds %struct.TileMap, ptr %76, i32 0, i32 1
  %78 = load i16, ptr %77, align 2, !tbaa !13
  %79 = load i32, ptr %21, align 4, !tbaa !5
  call void @VDP_setTileMapDataRectEx(i32 noundef %65, ptr noundef %70, i16 noundef zeroext %71, i16 noundef zeroext %72, i16 noundef zeroext %73, i16 noundef zeroext %74, i16 noundef zeroext %75, i16 noundef zeroext %78, i32 noundef %79)
  br label %80

80:                                               ; preds = %64, %63
  store i8 1, ptr %11, align 1
  store i32 1, ptr %24, align 4
  br label %81

81:                                               ; preds = %80, %61
  call void @llvm.lifetime.end.p0(i64 8, ptr %22) #3
  %82 = load i8, ptr %11, align 1
  ret i8 %82
}

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

declare void @MEM_free(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local void @VDP_setTileMapDataRectEx(i32 noundef, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i32 noundef) local_unnamed_addr #1

declare ptr @unpackTileMap(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local zeroext range(i8 0, 2) i8 @VDP_setTileMapEx(i32 noundef %0, ptr noundef %1, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4, i16 noundef zeroext %5, i16 noundef zeroext %6, i16 noundef zeroext %7, i16 noundef zeroext %8, i32 noundef %9) local_unnamed_addr #1 {
  %11 = getelementptr inbounds i8, ptr %1, i64 2
  %12 = load i16, ptr %11, align 2, !tbaa !5
  %13 = tail call i64 @mulu(i16 noundef zeroext %6, i16 noundef zeroext %12) #2
  %14 = zext i16 %5 to i64
  %15 = add i64 %13, %14
  %16 = load i16, ptr %1, align 8, !tbaa !11
  %.not = icmp eq i16 %16, 0
  br i1 %.not, label %25, label %17

17:                                               ; preds = %10
  %18 = tail call ptr @unpackTileMap(ptr noundef nonnull %1, ptr noundef null) #2
  %.not30 = icmp eq ptr %18, null
  br i1 %.not30, label %.critedge, label %19

19:                                               ; preds = %17
  %20 = getelementptr inbounds i8, ptr %18, i64 8
  %21 = load ptr, ptr %20, align 8, !tbaa !12
  %22 = getelementptr inbounds i16, ptr %21, i64 %15
  %23 = getelementptr inbounds i8, ptr %18, i64 2
  %24 = load i16, ptr %23, align 2, !tbaa !5
  tail call void @VDP_setTileMapDataRectEx(i32 noundef %0, ptr noundef %22, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4, i16 noundef zeroext %7, i16 noundef zeroext %8, i16 noundef zeroext %24, i32 noundef %9)
  tail call void @MEM_free(ptr noundef nonnull %18) #2
  br label %.critedge

25:                                               ; preds = %10
  %26 = getelementptr inbounds i8, ptr %1, i64 8
  %27 = load ptr, ptr %26, align 8, !tbaa !12
  %28 = getelementptr inbounds i16, ptr %27, i64 %15
  %29 = load i16, ptr %11, align 2, !tbaa !5
  tail call void @VDP_setTileMapDataRectEx(i32 noundef %0, ptr noundef %28, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4, i16 noundef zeroext %7, i16 noundef zeroext %8, i16 noundef zeroext %29, i32 noundef %9)
  br label %.critedge

.critedge:                                        ; preds = %25, %19, %17
  %.1 = phi i8 [ 0, %17 ], [ 1, %19 ], [ 1, %25 ]
  ret i8 %.1
}

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


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