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

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

9:                                                ; preds = %35, %1
  %10 = load i32, ptr %6, align 4, !tbaa !9
  %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) #2
  br label %38

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 !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds double, ptr %17, i64 %19
  %21 = load double, ptr %20, align 8, !tbaa !11
  %22 = fcmp une double %21, -3.210000e+02
  br i1 %22, label %23, label %34

23:                                               ; preds = %15
  %24 = getelementptr inbounds %class.R2Grid, ptr %8, i32 0, i32 3
  %25 = load ptr, ptr %24, align 8, !tbaa !18
  %26 = load i32, ptr %6, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds double, ptr %25, i64 %27
  %29 = load double, ptr %28, align 8, !tbaa !11
  %30 = load double, ptr %5, align 8, !tbaa !11
  %31 = fadd double %30, %29
  store double %31, ptr %5, align 8, !tbaa !11
  %32 = load i32, ptr %4, align 4, !tbaa !9
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %4, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %23, %15
  br label %35

35:                                               ; preds = %34
  %36 = load i32, ptr %6, align 4, !tbaa !9
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %6, align 4, !tbaa !9
  br label %9, !llvm.loop !19

38:                                               ; preds = %14
  %39 = load i32, ptr %4, align 4, !tbaa !9
  %40 = icmp eq i32 %39, 0
  br i1 %40, label %41, label %42

41:                                               ; preds = %38
  store double 0.000000e+00, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %47

42:                                               ; preds = %38
  %43 = load double, ptr %5, align 8, !tbaa !11
  %44 = load i32, ptr %4, align 4, !tbaa !9
  %45 = sitofp i32 %44 to double
  %46 = fdiv double %43, %45
  store double %46, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %47

47:                                               ; preds = %42, %41
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %48 = load double, ptr %2, align 8
  ret double %48
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = !{!14, !10, i64 204}
!14 = !{!"_ZTS6R2Grid", !15, i64 0, !15, i64 88, !12, i64 176, !6, i64 184, !7, i64 192, !10, i64 200, !10, i64 204}
!15 = !{!"_ZTS8R2Affine", !16, i64 0, !17, i64 8, !10, 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 nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef double @_ZNK6R2Grid4MeanEv(ptr nocapture noundef nonnull readonly align 8 dereferenceable(208) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 204
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %._crit_edge.thread

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

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

._crit_edge.unr-lcssa:                            ; preds = %18, %.lr.ph
  %.112.lcssa.ph = phi i32 [ poison, %.lr.ph ], [ %.112.1, %18 ]
  %.1.lcssa.ph = phi double [ poison, %.lr.ph ], [ %.1.1, %18 ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %18 ]
  %.01016.unr = phi double [ 0.000000e+00, %.lr.ph ], [ %.1.1, %18 ]
  %.01115.unr = phi i32 [ 0, %.lr.ph ], [ %.112.1, %18 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %._crit_edge.epilog-lcssa

._crit_edge.epilog-lcssa:                         ; preds = %._crit_edge.unr-lcssa
  %8 = getelementptr inbounds double, ptr %6, i64 %indvars.iv.unr
  %9 = load double, ptr %8, align 8, !tbaa !16
  %.fr.epil = freeze double %9
  %10 = fcmp une double %.fr.epil, -3.210000e+02
  %11 = fadd double %.01016.unr, %.fr.epil
  %12 = zext i1 %10 to i32
  %.112.epil = add i32 %.01115.unr, %12
  %.1.epil = select i1 %10, double %11, double %.01016.unr
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.unr-lcssa, %._crit_edge.epilog-lcssa
  %.112.lcssa = phi i32 [ %.112.lcssa.ph, %._crit_edge.unr-lcssa ], [ %.112.epil, %._crit_edge.epilog-lcssa ]
  %.1.lcssa = phi double [ %.1.lcssa.ph, %._crit_edge.unr-lcssa ], [ %.1.epil, %._crit_edge.epilog-lcssa ]
  %13 = icmp eq i32 %.112.lcssa, 0
  %14 = uitofp nneg i32 %.112.lcssa to double
  %15 = fdiv double %.1.lcssa, %14
  br i1 %13, label %._crit_edge.thread, label %16

._crit_edge.thread:                               ; preds = %1, %._crit_edge
  br label %16

16:                                               ; preds = %._crit_edge, %._crit_edge.thread
  %17 = phi double [ 0.000000e+00, %._crit_edge.thread ], [ %15, %._crit_edge ]
  ret double %17

18:                                               ; preds = %18, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %18 ]
  %.01016 = phi double [ 0.000000e+00, %.lr.ph.new ], [ %.1.1, %18 ]
  %.01115 = phi i32 [ 0, %.lr.ph.new ], [ %.112.1, %18 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %18 ]
  %19 = getelementptr inbounds double, ptr %6, i64 %indvars.iv
  %20 = load double, ptr %19, align 8, !tbaa !16
  %.fr = freeze double %20
  %21 = fcmp une double %.fr, -3.210000e+02
  %22 = fadd double %.01016, %.fr
  %23 = zext i1 %21 to i32
  %.112 = add i32 %.01115, %23
  %.1 = select i1 %21, double %22, double %.01016
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %24 = getelementptr inbounds double, ptr %6, i64 %indvars.iv.next
  %25 = load double, ptr %24, align 8, !tbaa !16
  %.fr.1 = freeze double %25
  %26 = fcmp une double %.fr.1, -3.210000e+02
  %27 = fadd double %.1, %.fr.1
  %28 = zext i1 %26 to i32
  %.112.1 = add i32 %.112, %28
  %.1.1 = select i1 %26, double %27, double %.1
  %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.unr-lcssa, label %18, !llvm.loop !17
}

attributes #0 = { 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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass