; LLVM IR from Ryuk17/SpeechAlgorithms :: WebRTC_VAD/src (signal_processing_library)

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

; 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
declare dso_local signext i16 @WebRtcSpl_GetScalingSquare(ptr noundef, i64 noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @WebRtcSpl_Energy(ptr noundef %0, i64 noundef %1, ptr noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = load i64, ptr %5, align 8, !tbaa !9
  %14 = load i64, ptr %5, align 8, !tbaa !9
  %15 = call signext i16 @WebRtcSpl_GetScalingSquare(ptr noundef %12, i64 noundef %13, i64 noundef %14)
  %16 = sext i16 %15 to i32
  store i32 %16, ptr %9, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %17 = load i64, ptr %5, align 8, !tbaa !9
  store i64 %17, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %18, ptr %11, align 8, !tbaa !5
  store i64 0, ptr %8, align 8, !tbaa !9
  br label %19

19:                                               ; preds = %37, %3
  %20 = load i64, ptr %8, align 8, !tbaa !9
  %21 = load i64, ptr %10, align 8, !tbaa !9
  %22 = icmp ult i64 %20, %21
  br i1 %22, label %23, label %40

23:                                               ; preds = %19
  %24 = load ptr, ptr %11, align 8, !tbaa !5
  %25 = load i16, ptr %24, align 2, !tbaa !13
  %26 = sext i16 %25 to i32
  %27 = load ptr, ptr %11, align 8, !tbaa !5
  %28 = load i16, ptr %27, align 2, !tbaa !13
  %29 = sext i16 %28 to i32
  %30 = mul nsw i32 %26, %29
  %31 = load i32, ptr %9, align 4, !tbaa !11
  %32 = ashr i32 %30, %31
  %33 = load i32, ptr %7, align 4, !tbaa !11
  %34 = add nsw i32 %33, %32
  store i32 %34, ptr %7, align 4, !tbaa !11
  %35 = load ptr, ptr %11, align 8, !tbaa !5
  %36 = getelementptr inbounds i16, ptr %35, i32 1
  store ptr %36, ptr %11, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %23
  %38 = load i64, ptr %8, align 8, !tbaa !9
  %39 = add i64 %38, 1
  store i64 %39, ptr %8, align 8, !tbaa !9
  br label %19, !llvm.loop !15

40:                                               ; preds = %19
  %41 = load i32, ptr %9, align 4, !tbaa !11
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 %41, ptr %42, align 4, !tbaa !11
  %43 = load i32, ptr %7, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret i32 %43
}

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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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: nounwind uwtable
declare dso_local signext i16 @WebRtcSpl_GetScalingSquare(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @WebRtcSpl_Energy(ptr noundef %0, i64 noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = tail call signext i16 @WebRtcSpl_GetScalingSquare(ptr noundef %0, i64 noundef %1, i64 noundef %1)
  %5 = sext i16 %4 to i32
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %min.iters.check = icmp ult i64 %1, 8
  br i1 %min.iters.check, label %.lr.ph.preheader24, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %1, -8
  %6 = shl i64 %n.vec, 1
  %ind.end = getelementptr i8, ptr %0, i64 %6
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %5, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %14, %vector.body ]
  %vec.phi21 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %15, %vector.body ]
  %offset.idx = shl i64 %index, 1
  %next.gep = getelementptr i8, ptr %0, i64 %offset.idx
  %7 = getelementptr i8, ptr %next.gep, i64 8
  %wide.load = load <4 x i16>, ptr %next.gep, align 2, !tbaa !5
  %wide.load23 = load <4 x i16>, ptr %7, align 2, !tbaa !5
  %8 = sext <4 x i16> %wide.load to <4 x i32>
  %9 = sext <4 x i16> %wide.load23 to <4 x i32>
  %10 = mul nsw <4 x i32> %8, %8
  %11 = mul nsw <4 x i32> %9, %9
  %12 = lshr <4 x i32> %10, %broadcast.splat
  %13 = lshr <4 x i32> %11, %broadcast.splat
  %14 = add <4 x i32> %12, %vec.phi
  %15 = add <4 x i32> %13, %vec.phi21
  %index.next = add nuw i64 %index, 8
  %16 = icmp eq i64 %index.next, %n.vec
  br i1 %16, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %15, %14
  %17 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %n.vec, %1
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader24

.lr.ph.preheader24:                               ; preds = %middle.block, %.lr.ph.preheader
  %.019.ph = phi ptr [ %0, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.01518.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %17, %middle.block ]
  %.01617.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader24, %.lr.ph
  %.019 = phi ptr [ %23, %.lr.ph ], [ %.019.ph, %.lr.ph.preheader24 ]
  %.01518 = phi i32 [ %22, %.lr.ph ], [ %.01518.ph, %.lr.ph.preheader24 ]
  %.01617 = phi i64 [ %24, %.lr.ph ], [ %.01617.ph, %.lr.ph.preheader24 ]
  %18 = load i16, ptr %.019, align 2, !tbaa !5
  %19 = sext i16 %18 to i32
  %20 = mul nsw i32 %19, %19
  %21 = lshr i32 %20, %5
  %22 = add nuw nsw i32 %21, %.01518
  %23 = getelementptr inbounds i8, ptr %.019, i64 2
  %24 = add nuw i64 %.01617, 1
  %exitcond.not = icmp eq i64 %24, %1
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %3
  %.015.lcssa = phi i32 [ 0, %3 ], [ %17, %middle.block ], [ %22, %.lr.ph ]
  store i32 %5, ptr %2, align 4, !tbaa !14
  ret i32 %.015.lcssa
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #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 speculatable willreturn memory(none) }

!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 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !12, !11}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass