#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*/ glmClosestPointOnLineSegment3f(float *, float const *,
                                                  float const *, float *);
int /*<<< orphan*/ glmClosestPointOnPlane3f(float *, float const *, float *,
                                            float const *);
float glmComputeDistance3f(float *, float const *);
int /*<<< orphan*/ glmComputeTriangleNormalf(float *, float const *,
                                             float const *, float const *);
int /*<<< orphan*/ glmCopyVector3f(float const *, float *);
scalar_t__ glmIsPointInTriangle3f(float const *, float const *, float const *,
                                  float const *);
int glmMinIndex3f(float, float, float, int /*<<< orphan*/, int, int);
void glmClosestPointOnTriangle3f(float result[3], const float v1[3],
                                 const float v2[3], const float v3[3],
                                 const float p[3]) {
  float temp[3];
  float normal[3];
  float d1, d2, d3;
  float t1[3], t2[3], t3[3];
  glmComputeTriangleNormalf(normal, v1, v2, v3);
  glmClosestPointOnPlane3f(temp, v1, normal, p);
  if (glmIsPointInTriangle3f(p, v1, v2, v3)) {
    glmCopyVector3f(p, result);
    return;
  }
  glmClosestPointOnLineSegment3f(t1, v1, v2, temp);
  glmClosestPointOnLineSegment3f(t2, v2, v3, temp);
  glmClosestPointOnLineSegment3f(t3, v3, v1, temp);
  d1 = glmComputeDistance3f(t1, p);
  d2 = glmComputeDistance3f(t2, p);
  d3 = glmComputeDistance3f(t3, p);
  switch (glmMinIndex3f(d1, d2, d3, 0, 1, 2)) {
  case 0:
    glmCopyVector3f(t1, result);
    return;
    break;
  case 1:
    glmCopyVector3f(t2, result);
    return;
    break;
  case 2:
    glmCopyVector3f(t3, result);
    return;
    break;
  }
}


