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

; 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> @_Z14vec2_normalize4Vec2(<2 x float>) #5

; Function Attrs: mustprogress uwtable
define dso_local void @_Z27nearest_edge_origin_polygonPK4Vec2j(ptr dead_on_unwind noalias writable sret(%struct.Nearest_Edge) align 4 %0, ptr noundef %1, i32 noundef %2) #5 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca %union.Vec2, align 4
  %9 = alloca %union.Vec2, 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 float, align 4
  %16 = alloca %union.Vec2, align 4
  %17 = alloca %union.Vec2, align 4
  store ptr %1, ptr %4, align 8, !tbaa !5
  store i32 %2, ptr %5, align 4, !tbaa !9
  %18 = getelementptr inbounds %struct.Nearest_Edge, ptr %0, i32 0, i32 1
  store float 0x47EFFFFFE0000000, ptr %18, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #6
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %78, %3
  %20 = load i32, ptr %6, align 4, !tbaa !9
  %21 = load i32, ptr %5, align 4, !tbaa !9
  %22 = icmp ult i32 %20, %21
  br i1 %22, label %24, label %23

23:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #6
  br label %81

24:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #6
  %25 = load i32, ptr %6, align 4, !tbaa !9
  %26 = add i32 %25, 1
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = icmp eq i32 %26, %27
  br i1 %28, label %29, label %30

29:                                               ; preds = %24
  br label %33

30:                                               ; preds = %24
  %31 = load i32, ptr %6, align 4, !tbaa !9
  %32 = add i32 %31, 1
  br label %33

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

69:                                               ; preds = %33
  %70 = getelementptr inbounds %struct.Nearest_Edge, ptr %0, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %70, ptr align 4 %13, i64 8, i1 false), !tbaa.struct !14
  %71 = load float, ptr %15, align 4, !tbaa !16
  %72 = getelementptr inbounds %struct.Nearest_Edge, ptr %0, i32 0, i32 1
  store float %71, ptr %72, align 4, !tbaa !11
  %73 = load i32, ptr %6, align 4, !tbaa !9
  %74 = getelementptr inbounds %struct.Nearest_Edge, ptr %0, i32 0, i32 2
  store i32 %73, ptr %74, align 4, !tbaa !17
  %75 = load i32, ptr %7, align 4, !tbaa !9
  %76 = getelementptr inbounds %struct.Nearest_Edge, ptr %0, i32 0, i32 3
  store i32 %75, ptr %76, align 4, !tbaa !18
  br label %77

77:                                               ; preds = %69, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #6
  br label %78

78:                                               ; preds = %77
  %79 = load i32, ptr %6, align 4, !tbaa !9
  %80 = add i32 %79, 1
  store i32 %80, ptr %6, align 4, !tbaa !9
  br label %19, !llvm.loop !19

81:                                               ; preds = %23
  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> @_Z14vec2_normalize4Vec2(<2 x float>) local_unnamed_addr #3

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

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

._crit_edge:                                      ; preds = %27
  store i32 %.02630, ptr %4, align 4, !tbaa !8
  store i32 %28, ptr %5, align 4, !tbaa !12
  br label %7

7:                                                ; preds = %._crit_edge, %3
  %.lcssa25 = phi <2 x float> [ %29, %._crit_edge ], [ %.promoted24, %3 ]
  %.lcssa = phi float [ %30, %._crit_edge ], [ 0x47EFFFFFE0000000, %3 ]
  %8 = getelementptr inbounds i8, ptr %0, i64 8
  store float %.lcssa, ptr %8, align 4, !tbaa !13
  store <2 x float> %.lcssa25, ptr %0, align 4, !tbaa !5
  ret void

9:                                                ; preds = %.lr.ph, %27
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %27 ]
  %10 = phi i32 [ %.promoted31, %.lr.ph ], [ %28, %27 ]
  %.02629 = phi i32 [ %.promoted28, %.lr.ph ], [ %.02630, %27 ]
  %11 = phi float [ 0x47EFFFFFE0000000, %.lr.ph ], [ %30, %27 ]
  %12 = phi <2 x float> [ %.promoted24, %.lr.ph ], [ %29, %27 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %13 = icmp eq i64 %indvars.iv.next, %6
  %14 = trunc nuw i64 %indvars.iv.next to i32
  %15 = select i1 %13, i32 0, i32 %14
  %16 = getelementptr inbounds %union.Vec2, ptr %1, i64 %indvars.iv
  %.sroa.09.0.copyload = load <2 x float>, ptr %16, align 4, !tbaa !5
  %17 = zext i32 %15 to i64
  %18 = getelementptr inbounds %union.Vec2, ptr %1, i64 %17
  %.sroa.08.0.copyload = load <2 x float>, ptr %18, align 4, !tbaa !5
  %19 = tail call <2 x float> @_Zmi4Vec2S_(<2 x float> %.sroa.08.0.copyload, <2 x float> %.sroa.09.0.copyload)
  %.sroa.07.4.vec.extract = extractelement <2 x float> %19, i64 1
  %20 = fneg float %.sroa.07.4.vec.extract
  %.sroa.07.0.vec.extract = extractelement <2 x float> %19, i64 0
  %21 = tail call <2 x float> @_Z4vec2ff(float noundef %20, float noundef %.sroa.07.0.vec.extract)
  %22 = tail call <2 x float> @_Z14vec2_normalize4Vec2(<2 x float> %21)
  %23 = tail call noundef float @_Z8vec2_dot4Vec2S_(<2 x float> %22, <2 x float> %.sroa.09.0.copyload)
  %24 = fcmp olt float %23, %11
  br i1 %24, label %25, label %27

25:                                               ; preds = %9
  %26 = trunc nuw i64 %indvars.iv to i32
  br label %27

27:                                               ; preds = %25, %9
  %28 = phi i32 [ %15, %25 ], [ %10, %9 ]
  %.02630 = phi i32 [ %26, %25 ], [ %.02629, %9 ]
  %29 = phi <2 x float> [ %22, %25 ], [ %12, %9 ]
  %30 = phi float [ %23, %25 ], [ %11, %9 ]
  %exitcond.not = icmp eq i64 %indvars.iv.next, %6
  br i1 %exitcond.not, label %._crit_edge, label %9, !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