; LLVM IR from niessner/Matterport :: code/gaps/pkgs/R2Shapes/R2Grid.ll (ZNK6R2Grid17L2DistanceSquaredERKS_)

; 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.R2Grid = type { %class.R2Affine, %class.R2Affine, double, ptr, [2 x i32], i32, i32 }
%class.R2Affine = type <{ %class.R2Transformation, %class.R3Matrix, i32, [4 x i8] }>
%class.R2Transformation = type { ptr }
%class.R3Matrix = type { [3 x [3 x double]] }

; 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: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef double @_ZNK6R2Grid17L2DistanceSquaredERKS_(ptr noundef nonnull align 8 dereferenceable(208) %0, ptr noundef nonnull align 8 dereferenceable(208) %1) #2 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca double, align 8
  %6 = alloca i32, align 4
  %7 = alloca double, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %8 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store double 0.000000e+00, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !11
  br label %9

9:                                                ; preds = %53, %2
  %10 = load i32, ptr %6, align 4, !tbaa !11
  %11 = getelementptr inbounds %class.R2Grid, ptr %8, i32 0, i32 6
  %12 = load i32, ptr %11, align 4, !tbaa !13
  %13 = icmp slt i32 %10, %12
  br i1 %13, label %15, label %14

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

15:                                               ; preds = %9
  %16 = getelementptr inbounds %class.R2Grid, ptr %8, i32 0, i32 3
  %17 = load ptr, ptr %16, align 8, !tbaa !18
  %18 = load i32, ptr %6, align 4, !tbaa !11
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds double, ptr %17, i64 %19
  %21 = load double, ptr %20, align 8, !tbaa !9
  %22 = fcmp oeq double %21, -3.210000e+02
  br i1 %22, label %23, label %24

23:                                               ; preds = %15
  br label %53

24:                                               ; preds = %15
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %class.R2Grid, ptr %25, i32 0, i32 3
  %27 = load ptr, ptr %26, align 8, !tbaa !18
  %28 = load i32, ptr %6, align 4, !tbaa !11
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds double, ptr %27, i64 %29
  %31 = load double, ptr %30, align 8, !tbaa !9
  %32 = fcmp oeq double %31, -3.210000e+02
  br i1 %32, label %33, label %34

33:                                               ; preds = %24
  br label %53

34:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %35 = getelementptr inbounds %class.R2Grid, ptr %8, i32 0, i32 3
  %36 = load ptr, ptr %35, align 8, !tbaa !18
  %37 = load i32, ptr %6, align 4, !tbaa !11
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds double, ptr %36, i64 %38
  %40 = load double, ptr %39, align 8, !tbaa !9
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %class.R2Grid, ptr %41, i32 0, i32 3
  %43 = load ptr, ptr %42, align 8, !tbaa !18
  %44 = load i32, ptr %6, align 4, !tbaa !11
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds double, ptr %43, i64 %45
  %47 = load double, ptr %46, align 8, !tbaa !9
  %48 = fsub double %40, %47
  store double %48, ptr %7, align 8, !tbaa !9
  %49 = load double, ptr %7, align 8, !tbaa !9
  %50 = load double, ptr %7, align 8, !tbaa !9
  %51 = load double, ptr %5, align 8, !tbaa !9
  %52 = call double @llvm.fmuladd.f64(double %49, double %50, double %51)
  store double %52, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %53

53:                                               ; preds = %34, %33, %23
  %54 = load i32, ptr %6, align 4, !tbaa !11
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %6, align 4, !tbaa !11
  br label %9, !llvm.loop !19

56:                                               ; preds = %14
  %57 = load double, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret double %57
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 204}
!14 = !{!"_ZTS6R2Grid", !15, i64 0, !15, i64 88, !10, i64 176, !6, i64 184, !7, i64 192, !12, i64 200, !12, i64 204}
!15 = !{!"_ZTS8R2Affine", !16, i64 0, !17, i64 8, !12, i64 80}
!16 = !{!"_ZTS16R2Transformation"}
!17 = !{!"_ZTS8R3Matrix", !7, i64 0}
!18 = !{!14, !6, i64 184}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


; 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 nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #0

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef double @_ZNK6R2Grid17L2DistanceSquaredERKS_(ptr nocapture noundef nonnull readonly align 8 dereferenceable(208) %0, ptr nocapture noundef nonnull readonly align 8 dereferenceable(208) %1) local_unnamed_addr #1 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 204
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2
  %6 = getelementptr inbounds i8, ptr %0, i64 184
  %7 = load ptr, ptr %6, align 8, !tbaa !15
  %8 = getelementptr inbounds i8, ptr %1, i64 184
  %9 = load ptr, ptr %8, align 8
  %wide.trip.count = zext nneg i32 %4 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %10 = icmp eq i32 %4, 1
  br i1 %10, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %21

._crit_edge.loopexit.unr-lcssa:                   ; preds = %43, %.lr.ph
  %.1.lcssa.ph = phi double [ poison, %.lr.ph ], [ %.1.1, %43 ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %43 ]
  %.016.unr = phi double [ 0.000000e+00, %.lr.ph ], [ %.1.1, %43 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.loopexit.unr-lcssa
  %11 = getelementptr inbounds double, ptr %7, i64 %indvars.iv.unr
  %12 = load double, ptr %11, align 8, !tbaa !16
  %13 = fcmp oeq double %12, -3.210000e+02
  br i1 %13, label %._crit_edge, label %14

14:                                               ; preds = %.epil.preheader
  %15 = getelementptr inbounds double, ptr %9, i64 %indvars.iv.unr
  %16 = load double, ptr %15, align 8, !tbaa !16
  %17 = fcmp oeq double %16, -3.210000e+02
  br i1 %17, label %._crit_edge, label %18

18:                                               ; preds = %14
  %19 = fsub double %12, %16
  %20 = tail call double @llvm.fmuladd.f64(double %19, double %19, double %.016.unr)
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %18, %14, %.epil.preheader, %2
  %.0.lcssa = phi double [ 0.000000e+00, %2 ], [ %.1.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.016.unr, %.epil.preheader ], [ %.016.unr, %14 ], [ %20, %18 ]
  ret double %.0.lcssa

21:                                               ; preds = %43, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %43 ]
  %.016 = phi double [ 0.000000e+00, %.lr.ph.new ], [ %.1.1, %43 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %43 ]
  %22 = getelementptr inbounds double, ptr %7, i64 %indvars.iv
  %23 = load double, ptr %22, align 8, !tbaa !16
  %24 = fcmp oeq double %23, -3.210000e+02
  br i1 %24, label %32, label %25

25:                                               ; preds = %21
  %26 = getelementptr inbounds double, ptr %9, i64 %indvars.iv
  %27 = load double, ptr %26, align 8, !tbaa !16
  %28 = fcmp oeq double %27, -3.210000e+02
  br i1 %28, label %32, label %29

29:                                               ; preds = %25
  %30 = fsub double %23, %27
  %31 = tail call double @llvm.fmuladd.f64(double %30, double %30, double %.016)
  br label %32

32:                                               ; preds = %25, %21, %29
  %.1 = phi double [ %.016, %21 ], [ %.016, %25 ], [ %31, %29 ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %33 = getelementptr inbounds double, ptr %7, i64 %indvars.iv.next
  %34 = load double, ptr %33, align 8, !tbaa !16
  %35 = fcmp oeq double %34, -3.210000e+02
  br i1 %35, label %43, label %36

36:                                               ; preds = %32
  %37 = getelementptr inbounds double, ptr %9, i64 %indvars.iv.next
  %38 = load double, ptr %37, align 8, !tbaa !16
  %39 = fcmp oeq double %38, -3.210000e+02
  br i1 %39, label %43, label %40

40:                                               ; preds = %36
  %41 = fsub double %34, %38
  %42 = tail call double @llvm.fmuladd.f64(double %41, double %41, double %.1)
  br label %43

43:                                               ; preds = %40, %36, %32
  %.1.1 = phi double [ %.1, %32 ], [ %.1, %36 ], [ %42, %40 ]
  %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.loopexit.unr-lcssa, label %21, !llvm.loop !17
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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, !12, i64 204}
!6 = !{!"_ZTS6R2Grid", !7, i64 0, !7, i64 88, !13, i64 176, !14, i64 184, !10, i64 192, !12, i64 200, !12, i64 204}
!7 = !{!"_ZTS8R2Affine", !8, i64 0, !9, i64 8, !12, i64 80}
!8 = !{!"_ZTS16R2Transformation"}
!9 = !{!"_ZTS8R3Matrix", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!"int", !10, i64 0}
!13 = !{!"double", !10, i64 0}
!14 = !{!"any pointer", !10, i64 0}
!15 = !{!6, !14, i64 184}
!16 = !{!13, !13, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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