; LLVM IR from NavInfoNC/nc-geo :: src/polygon_marker.ll (ZN13PolygonMarker4Cell20pointPolygonDistanceE5Point)

; 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.Point = type { i32, i32 }

@_ZN13PolygonMarker4Cell14s_polygonCountE = external dso_local global i32, align 4
@_ZN13PolygonMarker4Cell9s_polygonE = external dso_local global ptr, align 8

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

; Function Attrs: alwaysinline mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN13StaticPolygon11pointNumberEv(ptr noundef nonnull align 8 dereferenceable(40)) #1 align 2

; Function Attrs: alwaysinline mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN13StaticPolygon6pointsEv(ptr noundef nonnull align 8 dereferenceable(40)) #1 align 2

; 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
declare dso_local noundef i32 @_ZN13PolygonMarker4Cell20segmentPointDistanceE5PointS1_S1_(i64, i64, i64) #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN13PolygonMarker4Cell20pointPolygonDistanceE5Point(i64 %0) #2 align 2 {
  %2 = alloca %struct.Point, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca %struct.Point, align 4
  %12 = alloca %struct.Point, align 4
  %13 = alloca %struct.Point, align 4
  store i64 %0, ptr %2, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 2147483647, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %62, %1
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = load i32, ptr @_ZN13PolygonMarker4Cell14s_polygonCountE, align 4, !tbaa !5
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %65

19:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %20 = load ptr, ptr @_ZN13PolygonMarker4Cell9s_polygonE, align 8, !tbaa !9
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds ptr, ptr %20, i64 %22
  %24 = load ptr, ptr %23, align 8, !tbaa !9
  %25 = call noundef i32 @_ZN13StaticPolygon11pointNumberEv(ptr noundef nonnull align 8 dereferenceable(40) %24)
  store i32 %25, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %26 = load ptr, ptr @_ZN13PolygonMarker4Cell9s_polygonE, align 8, !tbaa !9
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds ptr, ptr %26, i64 %28
  %30 = load ptr, ptr %29, align 8, !tbaa !9
  %31 = call noundef ptr @_ZN13StaticPolygon6pointsEv(ptr noundef nonnull align 8 dereferenceable(40) %30)
  store ptr %31, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = sub nsw i32 %32, 1
  store i32 %33, ptr %9, align 4, !tbaa !5
  br label %34

34:                                               ; preds = %58, %19
  %35 = load i32, ptr %8, align 4, !tbaa !5
  %36 = load i32, ptr %6, align 4, !tbaa !5
  %37 = icmp slt i32 %35, %36
  br i1 %37, label %39, label %38

38:                                               ; preds = %34
  store i32 5, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %61

39:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  %40 = load ptr, ptr %7, align 8, !tbaa !9
  %41 = load i32, ptr %9, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds %struct.Point, ptr %40, i64 %42
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %11, ptr align 4 %43, i64 8, i1 false), !tbaa.struct !11
  %44 = load ptr, ptr %7, align 8, !tbaa !9
  %45 = load i32, ptr %8, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds %struct.Point, ptr %44, i64 %46
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %12, ptr align 4 %47, i64 8, i1 false), !tbaa.struct !11
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %13, ptr align 4 %2, i64 8, i1 false), !tbaa.struct !11
  %48 = load i64, ptr %11, align 4
  %49 = load i64, ptr %12, align 4
  %50 = load i64, ptr %13, align 4
  %51 = call noundef i32 @_ZN13PolygonMarker4Cell20segmentPointDistanceE5PointS1_S1_(i64 %48, i64 %49, i64 %50)
  store i32 %51, ptr %10, align 4, !tbaa !5
  %52 = load i32, ptr %10, align 4, !tbaa !5
  %53 = load i32, ptr %3, align 4, !tbaa !5
  %54 = icmp slt i32 %52, %53
  br i1 %54, label %55, label %57

55:                                               ; preds = %39
  %56 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %56, ptr %3, align 4, !tbaa !5
  br label %57

57:                                               ; preds = %55, %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  br label %58

58:                                               ; preds = %57
  %59 = load i32, ptr %8, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %8, align 4, !tbaa !5
  store i32 %59, ptr %9, align 4, !tbaa !5
  br label %34, !llvm.loop !12

61:                                               ; preds = %38
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %4, align 4, !tbaa !5
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %4, align 4, !tbaa !5
  br label %14, !llvm.loop !14

65:                                               ; preds = %18
  %66 = load i32, ptr %3, align 4, !tbaa !5
  store i32 1, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  ret i32 %66
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { alwaysinline 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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{i64 0, i64 4, !5, i64 4, i64 4, !5}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


; 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"

%struct.Point = type { i32, i32 }

@_ZN13PolygonMarker4Cell14s_polygonCountE = external dso_local local_unnamed_addr global i32, align 4
@_ZN13PolygonMarker4Cell9s_polygonE = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: alwaysinline mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN13StaticPolygon11pointNumberEv(ptr noundef nonnull align 8 dereferenceable(40)) local_unnamed_addr #0 align 2

; Function Attrs: alwaysinline mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN13StaticPolygon6pointsEv(ptr noundef nonnull align 8 dereferenceable(40)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN13PolygonMarker4Cell20segmentPointDistanceE5PointS1_S1_(i64, i64, i64) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN13PolygonMarker4Cell20pointPolygonDistanceE5Point(i64 %0) local_unnamed_addr #1 align 2 {
  %2 = load i32, ptr @_ZN13PolygonMarker4Cell14s_polygonCountE, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph28, label %._crit_edge29

._crit_edge29:                                    ; preds = %._crit_edge, %1
  %.0.lcssa = phi i32 [ 2147483647, %1 ], [ %.1.lcssa, %._crit_edge ]
  ret i32 %.0.lcssa

.lr.ph28:                                         ; preds = %1, %._crit_edge
  %indvars.iv32 = phi i64 [ %indvars.iv.next33, %._crit_edge ], [ 0, %1 ]
  %.026 = phi i32 [ %.1.lcssa, %._crit_edge ], [ 2147483647, %1 ]
  %4 = load ptr, ptr @_ZN13PolygonMarker4Cell9s_polygonE, align 8, !tbaa !9
  %5 = getelementptr inbounds ptr, ptr %4, i64 %indvars.iv32
  %6 = load ptr, ptr %5, align 8, !tbaa !9
  %7 = tail call noundef i32 @_ZN13StaticPolygon11pointNumberEv(ptr noundef nonnull align 8 dereferenceable(40) %6)
  %8 = load ptr, ptr @_ZN13PolygonMarker4Cell9s_polygonE, align 8, !tbaa !9
  %9 = getelementptr inbounds ptr, ptr %8, i64 %indvars.iv32
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = tail call noundef ptr @_ZN13StaticPolygon6pointsEv(ptr noundef nonnull align 8 dereferenceable(40) %10)
  %12 = icmp sgt i32 %7, 0
  br i1 %12, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.lr.ph28
  %13 = add nsw i32 %7, -1
  %wide.trip.count = zext nneg i32 %7 to i64
  br label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph28
  %.1.lcssa = phi i32 [ %.026, %.lr.ph28 ], [ %spec.select, %.lr.ph ]
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %14 = load i32, ptr @_ZN13PolygonMarker4Cell14s_polygonCountE, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = icmp slt i64 %indvars.iv.next33, %15
  br i1 %16, label %.lr.ph28, label %._crit_edge29, !llvm.loop !11

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %.124 = phi i32 [ %.026, %.lr.ph.preheader ], [ %spec.select, %.lr.ph ]
  %.02022 = phi i32 [ %13, %.lr.ph.preheader ], [ %21, %.lr.ph ]
  %17 = sext i32 %.02022 to i64
  %18 = getelementptr inbounds %struct.Point, ptr %11, i64 %17
  %.sroa.02.0.copyload = load i64, ptr %18, align 4
  %19 = getelementptr inbounds %struct.Point, ptr %11, i64 %indvars.iv
  %.sroa.01.0.copyload = load i64, ptr %19, align 4
  %20 = tail call noundef i32 @_ZN13PolygonMarker4Cell20segmentPointDistanceE5PointS1_S1_(i64 %.sroa.02.0.copyload, i64 %.sroa.01.0.copyload, i64 %0)
  %spec.select = tail call i32 @llvm.smin.i32(i32 %20, i32 %.124)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %21 = trunc nuw nsw i64 %indvars.iv to i32
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #2

attributes #0 = { alwaysinline 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 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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