; LLVM IR from FedericaPaoli1/stm32-speech-recognition-and-traduction :: Drivers/CMSIS/DSP/Source/FastMathFunctions (arm_cos_q15)

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

@sinTable_q15 = external constant [513 x i16], align 16

; Function Attrs: nounwind uwtable
define dso_local signext i16 @arm_cos_q15(i16 noundef signext %0) #0 {
  %2 = alloca i16, align 2
  %3 = alloca i16, align 2
  %4 = alloca i32, align 4
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  %7 = alloca i16, align 2
  store i16 %0, ptr %2, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #2
  %8 = load i16, ptr %2, align 2, !tbaa !5
  %9 = zext i16 %8 to i32
  %10 = add nsw i32 %9, 8192
  %11 = trunc i32 %10 to i16
  store i16 %11, ptr %2, align 2, !tbaa !5
  %12 = load i16, ptr %2, align 2, !tbaa !5
  %13 = sext i16 %12 to i32
  %14 = icmp slt i32 %13, 0
  br i1 %14, label %15, label %20

15:                                               ; preds = %1
  %16 = load i16, ptr %2, align 2, !tbaa !5
  %17 = zext i16 %16 to i32
  %18 = add nsw i32 %17, 32768
  %19 = trunc i32 %18 to i16
  store i16 %19, ptr %2, align 2, !tbaa !5
  br label %20

20:                                               ; preds = %15, %1
  %21 = load i16, ptr %2, align 2, !tbaa !5
  %22 = sext i16 %21 to i32
  %23 = lshr i32 %22, 6
  store i32 %23, ptr %4, align 4, !tbaa !9
  %24 = load i16, ptr %2, align 2, !tbaa !5
  %25 = sext i16 %24 to i32
  %26 = load i32, ptr %4, align 4, !tbaa !9
  %27 = shl i32 %26, 6
  %28 = sub nsw i32 %25, %27
  %29 = shl i32 %28, 9
  %30 = trunc i32 %29 to i16
  store i16 %30, ptr %7, align 2, !tbaa !5
  %31 = load i32, ptr %4, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [513 x i16], ptr @sinTable_q15, i64 0, i64 %32
  %34 = load i16, ptr %33, align 2, !tbaa !5
  store i16 %34, ptr %5, align 2, !tbaa !5
  %35 = load i32, ptr %4, align 4, !tbaa !9
  %36 = add nsw i32 %35, 1
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [513 x i16], ptr @sinTable_q15, i64 0, i64 %37
  %39 = load i16, ptr %38, align 2, !tbaa !5
  store i16 %39, ptr %6, align 2, !tbaa !5
  %40 = load i16, ptr %7, align 2, !tbaa !5
  %41 = sext i16 %40 to i32
  %42 = sub nsw i32 32768, %41
  %43 = load i16, ptr %5, align 2, !tbaa !5
  %44 = sext i16 %43 to i32
  %45 = mul nsw i32 %42, %44
  %46 = ashr i32 %45, 16
  %47 = trunc i32 %46 to i16
  store i16 %47, ptr %3, align 2, !tbaa !5
  %48 = load i16, ptr %3, align 2, !tbaa !5
  %49 = sext i16 %48 to i32
  %50 = shl i32 %49, 16
  %51 = load i16, ptr %7, align 2, !tbaa !5
  %52 = sext i16 %51 to i32
  %53 = load i16, ptr %6, align 2, !tbaa !5
  %54 = sext i16 %53 to i32
  %55 = mul nsw i32 %52, %54
  %56 = add nsw i32 %50, %55
  %57 = ashr i32 %56, 16
  %58 = trunc i32 %57 to i16
  store i16 %58, ptr %3, align 2, !tbaa !5
  %59 = load i16, ptr %3, align 2, !tbaa !5
  %60 = sext i16 %59 to i32
  %61 = shl i32 %60, 1
  %62 = trunc i32 %61 to i16
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %3) #2
  ret i16 %62
}

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

; 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 = { 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/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"

@sinTable_q15 = external local_unnamed_addr constant [513 x i16], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local signext i16 @arm_cos_q15(i16 noundef signext %0) local_unnamed_addr #0 {
  %2 = add i16 %0, 8192
  %3 = icmp slt i16 %2, 0
  %4 = add i16 %0, -24576
  %spec.select = select i1 %3, i16 %4, i16 %2
  %5 = sext i16 %spec.select to i32
  %6 = lshr i32 %5, 6
  %7 = shl i16 %spec.select, 9
  %8 = and i16 %7, 32256
  %9 = zext nneg i32 %6 to i64
  %10 = getelementptr inbounds [513 x i16], ptr @sinTable_q15, i64 0, i64 %9
  %11 = load i16, ptr %10, align 2, !tbaa !5
  %12 = add nuw nsw i32 %6, 1
  %13 = zext nneg i32 %12 to i64
  %14 = getelementptr inbounds [513 x i16], ptr @sinTable_q15, i64 0, i64 %13
  %15 = load i16, ptr %14, align 2, !tbaa !5
  %16 = zext nneg i16 %8 to i32
  %17 = sub nuw nsw i32 32768, %16
  %18 = sext i16 %11 to i32
  %19 = mul nsw i32 %17, %18
  %20 = and i32 %19, 2147418112
  %21 = sext i16 %15 to i32
  %22 = mul nsw i32 %21, %16
  %23 = add i32 %20, %22
  %24 = lshr i32 %23, 15
  %25 = trunc i32 %24 to i16
  %26 = and i16 %25, -2
  ret i16 %26
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(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, !6, i64 0}
!6 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


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