; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/cp/baised_standings.ll (Z12SumDistancesiPii)

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

10:                                               ; preds = %39, %3
  %11 = load i32, ptr %9, align 4, !tbaa !5
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = icmp sle i32 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %42

15:                                               ; preds = %10
  br label %16

16:                                               ; preds = %23, %15
  %17 = load ptr, ptr %5, align 8, !tbaa !9
  %18 = load i32, ptr %9, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i32, ptr %17, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %38

23:                                               ; preds = %16
  %24 = load i32, ptr %8, align 4, !tbaa !5
  %25 = load i32, ptr %9, align 4, !tbaa !5
  %26 = sub nsw i32 %24, %25
  %27 = call i32 @llvm.abs.i32(i32 %26, i1 true)
  %28 = load i32, ptr %7, align 4, !tbaa !5
  %29 = add nsw i32 %28, %27
  store i32 %29, ptr %7, align 4, !tbaa !5
  %30 = load ptr, ptr %5, align 8, !tbaa !9
  %31 = load i32, ptr %9, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds i32, ptr %30, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = add nsw i32 %34, -1
  store i32 %35, ptr %33, align 4, !tbaa !5
  %36 = load i32, ptr %8, align 4, !tbaa !5
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %8, align 4, !tbaa !5
  br label %16, !llvm.loop !11

38:                                               ; preds = %16
  br label %39

39:                                               ; preds = %38
  %40 = load i32, ptr %9, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %9, align 4, !tbaa !5
  br label %10, !llvm.loop !13

42:                                               ; preds = %14
  %43 = load i32, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret i32 %43
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #2

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple 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}


; 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(argmem: readwrite) uwtable
define dso_local noundef i32 @_Z12SumDistancesiPii(i32 noundef %0, ptr nocapture noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %.not20 = icmp slt i32 %0, 1
  br i1 %.not20, label %._crit_edge24, label %.preheader.preheader

.preheader.preheader:                             ; preds = %3
  %4 = add nuw i32 %0, 1
  %wide.trip.count = zext i32 %4 to i64
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %23
  %indvars.iv = phi i64 [ 1, %.preheader.preheader ], [ %indvars.iv.next, %23 ]
  %.01122 = phi i32 [ 1, %.preheader.preheader ], [ %.1.lcssa, %23 ]
  %.01221 = phi i32 [ 0, %.preheader.preheader ], [ %.113.lcssa, %23 ]
  %5 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv
  %.promoted = load i32, ptr %5, align 4, !tbaa !5
  %.not1516 = icmp eq i32 %.promoted, 0
  br i1 %.not1516, label %23, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %6 = trunc nuw nsw i64 %indvars.iv to i32
  %min.iters.check = icmp ult i32 %.promoted, 8
  br i1 %min.iters.check, label %.lr.ph.preheader33, label %vector.ph

.lr.ph.preheader33:                               ; preds = %middle.block, %.lr.ph.preheader
  %.118.ph = phi i32 [ %.01122, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.11317.ph = phi i32 [ %.01221, %.lr.ph.preheader ], [ %15, %middle.block ]
  %.ph = phi i32 [ %.promoted, %.lr.ph.preheader ], [ %ind.end29, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i32 %.promoted, -8
  %ind.end = add i32 %.01122, %n.vec
  %ind.end29 = and i32 %.promoted, 7
  %.splatinsert = insertelement <4 x i32> poison, i32 %.01122, i64 0
  %.splat = shufflevector <4 x i32> %.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  %induction = add <4 x i32> %.splat, <i32 0, i32 1, i32 2, i32 3>
  %7 = insertelement <4 x i32> <i32 poison, i32 0, i32 0, i32 0>, i32 %.01221, i64 0
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %6, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i32 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.ind = phi <4 x i32> [ %induction, %vector.ph ], [ %vec.ind.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ %7, %vector.ph ], [ %12, %vector.body ]
  %vec.phi32 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %13, %vector.body ]
  %step.add = add <4 x i32> %vec.ind, <i32 4, i32 4, i32 4, i32 4>
  %8 = sub nsw <4 x i32> %vec.ind, %broadcast.splat
  %9 = sub nsw <4 x i32> %step.add, %broadcast.splat
  %10 = tail call <4 x i32> @llvm.abs.v4i32(<4 x i32> %8, i1 true)
  %11 = tail call <4 x i32> @llvm.abs.v4i32(<4 x i32> %9, i1 true)
  %12 = add <4 x i32> %10, %vec.phi
  %13 = add <4 x i32> %11, %vec.phi32
  %index.next = add nuw i32 %index, 8
  %vec.ind.next = add <4 x i32> %vec.ind, <i32 8, i32 8, i32 8, i32 8>
  %14 = icmp eq i32 %index.next, %n.vec
  br i1 %14, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %13, %12
  %15 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i32 %.promoted, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader33

._crit_edge24:                                    ; preds = %23, %3
  %.012.lcssa = phi i32 [ 0, %3 ], [ %.113.lcssa, %23 ]
  ret i32 %.012.lcssa

.lr.ph:                                           ; preds = %.lr.ph.preheader33, %.lr.ph
  %.118 = phi i32 [ %21, %.lr.ph ], [ %.118.ph, %.lr.ph.preheader33 ]
  %.11317 = phi i32 [ %19, %.lr.ph ], [ %.11317.ph, %.lr.ph.preheader33 ]
  %16 = phi i32 [ %20, %.lr.ph ], [ %.ph, %.lr.ph.preheader33 ]
  %17 = sub nsw i32 %.118, %6
  %18 = tail call i32 @llvm.abs.i32(i32 %17, i1 true)
  %19 = add nuw nsw i32 %18, %.11317
  %20 = add nsw i32 %16, -1
  %21 = add nsw i32 %.118, 1
  %.not15 = icmp eq i32 %20, 0
  br i1 %.not15, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %middle.block
  %.lcssa = phi i32 [ %15, %middle.block ], [ %19, %.lr.ph ]
  %22 = add i32 %.01122, %.promoted
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %._crit_edge, %.preheader
  %.113.lcssa = phi i32 [ %.lcssa, %._crit_edge ], [ %.01221, %.preheader ]
  %.1.lcssa = phi i32 [ %22, %._crit_edge ], [ %.01122, %.preheader ]
  %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_edge24, label %.preheader, !llvm.loop !14
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <4 x i32> @llvm.abs.v4i32(<4 x i32>, i1 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #2

attributes #0 = { mustprogress 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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !12, !11}
!14 = distinct !{!14, !10}


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