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

; 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 @polyc_expandbinomial(i32 noundef %0, ptr noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  %8 = load i32, ptr %4, align 4, !tbaa !5
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %10, label %15

10:                                               ; preds = %2
  %11 = load ptr, ptr %5, align 8, !tbaa !9
  %12 = getelementptr inbounds { double, double }, ptr %11, i64 0
  %13 = getelementptr inbounds { double, double }, ptr %12, i32 0, i32 0
  %14 = getelementptr inbounds { double, double }, ptr %12, i32 0, i32 1
  store double 0.000000e+00, ptr %13, align 8
  store double 0.000000e+00, ptr %14, align 8
  store i32 0, ptr %3, align 4
  br label %80

15:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %32, %15
  %17 = load i32, ptr %6, align 4, !tbaa !5
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = icmp ule i32 %17, %18
  br i1 %19, label %20, label %35

20:                                               ; preds = %16
  %21 = load i32, ptr %6, align 4, !tbaa !5
  %22 = icmp eq i32 %21, 0
  %23 = zext i1 %22 to i64
  %24 = select i1 %22, i32 1, i32 0
  %25 = sitofp i32 %24 to double
  %26 = load ptr, ptr %5, align 8, !tbaa !9
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds { double, double }, ptr %26, i64 %28
  %30 = getelementptr inbounds { double, double }, ptr %29, i32 0, i32 0
  %31 = getelementptr inbounds { double, double }, ptr %29, i32 0, i32 1
  store double %25, ptr %30, align 8
  store double 0.000000e+00, ptr %31, align 8
  br label %32

32:                                               ; preds = %20
  %33 = load i32, ptr %6, align 4, !tbaa !5
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr %6, align 4, !tbaa !5
  br label %16, !llvm.loop !11

35:                                               ; preds = %16
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %76, %35
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = icmp ult i32 %37, %38
  br i1 %39, label %40, label %79

40:                                               ; preds = %36
  %41 = load i32, ptr %6, align 4, !tbaa !5
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %7, align 4, !tbaa !5
  br label %43

43:                                               ; preds = %72, %40
  %44 = load i32, ptr %7, align 4, !tbaa !5
  %45 = icmp sgt i32 %44, 0
  br i1 %45, label %46, label %75

46:                                               ; preds = %43
  %47 = load ptr, ptr %5, align 8, !tbaa !9
  %48 = load i32, ptr %7, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds { double, double }, ptr %47, i64 %49
  %51 = getelementptr inbounds { double, double }, ptr %50, i32 0, i32 0
  %52 = load double, ptr %51, align 8
  %53 = getelementptr inbounds { double, double }, ptr %50, i32 0, i32 1
  %54 = load double, ptr %53, align 8
  %55 = load ptr, ptr %5, align 8, !tbaa !9
  %56 = load i32, ptr %7, align 4, !tbaa !5
  %57 = sub nsw i32 %56, 1
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds { double, double }, ptr %55, i64 %58
  %60 = getelementptr inbounds { double, double }, ptr %59, i32 0, i32 0
  %61 = load double, ptr %60, align 8
  %62 = getelementptr inbounds { double, double }, ptr %59, i32 0, i32 1
  %63 = load double, ptr %62, align 8
  %64 = fadd double %52, %61
  %65 = fadd double %54, %63
  %66 = load ptr, ptr %5, align 8, !tbaa !9
  %67 = load i32, ptr %7, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds { double, double }, ptr %66, i64 %68
  %70 = getelementptr inbounds { double, double }, ptr %69, i32 0, i32 0
  %71 = getelementptr inbounds { double, double }, ptr %69, i32 0, i32 1
  store double %64, ptr %70, align 8
  store double %65, ptr %71, align 8
  br label %72

72:                                               ; preds = %46
  %73 = load i32, ptr %7, align 4, !tbaa !5
  %74 = add nsw i32 %73, -1
  store i32 %74, ptr %7, align 4, !tbaa !5
  br label %43, !llvm.loop !13

75:                                               ; preds = %43
  br label %76

76:                                               ; preds = %75
  %77 = load i32, ptr %6, align 4, !tbaa !5
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %6, align 4, !tbaa !5
  br label %36, !llvm.loop !14

79:                                               ; preds = %36
  store i32 0, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %80

80:                                               ; preds = %79, %10
  %81 = load i32, ptr %3, align 4
  ret i32 %81
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


; 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: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local noundef i32 @polyc_expandbinomial(i32 noundef %0, ptr nocapture noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq i32 %0, 0
  br i1 %3, label %5, label %.preheader24.preheader

.preheader24.preheader:                           ; preds = %2
  %4 = add i32 %0, 1
  %wide.trip.count = zext i32 %4 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %unroll_iter = and i64 %wide.trip.count, 4294967294
  br label %.preheader24

5:                                                ; preds = %2
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %1, i8 0, i64 16, i1 false)
  br label %.loopexit23

.preheader24:                                     ; preds = %.preheader24, %.preheader24.preheader
  %indvars.iv = phi i64 [ 0, %.preheader24.preheader ], [ %indvars.iv.next.1, %.preheader24 ]
  %niter = phi i64 [ 0, %.preheader24.preheader ], [ %niter.next.1, %.preheader24 ]
  %6 = icmp eq i64 %indvars.iv, 0
  %7 = uitofp i1 %6 to double
  %8 = getelementptr inbounds { double, double }, ptr %1, i64 %indvars.iv
  %9 = getelementptr inbounds i8, ptr %8, i64 8
  store double %7, ptr %8, align 8
  store double 0.000000e+00, ptr %9, align 8
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %10 = getelementptr inbounds { double, double }, ptr %1, i64 %indvars.iv.next
  %11 = getelementptr inbounds i8, ptr %10, i64 8
  store double 0.000000e+00, ptr %10, align 8
  store double 0.000000e+00, ptr %11, align 8
  %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 %.preheader.preheader.unr-lcssa, label %.preheader24, !llvm.loop !5

.preheader.preheader.unr-lcssa:                   ; preds = %.preheader24
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader.preheader, label %.preheader24.epil

.preheader24.epil:                                ; preds = %.preheader.preheader.unr-lcssa
  %12 = icmp eq i64 %indvars.iv.next.1, 0
  %13 = uitofp i1 %12 to double
  %14 = getelementptr inbounds { double, double }, ptr %1, i64 %indvars.iv.next.1
  %15 = getelementptr inbounds i8, ptr %14, i64 8
  store double %13, ptr %14, align 8
  store double 0.000000e+00, ptr %15, align 8
  br label %.preheader.preheader

.preheader.preheader:                             ; preds = %.preheader.preheader.unr-lcssa, %.preheader24.epil
  br label %.preheader

.loopexit:                                        ; preds = %.preheader.new, %.prol.loopexit
  %indvars.iv.next30 = add nuw nsw i64 %indvars.iv29, 1
  %exitcond35.not = icmp eq i64 %indvars.iv.next30, %wide.trip.count
  %indvar.next = add i64 %indvar, 1
  br i1 %exitcond35.not, label %.loopexit23, label %.preheader, !llvm.loop !7

.preheader:                                       ; preds = %.preheader.preheader, %.loopexit
  %indvar = phi i64 [ 0, %.preheader.preheader ], [ %indvar.next, %.loopexit ]
  %indvars.iv29 = phi i64 [ 1, %.preheader.preheader ], [ %indvars.iv.next30, %.loopexit ]
  %16 = and i64 %indvar, 1
  %lcmp.mod37.not.not = icmp eq i64 %16, 0
  br i1 %lcmp.mod37.not.not, label %.prol.loopexit.unr-lcssa, label %.prol.loopexit

.prol.loopexit.unr-lcssa:                         ; preds = %.preheader
  %17 = getelementptr inbounds { double, double }, ptr %1, i64 %indvars.iv29
  %18 = getelementptr i8, ptr %17, i64 -16
  %19 = load <2 x double>, ptr %17, align 8
  %20 = load <2 x double>, ptr %18, align 8
  %21 = fadd <2 x double> %19, %20
  store <2 x double> %21, ptr %17, align 8
  %indvars.iv.next32.prol = add nsw i64 %indvars.iv29, -1
  br label %.prol.loopexit

.prol.loopexit:                                   ; preds = %.prol.loopexit.unr-lcssa, %.preheader
  %indvars.iv31.unr = phi i64 [ %indvars.iv29, %.preheader ], [ %indvars.iv.next32.prol, %.prol.loopexit.unr-lcssa ]
  %22 = icmp eq i64 %indvar, 0
  br i1 %22, label %.loopexit, label %.preheader.new

.preheader.new:                                   ; preds = %.prol.loopexit, %.preheader.new
  %indvars.iv31 = phi i64 [ %indvars.iv.next32.1, %.preheader.new ], [ %indvars.iv31.unr, %.prol.loopexit ]
  %23 = getelementptr inbounds { double, double }, ptr %1, i64 %indvars.iv31
  %24 = getelementptr i8, ptr %23, i64 -16
  %25 = load <2 x double>, ptr %23, align 8
  %26 = load <2 x double>, ptr %24, align 8
  %27 = fadd <2 x double> %25, %26
  store <2 x double> %27, ptr %23, align 8
  %28 = getelementptr { double, double }, ptr %1, i64 %indvars.iv31
  %29 = getelementptr i8, ptr %28, i64 -16
  %30 = getelementptr i8, ptr %28, i64 -32
  %31 = load <2 x double>, ptr %30, align 8
  %32 = fadd <2 x double> %26, %31
  store <2 x double> %32, ptr %29, align 8
  %indvars.iv.next32.1 = add nsw i64 %indvars.iv31, -2
  %33 = icmp sgt i64 %indvars.iv31, 2
  br i1 %33, label %.preheader.new, label %.loopexit, !llvm.loop !8

.loopexit23:                                      ; preds = %.loopexit, %5
  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) #1

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: readwrite) 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 nounwind willreturn memory(argmem: write) }

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}
!8 = distinct !{!8, !6}


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