; LLVM IR from niessner/Matterport :: code/gaps/pkgs/R3Shapes/R3MeshProperty.ll (ZNK14R3MeshProperty6L2NormEv)

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

%struct.R3MeshProperty = type { ptr, [1024 x i8], i32, ptr, double, double, double, double, double, 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: nounwind
declare double @sqrt(double noundef) #2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef double @_ZNK14R3MeshProperty6L2NormEv(ptr noundef nonnull align 8 dereferenceable(1096) %0) #3 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca double, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr %2, align 8
  %7 = getelementptr inbounds %struct.R3MeshProperty, ptr %6, i32 0, i32 9
  %8 = load double, ptr %7, align 8, !tbaa !9
  %9 = fcmp oeq double %8, -1.000000e+00
  br i1 %9, label %10, label %56

10:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  store ptr %6, ptr %3, align 8, !tbaa !5
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.R3MeshProperty, ptr %11, i32 0, i32 9
  store double 0.000000e+00, ptr %12, align 8, !tbaa !9
  %13 = getelementptr inbounds %struct.R3MeshProperty, ptr %6, i32 0, i32 2
  %14 = load i32, ptr %13, align 8, !tbaa !13
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %16, label %55

16:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  store double 0.000000e+00, ptr %4, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !15
  br label %17

17:                                               ; preds = %47, %16
  %18 = load i32, ptr %5, align 4, !tbaa !15
  %19 = getelementptr inbounds %struct.R3MeshProperty, ptr %6, i32 0, i32 2
  %20 = load i32, ptr %19, align 8, !tbaa !13
  %21 = icmp slt i32 %18, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %50

23:                                               ; preds = %17
  %24 = getelementptr inbounds %struct.R3MeshProperty, ptr %6, i32 0, i32 3
  %25 = load ptr, ptr %24, align 8, !tbaa !16
  %26 = load i32, ptr %5, align 4, !tbaa !15
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds double, ptr %25, i64 %27
  %29 = load double, ptr %28, align 8, !tbaa !14
  %30 = fcmp oeq double %29, -1.000000e+00
  br i1 %30, label %31, label %32

31:                                               ; preds = %23
  br label %47

32:                                               ; preds = %23
  %33 = getelementptr inbounds %struct.R3MeshProperty, ptr %6, i32 0, i32 3
  %34 = load ptr, ptr %33, align 8, !tbaa !16
  %35 = load i32, ptr %5, align 4, !tbaa !15
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds double, ptr %34, i64 %36
  %38 = load double, ptr %37, align 8, !tbaa !14
  %39 = getelementptr inbounds %struct.R3MeshProperty, ptr %6, i32 0, i32 3
  %40 = load ptr, ptr %39, align 8, !tbaa !16
  %41 = load i32, ptr %5, align 4, !tbaa !15
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds double, ptr %40, i64 %42
  %44 = load double, ptr %43, align 8, !tbaa !14
  %45 = load double, ptr %4, align 8, !tbaa !14
  %46 = call double @llvm.fmuladd.f64(double %38, double %44, double %45)
  store double %46, ptr %4, align 8, !tbaa !14
  br label %47

47:                                               ; preds = %32, %31
  %48 = load i32, ptr %5, align 4, !tbaa !15
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %5, align 4, !tbaa !15
  br label %17, !llvm.loop !17

50:                                               ; preds = %22
  %51 = load double, ptr %4, align 8, !tbaa !14
  %52 = call double @sqrt(double noundef %51) #4
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.R3MeshProperty, ptr %53, i32 0, i32 9
  store double %52, ptr %54, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %55

55:                                               ; preds = %50, %10
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  br label %56

56:                                               ; preds = %55, %1
  %57 = getelementptr inbounds %struct.R3MeshProperty, ptr %6, i32 0, i32 9
  %58 = load double, ptr %57, align 8, !tbaa !9
  ret double %58
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nounwind "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 = { 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 #4 = { 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, !12, i64 1088}
!10 = !{!"_ZTS14R3MeshProperty", !6, i64 0, !7, i64 8, !11, i64 1032, !6, i64 1040, !12, i64 1048, !12, i64 1056, !12, i64 1064, !12, i64 1072, !12, i64 1080, !12, i64 1088}
!11 = !{!"int", !7, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = !{!10, !11, i64 1032}
!14 = !{!12, !12, i64 0}
!15 = !{!11, !11, i64 0}
!16 = !{!10, !6, i64 1040}
!17 = distinct !{!17, !18}
!18 = !{!"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 nounwind willreturn memory(write)
declare double @sqrt(double noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind memory(readwrite, inaccessiblemem: write) uwtable
define dso_local noundef double @_ZNK14R3MeshProperty6L2NormEv(ptr nocapture noundef nonnull align 8 dereferenceable(1096) %0) local_unnamed_addr #2 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 1088
  %3 = load double, ptr %2, align 8, !tbaa !5
  %4 = fcmp oeq double %3, -1.000000e+00
  br i1 %4, label %5, label %27

5:                                                ; preds = %1
  store double 0.000000e+00, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 1032
  %7 = load i32, ptr %6, align 8, !tbaa !12
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %.preheader, label %27

.preheader:                                       ; preds = %5
  %9 = getelementptr inbounds i8, ptr %0, i64 1040
  %10 = load ptr, ptr %9, align 8, !tbaa !13
  %wide.trip.count = zext nneg i32 %7 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %11 = icmp eq i32 %7, 1
  br i1 %11, label %.unr-lcssa, label %.preheader.new

.preheader.new:                                   ; preds = %.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %18

.unr-lcssa:                                       ; preds = %18, %.preheader
  %.1.lcssa.ph = phi double [ poison, %.preheader ], [ %.1.1, %18 ]
  %indvars.iv.unr = phi i64 [ 0, %.preheader ], [ %indvars.iv.next.1, %18 ]
  %.0911.unr = phi double [ 0.000000e+00, %.preheader ], [ %.1.1, %18 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %16, label %.epilog-lcssa

.epilog-lcssa:                                    ; preds = %.unr-lcssa
  %12 = getelementptr inbounds double, ptr %10, i64 %indvars.iv.unr
  %13 = load double, ptr %12, align 8, !tbaa !14
  %14 = fcmp oeq double %13, -1.000000e+00
  %15 = tail call double @llvm.fmuladd.f64(double %13, double %13, double %.0911.unr)
  %.1.epil = select i1 %14, double %.0911.unr, double %15
  br label %16

16:                                               ; preds = %.unr-lcssa, %.epilog-lcssa
  %.1.lcssa = phi double [ %.1.lcssa.ph, %.unr-lcssa ], [ %.1.epil, %.epilog-lcssa ]
  %17 = tail call double @sqrt(double noundef %.1.lcssa) #3
  store double %17, ptr %2, align 8, !tbaa !5
  br label %27

18:                                               ; preds = %18, %.preheader.new
  %indvars.iv = phi i64 [ 0, %.preheader.new ], [ %indvars.iv.next.1, %18 ]
  %.0911 = phi double [ 0.000000e+00, %.preheader.new ], [ %.1.1, %18 ]
  %niter = phi i64 [ 0, %.preheader.new ], [ %niter.next.1, %18 ]
  %19 = getelementptr inbounds double, ptr %10, i64 %indvars.iv
  %20 = load double, ptr %19, align 8, !tbaa !14
  %21 = fcmp oeq double %20, -1.000000e+00
  %22 = tail call double @llvm.fmuladd.f64(double %20, double %20, double %.0911)
  %.1 = select i1 %21, double %.0911, double %22
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %23 = getelementptr inbounds double, ptr %10, i64 %indvars.iv.next
  %24 = load double, ptr %23, align 8, !tbaa !14
  %25 = fcmp oeq double %24, -1.000000e+00
  %26 = tail call double @llvm.fmuladd.f64(double %24, double %24, double %.1)
  %.1.1 = select i1 %25, double %.1, double %26
  %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 %.unr-lcssa, label %18, !llvm.loop !15

27:                                               ; preds = %5, %16, %1
  %28 = phi double [ 0.000000e+00, %5 ], [ %17, %16 ], [ %3, %1 ]
  ret double %28
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { mustprogress nofree nounwind willreturn memory(write) "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 = { mustprogress nofree nounwind memory(readwrite, inaccessiblemem: write) 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, !11, i64 1088}
!6 = !{!"_ZTS14R3MeshProperty", !7, i64 0, !8, i64 8, !10, i64 1032, !7, i64 1040, !11, i64 1048, !11, i64 1056, !11, i64 1064, !11, i64 1072, !11, i64 1080, !11, i64 1088}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"double", !8, i64 0}
!12 = !{!6, !10, i64 1032}
!13 = !{!6, !7, i64 1040}
!14 = !{!11, !11, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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