; 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
define dso_local signext i16 @WebRtcSpl_GetScalingSquare(ptr noundef %0, i64 noundef %1, i64 noundef %2) #1 {
  %4 = alloca i16, align 2
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i16, align 2
  %9 = alloca i64, align 8
  %10 = alloca i16, align 2
  %11 = alloca i16, align 2
  %12 = alloca ptr, align 8
  %13 = alloca i16, align 2
  %14 = alloca i64, align 8
  %15 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #3
  %16 = load i64, ptr %7, align 8, !tbaa !9
  %17 = trunc i64 %16 to i32
  %18 = call signext i16 @WebRtcSpl_GetSizeInBits(i32 noundef %17)
  store i16 %18, ptr %8, align 2, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #3
  store i16 -1, ptr %10, align 2, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %19, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  %20 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %20, ptr %14, align 8, !tbaa !9
  %21 = load i64, ptr %14, align 8, !tbaa !9
  store i64 %21, ptr %9, align 8, !tbaa !9
  br label %22

22:                                               ; preds = %58, %3
  %23 = load i64, ptr %9, align 8, !tbaa !9
  %24 = icmp ugt i64 %23, 0
  br i1 %24, label %25, label %61

25:                                               ; preds = %22
  %26 = load ptr, ptr %12, align 8, !tbaa !5
  %27 = load i16, ptr %26, align 2, !tbaa !11
  %28 = sext i16 %27 to i32
  %29 = icmp sgt i32 %28, 0
  br i1 %29, label %30, label %35

30:                                               ; preds = %25
  %31 = load ptr, ptr %12, align 8, !tbaa !5
  %32 = getelementptr inbounds i16, ptr %31, i32 1
  store ptr %32, ptr %12, align 8, !tbaa !5
  %33 = load i16, ptr %31, align 2, !tbaa !11
  %34 = sext i16 %33 to i32
  br label %41

35:                                               ; preds = %25
  %36 = load ptr, ptr %12, align 8, !tbaa !5
  %37 = getelementptr inbounds i16, ptr %36, i32 1
  store ptr %37, ptr %12, align 8, !tbaa !5
  %38 = load i16, ptr %36, align 2, !tbaa !11
  %39 = sext i16 %38 to i32
  %40 = sub nsw i32 0, %39
  br label %41

41:                                               ; preds = %35, %30
  %42 = phi i32 [ %34, %30 ], [ %40, %35 ]
  %43 = trunc i32 %42 to i16
  store i16 %43, ptr %11, align 2, !tbaa !11
  %44 = load i16, ptr %11, align 2, !tbaa !11
  %45 = sext i16 %44 to i32
  %46 = load i16, ptr %10, align 2, !tbaa !11
  %47 = sext i16 %46 to i32
  %48 = icmp sgt i32 %45, %47
  br i1 %48, label %49, label %52

49:                                               ; preds = %41
  %50 = load i16, ptr %11, align 2, !tbaa !11
  %51 = sext i16 %50 to i32
  br label %55

52:                                               ; preds = %41
  %53 = load i16, ptr %10, align 2, !tbaa !11
  %54 = sext i16 %53 to i32
  br label %55

55:                                               ; preds = %52, %49
  %56 = phi i32 [ %51, %49 ], [ %54, %52 ]
  %57 = trunc i32 %56 to i16
  store i16 %57, ptr %10, align 2, !tbaa !11
  br label %58

58:                                               ; preds = %55
  %59 = load i64, ptr %9, align 8, !tbaa !9
  %60 = add i64 %59, -1
  store i64 %60, ptr %9, align 8, !tbaa !9
  br label %22, !llvm.loop !13

61:                                               ; preds = %22
  %62 = load i16, ptr %10, align 2, !tbaa !11
  %63 = sext i16 %62 to i32
  %64 = load i16, ptr %10, align 2, !tbaa !11
  %65 = sext i16 %64 to i32
  %66 = mul nsw i32 %63, %65
  %67 = call signext i16 @WebRtcSpl_NormW32(i32 noundef %66)
  store i16 %67, ptr %13, align 2, !tbaa !11
  %68 = load i16, ptr %10, align 2, !tbaa !11
  %69 = sext i16 %68 to i32
  %70 = icmp eq i32 %69, 0
  br i1 %70, label %71, label %72

71:                                               ; preds = %61
  store i16 0, ptr %4, align 2
  store i32 1, ptr %15, align 4
  br label %88

72:                                               ; preds = %61
  %73 = load i16, ptr %13, align 2, !tbaa !11
  %74 = sext i16 %73 to i32
  %75 = load i16, ptr %8, align 2, !tbaa !11
  %76 = sext i16 %75 to i32
  %77 = icmp sgt i32 %74, %76
  br i1 %77, label %78, label %79

78:                                               ; preds = %72
  br label %85

79:                                               ; preds = %72
  %80 = load i16, ptr %8, align 2, !tbaa !11
  %81 = sext i16 %80 to i32
  %82 = load i16, ptr %13, align 2, !tbaa !11
  %83 = sext i16 %82 to i32
  %84 = sub nsw i32 %81, %83
  br label %85

