; LLVM IR from P-py/c-numerical-methods :: lagrange (calculateLagrangePolynomials)

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

%struct.KnownPoint = type { double, double }

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

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

13:                                               ; preds = %65, %4
  %14 = load i32, ptr %9, align 4, !tbaa !11
  %15 = load ptr, ptr %6, align 8, !tbaa !9
  %16 = load i32, ptr %15, align 4, !tbaa !11
  %17 = icmp sle i32 %14, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %13
  store i32 2, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %68

19:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  store double 1.000000e+00, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 0, ptr %12, align 4, !tbaa !11
  br label %20

20:                                               ; preds = %56, %19
  %21 = load i32, ptr %12, align 4, !tbaa !11
  %22 = load ptr, ptr %6, align 8, !tbaa !9
  %23 = load i32, ptr %22, align 4, !tbaa !11
  %24 = icmp sle i32 %21, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %20
  store i32 5, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %59

26:                                               ; preds = %20
  %27 = load i32, ptr %9, align 4, !tbaa !11
  %28 = load i32, ptr %12, align 4, !tbaa !11
  %29 = icmp ne i32 %27, %28
  br i1 %29, label %30, label %55

30:                                               ; preds = %26
  %31 = load double, ptr %5, align 8, !tbaa !5
  %32 = load ptr, ptr %7, align 8, !tbaa !9
  %33 = load i32, ptr %12, align 4, !tbaa !11
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds %struct.KnownPoint, ptr %32, i64 %34
  %36 = getelementptr inbounds %struct.KnownPoint, ptr %35, i32 0, i32 0
  %37 = load double, ptr %36, align 8, !tbaa !13
  %38 = fsub double %31, %37
  %39 = load ptr, ptr %7, align 8, !tbaa !9
  %40 = load i32, ptr %9, align 4, !tbaa !11
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds %struct.KnownPoint, ptr %39, i64 %41
  %43 = getelementptr inbounds %struct.KnownPoint, ptr %42, i32 0, i32 0
  %44 = load double, ptr %43, align 8, !tbaa !13
  %45 = load ptr, ptr %7, align 8, !tbaa !9
  %46 = load i32, ptr %12, align 4, !tbaa !11
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds %struct.KnownPoint, ptr %45, i64 %47
  %49 = getelementptr inbounds %struct.KnownPoint, ptr %48, i32 0, i32 0
  %50 = load double, ptr %49, align 8, !tbaa !13
  %51 = fsub double %44, %50
  %52 = fdiv double %38, %51
  %53 = load double, ptr %11, align 8, !tbaa !5
  %54 = fmul double %53, %52
  store double %54, ptr %11, align 8, !tbaa !5
  br label %55

55:                                               ; preds = %30, %26
  br label %56

56:                                               ; preds = %55
  %57 = load i32, ptr %12, align 4, !tbaa !11
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %12, align 4, !tbaa !11
  br label %20, !llvm.loop !15

59:                                               ; preds = %25
  %60 = load double, ptr %11, align 8, !tbaa !5
  %61 = load ptr, ptr %8, align 8, !tbaa !9
  %62 = load i32, ptr %9, align 4, !tbaa !11
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds double, ptr %61, i64 %63
  store double %60, ptr %64, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  br label %65

65:                                               ; preds = %59
  %66 = load i32, ptr %9, align 4, !tbaa !11
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %9, align 4, !tbaa !11
  br label %13, !llvm.loop !17

68:                                               ; preds = %18
  ret void
}

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

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 = !{!"double", !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 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"", !6, i64 0, !6, i64 8}
!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"

