#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 */
double cart_3d(double *, double *);
int /*<<< orphan*/ cross(double *, double *, double *);
int /*<<< orphan*/ diff_3d(double *, double *, double *);
double inner_3d(double *, double *);
double norm_3d(double *);
double p_ls_dist(double *p0, double *x0, double *x1) {
  double p[3], x[3], u[3];
  double l, a;
  diff_3d(x, x1, x0);
  l = norm_3d(x);
  if (!l)
    return cart_3d(p0, x0);
  diff_3d(p, p0, x0);
  a = inner_3d(p, x) / l;
  if (a < 0)
    return norm_3d(p);
  else if (a > l)
    return cart_3d(p0, x1);
  cross(u, x, p);
  return norm_3d(u) / l;
}


x86 assembly (clang, -O0):
<p_ls_dist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <diff_3d@plt>
lea    -0x60(%rbp),%rdi
call   1030 <norm_3d@plt>
movsd  %xmm0,-0x88(%rbp)
ff
movsd  -0x88(%rbp),%xmm0
ff
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11bb <p_ls_dist+0x6b>
jp     11bb <p_ls_dist+0x6b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <cart_3d@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    1266 <p_ls_dist+0x116>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <diff_3d@plt>
lea    -0x40(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1050 <inner_3d@plt>
divsd  -0x88(%rbp),%xmm0
ff
movsd  %xmm0,-0x90(%rbp)
ff
xorps  %xmm0,%xmm0
ucomisd -0x90(%rbp),%xmm0
ff
jbe    120d <p_ls_dist+0xbd>
lea    -0x40(%rbp),%rdi
call   1030 <norm_3d@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    1266 <p_ls_dist+0x116>
movsd  -0x90(%rbp),%xmm0
ff
ucomisd -0x88(%rbp),%xmm0
ff
jbe    123a <p_ls_dist+0xea>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <cart_3d@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    1266 <p_ls_dist+0x116>
jmp    123f <p_ls_dist+0xef>
lea    -0x80(%rbp),%rdi
lea    -0x60(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1060 <cross@plt>
lea    -0x80(%rbp),%rdi
call   1030 <norm_3d@plt>
divsd  -0x88(%rbp),%xmm0
ff
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<p_ls_dist>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    %rdx,%rsi
mov    %rbx,%rdx
call   1070 <diff_3d@plt>
mov    %r12,%rdi
call   1030 <norm_3d@plt>
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    118b <p_ls_dist+0x3b>
jnp    11d1 <p_ls_dist+0x81>
lea    0x30(%rsp),%r12
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
movsd  %xmm0,0x8(%rsp)
call   1070 <diff_3d@plt>
lea    0x10(%rsp),%rsi
mov    %r12,%rdi
call   1050 <inner_3d@plt>
movsd  0x8(%rsp),%xmm1
divsd  %xmm1,%xmm0
xorpd  %xmm2,%xmm2
ucomisd %xmm0,%xmm2
jbe    11de <p_ls_dist+0x8e>
lea    0x30(%rsp),%rdi
call   1030 <norm_3d@plt>
jmp    1216 <p_ls_dist+0xc6>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <cart_3d@plt>
jmp    1216 <p_ls_dist+0xc6>
ucomisd %xmm1,%xmm0
jbe    11f1 <p_ls_dist+0xa1>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <cart_3d@plt>
jmp    1216 <p_ls_dist+0xc6>
lea    0x50(%rsp),%rbx
lea    0x10(%rsp),%rsi
lea    0x30(%rsp),%rdx
mov    %rbx,%rdi
call   1060 <cross@plt>
mov    %rbx,%rdi
call   1030 <norm_3d@plt>
divsd  0x8(%rsp),%xmm0
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define double @p_ls_dist(double* %0, double* %1, double* %2) {
%4 = alloca double
%5 = alloca double*
%6 = alloca double*
%7 = alloca double*
%8 = alloca [3 x double]
%9 = alloca [3 x double]
%10 = alloca [3 x double]
%11 = alloca double
%12 = alloca double
store double* %0, double** %5
store double* %1, double** %6
store double* %2, double** %7
%13 = getelementptr inbounds [3 x double], [3 x double]* %9, i64 0, i64 0
%14 = load double*, double** %7
%15 = load double*, double** %6
%16 = call i32 @diff_3d(double* %13, double* %14, double* %15)
%17 = getelementptr inbounds [3 x double], [3 x double]* %9, i64 0, i64 0
%18 = call double @norm_3d(double* %17)
store double %18, double* %11
%19 = load double, double* %11
%20 = fcmp une double %19, 0.000000e+00
br i1 %20, label %25, label %21
21: ; preds = %3
%22 = load double*, double** %5
%23 = load double*, double** %6
%24 = call double @cart_3d(double* %22, double* %23)
store double %24, double* %4
br label %58
25: ; preds = %3
%26 = getelementptr inbounds [3 x double], [3 x double]* %8, i64 0, i64 0
%27 = load double*, double** %5
%28 = load double*, double** %6
%29 = call i32 @diff_3d(double* %26, double* %27, double* %28)
%30 = getelementptr inbounds [3 x double], [3 x double]* %8, i64 0, i64 0
%31 = getelementptr inbounds [3 x double], [3 x double]* %9, i64 0, i64 0
%32 = call double @inner_3d(double* %30, double* %31)
%33 = load double, double* %11
%34 = fdiv double %32, %33
store double %34, double* %12
%35 = load double, double* %12
%36 = fcmp olt double %35, 0.000000e+00
br i1 %36, label %37, label %40
37: ; preds = %25
%38 = getelementptr inbounds [3 x double], [3 x double]* %8, i64 0, i64 0
%39 = call double @norm_3d(double* %38)
store double %39, double* %4
br label %58
40: ; preds = %25
%41 = load double, double* %12
%42 = load double, double* %11
%43 = fcmp ogt double %41, %42
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = load double*, double** %5
%46 = load double*, double** %7
%47 = call double @cart_3d(double* %45, double* %46)
store double %47, double* %4
br label %58
48: ; preds = %40
br label %49
49: ; preds = %48
%50 = getelementptr inbounds [3 x double], [3 x double]* %10, i64 0, i64 0
%51 = getelementptr inbounds [3 x double], [3 x double]* %9, i64 0, i64 0
%52 = getelementptr inbounds [3 x double], [3 x double]* %8, i64 0, i64 0
%53 = call i32 @cross(double* %50, double* %51, double* %52)
%54 = getelementptr inbounds [3 x double], [3 x double]* %10, i64 0, i64 0
%55 = call double @norm_3d(double* %54)
%56 = load double, double* %11
%57 = fdiv double %55, %56
store double %57, double* %4
br label %58
58: ; preds = %49, %44, %37, %21
%59 = load double, double* %4
ret double %59
}

LLVM IR (-O2):
define double @p_ls_dist(double* %0, double* %1, double* %2) {
%4 = alloca [3 x double]
%5 = alloca [3 x double]
%6 = alloca [3 x double]
%7 = bitcast [3 x double]* %4 to i8*
%8 = bitcast [3 x double]* %5 to i8*
%9 = bitcast [3 x double]* %6 to i8*
%10 = getelementptr inbounds [3 x double], [3 x double]* %5, i64 0, i64 0
%11 = call i32 @diff_3d(double* %10, double* %2, double* %1)
%12 = call double @norm_3d(double* %10)
%13 = fcmp une double %12, 0.000000e+00
br i1 %13, label %16, label %14
14: ; preds = %3
%15 = call double @cart_3d(double* %0, double* %1)
br label %33
16: ; preds = %3
%17 = getelementptr inbounds [3 x double], [3 x double]* %4, i64 0, i64 0
%18 = call i32 @diff_3d(double* %17, double* %0, double* %1)
%19 = call double @inner_3d(double* %17, double* %10)
%20 = fdiv double %19, %12
%21 = fcmp olt double %20, 0.000000e+00
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call double @norm_3d(double* %17)
br label %33
24: ; preds = %16
%25 = fcmp ogt double %20, %12
br i1 %25, label %26, label %28
26: ; preds = %24
%27 = call double @cart_3d(double* %0, double* %2)
br label %33
28: ; preds = %24
%29 = getelementptr inbounds [3 x double], [3 x double]* %6, i64 0, i64 0
%30 = call i32 @cross(double* %29, double* %10, double* %17)
%31 = call double @norm_3d(double* %29)
%32 = fdiv double %31, %12
br label %33
33: ; preds = %28, %26, %22, %14
%34 = phi double [ %23, %22 ], [ %27, %26 ], [ %32, %28 ], [ %15, %14 ]
ret double %34
}