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

; 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: mustprogress nounwind uwtable
define dso_local noundef double @_ZNK14R3MeshProperty4MeanEv(ptr noundef nonnull align 8 dereferenceable(1096) %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca double, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %7 = load ptr, ptr %2, align 8
  %8 = getelementptr inbounds %struct.R3MeshProperty, ptr %7, i32 0, i32 4
  %9 = load double, ptr %8, align 8, !tbaa !9
  %10 = fcmp oeq double %9, -1.000000e+00
  br i1 %10, label %11, label %59

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

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

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

23:                                               ; preds = %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %47

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

32:                                               ; preds = %24
  br label %44

33:                                               ; preds = %24
  %34 = getelementptr inbounds %struct.R3MeshProperty, ptr %7, i32 0, i32 3
  %35 = load ptr, ptr %34, align 8, !tbaa !16
  %36 = load i32, ptr %6, align 4, !tbaa !14
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds double, ptr %35, i64 %37
  %39 = load double, ptr %38, align 8, !tbaa !15
  %40 = load double, ptr %5, align 8, !tbaa !15
  %41 = fadd double %40, %39
  store double %41, ptr %5, align 8, !tbaa !15
  %42 = load i32, ptr %4, align 4, !tbaa !14
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %4, align 4, !tbaa !14
  br label %44

44:                                               ; preds = %33, %32
  %45 = load i32, ptr %6, align 4, !tbaa !14
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %6, align 4, !tbaa !14
  br label %18, !llvm.loop !17

47:                                               ; preds = %23
  %48 = load i32, ptr %4, align 4, !tbaa !14
  %49 = icmp sgt i32 %48, 0
  br i1 %49, label %50, label %57

50:                                               ; preds = %47
  %51 = load double, ptr %5, align 8, !tbaa !15
  %52 = load i32, ptr %4, align 4, !tbaa !14
  %53 = sitofp i32 %52 to double
  %54 = fdiv double %51, %53
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.R3MeshProperty, ptr %55, i32 0, i32 4
  store double %54, ptr %56, align 8, !tbaa !9
  br label %57

57:                                               ; preds = %50, %47
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %58

58:                                               ; preds = %57, %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  br label %59

59:                                               ; preds = %58, %1
  %60 = getelementptr inbounds %struct.R3MeshProperty, ptr %7, i32 0, i32 4
  %61 = load double, ptr %60, align 8, !tbaa !9
  ret double %61
}

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, !12, i64 1048}
!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 = !{!11, !11, i64 0}
!15 = !{!12, !12, 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 nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local noundef double @_ZNK14R3MeshProperty4MeanEv(ptr nocapture noundef nonnull align 8 dereferenceable(1096) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 1048
  %3 = load double, ptr %2, align 8, !tbaa !5
  %4 = fcmp oeq double %3, -1.000000e+00
  br i1 %4, label %5, label %32

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 %32

.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
  %.113.lcssa.ph = phi i32 [ poison, %.preheader ], [ %.113.1, %18 ]
  %.1.lcssa.ph = phi double [ poison, %.preheader ], [ %.1.1, %18 ]
  %indvars.iv.unr = phi i64 [ 0, %.preheader ], [ %indvars.iv.next.1, %18 ]
  %.01116.unr = phi double [ 0.000000e+00, %.preheader ], [ %.1.1, %18 ]
  %.01215.unr = phi i32 [ 0, %.preheader ], [ %.113.1, %18 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %17, 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 une double %13, -1.000000e+00
  %15 = fadd double %.01116.unr, %13
  %16 = zext i1 %14 to i32
  %.113.epil = add nuw nsw i32 %.01215.unr, %16
  %.1.epil = select i1 %14, double %15, double %.01116.unr
  br label %17

17:                                               ; preds = %.unr-lcssa, %.epilog-lcssa
  %.113.lcssa = phi i32 [ %.113.lcssa.ph, %.unr-lcssa ], [ %.113.epil, %.epilog-lcssa ]
  %.1.lcssa = phi double [ %.1.lcssa.ph, %.unr-lcssa ], [ %.1.epil, %.epilog-lcssa ]
  %.not = icmp eq i32 %.113.lcssa, 0
  br i1 %.not, label %32, label %29

18:                                               ; preds = %18, %.preheader.new
  %indvars.iv = phi i64 [ 0, %.preheader.new ], [ %indvars.iv.next.1, %18 ]
  %.01116 = phi double [ 0.000000e+00, %.preheader.new ], [ %.1.1, %18 ]
  %.01215 = phi i32 [ 0, %.preheader.new ], [ %.113.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 une double %20, -1.000000e+00
  %22 = fadd double %.01116, %20
  %23 = zext i1 %21 to i32
  %.113 = add nuw nsw i32 %.01215, %23
  %.1 = select i1 %21, double %22, double %.01116
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %24 = getelementptr inbounds double, ptr %10, i64 %indvars.iv.next
  %25 = load double, ptr %24, align 8, !tbaa !14
  %26 = fcmp une double %25, -1.000000e+00
  %27 = fadd double %.1, %25
  %28 = zext i1 %26 to i32
  %.113.1 = add nuw nsw i32 %.113, %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 %.unr-lcssa, label %18, !llvm.loop !15

29:                                               ; preds = %17
  %30 = uitofp nneg i32 %.113.lcssa to double
  %31 = fdiv double %.1.lcssa, %30
  store double %31, ptr %2, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %5, %29, %17, %1
  %33 = phi double [ 0.000000e+00, %5 ], [ %31, %29 ], [ 0.000000e+00, %17 ], [ %3, %1 ]
  ret double %33
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(read, argmem: readwrite, 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, !11, i64 1048}
!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,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass