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

; 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 speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #1

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @poly_expandroots(ptr noundef, i32 noundef, ptr noundef) #3

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

19:                                               ; preds = %28, %4
  %20 = load i32, ptr %10, align 4, !tbaa !9
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = icmp ult i32 %20, %21
  br i1 %22, label %23, label %31

23:                                               ; preds = %19
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = load i32, ptr %10, align 4, !tbaa !9
  %26 = zext i32 %25 to i64
  %27 = getelementptr inbounds double, ptr %24, i64 %26
  store double 0.000000e+00, ptr %27, align 8, !tbaa !11
  br label %28

28:                                               ; preds = %23
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = add i32 %29, 1
  store i32 %30, ptr %10, align 4, !tbaa !9
  br label %19, !llvm.loop !13

31:                                               ; preds = %19
  %32 = load i32, ptr %9, align 4, !tbaa !9
  %33 = zext i32 %32 to i64
  %34 = call ptr @llvm.stacksave.p0()
  store ptr %34, ptr %11, align 8
  %35 = alloca double, i64 %33, align 16
  store i64 %33, ptr %12, align 8
  %36 = load i32, ptr %7, align 4, !tbaa !9
  %37 = zext i32 %36 to i64
  %38 = alloca double, i64 %37, align 16
  store i64 %37, ptr %13, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #4
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %109, %31
  %40 = load i32, ptr %10, align 4, !tbaa !9
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = icmp ult i32 %40, %41
  br i1 %42, label %43, label %112

43:                                               ; preds = %39
  store i32 0, ptr %16, align 4, !tbaa !9
  store double 1.000000e+00, ptr %14, align 8, !tbaa !11
  store i32 0, ptr %15, align 4, !tbaa !9
  br label %44

44:                                               ; preds = %76, %43
  %45 = load i32, ptr %15, align 4, !tbaa !9
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = icmp ult i32 %45, %46
  br i1 %47, label %48, label %79

48:                                               ; preds = %44
  %49 = load i32, ptr %15, align 4, !tbaa !9
  %50 = load i32, ptr %10, align 4, !tbaa !9
  %51 = icmp ne i32 %49, %50
  br i1 %51, label %52, label %75

52:                                               ; preds = %48
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = load i32, ptr %15, align 4, !tbaa !9
  %55 = zext i32 %54 to i64
  %56 = getelementptr inbounds double, ptr %53, i64 %55
  %57 = load double, ptr %56, align 8, !tbaa !11
  %58 = load i32, ptr %16, align 4, !tbaa !9
  %59 = add i32 %58, 1
  store i32 %59, ptr %16, align 4, !tbaa !9
  %60 = zext i32 %58 to i64
  %61 = getelementptr inbounds double, ptr %35, i64 %60
  store double %57, ptr %61, align 8, !tbaa !11
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = load i32, ptr %10, align 4, !tbaa !9
  %64 = zext i32 %63 to i64
  %65 = getelementptr inbounds double, ptr %62, i64 %64
  %66 = load double, ptr %65, align 8, !tbaa !11
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = load i32, ptr %15, align 4, !tbaa !9
  %69 = zext i32 %68 to i64
  %70 = getelementptr inbounds double, ptr %67, i64 %69
  %71 = load double, ptr %70, align 8, !tbaa !11
  %72 = fsub double %66, %71
  %73 = load double, ptr %14, align 8, !tbaa !11
  %74 = fmul double %73, %72
  store double %74, ptr %14, align 8, !tbaa !11
  br label %75

75:                                               ; preds = %52, %48
  br label %76

76:                                               ; preds = %75
  %77 = load i32, ptr %15, align 4, !tbaa !9
  %78 = add i32 %77, 1
  store i32 %78, ptr %15, align 4, !tbaa !9
  br label %44, !llvm.loop !15

79:                                               ; preds = %44
  %80 = load ptr, ptr %6, align 8, !tbaa !5
  %81 = load i32, ptr %10, align 4, !tbaa !9
  %82 = zext i32 %81 to i64
  %83 = getelementptr inbounds double, ptr %80, i64 %82
  %84 = load double, ptr %83, align 8, !tbaa !11
  %85 = load double, ptr %14, align 8, !tbaa !11
  %86 = fdiv double %84, %85
  store double %86, ptr %14, align 8, !tbaa !11
  %87 = load i32, ptr %9, align 4, !tbaa !9
  %88 = call i32 @poly_expandroots(ptr noundef %35, i32 noundef %87, ptr noundef %38)
  store i32 0, ptr %15, align 4, !tbaa !9
  br label %89

89:                                               ; preds = %105, %79
  %90 = load i32, ptr %15, align 4, !tbaa !9
  %91 = load i32, ptr %7, align 4, !tbaa !9
  %92 = icmp ult i32 %90, %91
  br i1 %92, label %93, label %108