%struct.KnownPoint = type { double, double }

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @calculateLagrangePolynomials(double noundef %0, ptr nocapture noundef readonly %1, ptr nocapture noundef readonly %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %.not25 = icmp slt i32 %5, 0
  br i1 %.not25, label %._crit_edge27, label %.preheader.preheader

.preheader.preheader:                             ; preds = %4
  %6 = add nuw i32 %5, 1
  %wide.trip.count32 = zext i32 %6 to i64
  %xtraiter = and i64 %wide.trip.count32, 1
  %7 = icmp eq i32 %5, 0
  %unroll_iter = and i64 %wide.trip.count32, 4294967294
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %indvars.iv29 = phi i64 [ 0, %.preheader.preheader ], [ %indvars.iv.next30, %._crit_edge ]
  %8 = getelementptr inbounds %struct.KnownPoint, ptr %2, i64 %indvars.iv29
  br i1 %7, label %._crit_edge.unr-lcssa, label %.preheader.new

._crit_edge27:                                    ; preds = %._crit_edge, %4
  ret void

._crit_edge.unr-lcssa:                            ; preds = %35, %.preheader
  %.1.lcssa.ph = phi double [ poison, %.preheader ], [ %.1.1, %35 ]
  %indvars.iv.unr = phi i64 [ 0, %.preheader ], [ %indvars.iv.next.1, %35 ]
  %.01823.unr = phi double [ 1.000000e+00, %.preheader ], [ %.1.1, %35 ]
  br i1 %lcmp.mod.not, label %._crit_edge, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.unr-lcssa
  %.not21.epil = icmp eq i64 %indvars.iv29, %indvars.iv.unr
  br i1 %.not21.epil, label %._crit_edge, label %9

9:                                                ; preds = %.epil.preheader
  %10 = getelementptr inbounds %struct.KnownPoint, ptr %2, i64 %indvars.iv.unr
  %11 = load double, ptr %10, align 8, !tbaa !9
  %12 = fsub double %0, %11
  %13 = load double, ptr %8, align 8, !tbaa !9
  %14 = fsub double %13, %11
  %15 = fdiv double %12, %14
  %16 = fmul double %.01823.unr, %15
  br label %._crit_edge

._crit_edge:                                      ; preds = %.epil.preheader, %9, %._crit_edge.unr-lcssa
  %.1.lcssa = phi double [ %.1.lcssa.ph, %._crit_edge.unr-lcssa ], [ %16, %9 ], [ %.01823.unr, %.epil.preheader ]
  %17 = getelementptr inbounds double, ptr %3, i64 %indvars.iv29
  store double %.1.lcssa, ptr %17, align 8, !tbaa !12
  %indvars.iv.next30 = add nuw nsw i64 %indvars.iv29, 1
  %exitcond33.not = icmp eq i64 %indvars.iv.next30, %wide.trip.count32
  br i1 %exitcond33.not, label %._crit_edge27, label %.preheader, !llvm.loop !13

.preheader.new:                                   ; preds = %.preheader, %35
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %35 ], [ 0, %.preheader ]
  %.01823 = phi double [ %.1.1, %35 ], [ 1.000000e+00, %.preheader ]
  %niter = phi i64 [ %niter.next.1, %35 ], [ 0, %.preheader ]
  %.not21 = icmp eq i64 %indvars.iv29, %indvars.iv
  br i1 %.not21, label %26, label %18

18:                                               ; preds = %.preheader.new
  %19 = getelementptr inbounds %struct.KnownPoint, ptr %2, i64 %indvars.iv
  %20 = load double, ptr %19, align 8, !tbaa !9
  %21 = fsub double %0, %20
  %22 = load double, ptr %8, align 8, !tbaa !9
  %23 = fsub double %22, %20
  %24 = fdiv double %21, %23
  %25 = fmul double %.01823, %24
  br label %26

26:                                               ; preds = %.preheader.new, %18
  %.1 = phi double [ %25, %18 ], [ %.01823, %.preheader.new ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %.not21.1 = icmp eq i64 %indvars.iv29, %indvars.iv.next
  br i1 %.not21.1, label %35, label %27

27:                                               ; preds = %26
  %28 = getelementptr inbounds %struct.KnownPoint, ptr %2, i64 %indvars.iv.next
  %29 = load double, ptr %28, align 8, !tbaa !9
  %30 = fsub double %0, %29
  %31 = load double, ptr %8, align 8, !tbaa !9
  %32 = fsub double %31, %29
  %33 = fdiv double %30, %32
  %34 = fmul double %.1, %33
  br label %35

35:                                               ; preds = %27, %26
  %.1.1 = phi double [ %34, %27 ], [ %.1, %26 ]
  %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 %._crit_edge.unr-lcssa, label %.preheader.new, !llvm.loop !15
}

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

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass