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

; 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.fabs.f64(double) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef double @_ZNK6R2Grid10L1DistanceERKS_(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
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %7 = 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 %8

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

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

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

22:                                               ; preds = %14
  br label %51

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

32:                                               ; preds = %23
  br label %51

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

51:                                               ; preds = %33, %32, %22
  %52 = load i32, ptr %6, align 4, !tbaa !11
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %6, align 4, !tbaa !11
  br label %8, !llvm.loop !19

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

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.fabs.f64(double) #0

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef double @_ZNK6R2Grid10L1DistanceERKS_(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 %22

._crit_edge.loopexit.unr-lcssa:                   ; preds = %46, %.lr.ph
  %.1.lcssa.ph = phi double [ poison, %.lr.ph ], [ %.1.1, %46 ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %46 ]
  %.01013.unr = phi double [ 0.000000e+00, %.lr.ph ], [ %.1.1, %46 ]
  %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.fabs.f64(double %19)
  %21 = fadd double %.01013.unr, %20
  br label %._crit_edge

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

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

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

30:                                               ; preds = %26
  %31 = fsub double %24, %28
  %32 = tail call double @llvm.fabs.f64(double %31)
  %33 = fadd double %.01013, %32
  br label %34

34:                                               ; preds = %26, %22, %30
  %.1 = phi double [ %.01013, %22 ], [ %.01013, %26 ], [ %33, %30 ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %35 = getelementptr inbounds double, ptr %7, i64 %indvars.iv.next
  %36 = load double, ptr %35, align 8, !tbaa !16
  %37 = fcmp oeq double %36, -3.210000e+02
  br i1 %37, label %46, label %38

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

42:                                               ; preds = %38
  %43 = fsub double %36, %40
  %44 = tail call double @llvm.fabs.f64(double %43)
  %45 = fadd double %.1, %44
  br label %46

46:                                               ; preds = %42, %38, %34
  %.1.1 = phi double [ %.1, %34 ], [ %.1, %38 ], [ %45, %42 ]
  %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 %22, !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