; 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.TileSet = type { i16, i16, ptr }

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i16 @VDP_loadTileSet(ptr noundef %0, i16 noundef zeroext %1, i32 noundef %2) #0 {
  %4 = alloca i16, align 2
  %5 = alloca ptr, align 8
  %6 = alloca i16, align 2
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i16 %1, ptr %6, align 2, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !11
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.TileSet, ptr %10, i32 0, i32 0
  %12 = load i16, ptr %11, align 8, !tbaa !13
  %13 = zext i16 %12 to i32
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %15, label %34

15:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = call ptr @unpackTileSet(ptr noundef %16, ptr noundef null)
  store ptr %17, ptr %8, align 8, !tbaa !5
  %18 = load ptr, ptr %8, align 8, !tbaa !5
  %19 = icmp eq ptr %18, null
  br i1 %19, label %20, label %21

20:                                               ; preds = %15
  store i16 0, ptr %4, align 2
  store i32 1, ptr %9, align 4
  br label %31

21:                                               ; preds = %15
  %22 = load ptr, ptr %8, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.TileSet, ptr %22, i32 0, i32 2
  %24 = load ptr, ptr %23, align 8, !tbaa !15
  %25 = load i16, ptr %6, align 2, !tbaa !9
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.TileSet, ptr %26, i32 0, i32 1
  %28 = load i16, ptr %27, align 2, !tbaa !16
  %29 = load i32, ptr %7, align 4, !tbaa !11
  call void @VDP_loadTileData(ptr noundef %24, i16 noundef zeroext %25, i16 noundef zeroext %28, i32 noundef %29)
  %30 = load ptr, ptr %8, align 8, !tbaa !5
  call void @MEM_free(ptr noundef %30)
  store i32 0, ptr %9, align 4
  br label %31

31:                                               ; preds = %21, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %32 = load i32, ptr %9, align 4
  switch i32 %32, label %46 [
    i32 0, label %33
    i32 1, label %44
  ]

33:                                               ; preds = %31
  br label %43

34:                                               ; preds = %3
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.TileSet, ptr %35, i32 0, i32 2
  %37 = load ptr, ptr %36, align 8, !tbaa !15
  %38 = load i16, ptr %6, align 2, !tbaa !9
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.TileSet, ptr %39, i32 0, i32 1
  %41 = load i16, ptr %40, align 2, !tbaa !16
  %42 = load i32, ptr %7, align 4, !tbaa !11
  call void @VDP_loadTileData(ptr noundef %37, i16 noundef zeroext %38, i16 noundef zeroext %41, i32 noundef %42)
  br label %43

43:                                               ; preds = %34, %33
  store i16 1, ptr %4, align 2
  br label %44

44:                                               ; preds = %43, %31
  %45 = load i16, ptr %4, align 2
  ret i16 %45

46:                                               ; preds = %31
  unreachable
}

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

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

declare void @MEM_free(ptr noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"", !10, i64 0, !10, i64 2, !6, i64 8}
!15 = !{!14, !6, i64 8}
!16 = !{!14, !10, i64 2}


; 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
declare dso_local void @VDP_loadTileData(ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local zeroext range(i16 0, 2) i16 @VDP_loadTileSet(ptr noundef %0, i16 noundef zeroext %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = load i16, ptr %0, align 8, !tbaa !5
  %.not = icmp eq i16 %4, 0
  br i1 %.not, label %12, label %5

5:                                                ; preds = %3
  %6 = tail call ptr @unpackTileSet(ptr noundef nonnull %0, ptr noundef null) #2
  %7 = icmp eq ptr %6, null
  br i1 %7, label %17, label %.thread

.thread:                                          ; preds = %5
  %8 = getelementptr inbounds i8, ptr %6, i64 8
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = getelementptr inbounds i8, ptr %6, i64 2
  %11 = load i16, ptr %10, align 2, !tbaa !12
  tail call void @VDP_loadTileData(ptr noundef %9, i16 noundef zeroext %1, i16 noundef zeroext %11, i32 noundef %2)
  tail call void @MEM_free(ptr noundef nonnull %6) #2
  br label %17

12:                                               ; preds = %3
  %13 = getelementptr inbounds i8, ptr %0, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  %15 = getelementptr inbounds i8, ptr %0, i64 2
  %16 = load i16, ptr %15, align 2, !tbaa !12
  tail call void @VDP_loadTileData(ptr noundef %14, i16 noundef zeroext %1, i16 noundef zeroext %16, i32 noundef %2)
  br label %17

17:                                               ; preds = %12, %.thread, %5
  %.1 = phi i16 [ 0, %5 ], [ 1, %.thread ], [ 1, %12 ]
  ret i16 %.1
}

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

declare void @MEM_free(ptr 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 0}
!6 = !{!"", !7, i64 0, !7, i64 2, !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, !10, i64 8}
!12 = !{!6, !7, i64 2}


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