; LLVM IR from ekoontz/graphlab :: src/graphlab/extern/metis/libmetis/mkwayfmh.ll_libmetis (ComputeHKWayLoadImbalance)

; 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: nounwind uwtable
define dso_local void @libmetis__ComputeHKWayLoadImbalance(i64 noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) #1 {
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca float, align 4
  store i64 %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !5
  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 8, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i64 0, ptr %9, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %53, %4
  %13 = load i64, ptr %9, align 8, !tbaa !5
  %14 = load i64, ptr %5, align 8, !tbaa !5
  %15 = icmp slt i64 %13, %14
  br i1 %15, label %16, label %56

16:                                               ; preds = %12
  store float 0.000000e+00, ptr %11, align 4, !tbaa !11
  store i64 0, ptr %10, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %42, %16
  %18 = load i64, ptr %10, align 8, !tbaa !5
  %19 = load i64, ptr %6, align 8, !tbaa !5
  %20 = icmp slt i64 %18, %19
  br i1 %20, label %21, label %45

21:                                               ; preds = %17
  %22 = load ptr, ptr %7, align 8, !tbaa !9
  %23 = load i64, ptr %10, align 8, !tbaa !5
  %24 = load i64, ptr %5, align 8, !tbaa !5
  %25 = mul nsw i64 %23, %24
  %26 = load i64, ptr %9, align 8, !tbaa !5
  %27 = add nsw i64 %25, %26
  %28 = getelementptr inbounds float, ptr %22, i64 %27
  %29 = load float, ptr %28, align 4, !tbaa !11
  %30 = load float, ptr %11, align 4, !tbaa !11
  %31 = fcmp ogt float %29, %30
  br i1 %31, label %32, label %41

32:                                               ; preds = %21
  %33 = load ptr, ptr %7, align 8, !tbaa !9
  %34 = load i64, ptr %10, align 8, !tbaa !5
  %35 = load i64, ptr %5, align 8, !tbaa !5
  %36 = mul nsw i64 %34, %35
  %37 = load i64, ptr %9, align 8, !tbaa !5
  %38 = add nsw i64 %36, %37
  %39 = getelementptr inbounds float, ptr %33, i64 %38
  %40 = load float, ptr %39, align 4, !tbaa !11
  store float %40, ptr %11, align 4, !tbaa !11
  br label %41

41:                                               ; preds = %32, %21
  br label %42

42:                                               ; preds = %41
  %43 = load i64, ptr %10, align 8, !tbaa !5
  %44 = add nsw i64 %43, 1
  store i64 %44, ptr %10, align 8, !tbaa !5
  br label %17, !llvm.loop !13

45:                                               ; preds = %17
  %46 = load float, ptr %11, align 4, !tbaa !11
  %47 = load i64, ptr %6, align 8, !tbaa !5
  %48 = sitofp i64 %47 to float
  %49 = fmul float %46, %48
  %50 = load ptr, ptr %8, align 8, !tbaa !9
  %51 = load i64, ptr %9, align 8, !tbaa !5
  %52 = getelementptr inbounds float, ptr %50, i64 %51
  store float %49, ptr %52, align 4, !tbaa !11
  br label %53

53:                                               ; preds = %45
  %54 = load i64, ptr %9, align 8, !tbaa !5
  %55 = add nsw i64 %54, 1
  store i64 %55, ptr %9, align 8, !tbaa !5
  br label %12, !llvm.loop !15

56:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  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 = !{!"long", !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 = !{!"float", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !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: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @libmetis__ComputeHKWayLoadImbalance(i64 noundef %0, i64 noundef %1, ptr nocapture noundef readonly %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = icmp sgt i64 %0, 0
  br i1 %5, label %.preheader.lr.ph, label %._crit_edge26

.preheader.lr.ph:                                 ; preds = %4
  %6 = icmp sgt i64 %1, 0
  %7 = sitofp i64 %1 to float
  br i1 %6, label %.preheader.us.preheader, label %.preheader.lr.ph.split

.preheader.us.preheader:                          ; preds = %.preheader.lr.ph
  %xtraiter = and i64 %1, 1
  %8 = icmp eq i64 %1, 1
  %unroll_iter = and i64 %1, 9223372036854775806
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %.01925.us = phi i64 [ %22, %._crit_edge.us ], [ 0, %.preheader.us.preheader ]
  %invariant.gep.us = getelementptr float, ptr %2, i64 %.01925.us
  br i1 %8, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new

.preheader.us.new:                                ; preds = %.preheader.us, %.preheader.us.new
  %.024.us = phi float [ %.1.us.1, %.preheader.us.new ], [ 0.000000e+00, %.preheader.us ]
  %.01823.us = phi i64 [ %16, %.preheader.us.new ], [ 0, %.preheader.us ]
  %niter = phi i64 [ %niter.next.1, %.preheader.us.new ], [ 0, %.preheader.us ]
  %9 = mul nuw nsw i64 %.01823.us, %0
  %gep.us = getelementptr float, ptr %invariant.gep.us, i64 %9
  %10 = load float, ptr %gep.us, align 4, !tbaa !5
  %11 = fcmp ogt float %10, %.024.us
  %.1.us = select i1 %11, float %10, float %.024.us
  %12 = or disjoint i64 %.01823.us, 1
  %13 = mul nuw nsw i64 %12, %0
  %gep.us.1 = getelementptr float, ptr %invariant.gep.us, i64 %13
  %14 = load float, ptr %gep.us.1, align 4, !tbaa !5
  %15 = fcmp ogt float %14, %.1.us
  %.1.us.1 = select i1 %15, float %14, float %.1.us
  %16 = add nuw nsw i64 %.01823.us, 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.us.unr-lcssa, label %.preheader.us.new, !llvm.loop !9

._crit_edge.us.unr-lcssa:                         ; preds = %.preheader.us.new, %.preheader.us
  %.1.us.lcssa.ph = phi float [ poison, %.preheader.us ], [ %.1.us.1, %.preheader.us.new ]
  %.024.us.unr = phi float [ 0.000000e+00, %.preheader.us ], [ %.1.us.1, %.preheader.us.new ]
  %.01823.us.unr = phi i64 [ 0, %.preheader.us ], [ %16, %.preheader.us.new ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %._crit_edge.us.epilog-lcssa

._crit_edge.us.epilog-lcssa:                      ; preds = %._crit_edge.us.unr-lcssa
  %17 = mul nuw nsw i64 %.01823.us.unr, %0
  %gep.us.epil = getelementptr float, ptr %invariant.gep.us, i64 %17
  %18 = load float, ptr %gep.us.epil, align 4, !tbaa !5
  %19 = fcmp ogt float %18, %.024.us.unr
  %.1.us.epil = select i1 %19, float %18, float %.024.us.unr
  br label %._crit_edge.us

._crit_edge.us:                                   ; preds = %._crit_edge.us.unr-lcssa, %._crit_edge.us.epilog-lcssa
  %.1.us.lcssa = phi float [ %.1.us.lcssa.ph, %._crit_edge.us.unr-lcssa ], [ %.1.us.epil, %._crit_edge.us.epilog-lcssa ]
  %20 = fmul float %.1.us.lcssa, %7
  %21 = getelementptr inbounds float, ptr %3, i64 %.01925.us
  store float %20, ptr %21, align 4, !tbaa !5
  %22 = add nuw nsw i64 %.01925.us, 1
  %exitcond29.not = icmp eq i64 %22, %0
  br i1 %exitcond29.not, label %._crit_edge26, label %.preheader.us, !llvm.loop !11

.preheader.lr.ph.split:                           ; preds = %.preheader.lr.ph
  %23 = tail call float @llvm.copysign.f32(float 0.000000e+00, float %7)
  %min.iters.check = icmp ult i64 %0, 8
  br i1 %min.iters.check, label %.preheader.preheader, label %vector.ph

vector.ph:                                        ; preds = %.preheader.lr.ph.split
  %n.vec = and i64 %0, 9223372036854775800
  %broadcast.splatinsert = insertelement <4 x float> poison, float %23, i64 0
  %broadcast.splat = shufflevector <4 x float> %broadcast.splatinsert, <4 x float> poison, <4 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %24 = getelementptr inbounds float, ptr %3, i64 %index
  %25 = getelementptr inbounds i8, ptr %24, i64 16
  store <4 x float> %broadcast.splat, ptr %24, align 4, !tbaa !5
  store <4 x float> %broadcast.splat, ptr %25, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %26 = icmp eq i64 %index.next, %n.vec
  br i1 %26, label %middle.block, label %vector.body, !llvm.loop !12

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %0
  br i1 %cmp.n, label %._crit_edge26, label %.preheader.preheader

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

.preheader:                                       ; preds = %.preheader.preheader, %.preheader
  %.01925 = phi i64 [ %28, %.preheader ], [ %.01925.ph, %.preheader.preheader ]
  %27 = getelementptr inbounds float, ptr %3, i64 %.01925
  store float %23, ptr %27, align 4, !tbaa !5
  %28 = add nuw nsw i64 %.01925, 1
  %exitcond.not = icmp eq i64 %28, %0
  br i1 %exitcond.not, label %._crit_edge26, label %.preheader, !llvm.loop !15

._crit_edge26:                                    ; preds = %.preheader, %._crit_edge.us, %middle.block, %4
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.copysign.f32(float, float) #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 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 = !{!"float", !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 = distinct !{!12, !10, !13, !14}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !10, !14, !13}


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