; LLVM IR from ARM-software/Arm-2D :: examples/[template][pc][vscode]/platform/math (arm_sin_q31)

; 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_q31 = external constant [513 x i32], align 16

; Function Attrs: nounwind uwtable
define dso_local i32 @arm_sin_q31(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = icmp slt i32 %8, 0
  br i1 %9, label %10, label %13

10:                                               ; preds = %1
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = add i32 %11, -2147483648
  store i32 %12, ptr %2, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %10, %1
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = lshr i32 %14, 22
  store i32 %15, ptr %4, align 4, !tbaa !5
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = shl i32 %17, 22
  %19 = sub nsw i32 %16, %18
  %20 = shl i32 %19, 9
  store i32 %20, ptr %7, align 4, !tbaa !5
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [513 x i32], ptr @sinTable_q31, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !5
  store i32 %24, ptr %5, align 4, !tbaa !5
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = add nsw i32 %25, 1
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [513 x i32], ptr @sinTable_q31, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  store i32 %29, ptr %6, align 4, !tbaa !5
  %30 = load i32, ptr %7, align 4, !tbaa !5
  %31 = sub i32 -2147483648, %30
  %32 = zext i32 %31 to i64
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = mul nsw i64 %32, %34
  %36 = ashr i64 %35, 32
  %37 = trunc i64 %36 to i32
  store i32 %37, ptr %3, align 4, !tbaa !5
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = shl i64 %39, 32
  %41 = load i32, ptr %7, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = load i32, ptr %6, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = mul nsw i64 %42, %44
  %46 = add nsw i64 %40, %45
  %47 = ashr i64 %46, 32
  %48 = trunc i64 %47 to i32
  store i32 %48, ptr %3, align 4, !tbaa !5
  %49 = load i32, ptr %3, align 4, !tbaa !5
  %50 = shl i32 %49, 1
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %50
}

; 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


; 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_q31 = external local_unnamed_addr constant [513 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local range(i32 0, -1) i32 @arm_sin_q31(i32 noundef %0) local_unnamed_addr #0 {
  %2 = lshr i32 %0, 22
  %3 = and i32 %2, 511
  %4 = shl i32 %0, 9
  %5 = and i32 %4, 2147483136
  %6 = zext nneg i32 %3 to i64
  %7 = getelementptr inbounds [513 x i32], ptr @sinTable_q31, i64 0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = add nuw nsw i32 %3, 1
  %10 = zext nneg i32 %9 to i64
  %11 = getelementptr inbounds [513 x i32], ptr @sinTable_q31, i64 0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = sub nuw i32 -2147483648, %5
  %14 = zext i32 %13 to i64
  %15 = sext i32 %8 to i64
  %16 = mul nsw i64 %15, %14
  %17 = and i64 %16, 9223372032559808512
  %18 = zext nneg i32 %5 to i64
  %19 = sext i32 %12 to i64
  %20 = mul nsw i64 %19, %18
  %21 = add i64 %17, %20
  %sh.diff = lshr i64 %21, 31
  %tr.sh.diff = trunc i64 %sh.diff to i32
  %22 = and i32 %tr.sh.diff, -2
  ret i32 %22
}

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 = !{!"int", !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