; LLVM IR from OpenLoco/OpenLoco :: src/Math/src/Trigonometry.ll (ZN8OpenLoco4Math12Trigonometry24integerSinePrecisionHighEti)

; 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 { [4096 x i16] }

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_ZN8OpenLoco4Math12Trigonometry24integerSinePrecisionHighEti(i16 noundef zeroext %0, i32 noundef %1) #0 {
  %3 = alloca i16, align 2
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store i16 %0, ptr %3, align 2, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %7 = load i16, ptr %3, align 2, !tbaa !5
  %8 = zext i16 %7 to i32
  %9 = and i32 %8, 4096
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %11, label %15

11:                                               ; preds = %2
  %12 = load i16, ptr %3, align 2, !tbaa !5
  %13 = zext i16 %12 to i32
  %14 = xor i32 %13, -1
  br label %18

15:                                               ; preds = %2
  %16 = load i16, ptr %3, align 2, !tbaa !5
  %17 = zext i16 %16 to i32
  br label %18

18:                                               ; preds = %15, %11
  %19 = phi i32 [ %14, %11 ], [ %17, %15 ]
  %20 = and i32 %19, 4095
  store i32 %20, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %21 = load i16, ptr %3, align 2, !tbaa !5
  %22 = zext i16 %21 to i32
  %23 = and i32 %22, 8192
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %32

25:                                               ; preds = %18
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = call noundef nonnull align 2 dereferenceable(2) ptr @_ZNKSt5arrayIsLm4096EEixEm(ptr noundef nonnull align 2 dereferenceable(8192) @_ZN8OpenLoco4Math12Trigonometry4DataL12kQuarterSineE, i64 noundef %27) #2
  %29 = load i16, ptr %28, align 2, !tbaa !5
  %30 = sext i16 %29 to i32
  %31 = xor i32 %30, -1
  br label %38

32:                                               ; preds = %18
  %33 = load i32, ptr %5, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = call noundef nonnull align 2 dereferenceable(2) ptr @_ZNKSt5arrayIsLm4096EEixEm(ptr noundef nonnull align 2 dereferenceable(8192) @_ZN8OpenLoco4Math12Trigonometry4DataL12kQuarterSineE, i64 noundef %34) #2
  %36 = load i16, ptr %35, align 2, !tbaa !5
  %37 = sext i16 %36 to i32
  br label %38

38:                                               ; preds = %32, %25
  %39 = phi i32 [ %31, %25 ], [ %37, %32 ]
  store i32 %39, ptr %6, align 4, !tbaa !9
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = load i32, ptr %4, align 4, !tbaa !9
  %42 = mul nsw i32 %40, %41
  %43 = sdiv i32 %42, 32768
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %43
}

; 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 @_ZNKSt5arrayIsLm4096EEixEm(ptr noundef nonnull align 2 dereferenceable(8192), 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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !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 { [4096 x i16] }

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef range(i32 -65536, 65536) i32 @_ZN8OpenLoco4Math12Trigonometry24integerSinePrecisionHighEti(i16 noundef zeroext %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = zext i16 %0 to i32
  %4 = shl i32 %3, 19
  %sext = ashr i32 %4, 31
  %5 = xor i32 %sext, %3
  %6 = and i32 %5, 4095
  %7 = zext nneg i32 %6 to i64
  %8 = tail call noundef nonnull align 2 dereferenceable(2) ptr @_ZNKSt5arrayIsLm4096EEixEm(ptr noundef nonnull align 2 dereferenceable(8192) @_ZN8OpenLoco4Math12Trigonometry4DataL12kQuarterSineE, i64 noundef %7) #1
  %9 = load i16, ptr %8, align 2, !tbaa !5
  %10 = shl i32 %3, 18
  %sext8 = ashr i32 %10, 31
  %11 = trunc nsw i32 %sext8 to i16
  %.in = xor i16 %9, %11
  %12 = sext i16 %.in to i32
  %13 = mul nsw i32 %12, %1
  %14 = sdiv i32 %13, 32768
  ret i32 %14
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 2 dereferenceable(2) ptr @_ZNKSt5arrayIsLm4096EEixEm(ptr noundef nonnull align 2 dereferenceable(8192), 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 = !{!6, !6, i64 0}
!6 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


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