x86 assembly (clang, -O0):
<glmClosestPointOnTriangle3f>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1070 <glmComputeTriangleNormalf@plt>
lea    -0x34(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x40(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1040 <glmClosestPointOnPlane3f@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1060 <glmIsPointInTriangle3f@plt>
cmp    $0x0,%rax
je     11ea <glmClosestPointOnTriangle3f+0x7a>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <glmCopyVector3f@plt>
jmp    1309 <glmClosestPointOnTriangle3f+0x199>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x58(%rbp),%rdi
mov    %rdi,-0x90(%rbp)
lea    -0x34(%rbp),%rcx
mov    %rcx,-0x98(%rbp)
call   1090 <glmClosestPointOnLineSegment3f@plt>
mov    -0x98(%rbp),%rcx
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x64(%rbp),%rdi
mov    %rdi,-0x88(%rbp)
call   1090 <glmClosestPointOnLineSegment3f@plt>
mov    -0x98(%rbp),%rcx
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    -0x70(%rbp),%rdi
mov    %rdi,-0x80(%rbp)
call   1090 <glmClosestPointOnLineSegment3f@plt>
mov    -0x90(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <glmComputeDistance3f@plt>
mov    -0x88(%rbp),%rdi
movss  %xmm0,-0x44(%rbp)
mov    -0x28(%rbp),%rsi
call   1030 <glmComputeDistance3f@plt>
mov    -0x80(%rbp),%rdi
movss  %xmm0,-0x48(%rbp)
mov    -0x28(%rbp),%rsi
call   1030 <glmComputeDistance3f@plt>
movss  %xmm0,-0x4c(%rbp)
movss  -0x44(%rbp),%xmm0
movss  -0x48(%rbp),%xmm1
movss  -0x4c(%rbp),%xmm2
xor    %edi,%edi
mov    $0x1,%esi
mov    $0x2,%edx
call   1080 <glmMinIndex3f@plt>
mov    %eax,%ecx
mov    %ecx,-0x74(%rbp)
test   %eax,%eax
je     12d8 <glmClosestPointOnTriangle3f+0x168>
jmp    12b6 <glmClosestPointOnTriangle3f+0x146>
mov    -0x74(%rbp),%eax
sub    $0x1,%eax
je     12ea <glmClosestPointOnTriangle3f+0x17a>
jmp    12c7 <glmClosestPointOnTriangle3f+0x157>
mov    -0x74(%rbp),%eax
sub    $0x2,%eax
je     12fc <glmClosestPointOnTriangle3f+0x18c>
jmp    1309 <glmClosestPointOnTriangle3f+0x199>
lea    -0x58(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <glmCopyVector3f@plt>
jmp    1309 <glmClosestPointOnTriangle3f+0x199>
lea    -0x64(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <glmCopyVector3f@plt>
jmp    1309 <glmClosestPointOnTriangle3f+0x199>
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <glmCopyVector3f@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<glmClosestPointOnTriangle3f>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r8,%r15
mov    %rcx,%r12
mov    %rdx,%r13
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x3c(%rsp),%rbp
mov    %rbp,%rdi
call   1070 <glmComputeTriangleNormalf@plt>
lea    0x30(%rsp),%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
mov    %r15,%rcx
call   1040 <glmClosestPointOnPlane3f@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
mov    %r12,%rcx
call   1060 <glmIsPointInTriangle3f@plt>
test   %rax,%rax
je     11dd <glmClosestPointOnTriangle3f+0x6d>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <glmCopyVector3f@plt>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x18(%rsp),%rdi
lea    0x30(%rsp),%rbp
mov    %rbx,%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
call   1090 <glmClosestPointOnLineSegment3f@plt>
lea    0xc(%rsp),%rdi
mov    %r13,%rsi
mov    %r12,%rdx
mov    %rbp,%rcx
call   1090 <glmClosestPointOnLineSegment3f@plt>
lea    0x24(%rsp),%r13
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %rbp,%rcx
call   1090 <glmClosestPointOnLineSegment3f@plt>
lea    0x18(%rsp),%rdi
mov    %r15,%rsi
call   1030 <glmComputeDistance3f@plt>
movss  %xmm0,0x8(%rsp)
lea    0xc(%rsp),%rdi
mov    %r15,%rsi
call   1030 <glmComputeDistance3f@plt>
movss  %xmm0,0x4(%rsp)
mov    %r13,%rdi
mov    %r15,%rsi
call   1030 <glmComputeDistance3f@plt>
movaps %xmm0,%xmm2
movss  0x8(%rsp),%xmm0
movss  0x4(%rsp),%xmm1
xor    %edi,%edi
mov    $0x1,%esi
mov    $0x2,%edx
call   1080 <glmMinIndex3f@plt>
cmp    $0x2,%eax
je     128b <glmClosestPointOnTriangle3f+0x11b>
cmp    $0x1,%eax
je     1295 <glmClosestPointOnTriangle3f+0x125>
test   %eax,%eax
jne    11ce <glmClosestPointOnTriangle3f+0x5e>
lea    0x18(%rsp),%rdi
jmp    11c6 <glmClosestPointOnTriangle3f+0x56>
lea    0x24(%rsp),%rdi
jmp    11c6 <glmClosestPointOnTriangle3f+0x56>
lea    0xc(%rsp),%rdi
jmp    11c6 <glmClosestPointOnTriangle3f+0x56>

LLVM IR (-O0):
define void @glmClosestPointOnTriangle3f(float* %0, float* %1, float* %2, float* %3, float* %4) {
%6 = alloca float*
%7 = alloca float*
%8 = alloca float*
%9 = alloca float*
%10 = alloca float*
%11 = alloca [3 x float]
%12 = alloca [3 x float]
%13 = alloca float
%14 = alloca float
%15 = alloca float
%16 = alloca [3 x float]
%17 = alloca [3 x float]
%18 = alloca [3 x float]
store float* %0, float** %6
store float* %1, float** %7
store float* %2, float** %8
store float* %3, float** %9
store float* %4, float** %10
%19 = getelementptr inbounds [3 x float], [3 x float]* %12, i64 0, i64 0
%20 = load float*, float** %7
%21 = load float*, float** %8
%22 = load float*, float** %9
%23 = call i32 @glmComputeTriangleNormalf(float* %19, float* %20, float* %21, float* %22)
%24 = getelementptr inbounds [3 x float], [3 x float]* %11, i64 0, i64 0
%25 = load float*, float** %7
%26 = getelementptr inbounds [3 x float], [3 x float]* %12, i64 0, i64 0
%27 = load float*, float** %10
%28 = call i32 @glmClosestPointOnPlane3f(float* %24, float* %25, float* %26, float* %27)
%29 = load float*, float** %10
%30 = load float*, float** %7
%31 = load float*, float** %8
%32 = load float*, float** %9
%33 = call i64 @glmIsPointInTriangle3f(float* %29, float* %30, float* %31, float* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %5
%36 = load float*, float** %10
%37 = load float*, float** %6
%38 = call i32 @glmCopyVector3f(float* %36, float* %37)
br label %80
39: ; preds = %5
%40 = getelementptr inbounds [3 x float], [3 x float]* %16, i64 0, i64 0
%41 = load float*, float** %7
%42 = load float*, float** %8
%43 = getelementptr inbounds [3 x float], [3 x float]* %11, i64 0, i64 0
%44 = call i32 @glmClosestPointOnLineSegment3f(float* %40, float* %41, float* %42, float* %43)
%45 = getelementptr inbounds [3 x float], [3 x float]* %17, i64 0, i64 0
%46 = load float*, float** %8
%47 = load float*, float** %9
%48 = getelementptr inbounds [3 x float], [3 x float]* %11, i64 0, i64 0
%49 = call i32 @glmClosestPointOnLineSegment3f(float* %45, float* %46, float* %47, float* %48)
%50 = getelementptr inbounds [3 x float], [3 x float]* %18, i64 0, i64 0
%51 = load float*, float** %9
%52 = load float*, float** %7
%53 = getelementptr inbounds [3 x float], [3 x float]* %11, i64 0, i64 0
%54 = call i32 @glmClosestPointOnLineSegment3f(float* %50, float* %51, float* %52, float* %53)
%55 = getelementptr inbounds [3 x float], [3 x float]* %16, i64 0, i64 0
%56 = load float*, float** %10
%57 = call float @glmComputeDistance3f(float* %55, float* %56)
store float %57, float* %13
%58 = getelementptr inbounds [3 x float], [3 x float]* %17, i64 0, i64 0
%59 = load float*, float** %10
%60 = call float @glmComputeDistance3f(float* %58, float* %59)
store float %60, float* %14
%61 = getelementptr inbounds [3 x float], [3 x float]* %18, i64 0, i64 0
%62 = load float*, float** %10
%63 = call float @glmComputeDistance3f(float* %61, float* %62)
store float %63, float* %15
%64 = load float, float* %13
%65 = load float, float* %14
%66 = load float, float* %15
%67 = call i32 @glmMinIndex3f(float %64, float %65, float %66, i32 0, i32 1, i32 2)
switch i32 %67, label %80 [
i32 0, label %68
i32 1, label %72
i32 2, label %76
]
68: ; preds = %39
%69 = getelementptr inbounds [3 x float], [3 x float]* %16, i64 0, i64 0
%70 = load float*, float** %6
%71 = call i32 @glmCopyVector3f(float* %69, float* %70)
br label %80
72: ; preds = %39
%73 = getelementptr inbounds [3 x float], [3 x float]* %17, i64 0, i64 0
%74 = load float*, float** %6
%75 = call i32 @glmCopyVector3f(float* %73, float* %74)
br label %80
76: ; preds = %39
%77 = getelementptr inbounds [3 x float], [3 x float]* %18, i64 0, i64 0
%78 = load float*, float** %6
%79 = call i32 @glmCopyVector3f(float* %77, float* %78)
br label %80
80: ; preds = %35, %68, %72, %76, %39
ret void
}

LLVM IR (-O2):
define void @glmClosestPointOnTriangle3f(float* %0, float* %1, float* %2, float* %3, float* %4) {
%6 = alloca [3 x float]
%7 = alloca [3 x float]
%8 = alloca [3 x float]
%9 = alloca [3 x float]
%10 = alloca [3 x float]
%11 = bitcast [3 x float]* %6 to i8*
%12 = bitcast [3 x float]* %7 to i8*
%13 = bitcast [3 x float]* %8 to i8*
%14 = bitcast [3 x float]* %9 to i8*
%15 = bitcast [3 x float]* %10 to i8*
%16 = getelementptr inbounds [3 x float], [3 x float]* %7, i64 0, i64 0
%17 = call i32 @glmComputeTriangleNormalf(float* %16, float* %1, float* %2, float* %3)
%18 = getelementptr inbounds [3 x float], [3 x float]* %6, i64 0, i64 0
%19 = call i32 @glmClosestPointOnPlane3f(float* %18, float* %1, float* %16, float* %4)
%20 = call i64 @glmIsPointInTriangle3f(float* %4, float* %1, float* %2, float* %3)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %5
%23 = call i32 @glmCopyVector3f(float* %4, float* %0)
br label %41
24: ; preds = %5
%25 = getelementptr inbounds [3 x float], [3 x float]* %8, i64 0, i64 0
%26 = call i32 @glmClosestPointOnLineSegment3f(float* %25, float* %1, float* %2, float* %18)
%27 = getelementptr inbounds [3 x float], [3 x float]* %9, i64 0, i64 0
%28 = call i32 @glmClosestPointOnLineSegment3f(float* %27, float* %2, float* %3, float* %18)
%29 = getelementptr inbounds [3 x float], [3 x float]* %10, i64 0, i64 0
%30 = call i32 @glmClosestPointOnLineSegment3f(float* %29, float* %3, float* %1, float* %18)
%31 = call float @glmComputeDistance3f(float* %25, float* %4)
%32 = call float @glmComputeDistance3f(float* %27, float* %4)
%33 = call float @glmComputeDistance3f(float* %29, float* %4)
%34 = call i32 @glmMinIndex3f(float %31, float %32, float %33, i32 0, i32 1, i32 2)
switch i32 %34, label %41 [
i32 0, label %35
i32 1, label %37
i32 2, label %39
]
35: ; preds = %24
%36 = call i32 @glmCopyVector3f(float* %25, float* %0)
br label %41
37: ; preds = %24
%38 = call i32 @glmCopyVector3f(float* %27, float* %0)
br label %41
39: ; preds = %24
%40 = call i32 @glmCopyVector3f(float* %29, float* %0)
br label %41
41: ; preds = %24, %39, %37, %35, %22
ret void
}