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

; 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.R2Point = type { [2 x double] }
%class.R2Vector = type { [2 x double] }
%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

declare void @_ZN8R2VectorC1Edd(ptr noundef nonnull align 8 dereferenceable(16), double noundef, double noundef) unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZNK6R2Grid12GridCentroidEv(ptr dead_on_unwind noalias writable sret(%class.R2Point) align 8 %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 ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca %class.R2Vector, align 8
  %11 = alloca double, align 8
  %12 = alloca %class.R2Vector, align 8
  store ptr %0, ptr %3, align 8
  store ptr %1, ptr %4, align 8, !tbaa !5
  %13 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  store double 0.000000e+00, ptr %5, align 8, !tbaa !9
  call void @_ZN7R2PointC1Edd(ptr noundef nonnull align 8 dereferenceable(16) %0, double noundef 0.000000e+00, double noundef 0.000000e+00)
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %14 = getelementptr inbounds %class.R2Grid, ptr %13, i32 0, i32 3
  %15 = load ptr, ptr %14, align 8, !tbaa !11
  store ptr %15, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !17
  br label %16

16:                                               ; preds = %48, %2
  %17 = load i32, ptr %7, align 4, !tbaa !17
  %18 = getelementptr inbounds %class.R2Grid, ptr %13, i32 0, i32 4
  %19 = getelementptr inbounds [2 x i32], ptr %18, i64 0, i64 1
  %20 = load i32, ptr %19, align 4, !tbaa !17
  %21 = icmp slt i32 %17, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %16
  store i32 2, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %51

23:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !17
  br label %24

24:                                               ; preds = %44, %23
  %25 = load i32, ptr %9, align 4, !tbaa !17
  %26 = getelementptr inbounds %class.R2Grid, ptr %13, i32 0, i32 4
  %27 = getelementptr inbounds [2 x i32], ptr %26, i64 0, i64 0
  %28 = load i32, ptr %27, align 8, !tbaa !17
  %29 = icmp slt i32 %25, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %24
  store i32 5, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %47

31:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 16, ptr %10) #4
  %32 = load i32, ptr %9, align 4, !tbaa !17
  %33 = sitofp i32 %32 to double
  %34 = load i32, ptr %7, align 4, !tbaa !17
  %35 = sitofp i32 %34 to double
  call void @_ZN8R2VectorC1Edd(ptr noundef nonnull align 8 dereferenceable(16) %10, double noundef %33, double noundef %35)
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds double, ptr %36, i32 1
  store ptr %37, ptr %6, align 8, !tbaa !5
  %38 = load double, ptr %36, align 8, !tbaa !9
  store double %38, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %12) #4
  %39 = load double, ptr %11, align 8, !tbaa !9
  call void @_ZmldRK8R2Vector(ptr dead_on_unwind writable sret(%class.R2Vector) align 8 %12, double noundef %39, ptr noundef nonnull align 8 dereferenceable(16) %10)
  %40 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZN7R2PointpLERK8R2Vector(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr noundef nonnull align 8 dereferenceable(16) %12)
  call void @llvm.lifetime.end.p0(i64 16, ptr %12) #4
  %41 = load double, ptr %11, align 8, !tbaa !9
  %42 = load double, ptr %5, align 8, !tbaa !9
  %43 = fadd double %42, %41
  store double %43, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %10) #4
  br label %44

44:                                               ; preds = %31
  %45 = load i32, ptr %9, align 4, !tbaa !17
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %9, align 4, !tbaa !17
  br label %24, !llvm.loop !18

47:                                               ; preds = %30
  br label %48

48:                                               ; preds = %47
  %49 = load i32, ptr %7, align 4, !tbaa !17
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %7, align 4, !tbaa !17
  br label %16, !llvm.loop !20

51:                                               ; preds = %22
  %52 = load double, ptr %5, align 8, !tbaa !9
  %53 = fcmp ogt double %52, 0.000000e+00
  br i1 %53, label %54, label %57

54:                                               ; preds = %51
  %55 = load double, ptr %5, align 8, !tbaa !9
  %56 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZN7R2PointdVEd(ptr noundef nonnull align 8 dereferenceable(16) %0, double noundef %55)
  br label %57

57:                                               ; preds = %54, %51
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret void
}

declare void @_ZN7R2PointC1Edd(ptr noundef nonnull align 8 dereferenceable(16), double noundef, double noundef) unnamed_addr #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZmldRK8R2Vector(ptr dead_on_unwind noalias writable sret(%class.R2Vector) align 8, double noundef, ptr noundef nonnull align 8 dereferenceable(16)) #3

declare noundef nonnull align 8 dereferenceable(16) ptr @_ZN7R2PointpLERK8R2Vector(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) #1

declare noundef nonnull align 8 dereferenceable(16) ptr @_ZN7R2PointdVEd(ptr noundef nonnull align 8 dereferenceable(16), double noundef) #1

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 = { mustprogress 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 = { inlinehint mustprogress 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, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !6, i64 184}
!12 = !{!"_ZTS6R2Grid", !13, i64 0, !13, i64 88, !10, i64 176, !6, i64 184, !7, i64 192, !16, i64 200, !16, i64 204}
!13 = !{!"_ZTS8R2Affine", !14, i64 0, !15, i64 8, !16, i64 80}
!14 = !{!"_ZTS16R2Transformation"}
!15 = !{!"_ZTS8R3Matrix", !7, i64 0}
!16 = !{!"int", !7, i64 0}
!17 = !{!16, !16, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19}


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

%class.R2Point = type { [2 x double] }
%class.R2Vector = type { [2 x double] }

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

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

declare void @_ZN8R2VectorC1Edd(ptr noundef nonnull align 8 dereferenceable(16), double noundef, double noundef) unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZNK6R2Grid12GridCentroidEv(ptr dead_on_unwind noalias nonnull writable sret(%class.R2Point) align 8 %0, ptr nocapture noundef nonnull readonly align 8 dereferenceable(208) %1) local_unnamed_addr #2 align 2 {
  %3 = alloca %class.R2Vector, align 8
  %4 = alloca %class.R2Vector, align 8
  tail call void @_ZN7R2PointC1Edd(ptr noundef nonnull align 8 dereferenceable(16) %0, double noundef 0.000000e+00, double noundef 0.000000e+00)
  %5 = getelementptr inbounds i8, ptr %1, i64 192
  %6 = getelementptr inbounds i8, ptr %1, i64 196
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %.preheader.lr.ph, label %._crit_edge23.thread

.preheader.lr.ph:                                 ; preds = %2
  %9 = load i32, ptr %5, align 8, !tbaa !5
  %10 = icmp sgt i32 %9, 0
  br i1 %10, label %.preheader.preheader, label %._crit_edge23.thread

.preheader.preheader:                             ; preds = %.preheader.lr.ph
  %11 = getelementptr inbounds i8, ptr %1, i64 184
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %13 = phi i32 [ %18, %._crit_edge ], [ %7, %.preheader.preheader ]
  %14 = phi i32 [ %19, %._crit_edge ], [ %9, %.preheader.preheader ]
  %.022 = phi double [ %.1.lcssa, %._crit_edge ], [ 0.000000e+00, %.preheader.preheader ]
  %.01321 = phi i32 [ %20, %._crit_edge ], [ 0, %.preheader.preheader ]
  %.01420 = phi ptr [ %.115.lcssa, %._crit_edge ], [ %12, %.preheader.preheader ]
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader
  %16 = uitofp nneg i32 %.01321 to double
  br label %22

._crit_edge23:                                    ; preds = %._crit_edge
  %17 = fcmp ogt double %.1.lcssa, 0.000000e+00
  br i1 %17, label %31, label %._crit_edge23.thread

._crit_edge.loopexit:                             ; preds = %22
  %.pre = load i32, ptr %6, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %18 = phi i32 [ %13, %.preheader ], [ %.pre, %._crit_edge.loopexit ]
  %19 = phi i32 [ %14, %.preheader ], [ %29, %._crit_edge.loopexit ]
  %.115.lcssa = phi ptr [ %.01420, %.preheader ], [ %24, %._crit_edge.loopexit ]
  %.1.lcssa = phi double [ %.022, %.preheader ], [ %27, %._crit_edge.loopexit ]
  %20 = add nuw nsw i32 %.01321, 1
  %21 = icmp slt i32 %20, %18
  br i1 %21, label %.preheader, label %._crit_edge23, !llvm.loop !16

22:                                               ; preds = %.lr.ph, %22
  %.118 = phi double [ %.022, %.lr.ph ], [ %27, %22 ]
  %.01217 = phi i32 [ 0, %.lr.ph ], [ %28, %22 ]
  %.11516 = phi ptr [ %.01420, %.lr.ph ], [ %24, %22 ]
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #4
  %23 = uitofp nneg i32 %.01217 to double
  call void @_ZN8R2VectorC1Edd(ptr noundef nonnull align 8 dereferenceable(16) %3, double noundef %23, double noundef %16)
  %24 = getelementptr inbounds i8, ptr %.11516, i64 8
  %25 = load double, ptr %.11516, align 8, !tbaa !19
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #4
  call void @_ZmldRK8R2Vector(ptr dead_on_unwind nonnull writable sret(%class.R2Vector) align 8 %4, double noundef %25, ptr noundef nonnull align 8 dereferenceable(16) %3)
  %26 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZN7R2PointpLERK8R2Vector(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr noundef nonnull align 8 dereferenceable(16) %4)
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #4
  %27 = fadd double %.118, %25
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #4
  %28 = add nuw nsw i32 %.01217, 1
  %29 = load i32, ptr %5, align 8, !tbaa !5
  %30 = icmp slt i32 %28, %29
  br i1 %30, label %22, label %._crit_edge.loopexit, !llvm.loop !20

31:                                               ; preds = %._crit_edge23
  %32 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZN7R2PointdVEd(ptr noundef nonnull align 8 dereferenceable(16) %0, double noundef %.1.lcssa)
  br label %._crit_edge23.thread

._crit_edge23.thread:                             ; preds = %.preheader.lr.ph, %2, %31, %._crit_edge23
  ret void
}

declare void @_ZN7R2PointC1Edd(ptr noundef nonnull align 8 dereferenceable(16), double noundef, double noundef) unnamed_addr #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZmldRK8R2Vector(ptr dead_on_unwind noalias writable sret(%class.R2Vector) align 8, double noundef, ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #3

declare noundef nonnull align 8 dereferenceable(16) ptr @_ZN7R2PointpLERK8R2Vector(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #1

declare noundef nonnull align 8 dereferenceable(16) ptr @_ZN7R2PointdVEd(ptr noundef nonnull align 8 dereferenceable(16), double noundef) local_unnamed_addr #1

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 = { mustprogress 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 = { inlinehint mustprogress 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !15, i64 184}
!10 = !{!"_ZTS6R2Grid", !11, i64 0, !11, i64 88, !14, i64 176, !15, i64 184, !7, i64 192, !6, i64 200, !6, i64 204}
!11 = !{!"_ZTS8R2Affine", !12, i64 0, !13, i64 8, !6, i64 80}
!12 = !{!"_ZTS16R2Transformation"}
!13 = !{!"_ZTS8R3Matrix", !7, i64 0}
!14 = !{!"double", !7, i64 0}
!15 = !{!"any pointer", !7, i64 0}
!16 = distinct !{!16, !17, !18}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!"llvm.loop.unswitch.partial.disable"}
!19 = !{!14, !14, i64 0}
!20 = distinct !{!20, !17}


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