; LLVM IR from OpenLoco/OpenLoco :: src/Math/src/Vector.ll (ZN8OpenLoco4Math6Vector14fastSquareRootEj)

; 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.std::array" = type { [2048 x i16] }

@_ZN8OpenLoco4Math6Vector4DataL20kFastSquareRootTableE = external hidden constant %"struct.std::array", align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i16 @_ZN8OpenLoco4Math6Vector14fastSquareRootEj(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i8, align 1
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #2
  store i8 10, ptr %3, align 1, !tbaa !9
  br label %4

4:                                                ; preds = %8, %1
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = icmp uge i32 %5, 4096
  br i1 %6, label %7, label %13

7:                                                ; preds = %4
  br label %8

8:                                                ; preds = %7
  %9 = load i8, ptr %3, align 1, !tbaa !9
  %10 = add i8 %9, -1
  store i8 %10, ptr %3, align 1, !tbaa !9
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = lshr i32 %11, 2
  store i32 %12, ptr %2, align 4, !tbaa !5
  br label %4, !llvm.loop !10

13:                                               ; preds = %4
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = and i32 %14, 4094
  %16 = lshr i32 %15, 1
  %17 = zext i32 %16 to i64
  %18 = call noundef nonnull align 2 dereferenceable(2) ptr @_ZNKSt5arrayItLm2048EEixEm(ptr noundef nonnull align 2 dereferenceable(4096) @_ZN8OpenLoco4Math6Vector4DataL20kFastSquareRootTableE, i64 noundef %17) #2
  %19 = load i16, ptr %18, align 2, !tbaa !12
  %20 = zext i16 %19 to i32
  %21 = load i8, ptr %3, align 1, !tbaa !9
  %22 = zext i8 %21 to i32
  %23 = ashr i32 %20, %22
  %24 = trunc i32 %23 to i16
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #2
  ret i16 %24
}

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 2 dereferenceable(2) ptr @_ZNKSt5arrayItLm2048EEixEm(ptr noundef nonnull align 2 dereferenceable(4096), i64 noundef) #0 align 2

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

attributes #0 = { mustprogress 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 = { 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++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}


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

%"struct.std::array" = type { [2048 x i16] }

@_ZN8OpenLoco4Math6Vector4DataL20kFastSquareRootTableE = external hidden constant %"struct.std::array", align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i16 @_ZN8OpenLoco4Math6Vector14fastSquareRootEj(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp ugt i32 %0, 4095
  br i1 %2, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1, %.lr.ph
  %.07 = phi i8 [ %3, %.lr.ph ], [ 10, %1 ]
  %.056 = phi i32 [ %4, %.lr.ph ], [ %0, %1 ]
  %3 = add nsw i8 %.07, -1
  %4 = lshr i32 %.056, 2
  %5 = icmp ugt i32 %.056, 16383
  br i1 %5, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !5

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %6 = zext nneg i8 %3 to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %1
  %.05.lcssa = phi i32 [ %0, %1 ], [ %4, %._crit_edge.loopexit ]
  %.0.lcssa = phi i32 [ 10, %1 ], [ %6, %._crit_edge.loopexit ]
  %7 = lshr i32 %.05.lcssa, 1
  %8 = zext nneg i32 %7 to i64
  %9 = tail call noundef nonnull align 2 dereferenceable(2) ptr @_ZNKSt5arrayItLm2048EEixEm(ptr noundef nonnull align 2 dereferenceable(4096) @_ZN8OpenLoco4Math6Vector4DataL20kFastSquareRootTableE, i64 noundef %8) #1
  %10 = load i16, ptr %9, align 2, !tbaa !7
  %11 = zext i16 %10 to i32
  %12 = lshr i32 %11, %.0.lcssa
  %13 = trunc nuw i32 %12 to i16
  ret i16 %13
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 2 dereferenceable(2) ptr @_ZNKSt5arrayItLm2048EEixEm(ptr noundef nonnull align 2 dereferenceable(4096), i64 noundef) local_unnamed_addr #0 align 2

attributes #0 = { mustprogress 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 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"short", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}


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