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

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

%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: mustprogress nounwind uwtable
declare dso_local noundef float @_Z8vec2_dot4Vec2S_(<2 x float>, <2 x float>) #1

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

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

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

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

; Function Attrs: mustprogress uwtable
define dso_local <2 x float> @_Z27nearest_point_point_segment4Vec2S_S_Pf(<2 x float> %0, <2 x float> %1, <2 x float> %2, ptr noundef %3) #4 {
  %5 = alloca %union.Vec2, align 4
  %6 = alloca %union.Vec2, align 4
  %7 = alloca %union.Vec2, align 4
  %8 = alloca %union.Vec2, align 4
  %9 = alloca ptr, align 8
  %10 = alloca %union.Vec2, align 4
  %11 = alloca %union.Vec2, align 4
  %12 = alloca %union.Vec2, align 4
  %13 = alloca float, align 4
  %14 = alloca %union.Vec2, align 4
  %15 = alloca %union.Vec2, align 4
  %16 = alloca %union.Vec2, align 4
  %17 = alloca %union.Vec2, 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 = alloca %union.Vec2, align 4
  %23 = getelementptr inbounds %union.Vec2, ptr %6, i32 0, i32 0
  store <2 x float> %0, ptr %23, align 4
  %24 = getelementptr inbounds %union.Vec2, ptr %7, i32 0, i32 0
  store <2 x float> %1, ptr %24, align 4
  %25 = getelementptr inbounds %union.Vec2, ptr %8, i32 0, i32 0
  store <2 x float> %2, ptr %25, align 4
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %11, ptr align 4 %8, i64 8, i1 false), !tbaa.struct !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %12, ptr align 4 %7, i64 8, i1 false), !tbaa.struct !9
  %26 = getelementptr inbounds %union.Vec2, ptr %11, i32 0, i32 0
  %27 = load <2 x float>, ptr %26, align 4
  %28 = getelementptr inbounds %union.Vec2, ptr %12, i32 0, i32 0
  %29 = load <2 x float>, ptr %28, align 4
  %30 = call <2 x float> @_Zmi4Vec2S_(<2 x float> %27, <2 x float> %29)
  %31 = getelementptr inbounds %union.Vec2, ptr %10, i32 0, i32 0
  store <2 x float> %30, ptr %31, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %14, ptr align 4 %10, i64 8, i1 false), !tbaa.struct !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %15, ptr align 4 %10, i64 8, i1 false), !tbaa.struct !9
  %32 = getelementptr inbounds %union.Vec2, ptr %14, i32 0, i32 0
  %33 = load <2 x float>, ptr %32, align 4
  %34 = getelementptr inbounds %union.Vec2, ptr %15, i32 0, i32 0
  %35 = load <2 x float>, ptr %34, align 4
  %36 = call noundef float @_Z8vec2_dot4Vec2S_(<2 x float> %33, <2 x float> %35)
  store float %36, ptr %13, align 4, !tbaa !11
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %17, ptr align 4 %6, i64 8, i1 false), !tbaa.struct !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %18, ptr align 4 %7, i64 8, i1 false), !tbaa.struct !9
  %37 = getelementptr inbounds %union.Vec2, ptr %17, i32 0, i32 0
  %38 = load <2 x float>, ptr %37, align 4
  %39 = getelementptr inbounds %union.Vec2, ptr %18, i32 0, i32 0
  %40 = load <2 x float>, ptr %39, align 4
  %41 = call <2 x float> @_Zmi4Vec2S_(<2 x float> %38, <2 x float> %40)
  %42 = getelementptr inbounds %union.Vec2, ptr %16, i32 0, i32 0
  store <2 x float> %41, ptr %42, align 4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %19, ptr align 4 %10, i64 8, i1 false), !tbaa.struct !9
  %43 = getelementptr inbounds %union.Vec2, ptr %16, i32 0, i32 0
  %44 = load <2 x float>, ptr %43, align 4
  %45 = getelementptr inbounds %union.Vec2, ptr %19, i32 0, i32 0
  %46 = load <2 x float>, ptr %45, align 4
  %47 = call noundef float @_Z8vec2_dot4Vec2S_(<2 x float> %44, <2 x float> %46)
  %48 = load float, ptr %13, align 4, !tbaa !11
  %49 = fdiv float %47, %48
  %50 = load ptr, ptr %9, align 8, !tbaa !5
  store float %49, ptr %50, align 4, !tbaa !11
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  %52 = load float, ptr %51, align 4, !tbaa !11
  %53 = fcmp ogt float 0.000000e+00, %52
  br i1 %53, label %54, label %55

54:                                               ; preds = %4
  br label %58

55:                                               ; preds = %4
  %56 = load ptr, ptr %9, align 8, !tbaa !5
  %57 = load float, ptr %56, align 4, !tbaa !11
  br label %58

58:                                               ; preds = %55, %54
  %59 = phi float [ 0.000000e+00, %54 ], [ %57, %55 ]
  %60 = fcmp olt float 1.000000e+00, %59
  br i1 %60, label %61, label %62

61:                                               ; preds = %58
  br label %72

62:                                               ; preds = %58
  %63 = load ptr, ptr %9, align 8, !tbaa !5
  %64 = load float, ptr %63, align 4, !tbaa !11
  %65 = fcmp ogt float 0.000000e+00, %64
  br i1 %65, label %66, label %67

66:                                               ; preds = %62
  br label %70

67:                                               ; preds = %62
  %68 = load ptr, ptr %9, align 8, !tbaa !5
  %69 = load float, ptr %68, align 4, !tbaa !11
  br label %70

70:                                               ; preds = %67, %66
  %71 = phi float [ 0.000000e+00, %66 ], [ %69, %67 ]
  br label %72

72:                                               ; preds = %70, %61
  %73 = phi float [ 1.000000e+00, %61 ], [ %71, %70 ]
  %74 = load ptr, ptr %9, align 8, !tbaa !5
  store float %73, ptr %74, align 4, !tbaa !11
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %20, ptr align 4 %7, i64 8, i1 false), !tbaa.struct !9
  %75 = load ptr, ptr %9, align 8, !tbaa !5
  %76 = load float, ptr %75, align 4, !tbaa !11
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %22, ptr align 4 %10, i64 8, i1 false), !tbaa.struct !9
  %77 = getelementptr inbounds %union.Vec2, ptr %22, i32 0, i32 0
  %78 = load <2 x float>, ptr %77, align 4
  %79 = call <2 x float> @_Zmlf4Vec2(float noundef %76, <2 x float> %78)
  %80 = getelementptr inbounds %union.Vec2, ptr %21, i32 0, i32 0
  store <2 x float> %79, ptr %80, align 4
  %81 = getelementptr inbounds %union.Vec2, ptr %20, i32 0, i32 0
  %82 = load <2 x float>, ptr %81, align 4
  %83 = getelementptr inbounds %union.Vec2, ptr %21, i32 0, i32 0
  %84 = load <2 x float>, ptr %83, align 4
  %85 = call <2 x float> @_Zpl4Vec2S_(<2 x float> %82, <2 x float> %84)
  %86 = getelementptr inbounds %union.Vec2, ptr %5, i32 0, i32 0
  store <2 x float> %85, ptr %86, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  %87 = getelementptr inbounds %union.Vec2, ptr %5, i32 0, i32 0
  %88 = load <2 x float>, ptr %87, align 4
  ret <2 x float> %88
}

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

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { 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 = { 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 = !{i64 0, i64 8, !10}
!10 = !{!7, !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"float", !7, i64 0}


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

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

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

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

; Function Attrs: mustprogress uwtable
define dso_local <2 x float> @_Z27nearest_point_point_segment4Vec2S_S_Pf(<2 x float> %0, <2 x float> %1, <2 x float> %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #2 {
  %5 = tail call <2 x float> @_Zmi4Vec2S_(<2 x float> %2, <2 x float> %1)
  %6 = tail call noundef float @_Z8vec2_dot4Vec2S_(<2 x float> %5, <2 x float> %5)
  %7 = tail call <2 x float> @_Zmi4Vec2S_(<2 x float> %0, <2 x float> %1)
  %8 = tail call noundef float @_Z8vec2_dot4Vec2S_(<2 x float> %7, <2 x float> %5)
  %9 = fdiv float %8, %6
  %10 = fcmp olt float %9, 0.000000e+00
  %11 = fcmp ogt float %9, 1.000000e+00
  %not. = xor i1 %10, true
  %12 = and i1 %11, %not.
  %brmerge = or i1 %11, %10
  %.mux = select i1 %12, float 1.000000e+00, float 0.000000e+00
  %13 = select i1 %brmerge, float %.mux, float %9
  store float %13, ptr %3, align 4, !tbaa !5
  %14 = tail call <2 x float> @_Zmlf4Vec2(float noundef %13, <2 x float> %5)
  %15 = tail call <2 x float> @_Zpl4Vec2S_(<2 x float> %1, <2 x float> %14)
  ret <2 x float> %15
}

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

attributes #0 = { 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 = { 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 #2 = { 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 = !{!"float", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass