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

; 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.R2Polygon = type <{ %class.R2Solid, ptr, i32, [4 x i8], %class.R2Box, i32, [4 x i8] }>
%class.R2Solid = type { %class.R2Shape }
%class.R2Shape = type { ptr }
%class.R2Box = type { %class.R2Solid, %class.R2Point, %class.R2Point }

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

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_ZNK9R2Polygon12ClosestPointERK7R2Point(ptr dead_on_unwind noalias writable sret(%class.R2Point) align 8 %0, ptr noundef nonnull align 8 dereferenceable(68) %1, ptr noundef nonnull align 8 dereferenceable(16) %2) #2 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca double, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca double, align 8
  store ptr %0, ptr %4, align 8
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  %11 = load ptr, ptr %5, align 8
  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 %7) #3
  store double 0x47EFFFFFE0000000, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !11
  br label %12

12:                                               ; preds = %35, %3
  %13 = load i32, ptr %8, align 4, !tbaa !11
  %14 = getelementptr inbounds %class.R2Polygon, ptr %11, i32 0, i32 2
  %15 = load i32, ptr %14, align 8, !tbaa !13
  %16 = icmp slt i32 %13, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %38

18:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %19 = getelementptr inbounds %class.R2Polygon, ptr %11, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !19
  %21 = load i32, ptr %8, align 4, !tbaa !11
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds %class.R2Point, ptr %20, i64 %22
  store ptr %23, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = load ptr, ptr %9, align 8, !tbaa !5
  %26 = call noundef double @_Z17R2SquaredDistanceRK7R2PointS1_(ptr noundef nonnull align 8 dereferenceable(16) %24, ptr noundef nonnull align 8 dereferenceable(16) %25)
  store double %26, ptr %10, align 8, !tbaa !9
  %27 = load double, ptr %10, align 8, !tbaa !9
  %28 = load double, ptr %7, align 8, !tbaa !9
  %29 = fcmp olt double %27, %28
  br i1 %29, label %30, label %34

30:                                               ; preds = %18
  %31 = load ptr, ptr %9, align 8, !tbaa !5
  %32 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZN7R2PointaSERKS_(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr noundef nonnull align 8 dereferenceable(16) %31)
  %33 = load double, ptr %10, align 8, !tbaa !9
  store double %33, ptr %7, align 8, !tbaa !9
  br label %34

34:                                               ; preds = %30, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %35

35:                                               ; preds = %34
  %36 = load i32, ptr %8, align 4, !tbaa !11
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %8, align 4, !tbaa !11
  br label %12, !llvm.loop !20

38:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret void
}

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

declare noundef double @_Z17R2SquaredDistanceRK7R2PointS1_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) #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 = { 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 = !{!14, !12, i64 16}
!14 = !{!"_ZTS9R2Polygon", !15, i64 0, !6, i64 8, !12, i64 16, !17, i64 24, !12, i64 64}
!15 = !{!"_ZTS7R2Solid", !16, i64 0}
!16 = !{!"_ZTS7R2Shape"}
!17 = !{!"_ZTS5R2Box", !15, i64 0, !18, i64 8, !18, i64 24}
!18 = !{!"_ZTS7R2Point", !7, i64 0}
!19 = !{!14, !6, i64 8}
!20 = distinct !{!20, !21}
!21 = !{!"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.R2Point = type { [2 x double] }

declare noundef nonnull align 8 dereferenceable(16) ptr @_ZN7R2PointaSERKS_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #0

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

.lr.ph:                                           ; preds = %3
  %7 = getelementptr inbounds i8, ptr %1, i64 8
  br label %8

._crit_edge:                                      ; preds = %15, %3
  ret void

8:                                                ; preds = %.lr.ph, %15
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %15 ]
  %.012 = phi double [ 0x47EFFFFFE0000000, %.lr.ph ], [ %.1, %15 ]
  %9 = load ptr, ptr %7, align 8, !tbaa !15
  %10 = getelementptr inbounds %class.R2Point, ptr %9, i64 %indvars.iv
  %11 = tail call noundef double @_Z17R2SquaredDistanceRK7R2PointS1_(ptr noundef nonnull align 8 dereferenceable(16) %2, ptr noundef nonnull align 8 dereferenceable(16) %10)
  %12 = fcmp olt double %11, %.012
  br i1 %12, label %13, label %15

13:                                               ; preds = %8
  %14 = tail call noundef nonnull align 8 dereferenceable(16) ptr @_ZN7R2PointaSERKS_(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr noundef nonnull align 8 dereferenceable(16) %10)
  br label %15

15:                                               ; preds = %13, %8
  %.1 = phi double [ %11, %13 ], [ %.012, %8 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %16 = load i32, ptr %4, align 8, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = icmp slt i64 %indvars.iv.next, %17
  br i1 %18, label %8, label %._crit_edge, !llvm.loop !16
}

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

declare noundef double @_Z17R2SquaredDistanceRK7R2PointS1_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #0

attributes #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 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 16}
!6 = !{!"_ZTS9R2Polygon", !7, i64 0, !9, i64 8, !12, i64 16, !13, i64 24, !12, i64 64}
!7 = !{!"_ZTS7R2Solid", !8, i64 0}
!8 = !{!"_ZTS7R2Shape"}
!9 = !{!"any pointer", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!"int", !10, i64 0}
!13 = !{!"_ZTS5R2Box", !7, i64 0, !14, i64 8, !14, i64 24}
!14 = !{!"_ZTS7R2Point", !10, i64 0}
!15 = !{!6, !9, i64 8}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass