; LLVM IR from Zhefan-Xu/Intent-MPC :: trajectory_planner/include/trajectory_planner/clustering (Kmeans)

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

%class.KMeans = type { i32, i32, ptr }

; 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: mustprogress nounwind uwtable
define dso_local void @_ZN6KMeans10InitializeEiPPd(ptr noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %1, ptr noundef %2) #1 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca double, align 8
  %12 = 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
  %13 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %14

14:                                               ; preds = %76, %3
  %15 = load i32, ptr %7, align 4, !tbaa !9
  %16 = getelementptr inbounds %class.KMeans, ptr %13, i32 0, i32 1
  %17 = load i32, ptr %16, align 4, !tbaa !11
  %18 = icmp slt i32 %15, %17
  br i1 %18, label %20, label %19

19:                                               ; preds = %14
  store i32 2, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %79

20:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %21 = load i32, ptr %5, align 4, !tbaa !9
  %22 = getelementptr inbounds %class.KMeans, ptr %13, i32 0, i32 1
  %23 = load i32, ptr %22, align 4, !tbaa !11
  %24 = sdiv i32 %21, %23
  store i32 %24, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %72, %20
  %26 = load i32, ptr %10, align 4, !tbaa !9
  %27 = getelementptr inbounds %class.KMeans, ptr %13, i32 0, i32 0
  %28 = load i32, ptr %27, align 8, !tbaa !13
  %29 = icmp slt i32 %26, %28
  br i1 %29, label %31, label %30

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

31:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  store double 0.000000e+00, ptr %11, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = load i32, ptr %9, align 4, !tbaa !9
  %34 = mul nsw i32 %32, %33
  store i32 %34, ptr %12, align 4, !tbaa !9
  br label %35

35:                                               ; preds = %55, %31
  %36 = load i32, ptr %12, align 4, !tbaa !9
  %37 = load i32, ptr %7, align 4, !tbaa !9
  %38 = add nsw i32 %37, 1
  %39 = load i32, ptr %9, align 4, !tbaa !9
  %40 = mul nsw i32 %38, %39
  %41 = icmp slt i32 %36, %40
  br i1 %41, label %43, label %42

42:                                               ; preds = %35
  store i32 8, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %58

43:                                               ; preds = %35
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = load i32, ptr %12, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds ptr, ptr %44, i64 %46
  %48 = load ptr, ptr %47, align 8, !tbaa !5
  %49 = load i32, ptr %10, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds double, ptr %48, i64 %50
  %52 = load double, ptr %51, align 8, !tbaa !14
  %53 = load double, ptr %11, align 8, !tbaa !14
  %54 = fadd double %53, %52
  store double %54, ptr %11, align 8, !tbaa !14
  br label %55

55:                                               ; preds = %43
  %56 = load i32, ptr %12, align 4, !tbaa !9
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %12, align 4, !tbaa !9
  br label %35, !llvm.loop !16

58:                                               ; preds = %42
  %59 = load double, ptr %11, align 8, !tbaa !14
  %60 = load i32, ptr %9, align 4, !tbaa !9
  %61 = sitofp i32 %60 to double
  %62 = fdiv double %59, %61
  %63 = getelementptr inbounds %class.KMeans, ptr %13, i32 0, i32 2
  %64 = load ptr, ptr %63, align 8, !tbaa !18
  %65 = load i32, ptr %7, align 4, !tbaa !9
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds ptr, ptr %64, i64 %66
  %68 = load ptr, ptr %67, align 8, !tbaa !5
  %69 = load i32, ptr %10, align 4, !tbaa !9
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds double, ptr %68, i64 %70
  store double %62, ptr %71, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  br label %72

72:                                               ; preds = %58
  %73 = load i32, ptr %10, align 4, !tbaa !9
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %10, align 4, !tbaa !9
  br label %25, !llvm.loop !19

75:                                               ; preds = %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %76

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

79:                                               ; preds = %19
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 4}
!12 = !{!"_ZTS6KMeans", !10, i64 0, !10, i64 4, !6, i64 8}
!13 = !{!12, !10, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"double", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!12, !6, i64 8}
!19 = distinct !{!19, !17}
!20 = distinct !{!20, !17}


; 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 nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_ZN6KMeans10InitializeEiPPd(ptr nocapture noundef nonnull readonly align 8 dereferenceable(16) %0, i32 noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 4
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3
  %7 = sdiv i32 %1, %5
  %8 = load i32, ptr %0, align 8, !tbaa !11
  %9 = icmp sgt i32 %8, 0
  %10 = sitofp i32 %7 to double
  %11 = getelementptr inbounds i8, ptr %0, i64 8
  %12 = load ptr, ptr %11, align 8
  %13 = fdiv double 0.000000e+00, %10
  br i1 %9, label %.lr.ph27.us.preheader, label %._crit_edge

.lr.ph27.us.preheader:                            ; preds = %.lr.ph
  %14 = sext i32 %7 to i64
  %wide.trip.count52 = zext nneg i32 %5 to i64
  %wide.trip.count = zext nneg i32 %8 to i64
  %wide.trip.count47 = zext nneg i32 %8 to i64
  %15 = add i32 %7, -1
  %min.iters.check = icmp ult i32 %8, 4
  %n.vec = and i64 %wide.trip.count, 2147483644
  %broadcast.splatinsert = insertelement <2 x double> poison, double %13, i64 0
  %broadcast.splat = shufflevector <2 x double> %broadcast.splatinsert, <2 x double> poison, <2 x i32> zeroinitializer
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  %xtraiter = and i32 %7, 3
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  %16 = icmp ult i32 %15, 3
  %invariant.gep = getelementptr i8, ptr %2, i64 8
  %invariant.gep60 = getelementptr i8, ptr %2, i64 16
  %invariant.gep62 = getelementptr i8, ptr %2, i64 24
  br label %.lr.ph27.us

.lr.ph27.us:                                      ; preds = %.lr.ph27.us.preheader, %._crit_edge28.us
  %indvars.iv49 = phi i64 [ 0, %.lr.ph27.us.preheader ], [ %indvars.iv.next50, %._crit_edge28.us ]
  %indvars.iv41 = phi i32 [ %7, %.lr.ph27.us.preheader ], [ %indvars.iv.next42, %._crit_edge28.us ]
  %indvars.iv36 = phi i64 [ 0, %.lr.ph27.us.preheader ], [ %indvars.iv.next37, %._crit_edge28.us ]
  %17 = mul nsw i64 %indvars.iv49, %14
  %indvars.iv.next50 = add nuw nsw i64 %indvars.iv49, 1
  %18 = mul nsw i64 %indvars.iv.next50, %14
  %19 = icmp slt i64 %17, %18
  %20 = getelementptr inbounds ptr, ptr %12, i64 %indvars.iv49
  %21 = load ptr, ptr %20, align 8, !tbaa !12
  br i1 %19, label %.lr.ph.us.us, label %.lr.ph27.split.us32.preheader

.lr.ph27.split.us32.preheader:                    ; preds = %.lr.ph27.us
  br i1 %min.iters.check, label %.lr.ph27.split.us32.preheader56, label %vector.body

vector.body:                                      ; preds = %.lr.ph27.split.us32.preheader, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %.lr.ph27.split.us32.preheader ]
  %22 = getelementptr inbounds double, ptr %21, i64 %index
  %23 = getelementptr inbounds i8, ptr %22, i64 16
  store <2 x double> %broadcast.splat, ptr %22, align 8, !tbaa !13
  store <2 x double> %broadcast.splat, ptr %23, align 8, !tbaa !13
  %index.next = add nuw i64 %index, 4
  %24 = icmp eq i64 %index.next, %n.vec
  br i1 %24, label %middle.block, label %vector.body, !llvm.loop !15

middle.block:                                     ; preds = %vector.body
  br i1 %cmp.n, label %._crit_edge28.us, label %.lr.ph27.split.us32.preheader56

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

.lr.ph27.split.us32:                              ; preds = %.lr.ph27.split.us32.preheader56, %.lr.ph27.split.us32
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph27.split.us32 ], [ %indvars.iv.ph, %.lr.ph27.split.us32.preheader56 ]
  %25 = getelementptr inbounds double, ptr %21, i64 %indvars.iv
  store double %13, ptr %25, align 8, !tbaa !13
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge28.us, label %.lr.ph27.split.us32, !llvm.loop !19

._crit_edge28.us:                                 ; preds = %.lr.ph27.split.us32, %._crit_edge.us.us, %middle.block
  %indvars.iv.next37 = add nsw i64 %indvars.iv36, %14
  %indvars.iv.next42 = add i32 %indvars.iv41, %7
  %exitcond53.not = icmp eq i64 %indvars.iv.next50, %wide.trip.count52
  br i1 %exitcond53.not, label %._crit_edge, label %.lr.ph27.us, !llvm.loop !20

.lr.ph.us.us:                                     ; preds = %.lr.ph27.us, %._crit_edge.us.us
  %indvars.iv44 = phi i64 [ %indvars.iv.next45, %._crit_edge.us.us ], [ 0, %.lr.ph27.us ]
  br i1 %lcmp.mod.not, label %.prol.loopexit, label %.prol.preheader

.prol.preheader:                                  ; preds = %.lr.ph.us.us, %.prol.preheader
  %indvars.iv38.prol = phi i64 [ %indvars.iv.next39.prol, %.prol.preheader ], [ %indvars.iv36, %.lr.ph.us.us ]
  %.01923.us.us.prol = phi double [ %30, %.prol.preheader ], [ 0.000000e+00, %.lr.ph.us.us ]
  %prol.iter = phi i32 [ %prol.iter.next, %.prol.preheader ], [ 0, %.lr.ph.us.us ]
  %26 = getelementptr inbounds ptr, ptr %2, i64 %indvars.iv38.prol
  %27 = load ptr, ptr %26, align 8, !tbaa !12
  %28 = getelementptr inbounds double, ptr %27, i64 %indvars.iv44
  %29 = load double, ptr %28, align 8, !tbaa !13
  %30 = fadd double %.01923.us.us.prol, %29
  %indvars.iv.next39.prol = add nsw i64 %indvars.iv38.prol, 1
  %prol.iter.next = add i32 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i32 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.prol.loopexit, label %.prol.preheader, !llvm.loop !21

.prol.loopexit:                                   ; preds = %.prol.preheader, %.lr.ph.us.us
  %.lcssa.unr = phi double [ poison, %.lr.ph.us.us ], [ %30, %.prol.preheader ]
  %indvars.iv38.unr = phi i64 [ %indvars.iv36, %.lr.ph.us.us ], [ %indvars.iv.next39.prol, %.prol.preheader ]
  %.01923.us.us.unr = phi double [ 0.000000e+00, %.lr.ph.us.us ], [ %30, %.prol.preheader ]
  br i1 %16, label %._crit_edge.us.us, label %.lr.ph.us.us.new

.lr.ph.us.us.new:                                 ; preds = %.prol.loopexit, %.lr.ph.us.us.new
  %indvars.iv38 = phi i64 [ %indvars.iv.next39.3, %.lr.ph.us.us.new ], [ %indvars.iv38.unr, %.prol.loopexit ]
  %.01923.us.us = phi double [ %47, %.lr.ph.us.us.new ], [ %.01923.us.us.unr, %.prol.loopexit ]
  %31 = getelementptr inbounds ptr, ptr %2, i64 %indvars.iv38
  %32 = load ptr, ptr %31, align 8, !tbaa !12
  %33 = getelementptr inbounds double, ptr %32, i64 %indvars.iv44
  %34 = load double, ptr %33, align 8, !tbaa !13
  %35 = fadd double %.01923.us.us, %34
  %gep = getelementptr ptr, ptr %invariant.gep, i64 %indvars.iv38
  %36 = load ptr, ptr %gep, align 8, !tbaa !12
  %37 = getelementptr inbounds double, ptr %36, i64 %indvars.iv44
  %38 = load double, ptr %37, align 8, !tbaa !13
  %39 = fadd double %35, %38
  %gep61 = getelementptr ptr, ptr %invariant.gep60, i64 %indvars.iv38
  %40 = load ptr, ptr %gep61, align 8, !tbaa !12
  %41 = getelementptr inbounds double, ptr %40, i64 %indvars.iv44
  %42 = load double, ptr %41, align 8, !tbaa !13
  %43 = fadd double %39, %42
  %gep63 = getelementptr ptr, ptr %invariant.gep62, i64 %indvars.iv38
  %44 = load ptr, ptr %gep63, align 8, !tbaa !12
  %45 = getelementptr inbounds double, ptr %44, i64 %indvars.iv44
  %46 = load double, ptr %45, align 8, !tbaa !13
  %47 = fadd double %43, %46
  %indvars.iv.next39.3 = add nsw i64 %indvars.iv38, 4
  %lftr.wideiv.3 = trunc i64 %indvars.iv.next39.3 to i32
  %exitcond43.not.3 = icmp eq i32 %indvars.iv41, %lftr.wideiv.3
  br i1 %exitcond43.not.3, label %._crit_edge.us.us, label %.lr.ph.us.us.new, !llvm.loop !23

._crit_edge.us.us:                                ; preds = %.lr.ph.us.us.new, %.prol.loopexit
  %.lcssa = phi double [ %.lcssa.unr, %.prol.loopexit ], [ %47, %.lr.ph.us.us.new ]
  %48 = fdiv double %.lcssa, %10
  %49 = getelementptr inbounds double, ptr %21, i64 %indvars.iv44
  store double %48, ptr %49, align 8, !tbaa !13
  %indvars.iv.next45 = add nuw nsw i64 %indvars.iv44, 1
  %exitcond48.not = icmp eq i64 %indvars.iv.next45, %wide.trip.count47
  br i1 %exitcond48.not, label %._crit_edge28.us, label %.lr.ph.us.us, !llvm.loop !24

._crit_edge:                                      ; preds = %._crit_edge28.us, %.lr.ph, %3
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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, !7, i64 4}
!6 = !{!"_ZTS6KMeans", !7, i64 0, !7, i64 4, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!10, !10, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"double", !8, i64 0}
!15 = distinct !{!15, !16, !17, !18}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!"llvm.loop.isvectorized", i32 1}
!18 = !{!"llvm.loop.unroll.runtime.disable"}
!19 = distinct !{!19, !16, !18, !17}
!20 = distinct !{!20, !16}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.unroll.disable"}
!23 = distinct !{!23, !16}
!24 = distinct !{!24, !16}


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