93:                                               ; preds = %89
  %94 = load double, ptr %14, align 8, !tbaa !11
  %95 = load i32, ptr %15, align 4, !tbaa !9
  %96 = zext i32 %95 to i64
  %97 = getelementptr inbounds double, ptr %38, i64 %96
  %98 = load double, ptr %97, align 8, !tbaa !11
  %99 = load ptr, ptr %8, align 8, !tbaa !5
  %100 = load i32, ptr %15, align 4, !tbaa !9
  %101 = zext i32 %100 to i64
  %102 = getelementptr inbounds double, ptr %99, i64 %101
  %103 = load double, ptr %102, align 8, !tbaa !11
  %104 = call double @llvm.fmuladd.f64(double %94, double %98, double %103)
  store double %104, ptr %102, align 8, !tbaa !11
  br label %105

105:                                              ; preds = %93
  %106 = load i32, ptr %15, align 4, !tbaa !9
  %107 = add i32 %106, 1
  store i32 %107, ptr %15, align 4, !tbaa !9
  br label %89, !llvm.loop !16

108:                                              ; preds = %89
  br label %109

109:                                              ; preds = %108
  %110 = load i32, ptr %10, align 4, !tbaa !9
  %111 = add i32 %110, 1
  store i32 %111, ptr %10, align 4, !tbaa !9
  br label %39, !llvm.loop !17

112:                                              ; preds = %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  %113 = load ptr, ptr %11, align 8
  call void @llvm.stackrestore.p0(ptr %113)
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  ret i32 0
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nosync nounwind willreturn }
attributes #3 = { 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 #4 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}
!17 = distinct !{!17, !14}


; 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: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @poly_expandroots(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @poly_fit_lagrange(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i32 noundef %2, ptr nocapture noundef %3) local_unnamed_addr #1 {
  %5 = add i32 %2, -1
  %.not49 = icmp eq i32 %2, 0
  br i1 %.not49, label %._crit_edge48, label %.preheader.preheader

.preheader.preheader:                             ; preds = %4
  %6 = zext i32 %2 to i64
  %7 = shl nuw nsw i64 %6, 3
  tail call void @llvm.memset.p0.i64(ptr align 8 %3, i8 0, i64 %7, i1 false), !tbaa !5
  %8 = zext i32 %5 to i64
  %9 = alloca double, i64 %8, align 16
  %10 = alloca double, i64 %6, align 16
  %xtraiter = and i64 %6, 1
  %11 = icmp eq i32 %2, 1
  %unroll_iter = and i64 %6, 4294967294
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  %min.iters.check = icmp ult i32 %2, 4
  %n.vec = and i64 %6, 4294967292
  %cmp.n = icmp eq i64 %n.vec, %6
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %.loopexit
  %indvars.iv58 = phi i64 [ 0, %.preheader.preheader ], [ %indvars.iv.next59, %.loopexit ]
  %12 = getelementptr inbounds double, ptr %0, i64 %indvars.iv58
  br i1 %11, label %.unr-lcssa, label %.preheader.new

.preheader.new:                                   ; preds = %.preheader, %32
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %32 ], [ 0, %.preheader ]
  %.045 = phi i32 [ %.1.1, %32 ], [ 0, %.preheader ]
  %.03743 = phi double [ %.138.1, %32 ], [ 1.000000e+00, %.preheader ]
  %niter = phi i64 [ %niter.next.1, %32 ], [ 0, %.preheader ]
  %.not = icmp eq i64 %indvars.iv, %indvars.iv58
  br i1 %.not, label %22, label %13

13:                                               ; preds = %.preheader.new
  %14 = getelementptr inbounds double, ptr %0, i64 %indvars.iv
  %15 = load double, ptr %14, align 8, !tbaa !5
  %16 = add i32 %.045, 1
  %17 = zext i32 %.045 to i64
  %18 = getelementptr inbounds double, ptr %9, i64 %17
  store double %15, ptr %18, align 8, !tbaa !5
  %19 = load double, ptr %12, align 8, !tbaa !5
  %20 = fsub double %19, %15
  %21 = fmul double %.03743, %20
  br label %22

