; 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_getAllocated(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 = %40, %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 %41

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 %34

18:                                               ; preds = %13
  %19 = load i16, ptr %4, align 2, !tbaa !12
  %20 = zext i16 %19 to i32
  %21 = and i32 %20, 32767
  %22 = trunc i32 %21 to i16
  store i16 %22, ptr %4, align 2, !tbaa !12
  %23 = load i16, ptr %4, align 2, !tbaa !12
  %24 = zext i16 %23 to i32
  %25 = load i16, ptr %5, align 2, !tbaa !12
  %26 = zext i16 %25 to i32
  %27 = add nsw i32 %26, %24
  %28 = trunc i32 %27 to i16
  store i16 %28, ptr %5, align 2, !tbaa !12
  %29 = load i16, ptr %4, align 2, !tbaa !12
  %30 = zext i16 %29 to i32
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = sext i32 %30 to i64
  %33 = getelementptr inbounds i16, ptr %31, i64 %32
  store ptr %33, ptr %3, align 8, !tbaa !5
  br label %40

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

40:                                               ; preds = %34, %18
  br label %9, !llvm.loop !13

41:                                               ; preds = %9
  %42 = 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 %42
}

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_getAllocated(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 [ %8, %.lr.ph ], [ %4, %1 ]
  %.017 = phi i16 [ %.1, %.lr.ph ], [ 0, %1 ]
  %.01016 = phi ptr [ %.111, %.lr.ph ], [ %3, %1 ]
  %6 = and i16 %5, 32767
  %.not1314 = icmp slt i16 %5, 0
  %7 = select i1 %.not1314, i16 %6, i16 0
  %.1 = add i16 %7, %.017
  %.pn = zext nneg i16 %6 to i64
  %.111 = getelementptr inbounds i16, ptr %.01016, i64 %.pn
  %8 = load i16, ptr %.111, align 2, !tbaa !11
  %.not = icmp eq i16 %8, 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 ], [ %.1, %.lr.ph ]
  ret i16 %.0.lcssa
}

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

!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,ReassociatePass,SROAPass,SimplifyCFGPass