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

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

@.str.14 = external hidden unnamed_addr constant [30 x i8], align 1

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

declare ptr @libmetis__idxsmalloc(i64 noundef, i64 noundef, ptr noundef) #1

declare i64 @libmetis__idxargmax(i64 noundef, ptr noundef) #1

declare i64 @libmetis__idxsum(i64 noundef, ptr noundef, i64 noundef) #1

declare void @gk_free(ptr noundef, ...) #1

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

12:                                               ; preds = %25, %3
  %13 = load i64, ptr %7, align 8, !tbaa !5
  %14 = load i64, ptr %4, align 8, !tbaa !5
  %15 = icmp slt i64 %13, %14
  br i1 %15, label %16, label %28

16:                                               ; preds = %12
  %17 = load ptr, ptr %8, align 8, !tbaa !9
  %18 = load ptr, ptr %6, align 8, !tbaa !9
  %19 = load i64, ptr %7, align 8, !tbaa !5
  %20 = getelementptr inbounds i64, ptr %18, i64 %19
  %21 = load i64, ptr %20, align 8, !tbaa !5
  %22 = getelementptr inbounds i64, ptr %17, i64 %21
  %23 = load i64, ptr %22, align 8, !tbaa !5
  %24 = add nsw i64 %23, 1
  store i64 %24, ptr %22, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %16
  %26 = load i64, ptr %7, align 8, !tbaa !5
  %27 = add nsw i64 %26, 1
  store i64 %27, ptr %7, align 8, !tbaa !5
  br label %12, !llvm.loop !11

28:                                               ; preds = %12
  %29 = load i64, ptr %5, align 8, !tbaa !5
  %30 = sitofp i64 %29 to double
  %31 = fmul double 1.000000e+00, %30
  %32 = load ptr, ptr %8, align 8, !tbaa !9
  %33 = load i64, ptr %5, align 8, !tbaa !5
  %34 = load ptr, ptr %8, align 8, !tbaa !9
  %35 = call i64 @libmetis__idxargmax(i64 noundef %33, ptr noundef %34)
  %36 = getelementptr inbounds i64, ptr %32, i64 %35
  %37 = load i64, ptr %36, align 8, !tbaa !5
  %38 = sitofp i64 %37 to double
  %39 = fmul double %31, %38
  %40 = load i64, ptr %5, align 8, !tbaa !5
  %41 = load ptr, ptr %8, align 8, !tbaa !9
  %42 = call i64 @libmetis__idxsum(i64 noundef %40, ptr noundef %41, i64 noundef 1)
  %43 = sitofp i64 %42 to double
  %44 = fmul double 1.000000e+00, %43
  %45 = fdiv double %39, %44
  %46 = fptrunc double %45 to float
  store float %46, ptr %9, align 4, !tbaa !13
  call void (ptr, ...) @gk_free(ptr noundef %8, ptr noundef null)
  %47 = load float, ptr %9, align 4, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret float %47
}

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


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

@.str.14 = external hidden unnamed_addr constant [30 x i8], align 1

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

declare ptr @libmetis__idxsmalloc(i64 noundef, i64 noundef, ptr noundef) local_unnamed_addr #1

declare i64 @libmetis__idxargmax(i64 noundef, ptr noundef) local_unnamed_addr #1

declare i64 @libmetis__idxsum(i64 noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

declare void @gk_free(ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: mustprogress 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 float @libmetis__ComputeElementBalance(i64 noundef %0, i64 noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #2 {
  %4 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  %5 = tail call ptr @libmetis__idxsmalloc(i64 noundef %1, i64 noundef 0, ptr noundef nonnull @.str.14) #3
  store ptr %5, ptr %4, align 8, !tbaa !5
  %6 = icmp sgt i64 %0, 0
  br i1 %6, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %3
  %xtraiter = and i64 %0, 3
  %7 = icmp ult i64 %0, 4
  br i1 %7, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %0, 9223372036854775804
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.09 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %31, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %8 = getelementptr inbounds i64, ptr %2, i64 %.09
  %9 = load i64, ptr %8, align 8, !tbaa !9
  %10 = getelementptr inbounds i64, ptr %5, i64 %9
  %11 = load i64, ptr %10, align 8, !tbaa !9
  %12 = add nsw i64 %11, 1
  store i64 %12, ptr %10, align 8, !tbaa !9
  %13 = or disjoint i64 %.09, 1
  %14 = getelementptr inbounds i64, ptr %2, i64 %13
  %15 = load i64, ptr %14, align 8, !tbaa !9
  %16 = getelementptr inbounds i64, ptr %5, i64 %15
  %17 = load i64, ptr %16, align 8, !tbaa !9
  %18 = add nsw i64 %17, 1
  store i64 %18, ptr %16, align 8, !tbaa !9
  %19 = or disjoint i64 %.09, 2
  %20 = getelementptr inbounds i64, ptr %2, i64 %19
  %21 = load i64, ptr %20, align 8, !tbaa !9
  %22 = getelementptr inbounds i64, ptr %5, i64 %21
  %23 = load i64, ptr %22, align 8, !tbaa !9
  %24 = add nsw i64 %23, 1
  store i64 %24, ptr %22, align 8, !tbaa !9
  %25 = or disjoint i64 %.09, 3
  %26 = getelementptr inbounds i64, ptr %2, i64 %25
  %27 = load i64, ptr %26, align 8, !tbaa !9
  %28 = getelementptr inbounds i64, ptr %5, i64 %27
  %29 = load i64, ptr %28, align 8, !tbaa !9
  %30 = add nsw i64 %29, 1
  store i64 %30, ptr %28, align 8, !tbaa !9
  %31 = add nuw nsw i64 %.09, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !11

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.09.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %31, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil
  %.09.epil = phi i64 [ %37, %.lr.ph.epil ], [ %.09.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %32 = getelementptr inbounds i64, ptr %2, i64 %.09.epil
  %33 = load i64, ptr %32, align 8, !tbaa !9
  %34 = getelementptr inbounds i64, ptr %5, i64 %33
  %35 = load i64, ptr %34, align 8, !tbaa !9
  %36 = add nsw i64 %35, 1
  store i64 %36, ptr %34, align 8, !tbaa !9
  %37 = add nuw nsw i64 %.09.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph.epil, !llvm.loop !13

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil, %3
  %38 = sitofp i64 %1 to double
  %39 = tail call i64 @libmetis__idxargmax(i64 noundef %1, ptr noundef %5) #3
  %40 = getelementptr inbounds i64, ptr %5, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !9
  %42 = sitofp i64 %41 to double
  %43 = fmul double %38, %42
  %44 = tail call i64 @libmetis__idxsum(i64 noundef %1, ptr noundef %5, i64 noundef 1) #3
  %45 = sitofp i64 %44 to double
  %46 = fdiv double %43, %45
  %47 = fptrunc double %46 to float
  call void (ptr, ...) @gk_free(ptr noundef nonnull %4, ptr noundef null) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  ret float %47
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 #3 = { 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 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.unroll.disable"}


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