#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ glmAddVector2f(float *, float const *, float *);
int /*<<< orphan*/ glmCopyVector2f(float const *, float *);
float glmDotProduct2f(float *, float *);
int /*<<< orphan*/ glmMakeVector2f(float *, float const *, float const *);
int /*<<< orphan*/ glmMultVectorByScalar2f(float *, float);
float glmNormalizeVector2f(float *);
void glmClosestPointOnLineSegment2f(float result[2], const float a[2],
                                    const float b[2], const float p[2]) {
  float c[2], v[2];
  float d, t;
  glmMakeVector2f(c, a, p);
  glmMakeVector2f(v, a, b);
  d = glmNormalizeVector2f(v);
  t = glmDotProduct2f(v, c);
  if (t < 0) {
    glmCopyVector2f(a, result);
    return;
  }
  if (t > d) {
    glmCopyVector2f(b, result);
    return;
  }
  glmMultVectorByScalar2f(v, t);
  glmAddVector2f(result, a, v);
}


x86 assembly (clang, -O0):
<glmClosestPointOnLineSegment2f>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <glmMakeVector2f@plt>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <glmMakeVector2f@plt>
lea    -0x30(%rbp),%rdi
call   1060 <glmNormalizeVector2f@plt>
movss  %xmm0,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <glmDotProduct2f@plt>
movss  %xmm0,-0x38(%rbp)
xorps  %xmm0,%xmm0
ucomiss -0x38(%rbp),%xmm0
jbe    11d9 <glmClosestPointOnLineSegment2f+0x79>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <glmCopyVector2f@plt>
jmp    1219 <glmClosestPointOnLineSegment2f+0xb9>
movss  -0x38(%rbp),%xmm0
ucomiss -0x34(%rbp),%xmm0
jbe    11fa <glmClosestPointOnLineSegment2f+0x9a>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <glmCopyVector2f@plt>
jmp    1219 <glmClosestPointOnLineSegment2f+0xb9>
lea    -0x30(%rbp),%rdi
movss  -0x38(%rbp),%xmm0
call   1030 <glmMultVectorByScalar2f@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1050 <glmAddVector2f@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<glmClosestPointOnLineSegment2f>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%r14
lea    0x18(%rsp),%r12
mov    %r12,%rdi
mov    %rcx,%rdx
call   1070 <glmMakeVector2f@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
call   1070 <glmMakeVector2f@plt>
mov    %rbx,%rdi
call   1060 <glmNormalizeVector2f@plt>
movss  %xmm0,0xc(%rsp)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <glmDotProduct2f@plt>
xorps  %xmm1,%xmm1
ucomiss %xmm0,%xmm1
jbe    11bf <glmClosestPointOnLineSegment2f+0x5f>
mov    %r13,%rdi
jmp    11c9 <glmClosestPointOnLineSegment2f+0x69>
ucomiss 0xc(%rsp),%xmm0
jbe    11d3 <glmClosestPointOnLineSegment2f+0x73>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <glmCopyVector2f@plt>
jmp    11ee <glmClosestPointOnLineSegment2f+0x8e>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <glmMultVectorByScalar2f@plt>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1050 <glmAddVector2f@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @glmClosestPointOnLineSegment2f(float* %0, float* %1, float* %2, float* %3) {
%5 = alloca float*
%6 = alloca float*
%7 = alloca float*
%8 = alloca float*
%9 = alloca [2 x float]
%10 = alloca [2 x float]
%11 = alloca float
%12 = alloca float
store float* %0, float** %5
store float* %1, float** %6
store float* %2, float** %7
store float* %3, float** %8
%13 = getelementptr inbounds [2 x float], [2 x float]* %9, i64 0, i64 0
%14 = load float*, float** %6
%15 = load float*, float** %8
%16 = call i32 @glmMakeVector2f(float* %13, float* %14, float* %15)
%17 = getelementptr inbounds [2 x float], [2 x float]* %10, i64 0, i64 0
%18 = load float*, float** %6
%19 = load float*, float** %7
%20 = call i32 @glmMakeVector2f(float* %17, float* %18, float* %19)
%21 = getelementptr inbounds [2 x float], [2 x float]* %10, i64 0, i64 0
%22 = call float @glmNormalizeVector2f(float* %21)
store float %22, float* %11
%23 = getelementptr inbounds [2 x float], [2 x float]* %10, i64 0, i64 0
%24 = getelementptr inbounds [2 x float], [2 x float]* %9, i64 0, i64 0
%25 = call float @glmDotProduct2f(float* %23, float* %24)
store float %25, float* %12
%26 = load float, float* %12
%27 = fcmp olt float %26, 0.000000e+00
br i1 %27, label %28, label %32
28: ; preds = %4
%29 = load float*, float** %6
%30 = load float*, float** %5
%31 = call i32 @glmCopyVector2f(float* %29, float* %30)
br label %48
32: ; preds = %4
%33 = load float, float* %12
%34 = load float, float* %11
%35 = fcmp ogt float %33, %34
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = load float*, float** %7
%38 = load float*, float** %5
%39 = call i32 @glmCopyVector2f(float* %37, float* %38)
br label %48
40: ; preds = %32
%41 = getelementptr inbounds [2 x float], [2 x float]* %10, i64 0, i64 0
%42 = load float, float* %12
%43 = call i32 @glmMultVectorByScalar2f(float* %41, float %42)
%44 = load float*, float** %5
%45 = load float*, float** %6
%46 = getelementptr inbounds [2 x float], [2 x float]* %10, i64 0, i64 0
%47 = call i32 @glmAddVector2f(float* %44, float* %45, float* %46)
br label %48
48: ; preds = %40, %36, %28
ret void
}

LLVM IR (-O2):
define void @glmClosestPointOnLineSegment2f(float* %0, float* %1, float* %2, float* %3) {
%5 = alloca [2 x float]
%6 = alloca [2 x float]
%7 = bitcast [2 x float]* %5 to i8*
%8 = bitcast [2 x float]* %6 to i8*
%9 = getelementptr inbounds [2 x float], [2 x float]* %5, i64 0, i64 0
%10 = call i32 @glmMakeVector2f(float* %9, float* %1, float* %3)
%11 = getelementptr inbounds [2 x float], [2 x float]* %6, i64 0, i64 0
%12 = call i32 @glmMakeVector2f(float* %11, float* %1, float* %2)
%13 = call float @glmNormalizeVector2f(float* %11)
%14 = call float @glmDotProduct2f(float* %11, float* %9)
%15 = fcmp olt float %14, 0.000000e+00
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = call i32 @glmCopyVector2f(float* %1, float* %0)
br label %25
18: ; preds = %4
%19 = fcmp ogt float %14, %13
br i1 %19, label %20, label %22
20: ; preds = %18
%21 = call i32 @glmCopyVector2f(float* %2, float* %0)
br label %25
22: ; preds = %18
%23 = call i32 @glmMultVectorByScalar2f(float* %11, float %14)
%24 = call i32 @glmAddVector2f(float* %0, float* %1, float* %11)
br label %25
25: ; preds = %22, %20, %16
ret void
}