#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 fabs(double);
double sqrt(double);
double distanceToLineSegment(double x, double y, double x1, double y1,
                             double x2, double y2) {
  double dx = x2 - x1;
  double dy = y2 - y1;
  double dx1 = x1 - x;
  double dy1 = y1 - y;
  double dx2 = x2 - x;
  double dy2 = y2 - y;
  double dot_product_ab_ac = -dx * dx1 - dy * dy1;
  if (dot_product_ab_ac <= 0) {
    return sqrt(dx1 * dx1 + dy1 * dy1);
  }
  double dot_product_ba_bc = dx * dx2 + dy * dy2;
  if (dot_product_ba_bc <= 0) {
    return sqrt(dx2 * dx2 + dy2 * dy2);
  }
  return fabs(dy * x - dx * y + x2 * y1 - y2 * x1) / sqrt(dy * dy + dx * dx);
}


x86 assembly (clang, -O0):
<distanceToLineSegment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
movsd  %xmm3,-0x28(%rbp)
movsd  %xmm4,-0x30(%rbp)
movsd  %xmm5,-0x38(%rbp)
movsd  -0x30(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x38(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x20(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x28(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x30(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x38(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x68(%rbp)
movsd  -0x40(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  -0x50(%rbp),%xmm2
movsd  -0x48(%rbp),%xmm1
mulsd  -0x58(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x70(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x70(%rbp),%xmm0
jb     121b <distanceToLineSegment+0x10b>
movsd  -0x50(%rbp),%xmm0
movsd  -0x50(%rbp),%xmm2
movsd  -0x58(%rbp),%xmm1
mulsd  -0x58(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1030 <sqrt@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    1325 <distanceToLineSegment+0x215>
movsd  -0x40(%rbp),%xmm0
movsd  -0x60(%rbp),%xmm2
movsd  -0x48(%rbp),%xmm1
mulsd  -0x68(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x78(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x78(%rbp),%xmm0
jb     1275 <distanceToLineSegment+0x165>
movsd  -0x60(%rbp),%xmm0
movsd  -0x60(%rbp),%xmm2
movsd  -0x68(%rbp),%xmm1
mulsd  -0x68(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1030 <sqrt@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    1325 <distanceToLineSegment+0x215>
movsd  -0x48(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
movsd  -0x40(%rbp),%xmm1
mulsd  -0x18(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  -0x30(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movsd  -0x38(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movaps 0xd16(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x80(%rbp)
movsd  -0x48(%rbp),%xmm0
movsd  -0x48(%rbp),%xmm2
movsd  -0x40(%rbp),%xmm1
mulsd  -0x40(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1030 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x80(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<distanceToLineSegment>:
sub    $0x18,%rsp
movapd %xmm0,%xmm8
movapd %xmm4,%xmm10
subsd  %xmm2,%xmm10
movapd %xmm5,%xmm0
subsd  %xmm3,%xmm0
movapd %xmm2,%xmm13
subsd  %xmm8,%xmm13
movapd %xmm3,%xmm12
subsd  %xmm1,%xmm12
movapd 0xeb8(%rip),%xmm11
xorpd  %xmm10,%xmm11
movapd %xmm12,%xmm7
mulsd  %xmm0,%xmm7
movapd %xmm11,%xmm6
mulsd  %xmm13,%xmm6
subsd  %xmm7,%xmm6
xorpd  %xmm9,%xmm9
ucomisd %xmm6,%xmm9
jae    11e8 <distanceToLineSegment+0xd8>
movapd %xmm4,%xmm6
subsd  %xmm8,%xmm6
movapd %xmm5,%xmm12
subsd  %xmm1,%xmm12
movapd %xmm0,%xmm13
mulsd  %xmm12,%xmm13
movapd %xmm10,%xmm7
mulsd  %xmm6,%xmm7
addsd  %xmm13,%xmm7
ucomisd %xmm7,%xmm9
jae    1208 <distanceToLineSegment+0xf8>
mulsd  %xmm1,%xmm11
mulsd  %xmm0,%xmm8
addsd  %xmm11,%xmm8
mulsd  %xmm3,%xmm4
addsd  %xmm8,%xmm4
mulsd  %xmm2,%xmm5
subsd  %xmm5,%xmm4
andpd  0xe46(%rip),%xmm4
mulsd  %xmm10,%xmm10
mulsd  %xmm0,%xmm0
addsd  %xmm10,%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     1237 <distanceToLineSegment+0x127>
sqrtsd %xmm0,%xmm0
jmp    1246 <distanceToLineSegment+0x136>
mulsd  %xmm12,%xmm12
mulsd  %xmm13,%xmm13
addsd  %xmm12,%xmm13
ucomisd %xmm9,%xmm13
jb     1229 <distanceToLineSegment+0x119>
xorps  %xmm4,%xmm4
sqrtsd %xmm13,%xmm4
jmp    124a <distanceToLineSegment+0x13a>
mulsd  %xmm12,%xmm12
mulsd  %xmm6,%xmm6
addsd  %xmm12,%xmm6
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm6
jb     1253 <distanceToLineSegment+0x143>
xorps  %xmm4,%xmm4
sqrtsd %xmm6,%xmm4
jmp    124a <distanceToLineSegment+0x13a>
movapd %xmm13,%xmm0
add    $0x18,%rsp
jmp    1030 <sqrt@plt>
movapd %xmm4,(%rsp)
call   1030 <sqrt@plt>
movapd (%rsp),%xmm4
divsd  %xmm0,%xmm4
movapd %xmm4,%xmm0
add    $0x18,%rsp
ret
movapd %xmm6,%xmm0
add    $0x18,%rsp
jmp    1030 <sqrt@plt>

LLVM IR (-O0):
define double @distanceToLineSegment(double %0, double %1, double %2, double %3, double %4, double %5) {
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca double
%16 = alloca double
%17 = alloca double
%18 = alloca double
%19 = alloca double
%20 = alloca double
%21 = alloca double
store double %0, double* %8
store double %1, double* %9
store double %2, double* %10
store double %3, double* %11
store double %4, double* %12
store double %5, double* %13
%22 = load double, double* %12
%23 = load double, double* %10
%24 = fsub double %22, %23
store double %24, double* %14
%25 = load double, double* %13
%26 = load double, double* %11
%27 = fsub double %25, %26
store double %27, double* %15
%28 = load double, double* %10
%29 = load double, double* %8
%30 = fsub double %28, %29
store double %30, double* %16
%31 = load double, double* %11
%32 = load double, double* %9
%33 = fsub double %31, %32
store double %33, double* %17
%34 = load double, double* %12
%35 = load double, double* %8
%36 = fsub double %34, %35
store double %36, double* %18
%37 = load double, double* %13
%38 = load double, double* %9
%39 = fsub double %37, %38
store double %39, double* %19
%40 = load double, double* %14
%41 = fneg double %40
%42 = load double, double* %16
%43 = load double, double* %15
%44 = load double, double* %17
%45 = fmul double %43, %44
%46 = fneg double %45
%47 = call double @llvm.fmuladd.f64(double %41, double %42, double %46)
store double %47, double* %20
%48 = load double, double* %20
%49 = fcmp ole double %48, 0.000000e+00
br i1 %49, label %50, label %58
50: ; preds = %6
%51 = load double, double* %16
%52 = load double, double* %16
%53 = load double, double* %17
%54 = load double, double* %17
%55 = fmul double %53, %54
%56 = call double @llvm.fmuladd.f64(double %51, double %52, double %55)
%57 = call double @sqrt(double %56)
store double %57, double* %7
br label %99
58: ; preds = %6
%59 = load double, double* %14
%60 = load double, double* %18
%61 = load double, double* %15
%62 = load double, double* %19
%63 = fmul double %61, %62
%64 = call double @llvm.fmuladd.f64(double %59, double %60, double %63)
store double %64, double* %21
%65 = load double, double* %21
%66 = fcmp ole double %65, 0.000000e+00
br i1 %66, label %67, label %75
67: ; preds = %58
%68 = load double, double* %18
%69 = load double, double* %18
%70 = load double, double* %19
%71 = load double, double* %19
%72 = fmul double %70, %71
%73 = call double @llvm.fmuladd.f64(double %68, double %69, double %72)
%74 = call double @sqrt(double %73)
store double %74, double* %7
br label %99
75: ; preds = %58
%76 = load double, double* %15
%77 = load double, double* %8
%78 = load double, double* %14
%79 = load double, double* %9
%80 = fmul double %78, %79
%81 = fneg double %80
%82 = call double @llvm.fmuladd.f64(double %76, double %77, double %81)
%83 = load double, double* %12
%84 = load double, double* %11
%85 = call double @llvm.fmuladd.f64(double %83, double %84, double %82)
%86 = load double, double* %13
%87 = load double, double* %10
%88 = fneg double %86
%89 = call double @llvm.fmuladd.f64(double %88, double %87, double %85)
%90 = call double @llvm.fabs.f64(double %89)
%91 = load double, double* %15
%92 = load double, double* %15
%93 = load double, double* %14
%94 = load double, double* %14
%95 = fmul double %93, %94
%96 = call double @llvm.fmuladd.f64(double %91, double %92, double %95)
%97 = call double @sqrt(double %96)
%98 = fdiv double %90, %97
store double %98, double* %7
br label %99
99: ; preds = %75, %67, %50
%100 = load double, double* %7
ret double %100
}

LLVM IR (-O2):
define double @distanceToLineSegment(double %0, double %1, double %2, double %3, double %4, double %5) {
%7 = fsub double %4, %2
%8 = fsub double %5, %3
%9 = fsub double %2, %0
%10 = fsub double %3, %1
%11 = fsub double %4, %0
%12 = fsub double %5, %1
%13 = fneg double %7
%14 = fneg double %8
%15 = fmul double %10, %14
%16 = tail call double @llvm.fmuladd.f64(double %13, double %9, double %15)
%17 = fcmp ugt double %16, 0.000000e+00
br i1 %17, label %22, label %18
18: ; preds = %6
%19 = fmul double %10, %10
%20 = tail call double @llvm.fmuladd.f64(double %9, double %9, double %19)
%21 = tail call double @sqrt(double %20)
br label %41
22: ; preds = %6
%23 = fmul double %8, %12
%24 = tail call double @llvm.fmuladd.f64(double %7, double %11, double %23)
%25 = fcmp ugt double %24, 0.000000e+00
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = fmul double %12, %12
%28 = tail call double @llvm.fmuladd.f64(double %11, double %11, double %27)
%29 = tail call double @sqrt(double %28)
br label %41
30: ; preds = %22
%31 = fmul double %13, %1
%32 = tail call double @llvm.fmuladd.f64(double %8, double %0, double %31)
%33 = tail call double @llvm.fmuladd.f64(double %4, double %3, double %32)
%34 = fneg double %5
%35 = tail call double @llvm.fmuladd.f64(double %34, double %2, double %33)
%36 = tail call double @llvm.fabs.f64(double %35)
%37 = fmul double %7, %7
%38 = tail call double @llvm.fmuladd.f64(double %8, double %8, double %37)
%39 = tail call double @sqrt(double %38)
%40 = fdiv double %36, %39
br label %41
41: ; preds = %26, %30, %18
%42 = phi double [ %21, %18 ], [ %29, %26 ], [ %40, %30 ]
ret double %42
}