85:                                               ; preds = %79, %78
  %86 = phi i32 [ 0, %78 ], [ %84, %79 ]
  %87 = trunc i32 %86 to i16
  store i16 %87, ptr %4, align 2
  store i32 1, ptr %15, align 4
  br label %88

88:                                               ; preds = %85, %71
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #3
  %89 = load i16, ptr %4, align 2
  ret i16 %89
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden signext i16 @WebRtcSpl_GetSizeInBits(i32 noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden signext i16 @WebRtcSpl_NormW32(i32 noundef) #2

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 = { inlinehint 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 #3 = { 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 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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
define dso_local signext i16 @WebRtcSpl_GetScalingSquare(ptr nocapture noundef readonly %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = trunc i64 %2 to i32
  %5 = tail call signext i16 @WebRtcSpl_GetSizeInBits(i32 noundef %4)
  %.not25 = icmp eq i64 %1, 0
  br i1 %.not25, label %._crit_edge, label %.lr.ph.preheader

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

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %1, -16
  %ind.end = and i64 %1, 15
  %6 = shl i64 %n.vec, 1
  %ind.end29 = getelementptr i8, ptr %0, i64 %6
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <8 x i16> [ <i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1>, %vector.ph ], [ %10, %vector.body ]
  %vec.phi31 = phi <8 x i16> [ <i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1>, %vector.ph ], [ %11, %vector.body ]
  %offset.idx = shl i64 %index, 1
  %next.gep = getelementptr i8, ptr %0, i64 %offset.idx
  %7 = getelementptr i8, ptr %next.gep, i64 16
  %wide.load = load <8 x i16>, ptr %next.gep, align 2, !tbaa !5
  %wide.load33 = load <8 x i16>, ptr %7, align 2, !tbaa !5
  %8 = tail call <8 x i16> @llvm.abs.v8i16(<8 x i16> %wide.load, i1 false)
  %9 = tail call <8 x i16> @llvm.abs.v8i16(<8 x i16> %wide.load33, i1 false)
  %10 = tail call <8 x i16> @llvm.smax.v8i16(<8 x i16> %8, <8 x i16> %vec.phi)
  %11 = tail call <8 x i16> @llvm.smax.v8i16(<8 x i16> %9, <8 x i16> %vec.phi31)
  %index.next = add nuw i64 %index, 16
  %12 = icmp eq i64 %index.next, %n.vec
  br i1 %12, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %rdx.minmax = tail call <8 x i16> @llvm.smax.v8i16(<8 x i16> %10, <8 x i16> %11)
  %13 = tail call i16 @llvm.vector.reduce.smax.v8i16(<8 x i16> %rdx.minmax)
  %cmp.n = icmp eq i64 %n.vec, %1
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader34

.lr.ph.preheader34:                               ; preds = %middle.block, %.lr.ph.preheader
  %.02028.ph = phi i64 [ %1, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.02127.ph = phi ptr [ %0, %.lr.ph.preheader ], [ %ind.end29, %middle.block ]
  %.02226.ph = phi i16 [ -1, %.lr.ph.preheader ], [ %13, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader34, %.lr.ph
  %.02028 = phi i64 [ %17, %.lr.ph ], [ %.02028.ph, %.lr.ph.preheader34 ]
  %.02127 = phi ptr [ %.1, %.lr.ph ], [ %.02127.ph, %.lr.ph.preheader34 ]
  %.02226 = phi i16 [ %16, %.lr.ph ], [ %.02226.ph, %.lr.ph.preheader34 ]
  %14 = load i16, ptr %.02127, align 2, !tbaa !5
  %15 = tail call i16 @llvm.abs.i16(i16 %14, i1 false)
  %.1 = getelementptr inbounds i8, ptr %.02127, i64 2
  %16 = tail call i16 @llvm.smax.i16(i16 %15, i16 %.02226)
  %17 = add i64 %.02028, -1
  %.not = icmp eq i64 %17, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %3
  %.022.lcssa = phi i16 [ -1, %3 ], [ %13, %middle.block ], [ %16, %.lr.ph ]
  %18 = sext i16 %.022.lcssa to i32
  %19 = mul nsw i32 %18, %18
  %20 = tail call signext i16 @WebRtcSpl_NormW32(i32 noundef %19)
  %21 = icmp eq i16 %.022.lcssa, 0
  %22 = icmp sgt i16 %20, %5
  %23 = sub i16 %5, %20
  %24 = select i1 %21, i1 true, i1 %22
  %.0 = select i1 %24, i16 0, i16 %23
  ret i16 %.0
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden signext i16 @WebRtcSpl_GetSizeInBits(i32 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden signext i16 @WebRtcSpl_NormW32(i32 noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.smax.i16(i16, i16) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.abs.i16(i16, i1 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <8 x i16> @llvm.abs.v8i16(<8 x i16>, i1 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <8 x i16> @llvm.smax.v8i16(<8 x i16>, <8 x i16>) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.vector.reduce.smax.v8i16(<8 x i16>) #2

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 = { inlinehint 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 = { 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}


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