22:                                               ; preds = %.preheader.new, %13
  %.138 = phi double [ %21, %13 ], [ %.03743, %.preheader.new ]
  %.1 = phi i32 [ %16, %13 ], [ %.045, %.preheader.new ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %.not.1 = icmp eq i64 %indvars.iv.next, %indvars.iv58
  br i1 %.not.1, label %32, label %23

23:                                               ; preds = %22
  %24 = getelementptr inbounds double, ptr %0, i64 %indvars.iv.next
  %25 = load double, ptr %24, align 8, !tbaa !5
  %26 = add i32 %.1, 1
  %27 = zext i32 %.1 to i64
  %28 = getelementptr inbounds double, ptr %9, i64 %27
  store double %25, ptr %28, align 8, !tbaa !5
  %29 = load double, ptr %12, align 8, !tbaa !5
  %30 = fsub double %29, %25
  %31 = fmul double %.138, %30
  br label %32

32:                                               ; preds = %23, %22
  %.138.1 = phi double [ %31, %23 ], [ %.138, %22 ]
  %.1.1 = phi i32 [ %26, %23 ], [ %.1, %22 ]
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.unr-lcssa, label %.preheader.new, !llvm.loop !9

.unr-lcssa:                                       ; preds = %32, %.preheader
  %.138.lcssa.ph = phi double [ poison, %.preheader ], [ %.138.1, %32 ]
  %indvars.iv.unr = phi i64 [ 0, %.preheader ], [ %indvars.iv.next.1, %32 ]
  %.045.unr = phi i32 [ 0, %.preheader ], [ %.1.1, %32 ]
  %.03743.unr = phi double [ 1.000000e+00, %.preheader ], [ %.138.1, %32 ]
  br i1 %lcmp.mod.not, label %.epilog-lcssa, label %.epil.preheader

.epil.preheader:                                  ; preds = %.unr-lcssa
  %.not.epil = icmp eq i64 %indvars.iv.unr, %indvars.iv58
  br i1 %.not.epil, label %.epilog-lcssa, label %33

33:                                               ; preds = %.epil.preheader
  %34 = getelementptr inbounds double, ptr %0, i64 %indvars.iv.unr
  %35 = load double, ptr %34, align 8, !tbaa !5
  %36 = zext i32 %.045.unr to i64
  %37 = getelementptr inbounds double, ptr %9, i64 %36
  store double %35, ptr %37, align 8, !tbaa !5
  %38 = load double, ptr %12, align 8, !tbaa !5
  %39 = fsub double %38, %35
  %40 = fmul double %.03743.unr, %39
  br label %.epilog-lcssa

.epilog-lcssa:                                    ; preds = %.epil.preheader, %33, %.unr-lcssa
  %.138.lcssa = phi double [ %.138.lcssa.ph, %.unr-lcssa ], [ %40, %33 ], [ %.03743.unr, %.epil.preheader ]
  %41 = getelementptr inbounds double, ptr %1, i64 %indvars.iv58
  %42 = load double, ptr %41, align 8, !tbaa !5
  %43 = fdiv double %42, %.138.lcssa
  %44 = call i32 @poly_expandroots(ptr noundef nonnull %9, i32 noundef %5, ptr noundef nonnull %10)
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.epilog-lcssa
  %broadcast.splatinsert = insertelement <2 x double> poison, double %43, i64 0
  %broadcast.splat = shufflevector <2 x double> %broadcast.splatinsert, <2 x double> poison, <2 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %45 = getelementptr inbounds double, ptr %10, i64 %index
  %46 = getelementptr inbounds i8, ptr %45, i64 16
  %wide.load = load <2 x double>, ptr %45, align 16, !tbaa !5
  %wide.load64 = load <2 x double>, ptr %46, align 16, !tbaa !5
  %47 = getelementptr inbounds double, ptr %3, i64 %index
  %48 = getelementptr inbounds i8, ptr %47, i64 16
  %wide.load65 = load <2 x double>, ptr %47, align 8, !tbaa !5
  %wide.load66 = load <2 x double>, ptr %48, align 8, !tbaa !5
  %49 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %broadcast.splat, <2 x double> %wide.load, <2 x double> %wide.load65)
  %50 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %broadcast.splat, <2 x double> %wide.load64, <2 x double> %wide.load66)
  store <2 x double> %49, ptr %47, align 8, !tbaa !5
  store <2 x double> %50, ptr %48, align 8, !tbaa !5
  %index.next = add nuw i64 %index, 4
  %51 = icmp eq i64 %index.next, %n.vec
  br i1 %51, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  br i1 %cmp.n, label %.loopexit, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.epilog-lcssa
  %indvars.iv53.ph = phi i64 [ 0, %.epilog-lcssa ], [ %n.vec, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv53 = phi i64 [ %indvars.iv.next54, %scalar.ph ], [ %indvars.iv53.ph, %scalar.ph.preheader ]
  %52 = getelementptr inbounds double, ptr %10, i64 %indvars.iv53
  %53 = load double, ptr %52, align 8, !tbaa !5
  %54 = getelementptr inbounds double, ptr %3, i64 %indvars.iv53
  %55 = load double, ptr %54, align 8, !tbaa !5
  %56 = call double @llvm.fmuladd.f64(double %43, double %53, double %55)
  store double %56, ptr %54, align 8, !tbaa !5
  %indvars.iv.next54 = add nuw nsw i64 %indvars.iv53, 1
  %exitcond57.not = icmp eq i64 %indvars.iv.next54, %6
  br i1 %exitcond57.not, label %.loopexit, label %scalar.ph, !llvm.loop !14

.loopexit:                                        ; preds = %scalar.ph, %middle.block
  %indvars.iv.next59 = add nuw nsw i64 %indvars.iv58, 1
  %exitcond62.not = icmp eq i64 %indvars.iv.next59, %6
  br i1 %exitcond62.not, label %._crit_edge48, label %.preheader, !llvm.loop !15

._crit_edge48:                                    ; preds = %.loopexit, %4
  ret i32 0
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <2 x double> @llvm.fmuladd.v2f64(<2 x double>, <2 x double>, <2 x double>) #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10, !12, !13}
!12 = !{!"llvm.loop.isvectorized", i32 1}
!13 = !{!"llvm.loop.unroll.runtime.disable"}
!14 = distinct !{!14, !10, !13, !12}
!15 = distinct !{!15, !10}


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