; LLVM IR from ashishzero/karma :: src/modules/core/lin_maths.ll (Z26nearest_edge_point_polygonPK4Vec2jS_)

; 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.Nearest_Edge = type { %union.Vec2, float, i32, i32 }
%union.Vec2 = type { %struct.anon.1 }
%struct.anon.1 = type { float, float }

; 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) #0

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local <2 x float> @_Z4vec2ff(float noundef, float noundef) #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef float @_Z8vec2_dot4Vec2S_(<2 x float>, <2 x float>) #2

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

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local <2 x float> @_Zmi4Vec2S_(<2 x float>, <2 x float>) #4

; Function Attrs: mustprogress uwtable
declare dso_local <2 x float> @_Z20vec2_normalize_check4Vec2(<2 x float>) #5

; Function Attrs: mustprogress uwtable
define dso_local void @_Z26nearest_edge_point_polygonPK4Vec2jS_(ptr dead_on_unwind noalias writable sret(%struct.Nearest_Edge) align 4 %0, ptr noundef %1, i32 noundef %2, <2 x float> %3) #5 {
  %5 = alloca %union.Vec2, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca %union.Vec2, align 4
  %11 = alloca %union.Vec2, align 4
  %12 = alloca %union.Vec2, align 4
  %13 = alloca %union.Vec2, align 4
  %14 = alloca %union.Vec2, align 4
  %15 = alloca %union.Vec2, align 4
  %16 = alloca %union.Vec2, align 4
  %17 = alloca float, align 4
  %18 = alloca %union.Vec2, align 4
  %19 = alloca %union.Vec2, align 4
  %20 = alloca %union.Vec2, align 4
  %21 = alloca %union.Vec2, align 4
  %22 = getelementptr inbounds %union.Vec2, ptr %5, i32 0, i32 0
  store <2 x float> %3, ptr %22, align 4
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  %23 = getelementptr inbounds %struct.Nearest_Edge, ptr %0, i32 0, i32 1
  store float 0x47EFFFFFE0000000, ptr %23, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #6
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %89, %4
  %25 = load i32, ptr %8, align 4, !tbaa !9
  %26 = load i32, ptr %7, align 4, !tbaa !9
  %27 = icmp ult i32 %25, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #6
  br label %92

29:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #6
  %30 = load i32, ptr %8, align 4, !tbaa !9
  %31 = add i32 %30, 1
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = icmp eq i32 %31, %32
  br i1 %33, label %34, label %35

34:                                               ; preds = %29
  br label %38

35:                                               ; preds = %29
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = add i32 %36, 1
  br label %38

38:                                               ; preds = %35, %34
  %39 = phi i32 [ 0, %34 ], [ %37, %35 ]
  store i32 %39, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #6
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = load i32, ptr %8, align 4, !tbaa !9
  %42 = zext i32 %41 to i64
  %43 = getelementptr inbounds %union.Vec2, ptr %40, i64 %42
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %10, ptr align 4 %43, i64 8, i1 false), !tbaa.struct !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #6
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = load i32, ptr %9, align 4, !tbaa !9
  %46 = zext i32 %45 to i64
  %47 = getelementptr inbounds %union.Vec2, ptr %44, i64 %46
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %11, ptr align 4 %47, i64 8, i1 false), !tbaa.struct !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #6
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %13, ptr align 4 %11, i64 8, i1 false), !tbaa.struct !14
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %14, ptr align 4 %10, i64 8, i1 false), !tbaa.struct !14
  %48 = getelementptr inbounds %union.Vec2, ptr %13, i32 0, i32 0
  %49 = load <2 x float>, ptr %48, align 4
  %50 = getelementptr inbounds %union.Vec2, ptr %14, i32 0, i32 0
  %51 = load <2 x float>, ptr %50, align 4
  %52 = call <2 x float> @_Zmi4Vec2S_(<2 x float> %49, <2 x float> %51)
  %53 = getelementptr inbounds %union.Vec2, ptr %12, i32 0, i32 0
  store <2 x float> %52, ptr %53, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #6
  %54 = getelementptr inbounds %struct.anon.1, ptr %12, i32 0, i32 1
  %55 = load float, ptr %54, align 4, !tbaa !15
  %56 = fneg float %55
  %57 = getelementptr inbounds %struct.anon.1, ptr %12, i32 0, i32 0
  %58 = load float, ptr %57, align 4, !tbaa !15
  %59 = call <2 x float> @_Z4vec2ff(float noundef %56, float noundef %58)
  %60 = getelementptr inbounds %union.Vec2, ptr %16, i32 0, i32 0
  store <2 x float> %59, ptr %60, align 4
  %61 = getelementptr inbounds %union.Vec2, ptr %16, i32 0, i32 0
  %62 = load <2 x float>, ptr %61, align 4
  %63 = call <2 x float> @_Z20vec2_normalize_check4Vec2(<2 x float> %62)
  %64 = getelementptr inbounds %union.Vec2, ptr %15, i32 0, i32 0
  store <2 x float> %63, ptr %64, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #6
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %18, ptr align 4 %15, i64 8, i1 false), !tbaa.struct !14
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %20, ptr align 4 %10, i64 8, i1 false), !tbaa.struct !14
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %21, ptr align 4 %5, i64 8, i1 false), !tbaa.struct !14
  %65 = getelementptr inbounds %union.Vec2, ptr %20, i32 0, i32 0
  %66 = load <2 x float>, ptr %65, align 4
  %67 = getelementptr inbounds %union.Vec2, ptr %21, i32 0, i32 0
  %68 = load <2 x float>, ptr %67, align 4
  %69 = call <2 x float> @_Zmi4Vec2S_(<2 x float> %66, <2 x float> %68)
  %70 = getelementptr inbounds %union.Vec2, ptr %19, i32 0, i32 0
  store <2 x float> %69, ptr %70, align 4
  %71 = getelementptr inbounds %union.Vec2, ptr %18, i32 0, i32 0
  %72 = load <2 x float>, ptr %71, align 4
  %73 = getelementptr inbounds %union.Vec2, ptr %19, i32 0, i32 0
  %74 = load <2 x float>, ptr %73, align 4
  %75 = call noundef float @_Z8vec2_dot4Vec2S_(<2 x float> %72, <2 x float> %74)
  store float %75, ptr %17, align 4, !tbaa !16
  %76 = load float, ptr %17, align 4, !tbaa !16
  %77 = getelementptr inbounds %struct.Nearest_Edge, ptr %0, i32 0, i32 1
  %78 = load float, ptr %77, align 4, !tbaa !11
  %79 = fcmp olt float %76, %78
  br i1 %79, label %80, label %88

