#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*/ glmAddVector2d(double *, double const *, double *);
int /*<<< orphan*/ glmCopyVector2d(double const *, double *);
double glmDotProduct2d(double *, double *);
int /*<<< orphan*/ glmMakeVector2d(double *, double const *, double const *);
int /*<<< orphan*/ glmMultVectorByScalar2d(double *, double);
double glmNormalizeVector2d(double *);
void glmClosestPointOnLineSegment2d(double result[2], const double a[2],
                                    const double b[2], const double p[2]) {
  double c[2], v[2];
  double d, t;
  glmMakeVector2d(c, a, p);
  glmMakeVector2d(v, a, b);
  d = glmNormalizeVector2d(v);
  t = glmDotProduct2d(v, c);
  if (t < 0) {
    glmCopyVector2d(a, result);
    return;
  }
  if (t > d) {
    glmCopyVector2d(b, result);
    return;
  }
  glmMultVectorByScalar2d(v, t);
  glmAddVector2d(result, a, v);
}


x86 assembly (clang, -O0):
<glmClosestPointOnLineSegment2d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <glmMakeVector2d@plt>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <glmMakeVector2d@plt>
lea    -0x40(%rbp),%rdi
call   1030 <glmNormalizeVector2d@plt>
movsd  %xmm0,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <glmDotProduct2d@plt>
movsd  %xmm0,-0x50(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x50(%rbp),%xmm0
jbe    11da <glmClosestPointOnLineSegment2d+0x7a>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <glmCopyVector2d@plt>
jmp    121b <glmClosestPointOnLineSegment2d+0xbb>
movsd  -0x50(%rbp),%xmm0
ucomisd -0x48(%rbp),%xmm0
jbe    11fc <glmClosestPointOnLineSegment2d+0x9c>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <glmCopyVector2d@plt>
jmp    121b <glmClosestPointOnLineSegment2d+0xbb>
lea    -0x40(%rbp),%rdi
movsd  -0x50(%rbp),%xmm0
call   1040 <glmMultVectorByScalar2d@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1060 <glmAddVector2d@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<glmClosestPointOnLineSegment2d>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%r14
lea    0x20(%rsp),%r12
mov    %r12,%rdi
mov    %rcx,%rdx
call   1070 <glmMakeVector2d@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
call   1070 <glmMakeVector2d@plt>
mov    %rbx,%rdi
call   1030 <glmNormalizeVector2d@plt>
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <glmDotProduct2d@plt>
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jbe    11c1 <glmClosestPointOnLineSegment2d+0x61>
mov    %r13,%rdi
jmp    11cc <glmClosestPointOnLineSegment2d+0x6c>
ucomisd 0x8(%rsp),%xmm0
jbe    11d6 <glmClosestPointOnLineSegment2d+0x76>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <glmCopyVector2d@plt>
jmp    11f1 <glmClosestPointOnLineSegment2d+0x91>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <glmMultVectorByScalar2d@plt>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1060 <glmAddVector2d@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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