; 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_setTileMapDataRect(i32 noundef, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i32 noundef) #2

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

32:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #3
  %33 = load ptr, ptr %10, align 8, !tbaa !9
  %34 = call ptr @unpackTileMap(ptr noundef %33, ptr noundef null)
  store ptr %34, ptr %17, align 8, !tbaa !9
  %35 = load ptr, ptr %17, align 8, !tbaa !9
  %36 = icmp eq ptr %35, null
  br i1 %36, label %37, label %38

37:                                               ; preds = %32
  store i8 0, ptr %8, align 1
  store i32 1, ptr %18, align 4
  br label %54

38:                                               ; preds = %32
  %39 = load i32, ptr %9, align 4, !tbaa !5
  %40 = load ptr, ptr %17, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.TileMap, ptr %40, i32 0, i32 3
  %42 = load ptr, ptr %41, align 8, !tbaa !18
  %43 = load i64, ptr %16, align 8, !tbaa !15
  %44 = getelementptr inbounds i16, ptr %42, i64 %43
  %45 = load i16, ptr %11, align 2, !tbaa !11
  %46 = load i16, ptr %12, align 2, !tbaa !11
  %47 = load i16, ptr %13, align 2, !tbaa !11
  %48 = load i16, ptr %14, align 2, !tbaa !11
  %49 = load ptr, ptr %17, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.TileMap, ptr %49, i32 0, i32 1
  %51 = load i16, ptr %50, align 2, !tbaa !13
  %52 = load i32, ptr %15, align 4, !tbaa !5
  call void @VDP_setTileMapDataRect(i32 noundef %39, ptr noundef %44, i16 noundef zeroext %45, i16 noundef zeroext %46, i16 noundef zeroext %47, i16 noundef zeroext %48, i16 noundef zeroext %51, i32 noundef %52)
  %53 = load ptr, ptr %17, align 8, !tbaa !9
  call void @MEM_free(ptr noundef %53)
  store i32 0, ptr %18, align 4
  br label %54

54:                                               ; preds = %38, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #3
  %55 = load i32, ptr %18, align 4
  switch i32 %55, label %73 [
    i32 0, label %56
  ]

56:                                               ; preds = %54
  br label %72

57:                                               ; preds = %7
  %58 = load i32, ptr %9, align 4, !tbaa !5
  %59 = load ptr, ptr %10, align 8, !tbaa !9
  %60 = getelementptr inbounds %struct.TileMap, ptr %59, i32 0, i32 3
  %61 = load ptr, ptr %60, align 8, !tbaa !18
  %62 = load i64, ptr %16, align 8, !tbaa !15
  %63 = getelementptr inbounds i16, ptr %61, i64 %62
  %64 = load i16, ptr %11, align 2, !tbaa !11
  %65 = load i16, ptr %12, align 2, !tbaa !11
  %66 = load i16, ptr %13, align 2, !tbaa !11
  %67 = load i16, ptr %14, align 2, !tbaa !11
  %68 = load ptr, ptr %10, align 8, !tbaa !9
  %69 = getelementptr inbounds %struct.TileMap, ptr %68, i32 0, i32 1
  %70 = load i16, ptr %69, align 2, !tbaa !13
  %71 = load i32, ptr %15, align 4, !tbaa !5
  call void @VDP_setTileMapDataRect(i32 noundef %58, ptr noundef %63, i16 noundef zeroext %64, i16 noundef zeroext %65, i16 noundef zeroext %66, i16 noundef zeroext %67, i16 noundef zeroext %70, i32 noundef %71)
  br label %72

72:                                               ; preds = %57, %56
  store i8 1, ptr %8, align 1
  store i32 1, ptr %18, align 4
  br label %73

73:                                               ; preds = %72, %54
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #3
  %74 = load i8, ptr %8, align 1
  ret i8 %74
}

declare ptr @unpackTileMap(ptr noundef, ptr 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 = !{!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_setTileMapDataRect(i32 noundef, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i32 noundef) local_unnamed_addr #1

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

14:                                               ; preds = %7
  %15 = tail call ptr @unpackTileMap(ptr noundef nonnull %1, ptr noundef null) #2
  %.not28 = icmp eq ptr %15, null
  br i1 %.not28, label %.critedge, label %16

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

22:                                               ; preds = %7
  %23 = getelementptr inbounds i8, ptr %1, i64 8
  %24 = load ptr, ptr %23, align 8, !tbaa !12
  %25 = getelementptr inbounds i16, ptr %24, i64 %12
  %26 = load i16, ptr %8, align 2, !tbaa !5
  tail call void @VDP_setTileMapDataRect(i32 noundef %0, ptr noundef %25, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4, i16 noundef zeroext %5, i16 noundef zeroext %26, i32 noundef %6)
  br label %.critedge

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

declare ptr @unpackTileMap(ptr noundef, ptr 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 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