80:                                               ; preds = %38
  %81 = getelementptr inbounds %struct.Nearest_Edge, ptr %0, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %81, ptr align 4 %15, i64 8, i1 false), !tbaa.struct !14
  %82 = load float, ptr %17, align 4, !tbaa !16
  %83 = getelementptr inbounds %struct.Nearest_Edge, ptr %0, i32 0, i32 1
  store float %82, ptr %83, align 4, !tbaa !11
  %84 = load i32, ptr %8, align 4, !tbaa !9
  %85 = getelementptr inbounds %struct.Nearest_Edge, ptr %0, i32 0, i32 2
  store i32 %84, ptr %85, align 4, !tbaa !17
  %86 = load i32, ptr %9, align 4, !tbaa !9
  %87 = getelementptr inbounds %struct.Nearest_Edge, ptr %0, i32 0, i32 3
  store i32 %86, ptr %87, align 4, !tbaa !18
  br label %88

88:                                               ; preds = %80, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #6
  br label %89

89:                                               ; preds = %88
  %90 = load i32, ptr %8, align 4, !tbaa !9
  %91 = add i32 %90, 1
  store i32 %91, ptr %8, align 4, !tbaa !9
  br label %24, !llvm.loop !19

92:                                               ; preds = %28
  ret void
}

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint mustprogress nounwind uwtable "min-legal-vector-width"="64" "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 nounwind uwtable "min-legal-vector-width"="64" "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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { inlinehint mustprogress uwtable "min-legal-vector-width"="64" "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 = { mustprogress uwtable "min-legal-vector-width"="64" "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 #6 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !13, i64 8}
!12 = !{!"_ZTS12Nearest_Edge", !7, i64 0, !13, i64 8, !10, i64 12, !10, i64 16}
!13 = !{!"float", !7, i64 0}
!14 = !{i64 0, i64 8, !15}
!15 = !{!7, !7, i64 0}
!16 = !{!13, !13, i64 0}
!17 = !{!12, !10, i64 12}
!18 = !{!12, !10, i64 16}
!19 = distinct !{!19, !20}
!20 = !{!"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"

%struct.Nearest_Edge = type { %union.Vec2, float, i32, i32 }
%union.Vec2 = type { %struct.anon.1 }
%struct.anon.1 = type { float, float }

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local <2 x float> @_Z4vec2ff(float noundef, float noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef float @_Z8vec2_dot4Vec2S_(<2 x float>, <2 x float>) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local <2 x float> @_Zmi4Vec2S_(<2 x float>, <2 x float>) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
declare dso_local <2 x float> @_Z20vec2_normalize_check4Vec2(<2 x float>) local_unnamed_addr #3

; Function Attrs: mustprogress uwtable
define dso_local void @_Z26nearest_edge_point_polygonPK4Vec2jS_(ptr dead_on_unwind noalias nocapture writable sret(%struct.Nearest_Edge) align 4 %0, ptr nocapture noundef readonly %1, i32 noundef %2, <2 x float> %3) local_unnamed_addr #3 {
  %.promoted27 = load <2 x float>, ptr %0, align 4, !tbaa !5
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %8, label %.lr.ph

.lr.ph:                                           ; preds = %4
  %5 = getelementptr inbounds i8, ptr %0, i64 12
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %.promoted31 = load i32, ptr %5, align 4, !tbaa !8
  %.promoted34 = load i32, ptr %6, align 4, !tbaa !12
  %7 = zext i32 %2 to i64
  br label %10

._crit_edge:                                      ; preds = %29
  store i32 %.02933, ptr %5, align 4, !tbaa !8
  store i32 %30, ptr %6, align 4, !tbaa !12
  br label %8

8:                                                ; preds = %._crit_edge, %4
  %.lcssa28 = phi <2 x float> [ %31, %._crit_edge ], [ %.promoted27, %4 ]
  %.lcssa = phi float [ %32, %._crit_edge ], [ 0x47EFFFFFE0000000, %4 ]
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  store float %.lcssa, ptr %9, align 4, !tbaa !13
  store <2 x float> %.lcssa28, ptr %0, align 4, !tbaa !5
  ret void

10:                                               ; preds = %.lr.ph, %29
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %29 ]
  %11 = phi i32 [ %.promoted34, %.lr.ph ], [ %30, %29 ]
  %.02932 = phi i32 [ %.promoted31, %.lr.ph ], [ %.02933, %29 ]
  %12 = phi float [ 0x47EFFFFFE0000000, %.lr.ph ], [ %32, %29 ]
  %13 = phi <2 x float> [ %.promoted27, %.lr.ph ], [ %31, %29 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %14 = icmp eq i64 %indvars.iv.next, %7
  %15 = trunc nuw i64 %indvars.iv.next to i32
  %16 = select i1 %14, i32 0, i32 %15
  %17 = getelementptr inbounds %union.Vec2, ptr %1, i64 %indvars.iv
  %.sroa.011.0.copyload = load <2 x float>, ptr %17, align 4, !tbaa !5
  %18 = zext i32 %16 to i64
  %19 = getelementptr inbounds %union.Vec2, ptr %1, i64 %18
  %.sroa.010.0.copyload = load <2 x float>, ptr %19, align 4, !tbaa !5
  %20 = tail call <2 x float> @_Zmi4Vec2S_(<2 x float> %.sroa.010.0.copyload, <2 x float> %.sroa.011.0.copyload)
  %.sroa.09.4.vec.extract = extractelement <2 x float> %20, i64 1
  %21 = fneg float %.sroa.09.4.vec.extract
  %.sroa.09.0.vec.extract = extractelement <2 x float> %20, i64 0
  %22 = tail call <2 x float> @_Z4vec2ff(float noundef %21, float noundef %.sroa.09.0.vec.extract)
  %23 = tail call <2 x float> @_Z20vec2_normalize_check4Vec2(<2 x float> %22)
  %24 = tail call <2 x float> @_Zmi4Vec2S_(<2 x float> %.sroa.011.0.copyload, <2 x float> %3)
  %25 = tail call noundef float @_Z8vec2_dot4Vec2S_(<2 x float> %23, <2 x float> %24)
  %26 = fcmp olt float %25, %12
  br i1 %26, label %27, label %29

27:                                               ; preds = %10
  %28 = trunc nuw i64 %indvars.iv to i32
  br label %29

29:                                               ; preds = %27, %10
  %30 = phi i32 [ %16, %27 ], [ %11, %10 ]
  %.02933 = phi i32 [ %28, %27 ], [ %.02932, %10 ]
  %31 = phi <2 x float> [ %23, %27 ], [ %13, %10 ]
  %32 = phi float [ %25, %27 ], [ %12, %10 ]
  %exitcond.not = icmp eq i64 %indvars.iv.next, %7
  br i1 %exitcond.not, label %._crit_edge, label %10, !llvm.loop !14
}

attributes #0 = { inlinehint mustprogress nounwind uwtable "min-legal-vector-width"="64" "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 nounwind uwtable "min-legal-vector-width"="64" "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"="64" "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"="64" "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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !11, i64 12}
!9 = !{!"_ZTS12Nearest_Edge", !6, i64 0, !10, i64 8, !11, i64 12, !11, i64 16}
!10 = !{!"float", !6, i64 0}
!11 = !{!"int", !6, i64 0}
!12 = !{!9, !11, i64 16}
!13 = !{!9, !10, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass