; LLVM IR from jgaeddert/liquid-dsp :: src/math/src (polyf)

; 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 i32 @polyf_findroots(ptr noundef %0, i32 noundef %1, ptr noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %11 = load i32, ptr %5, align 4, !tbaa !9
  %12 = zext i32 %11 to i64
  %13 = mul i64 %12, 8
  %14 = call noalias ptr @malloc(i64 noundef %13) #6
  store ptr %14, ptr %8, align 8, !tbaa !5
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %15

15:                                               ; preds = %30, %3
  %16 = load i32, ptr %7, align 4, !tbaa !9
  %17 = load i32, ptr %5, align 4, !tbaa !9
  %18 = icmp ult i32 %16, %17
  br i1 %18, label %19, label %33

19:                                               ; preds = %15
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = zext i32 %21 to i64
  %23 = getelementptr inbounds float, ptr %20, i64 %22
  %24 = load float, ptr %23, align 4, !tbaa !11
  %25 = fpext float %24 to double
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  %27 = load i32, ptr %7, align 4, !tbaa !9
  %28 = zext i32 %27 to i64
  %29 = getelementptr inbounds double, ptr %26, i64 %28
  store double %25, ptr %29, align 8, !tbaa !13
  br label %30

30:                                               ; preds = %19
  %31 = load i32, ptr %7, align 4, !tbaa !9
  %32 = add i32 %31, 1
  store i32 %32, ptr %7, align 4, !tbaa !9
  br label %15, !llvm.loop !15

33:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %34 = load i32, ptr %5, align 4, !tbaa !9
  %35 = sub i32 %34, 1
  %36 = zext i32 %35 to i64
  %37 = mul i64 %36, 16
  %38 = call noalias ptr @malloc(i64 noundef %37) #6
  store ptr %38, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = load i32, ptr %5, align 4, !tbaa !9
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  %42 = call i32 @liquid_poly_findroots_bairstow(ptr noundef %39, i32 noundef %40, ptr noundef %41)
  store i32 %42, ptr %10, align 4, !tbaa !9
  %43 = load ptr, ptr %9, align 8, !tbaa !5
  %44 = load i32, ptr %5, align 4, !tbaa !9
  %45 = sub i32 %44, 1
  %46 = zext i32 %45 to i64
  call void @qsort(ptr noundef %43, i64 noundef %46, i64 noundef 16, ptr noundef @liquid_poly_sort_roots_compare)
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %47

47:                                               ; preds = %69, %33
  %48 = load i32, ptr %7, align 4, !tbaa !9
  %49 = load i32, ptr %5, align 4, !tbaa !9
  %50 = sub i32 %49, 1
  %51 = icmp ult i32 %48, %50
  br i1 %51, label %52, label %72

52:                                               ; preds = %47
  %53 = load ptr, ptr %9, align 8, !tbaa !5
  %54 = load i32, ptr %7, align 4, !tbaa !9
  %55 = zext i32 %54 to i64
  %56 = getelementptr inbounds { double, double }, ptr %53, i64 %55
  %57 = getelementptr inbounds { double, double }, ptr %56, i32 0, i32 0
  %58 = load double, ptr %57, align 8
  %59 = getelementptr inbounds { double, double }, ptr %56, i32 0, i32 1
  %60 = load double, ptr %59, align 8
  %61 = fptrunc double %58 to float
  %62 = fptrunc double %60 to float
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = load i32, ptr %7, align 4, !tbaa !9
  %65 = zext i32 %64 to i64
  %66 = getelementptr inbounds { float, float }, ptr %63, i64 %65
  %67 = getelementptr inbounds { float, float }, ptr %66, i32 0, i32 0
  %68 = getelementptr inbounds { float, float }, ptr %66, i32 0, i32 1
  store float %61, ptr %67, align 4
  store float %62, ptr %68, align 4
  br label %69

69:                                               ; preds = %52
  %70 = load i32, ptr %7, align 4, !tbaa !9
  %71 = add i32 %70, 1
  store i32 %71, ptr %7, align 4, !tbaa !9
  br label %47, !llvm.loop !17

72:                                               ; preds = %47
  %73 = load ptr, ptr %8, align 8, !tbaa !5
  call void @free(ptr noundef %73) #5
  %74 = load ptr, ptr %9, align 8, !tbaa !5
  call void @free(ptr noundef %74) #5
  %75 = load i32, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  ret i32 %75
}

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

declare i32 @liquid_poly_findroots_bairstow(ptr noundef, i32 noundef, ptr noundef) #3

declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #3

declare i32 @liquid_poly_sort_roots_compare(ptr noundef, ptr noundef) #3

; Function Attrs: nounwind
declare void @free(ptr noundef) #4

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 allocsize(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 = { "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 #4 = { nounwind "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 #5 = { nounwind }
attributes #6 = { nounwind allocsize(0) }

!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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"float", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"double", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


; 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 i32 @polyf_findroots(ptr nocapture noundef readonly %0, i32 noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = zext i32 %1 to i64
  %5 = shl nuw nsw i64 %4, 3
  %6 = tail call noalias ptr @malloc(i64 noundef %5) #5
  %.not = icmp eq i32 %1, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %min.iters.check = icmp ult i32 %1, 4
  br i1 %min.iters.check, label %.lr.ph.preheader54, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %4, 4294967292
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %7 = getelementptr inbounds float, ptr %0, i64 %index
  %8 = getelementptr inbounds i8, ptr %7, i64 8
  %wide.load = load <2 x float>, ptr %7, align 4, !tbaa !5
  %wide.load36 = load <2 x float>, ptr %8, align 4, !tbaa !5
  %9 = fpext <2 x float> %wide.load to <2 x double>
  %10 = fpext <2 x float> %wide.load36 to <2 x double>
  %11 = getelementptr inbounds double, ptr %6, i64 %index
  %12 = getelementptr inbounds i8, ptr %11, i64 16
  store <2 x double> %9, ptr %11, align 8, !tbaa !9
  store <2 x double> %10, ptr %12, align 8, !tbaa !9
  %index.next = add nuw i64 %index, 4
  %13 = icmp eq i64 %index.next, %n.vec
  br i1 %13, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %4
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader54

.lr.ph.preheader54:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader54, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader54 ]
  %14 = getelementptr inbounds float, ptr %0, i64 %indvars.iv
  %15 = load float, ptr %14, align 4, !tbaa !5
  %16 = fpext float %15 to double
  %17 = getelementptr inbounds double, ptr %6, i64 %indvars.iv
  store double %16, ptr %17, align 8, !tbaa !9
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %4
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !15

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %3
  %18 = add i32 %1, -1
  %19 = zext i32 %18 to i64
  %20 = shl nuw nsw i64 %19, 4
  %21 = tail call noalias ptr @malloc(i64 noundef %20) #5
  %22 = tail call i32 @liquid_poly_findroots_bairstow(ptr noundef %6, i32 noundef %1, ptr noundef %21) #6
  tail call void @qsort(ptr noundef %21, i64 noundef %19, i64 noundef 16, ptr noundef nonnull @liquid_poly_sort_roots_compare) #6
  %.not29 = icmp eq i32 %18, 0
  br i1 %.not29, label %._crit_edge28, label %.lr.ph27.preheader

.lr.ph27.preheader:                               ; preds = %._crit_edge
  %min.iters.check39 = icmp ult i32 %18, 4
  br i1 %min.iters.check39, label %.lr.ph27.preheader53, label %vector.ph40

vector.ph40:                                      ; preds = %.lr.ph27.preheader
  %n.vec42 = and i64 %19, 4294967292
  br label %vector.body44

vector.body44:                                    ; preds = %vector.body44, %vector.ph40
  %index45 = phi i64 [ 0, %vector.ph40 ], [ %index.next51, %vector.body44 ]
  %23 = or disjoint i64 %index45, 2
  %24 = getelementptr inbounds { double, double }, ptr %21, i64 %index45
  %25 = getelementptr inbounds { double, double }, ptr %21, i64 %23
  %26 = load <4 x double>, ptr %24, align 8
  %27 = load <4 x double>, ptr %25, align 8
  %28 = getelementptr inbounds { float, float }, ptr %2, i64 %index45
  %29 = getelementptr inbounds { float, float }, ptr %2, i64 %23
  %interleaved.vec = fptrunc <4 x double> %26 to <4 x float>
  store <4 x float> %interleaved.vec, ptr %28, align 4
  %interleaved.vec50 = fptrunc <4 x double> %27 to <4 x float>
  store <4 x float> %interleaved.vec50, ptr %29, align 4
  %index.next51 = add nuw i64 %index45, 4
  %30 = icmp eq i64 %index.next51, %n.vec42
  br i1 %30, label %middle.block37, label %vector.body44, !llvm.loop !16

middle.block37:                                   ; preds = %vector.body44
  %cmp.n52 = icmp eq i64 %n.vec42, %19
  br i1 %cmp.n52, label %._crit_edge28, label %.lr.ph27.preheader53

.lr.ph27.preheader53:                             ; preds = %middle.block37, %.lr.ph27.preheader
  %indvars.iv31.ph = phi i64 [ 0, %.lr.ph27.preheader ], [ %n.vec42, %middle.block37 ]
  br label %.lr.ph27

.lr.ph27:                                         ; preds = %.lr.ph27.preheader53, %.lr.ph27
  %indvars.iv31 = phi i64 [ %indvars.iv.next32, %.lr.ph27 ], [ %indvars.iv31.ph, %.lr.ph27.preheader53 ]
  %31 = getelementptr inbounds { double, double }, ptr %21, i64 %indvars.iv31
  %32 = getelementptr inbounds { float, float }, ptr %2, i64 %indvars.iv31
  %33 = load <2 x double>, ptr %31, align 8
  %34 = fptrunc <2 x double> %33 to <2 x float>
  store <2 x float> %34, ptr %32, align 4
  %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1
  %exitcond35.not = icmp eq i64 %indvars.iv.next32, %19
  br i1 %exitcond35.not, label %._crit_edge28, label %.lr.ph27, !llvm.loop !17

._crit_edge28:                                    ; preds = %.lr.ph27, %middle.block37, %._crit_edge
  tail call void @free(ptr noundef %6) #6
  tail call void @free(ptr noundef %21) #6
  ret i32 %22
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

declare i32 @liquid_poly_findroots_bairstow(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nofree
declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #3

declare i32 @liquid_poly_sort_roots_compare(ptr noundef, ptr noundef) #2

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #4

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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { "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 = { nofree "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 #4 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 #5 = { nounwind allocsize(0) }
attributes #6 = { 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 = !{!"float", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = distinct !{!11, !12, !13, !14}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !12, !14, !13}
!16 = distinct !{!16, !12, !13, !14}
!17 = distinct !{!17, !12, !14, !13}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass,VectorCombinePass