; LLVM IR from niessner/Matterport :: code/gaps/apps/mpview/mp.ll (ZNK7MPHouse16FindClosestImageERK7R3PointRK8R3Vectordii)

; 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.R3Halfspace = type { %class.R3Plane }
%class.R3Plane = type { %class.R3Vector, double }
%class.R3Vector = type { [3 x double] }
%struct.MPHouse = type { %class.RNArray.0, %class.RNArray.1, %class.RNArray.2, %class.RNArray.3, %class.RNArray.4, %class.RNArray.5, %class.RNArray.6, %class.RNArray.7, %class.RNArray.8, %class.RNArray.9, %class.RGBDConfiguration, ptr, ptr, %class.R3Box, ptr, ptr }
%class.RNArray.0 = type { %class.RNVArray }
%class.RNVArray = type { ptr, i32, i32 }
%class.RNArray.1 = type { %class.RNVArray }
%class.RNArray.2 = type { %class.RNVArray }
%class.RNArray.3 = type { %class.RNVArray }
%class.RNArray.4 = type { %class.RNVArray }
%class.RNArray.5 = type { %class.RNVArray }
%class.RNArray.6 = type { %class.RNVArray }
%class.RNArray.7 = type { %class.RNVArray }
%class.RNArray.8 = type { %class.RNVArray }
%class.RNArray.9 = type { %class.RNVArray }
%class.RGBDConfiguration = type { ptr, %class.RNArray.10, %class.RNArray.11, ptr, ptr, ptr, ptr, %class.R3Box }
%class.RNArray.10 = type { %class.RNVArray }
%class.RNArray.11 = type { %class.RNVArray }
%class.R3Box = type { %class.R3Solid, %class.R3Point, %class.R3Point }
%class.R3Solid = type { %class.R3Shape }
%class.R3Shape = type { ptr }
%class.R3Point = type { [3 x double] }
%struct.MPImage = type { ptr, i32, ptr, i32, ptr, i32, i32, %class.RGBDImage, %class.R4Matrix, %class.R3Matrix, i32, i32, %class.R3Point }
%class.RGBDImage = type { ptr, ptr, i32, %class.RNArray, i32, i32, %class.R3Affine, %class.R3Matrix, %class.R3Box, i32, ptr, ptr, ptr, i32, i32 }
%class.RNArray = type { %class.RNVArray }
%class.R3Affine = type { %class.R3Transformation, %class.R4Matrix, %class.R4Matrix, %class.RNFlags }
%class.R3Transformation = type { ptr }
%class.RNFlags = type { i64 }
%class.R4Matrix = type { [4 x [4 x double]] }
%class.R3Matrix = type { [3 x [3 x double]] }

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK8RNVArray8NEntriesEv(ptr noundef nonnull align 8 dereferenceable(16)) #0 align 2

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

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZNK7RNArrayIP7MPImageE3KthEi(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef) #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZNK7MPHouse16FindClosestImageERK7R3PointRK8R3Vectordii(ptr noundef nonnull align 8 dereferenceable(376) %0, ptr noundef nonnull align 8 dereferenceable(24) %1, ptr noundef nonnull align 8 dereferenceable(24) %2, double noundef %3, i32 noundef %4, i32 noundef %5) #2 align 2 {
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca double, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca double, align 8
  %15 = alloca %class.R3Halfspace, align 8
  %16 = alloca i32, align 4
  %17 = alloca i32, align 4
  %18 = alloca ptr, align 8
  %19 = alloca double, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store ptr %2, ptr %9, align 8, !tbaa !5
  store double %3, ptr %10, align 8, !tbaa !9
  store i32 %4, ptr %11, align 4, !tbaa !11
  store i32 %5, ptr %12, align 4, !tbaa !11
  %20 = load ptr, ptr %7, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  store ptr null, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #5
  %21 = load double, ptr %10, align 8, !tbaa !9
  store double %21, ptr %14, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 32, ptr %15) #5
  %22 = load ptr, ptr %8, align 8, !tbaa !5
  %23 = load ptr, ptr %9, align 8, !tbaa !5
  call void @_ZN11R3HalfspaceC1ERK7R3PointRK8R3Vector(ptr noundef nonnull align 8 dereferenceable(32) %15, ptr noundef nonnull align 8 dereferenceable(24) %22, ptr noundef nonnull align 8 dereferenceable(24) %23)
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #5
  store i32 0, ptr %16, align 4, !tbaa !11
  br label %24

24:                                               ; preds = %58, %6
  %25 = load i32, ptr %16, align 4, !tbaa !11
  %26 = getelementptr inbounds %struct.MPHouse, ptr %20, i32 0, i32 0
  %27 = call noundef i32 @_ZNK8RNVArray8NEntriesEv(ptr noundef nonnull align 8 dereferenceable(16) %26)
  %28 = icmp slt i32 %25, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %24
  store i32 2, ptr %17, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #5
  br label %61

30:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #5
  %31 = getelementptr inbounds %struct.MPHouse, ptr %20, i32 0, i32 0
  %32 = load i32, ptr %16, align 4, !tbaa !11
  %33 = call noundef ptr @_ZNK7RNArrayIP7MPImageE3KthEi(ptr noundef nonnull align 8 dereferenceable(16) %31, i32 noundef %32)
  store ptr %33, ptr %18, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %19) #5
  %34 = load ptr, ptr %8, align 8, !tbaa !5
  %35 = load ptr, ptr %18, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.MPImage, ptr %35, i32 0, i32 12
  %37 = call noundef double @_Z10R3DistanceRK7R3PointS1_(ptr noundef nonnull align 8 dereferenceable(24) %34, ptr noundef nonnull align 8 dereferenceable(24) %36)
  store double %37, ptr %19, align 8, !tbaa !9
  %38 = load double, ptr %19, align 8, !tbaa !9
  %39 = load double, ptr %14, align 8, !tbaa !9
  %40 = fcmp oge double %38, %39
  br i1 %40, label %41, label %42

41:                                               ; preds = %30
  store i32 4, ptr %17, align 4
  br label %55

42:                                               ; preds = %30
  %43 = load i32, ptr %11, align 4, !tbaa !11
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %52

45:                                               ; preds = %42
  %46 = load ptr, ptr %18, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.MPImage, ptr %46, i32 0, i32 12
  %48 = call noundef i32 @_Z12R3IntersectsRK11R3HalfspaceRK7R3Point(ptr noundef nonnull align 8 dereferenceable(32) %15, ptr noundef nonnull align 8 dereferenceable(24) %47)
  %49 = icmp ne i32 %48, 0
  br i1 %49, label %51, label %50

50:                                               ; preds = %45
  store i32 4, ptr %17, align 4
  br label %55

51:                                               ; preds = %45
  br label %52

52:                                               ; preds = %51, %42
  %53 = load ptr, ptr %18, align 8, !tbaa !5
  store ptr %53, ptr %13, align 8, !tbaa !5
  %54 = load double, ptr %19, align 8, !tbaa !9
  store double %54, ptr %14, align 8, !tbaa !9
  store i32 0, ptr %17, align 4
  br label %55

55:                                               ; preds = %52, %50, %41
  call void @llvm.lifetime.end.p0(i64 8, ptr %19) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #5
  %56 = load i32, ptr %17, align 4
  switch i32 %56, label %63 [
    i32 0, label %57
    i32 4, label %58
  ]

57:                                               ; preds = %55
  br label %58

58:                                               ; preds = %57, %55
  %59 = load i32, ptr %16, align 4, !tbaa !11
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %16, align 4, !tbaa !11
  br label %24, !llvm.loop !13

61:                                               ; preds = %29
  %62 = load ptr, ptr %13, align 8, !tbaa !5
  store i32 1, ptr %17, align 4
  call void @llvm.lifetime.end.p0(i64 32, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  ret ptr %62

63:                                               ; preds = %55
  unreachable
}

declare void @_ZN11R3HalfspaceC1ERK7R3PointRK8R3Vector(ptr noundef nonnull align 8 dereferenceable(32), ptr noundef nonnull align 8 dereferenceable(24), ptr noundef nonnull align 8 dereferenceable(24)) unnamed_addr #3

declare noundef double @_Z10R3DistanceRK7R3PointS1_(ptr noundef nonnull align 8 dereferenceable(24), ptr noundef nonnull align 8 dereferenceable(24)) #3

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i32 @_Z12R3IntersectsRK11R3HalfspaceRK7R3Point(ptr noundef nonnull align 8 dereferenceable(32), ptr noundef nonnull align 8 dereferenceable(24)) #4

attributes #0 = { inlinehint 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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 = { 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 #5 = { 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 = distinct !{!13, !14}
!14 = !{!"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"

%class.R3Halfspace = type { %class.R3Plane }
%class.R3Plane = type { %class.R3Vector, double }
%class.R3Vector = type { [3 x double] }

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK8RNVArray8NEntriesEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #0 align 2

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

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZNK7RNArrayIP7MPImageE3KthEi(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZNK7MPHouse16FindClosestImageERK7R3PointRK8R3Vectordii(ptr noundef nonnull align 8 dereferenceable(376) %0, ptr noundef nonnull align 8 dereferenceable(24) %1, ptr noundef nonnull align 8 dereferenceable(24) %2, double noundef %3, i32 noundef %4, i32 noundef %5) local_unnamed_addr #2 align 2 {
  %7 = alloca %class.R3Halfspace, align 8
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %7) #5
  call void @_ZN11R3HalfspaceC1ERK7R3PointRK8R3Vector(ptr noundef nonnull align 8 dereferenceable(32) %7, ptr noundef nonnull align 8 dereferenceable(24) %1, ptr noundef nonnull align 8 dereferenceable(24) %2)
  %8 = call noundef i32 @_ZNK8RNVArray8NEntriesEv(ptr noundef nonnull align 8 dereferenceable(16) %0)
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %6
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %.lr.ph.split.us
  %.022.us = phi ptr [ %.1.us, %.lr.ph.split.us ], [ null, %.lr.ph ]
  %.01521.us = phi double [ %.116.us, %.lr.ph.split.us ], [ %3, %.lr.ph ]
  %.01720.us = phi i32 [ %14, %.lr.ph.split.us ], [ 0, %.lr.ph ]
  %10 = call noundef ptr @_ZNK7RNArrayIP7MPImageE3KthEi(ptr noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %.01720.us)
  %11 = getelementptr inbounds i8, ptr %10, i64 744
  %12 = call noundef double @_Z10R3DistanceRK7R3PointS1_(ptr noundef nonnull align 8 dereferenceable(24) %1, ptr noundef nonnull align 8 dereferenceable(24) %11)
  %13 = fcmp ult double %12, %.01521.us
  %.116.us = select i1 %13, double %12, double %.01521.us
  %.1.us = select i1 %13, ptr %10, ptr %.022.us
  %14 = add nuw nsw i32 %.01720.us, 1
  %15 = call noundef i32 @_ZNK8RNVArray8NEntriesEv(ptr noundef nonnull align 8 dereferenceable(16) %0)
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %.lr.ph.split.us, label %._crit_edge, !llvm.loop !5

._crit_edge:                                      ; preds = %24, %.lr.ph.split.us, %6
  %.0.lcssa = phi ptr [ null, %6 ], [ %.1.us, %.lr.ph.split.us ], [ %.1, %24 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %7) #5
  ret ptr %.0.lcssa

.lr.ph.split:                                     ; preds = %.lr.ph, %24
  %.022 = phi ptr [ %.1, %24 ], [ null, %.lr.ph ]
  %.01521 = phi double [ %.116, %24 ], [ %3, %.lr.ph ]
  %.01720 = phi i32 [ %25, %24 ], [ 0, %.lr.ph ]
  %17 = call noundef ptr @_ZNK7RNArrayIP7MPImageE3KthEi(ptr noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %.01720)
  %18 = getelementptr inbounds i8, ptr %17, i64 744
  %19 = call noundef double @_Z10R3DistanceRK7R3PointS1_(ptr noundef nonnull align 8 dereferenceable(24) %1, ptr noundef nonnull align 8 dereferenceable(24) %18)
  %20 = fcmp ult double %19, %.01521
  br i1 %20, label %21, label %24

21:                                               ; preds = %.lr.ph.split
  %22 = call noundef i32 @_Z12R3IntersectsRK11R3HalfspaceRK7R3Point(ptr noundef nonnull align 8 dereferenceable(32) %7, ptr noundef nonnull align 8 dereferenceable(24) %18)
  %.not19 = icmp eq i32 %22, 0
  br i1 %.not19, label %24, label %23

23:                                               ; preds = %21
  br label %24

24:                                               ; preds = %21, %.lr.ph.split, %23
  %.116 = phi double [ %19, %23 ], [ %.01521, %.lr.ph.split ], [ %.01521, %21 ]
  %.1 = phi ptr [ %17, %23 ], [ %.022, %.lr.ph.split ], [ %.022, %21 ]
  %25 = add nuw nsw i32 %.01720, 1
  %26 = call noundef i32 @_ZNK8RNVArray8NEntriesEv(ptr noundef nonnull align 8 dereferenceable(16) %0)
  %27 = icmp slt i32 %25, %26
  br i1 %27, label %.lr.ph.split, label %._crit_edge, !llvm.loop !5
}

declare void @_ZN11R3HalfspaceC1ERK7R3PointRK8R3Vector(ptr noundef nonnull align 8 dereferenceable(32), ptr noundef nonnull align 8 dereferenceable(24), ptr noundef nonnull align 8 dereferenceable(24)) unnamed_addr #3

declare noundef double @_Z10R3DistanceRK7R3PointS1_(ptr noundef nonnull align 8 dereferenceable(24), ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #3

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i32 @_Z12R3IntersectsRK11R3HalfspaceRK7R3Point(ptr noundef nonnull align 8 dereferenceable(32), ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #4

attributes #0 = { inlinehint 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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 = { 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 #5 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass