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

; 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.VRAMRegion = type { 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 i16 @VRAM_getLargestFreeBlock(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #2
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.VRAMRegion, ptr %6, i32 0, i32 3
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  store ptr %8, ptr %3, align 8, !tbaa !5
  store i16 0, ptr %5, align 2, !tbaa !12
  br label %9

9:                                                ; preds = %39, %1
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = load i16, ptr %10, align 2, !tbaa !12
  store i16 %11, ptr %4, align 2, !tbaa !12
  %12 = icmp ne i16 %11, 0
  br i1 %12, label %13, label %40

13:                                               ; preds = %9
  %14 = load i16, ptr %4, align 2, !tbaa !12
  %15 = zext i16 %14 to i32
  %16 = and i32 %15, 32768
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %25

18:                                               ; preds = %13
  %19 = load i16, ptr %4, align 2, !tbaa !12
  %20 = zext i16 %19 to i32
  %21 = and i32 %20, 32767
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = sext i32 %21 to i64
  %24 = getelementptr inbounds i16, ptr %22, i64 %23
  store ptr %24, ptr %3, align 8, !tbaa !5
  br label %39

25:                                               ; preds = %13
  %26 = load i16, ptr %4, align 2, !tbaa !12
  %27 = zext i16 %26 to i32
  %28 = load i16, ptr %5, align 2, !tbaa !12
  %29 = zext i16 %28 to i32
  %30 = icmp sgt i32 %27, %29
  br i1 %30, label %31, label %33

31:                                               ; preds = %25
  %32 = load i16, ptr %4, align 2, !tbaa !12
  store i16 %32, ptr %5, align 2, !tbaa !12
  br label %33

33:                                               ; preds = %31, %25
  %34 = load i16, ptr %4, align 2, !tbaa !12
  %35 = zext i16 %34 to i32
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = sext i32 %35 to i64
  %38 = getelementptr inbounds i16, ptr %36, i64 %37
  store ptr %38, ptr %3, align 8, !tbaa !5
  br label %39

39:                                               ; preds = %33, %18
  br label %9, !llvm.loop !13

40:                                               ; preds = %9
  %41 = load i16, ptr %5, align 2, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret i16 %41
}

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 = { 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, !6, i64 16}
!10 = !{!"", !11, i64 0, !11, i64 2, !6, i64 8, !6, i64 16}
!11 = !{!"short", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; 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: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local zeroext i16 @VRAM_getLargestFreeBlock(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 16
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = load i16, ptr %3, align 2, !tbaa !11
  %.not15 = icmp eq i16 %4, 0
  br i1 %.not15, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1, %.lr.ph
  %5 = phi i16 [ %7, %.lr.ph ], [ %4, %1 ]
  %.017 = phi i16 [ %.2, %.lr.ph ], [ 0, %1 ]
  %.01016 = phi ptr [ %.111, %.lr.ph ], [ %3, %1 ]
  %6 = and i16 %5, 32767
  %spec.select = tail call i16 @llvm.umax.i16(i16 %5, i16 %.017)
  %.not1314 = icmp slt i16 %5, 0
  %.2 = select i1 %.not1314, i16 %.017, i16 %spec.select
  %.pn = zext nneg i16 %6 to i64
  %.111 = getelementptr inbounds i16, ptr %.01016, i64 %.pn
  %7 = load i16, ptr %.111, align 2, !tbaa !11
  %.not = icmp eq i16 %7, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph, %1
  %.0.lcssa = phi i16 [ 0, %1 ], [ %.2, %.lr.ph ]
  ret i16 %.0.lcssa
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.umax.i16(i16, i16) #1

attributes #0 = { nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) 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 speculatable willreturn memory(none) }

!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, !10, i64 16}
!6 = !{!"", !7, i64 0, !7, i64 2, !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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass