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

; 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.R2Span = type { %class.R2Ray, %class.R2Point, double }
%class.R2Ray = type { %class.R2Line, %class.R2Point }
%class.R2Line = type { %class.R2Vector, %class.R2Vector, double }
%class.R2Vector = type { [2 x double] }
%class.R2Point = type { [2 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 @_ZN6R2SpanC1ERK7R2PointS2_(ptr noundef nonnull align 8 dereferenceable(80), ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) unnamed_addr #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef double @_Z10R2DistanceRK6R2SpanRK7R2Point(ptr noundef nonnull align 8 dereferenceable(80), ptr noundef nonnull align 8 dereferenceable(16)) #2

; Function Attrs: mustprogress uwtable
define dso_local noundef double @_Z10R2DistanceRK7R2PointRK9R2Polygon(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr noundef nonnull align 8 dereferenceable(68) %1) #3 {
  %3 = alloca double, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca double, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca %class.R2Span, align 8
  %11 = alloca double, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = call noundef i32 @_Z10R2ContainsRK9R2PolygonRK7R2Point(ptr noundef nonnull align 8 dereferenceable(68) %12, ptr noundef nonnull align 8 dereferenceable(16) %13)
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %2
  store double 0.000000e+00, ptr %3, align 8
  br label %50

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

18:                                               ; preds = %45, %17
  %19 = load i32, ptr %7, align 4, !tbaa !11
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = call noundef i32 @_ZNK9R2Polygon7NPointsEv(ptr noundef nonnull align 8 dereferenceable(68) %20)
  %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 %7) #5
  br label %48

24:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = load i32, ptr %7, align 4, !tbaa !11
  %27 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNK9R2Polygon5PointEi(ptr noundef nonnull align 8 dereferenceable(68) %25, i32 noundef %26)
  store ptr %27, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = load i32, ptr %7, align 4, !tbaa !11
  %30 = add nsw i32 %29, 1
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = call noundef i32 @_ZNK9R2Polygon7NPointsEv(ptr noundef nonnull align 8 dereferenceable(68) %31)
  %33 = srem i32 %30, %32
  %34 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNK9R2Polygon5PointEi(ptr noundef nonnull align 8 dereferenceable(68) %28, i32 noundef %33)
  store ptr %34, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 80, ptr %10) #5
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = load ptr, ptr %9, align 8, !tbaa !5
  call void @_ZN6R2SpanC1ERK7R2PointS2_(ptr noundef nonnull align 8 dereferenceable(80) %10, ptr noundef nonnull align 8 dereferenceable(16) %35, ptr noundef nonnull align 8 dereferenceable(16) %36)
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = call noundef double @_Z10R2DistanceRK6R2SpanRK7R2Point(ptr noundef nonnull align 8 dereferenceable(80) %10, ptr noundef nonnull align 8 dereferenceable(16) %37)
  store double %38, ptr %11, align 8, !tbaa !9
  %39 = load double, ptr %11, align 8, !tbaa !9
  %40 = load double, ptr %6, align 8, !tbaa !9
  %41 = fcmp olt double %39, %40
  br i1 %41, label %42, label %44

42:                                               ; preds = %24
  %43 = load double, ptr %11, align 8, !tbaa !9
  store double %43, ptr %6, align 8, !tbaa !9
  br label %44

44:                                               ; preds = %42, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 80, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  br label %45

45:                                               ; preds = %44
  %46 = load i32, ptr %7, align 4, !tbaa !11
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %7, align 4, !tbaa !11
  br label %18, !llvm.loop !13

48:                                               ; preds = %23
  %49 = load double, ptr %6, align 8, !tbaa !9
  store double %49, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  br label %50

50:                                               ; preds = %48, %16
  %51 = load double, ptr %3, align 8
  ret double %51
}

declare noundef i32 @_Z10R2ContainsRK9R2PolygonRK7R2Point(ptr noundef nonnull align 8 dereferenceable(68), ptr noundef nonnull align 8 dereferenceable(16)) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK9R2Polygon7NPointsEv(ptr noundef nonnull align 8 dereferenceable(68)) #4 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(16) ptr @_ZNK9R2Polygon5PointEi(ptr noundef nonnull align 8 dereferenceable(68), i32 noundef) #4 align 2

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 = { 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 #3 = { 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 = { 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 #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.R2Span = type { %class.R2Ray, %class.R2Point, double }
%class.R2Ray = type { %class.R2Line, %class.R2Point }
%class.R2Line = type { %class.R2Vector, %class.R2Vector, double }
%class.R2Vector = type { [2 x double] }
%class.R2Point = 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 @_ZN6R2SpanC1ERK7R2PointS2_(ptr noundef nonnull align 8 dereferenceable(80), ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) unnamed_addr #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef double @_Z10R2DistanceRK6R2SpanRK7R2Point(ptr noundef nonnull align 8 dereferenceable(80), ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
define dso_local noundef double @_Z10R2DistanceRK7R2PointRK9R2Polygon(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr noundef nonnull align 8 dereferenceable(68) %1) local_unnamed_addr #3 {
  %3 = alloca %class.R2Span, align 8
  %4 = tail call noundef i32 @_Z10R2ContainsRK9R2PolygonRK7R2Point(ptr noundef nonnull align 8 dereferenceable(68) %1, ptr noundef nonnull align 8 dereferenceable(16) %0)
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %2
  %5 = tail call noundef i32 @_ZNK9R2Polygon7NPointsEv(ptr noundef nonnull align 8 dereferenceable(68) %1)
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.01721 = phi double [ %.1, %.lr.ph ], [ 0x47EFFFFFE0000000, %.preheader ]
  %.01820 = phi i32 [ %8, %.lr.ph ], [ 0, %.preheader ]
  %7 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNK9R2Polygon5PointEi(ptr noundef nonnull align 8 dereferenceable(68) %1, i32 noundef %.01820)
  %8 = add nuw nsw i32 %.01820, 1
  %9 = call noundef i32 @_ZNK9R2Polygon7NPointsEv(ptr noundef nonnull align 8 dereferenceable(68) %1)
  %10 = srem i32 %8, %9
  %11 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNK9R2Polygon5PointEi(ptr noundef nonnull align 8 dereferenceable(68) %1, i32 noundef %10)
  call void @llvm.lifetime.start.p0(i64 80, ptr nonnull %3) #5
  call void @_ZN6R2SpanC1ERK7R2PointS2_(ptr noundef nonnull align 8 dereferenceable(80) %3, ptr noundef nonnull align 8 dereferenceable(16) %7, ptr noundef nonnull align 8 dereferenceable(16) %11)
  %12 = call noundef double @_Z10R2DistanceRK6R2SpanRK7R2Point(ptr noundef nonnull align 8 dereferenceable(80) %3, ptr noundef nonnull align 8 dereferenceable(16) %0)
  %13 = fcmp olt double %12, %.01721
  %.1 = select i1 %13, double %12, double %.01721
  call void @llvm.lifetime.end.p0(i64 80, ptr nonnull %3) #5
  %14 = call noundef i32 @_ZNK9R2Polygon7NPointsEv(ptr noundef nonnull align 8 dereferenceable(68) %1)
  %15 = icmp slt i32 %8, %14
  br i1 %15, label %.lr.ph, label %.loopexit, !llvm.loop !5

.loopexit:                                        ; preds = %.lr.ph, %.preheader, %2
  %.0 = phi double [ 0.000000e+00, %2 ], [ 0x47EFFFFFE0000000, %.preheader ], [ %.1, %.lr.ph ]
  ret double %.0
}

declare noundef i32 @_Z10R2ContainsRK9R2PolygonRK7R2Point(ptr noundef nonnull align 8 dereferenceable(68), ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK9R2Polygon7NPointsEv(ptr noundef nonnull align 8 dereferenceable(68)) local_unnamed_addr #4 align 2

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

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 = { 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 #3 = { 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 = { 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 #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,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass