double grib_nearest_distance(double radius, double lon1, double lat1,
                             double lon2, double lat2) {
  double rlat1 = ((lat1)*acos(0.0) / 90.0);
  double rlat2 = ((lat2)*acos(0.0) / 90.0);
  double rlon1 = lon1;
  double rlon2 = lon2;
  double a;
  if (rlon1 >= 360)
    rlon1 -= 360.0;
  rlon1 = ((rlon1)*acos(0.0) / 90.0);
  if (rlon2 >= 360)
    rlon2 -= 360.0;
  rlon2 = ((rlon2)*acos(0.0) / 90.0);
  a = sin(rlat1) * sin(rlat2) + cos(rlat1) * cos(rlat2) * cos(rlon2 - rlon1);
  if (a > 1 || a < -1)
    a = (int)a;
  return radius * acos(a);
}


x86 assembly (clang, -O0):
<grib_nearest_distance>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movsd  %xmm2,-0x18(%rbp)
movsd  %xmm3,-0x20(%rbp)
movsd  %xmm4,-0x28(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
xorps  %xmm0,%xmm0
call   1030 <acos@plt>
movaps %xmm0,%xmm1
movsd  -0x60(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  0xe8e(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
xorps  %xmm0,%xmm0
call   1030 <acos@plt>
movaps %xmm0,%xmm1
movsd  -0x58(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  0xe5f(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  0xe2d(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11f3 <grib_nearest_distance+0xc3>
movsd  -0x40(%rbp),%xmm0
movsd  0xe16(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x68(%rbp)
xorps  %xmm0,%xmm0
call   1030 <acos@plt>
movaps %xmm0,%xmm1
movsd  -0x68(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  0xdef(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x48(%rbp),%xmm0
movsd  0xdd1(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     124f <grib_nearest_distance+0x11f>
movsd  -0x48(%rbp),%xmm0
movsd  0xdba(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x90(%rbp)
ff
xorps  %xmm0,%xmm0
call   1030 <acos@plt>
movaps %xmm0,%xmm1
movsd  -0x90(%rbp),%xmm0
ff
mulsd  %xmm1,%xmm0
movsd  0xd8d(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1040 <sin@plt>
movsd  %xmm0,-0x70(%rbp)
movsd  -0x38(%rbp),%xmm0
call   1040 <sin@plt>
movsd  %xmm0,-0x78(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1050 <cos@plt>
movsd  %xmm0,-0x88(%rbp)
ff
movsd  -0x38(%rbp),%xmm0
call   1050 <cos@plt>
movaps %xmm0,%xmm1
movsd  -0x88(%rbp),%xmm0
ff
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x80(%rbp)
movsd  -0x48(%rbp),%xmm0
subsd  -0x40(%rbp),%xmm0
call   1050 <cos@plt>
movsd  -0x80(%rbp),%xmm1
movsd  -0x78(%rbp),%xmm2
movaps %xmm0,%xmm3
movsd  -0x70(%rbp),%xmm0
mulsd  %xmm3,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
movsd  0xcff(%rip),%xmm1
ucomisd %xmm1,%xmm0
ja     132e <grib_nearest_distance+0x1fe>
movsd  0xcf5(%rip),%xmm0
ucomisd -0x50(%rbp),%xmm0
jbe    133c <grib_nearest_distance+0x20c>
cvttsd2si -0x50(%rbp),%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x98(%rbp)
ff
movsd  -0x50(%rbp),%xmm0
call   1030 <acos@plt>
movaps %xmm0,%xmm1
movsd  -0x98(%rbp),%xmm0
ff
mulsd  %xmm1,%xmm0
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grib_nearest_distance>:
sub    $0x48,%rsp
movsd  %xmm0,0x18(%rsp)
movsd  0xebe(%rip),%xmm5
mulsd  %xmm5,%xmm2
movsd  0xeba(%rip),%xmm0
divsd  %xmm0,%xmm2
movsd  %xmm2,0x8(%rsp)
mulsd  %xmm4,%xmm5
divsd  %xmm0,%xmm5
movsd  %xmm5,(%rsp)
unpcklpd %xmm1,%xmm3
movapd 0xeaf(%rip),%xmm1
cmpnlepd %xmm3,%xmm1
movapd 0xeb2(%rip),%xmm0
addpd  %xmm3,%xmm0
andpd  %xmm1,%xmm3
andnpd %xmm0,%xmm1
orpd   %xmm3,%xmm1
mulpd  0xeaa(%rip),%xmm1
divpd  0xeb2(%rip),%xmm1
movapd %xmm1,0x20(%rsp)
movapd %xmm2,%xmm0
call   1040 <sin@plt>
movsd  %xmm0,0x10(%rsp)
movsd  (%rsp),%xmm0
call   1040 <sin@plt>
movaps %xmm0,0x30(%rsp)
movsd  0x8(%rsp),%xmm0
call   1050 <cos@plt>
movsd  %xmm0,0x8(%rsp)
movsd  (%rsp),%xmm0
call   1050 <cos@plt>
mulsd  0x8(%rsp),%xmm0
movsd  %xmm0,(%rsp)
movapd 0x20(%rsp),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
subsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
call   1050 <cos@plt>
mulsd  (%rsp),%xmm0
movapd 0x30(%rsp),%xmm3
mulsd  0x10(%rsp),%xmm3
addsd  %xmm0,%xmm3
cvttpd2dq %xmm3,%xmm0
cvtdq2pd %xmm0,%xmm1
movapd %xmm3,%xmm2
cmpltsd 0xde3(%rip),%xmm2
movapd %xmm2,%xmm0
andnpd %xmm3,%xmm0
andpd  %xmm1,%xmm2
orpd   %xmm0,%xmm2
movsd  0xdd3(%rip),%xmm0
cmpltsd %xmm3,%xmm0
movapd %xmm0,%xmm3
andnpd %xmm2,%xmm3
andpd  %xmm1,%xmm0
orpd   %xmm3,%xmm0
call   1030 <acos@plt>
mulsd  0x18(%rsp),%xmm0
add    $0x48,%rsp
ret

LLVM IR (-O0):
define double @grib_nearest_distance(double %0, double %1, double %2, double %3, double %4) {
%6 = alloca double
%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
store double %0, double* %6
store double %1, double* %7
store double %2, double* %8
store double %3, double* %9
store double %4, double* %10
%16 = load double, double* %8
%17 = call double @acos(double 0.000000e+00)
%18 = fmul double %16, %17
%19 = fdiv double %18, 9.000000e+01
store double %19, double* %11
%20 = load double, double* %10
%21 = call double @acos(double 0.000000e+00)
%22 = fmul double %20, %21
%23 = fdiv double %22, 9.000000e+01
store double %23, double* %12
%24 = load double, double* %7
store double %24, double* %13
%25 = load double, double* %9
store double %25, double* %14
%26 = load double, double* %13
%27 = fcmp oge double %26, 3.600000e+02
br i1 %27, label %28, label %31
28: ; preds = %5
%29 = load double, double* %13
%30 = fsub double %29, 3.600000e+02
store double %30, double* %13
br label %31
31: ; preds = %28, %5
%32 = load double, double* %13
%33 = call double @acos(double 0.000000e+00)
%34 = fmul double %32, %33
%35 = fdiv double %34, 9.000000e+01
store double %35, double* %13
%36 = load double, double* %14
%37 = fcmp oge double %36, 3.600000e+02
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = load double, double* %14
%40 = fsub double %39, 3.600000e+02
store double %40, double* %14
br label %41
41: ; preds = %38, %31
%42 = load double, double* %14
%43 = call double @acos(double 0.000000e+00)
%44 = fmul double %42, %43
%45 = fdiv double %44, 9.000000e+01
store double %45, double* %14
%46 = load double, double* %11
%47 = call double @sin(double %46)
%48 = load double, double* %12
%49 = call double @sin(double %48)
%50 = load double, double* %11
%51 = call double @cos(double %50)
%52 = load double, double* %12
%53 = call double @cos(double %52)
%54 = fmul double %51, %53
%55 = load double, double* %14
%56 = load double, double* %13
%57 = fsub double %55, %56
%58 = call double @cos(double %57)
%59 = fmul double %54, %58
%60 = call double @llvm.fmuladd.f64(double %47, double %49, double %59)
store double %60, double* %15
%61 = load double, double* %15
%62 = fcmp ogt double %61, 1.000000e+00
br i1 %62, label %66, label %63
63: ; preds = %41
%64 = load double, double* %15
%65 = fcmp olt double %64, -1.000000e+00
br i1 %65, label %66, label %70
66: ; preds = %63, %41
%67 = load double, double* %15
%68 = fptosi double %67 to i32
%69 = sitofp i32 %68 to double
store double %69, double* %15
br label %70
70: ; preds = %66, %63
%71 = load double, double* %6
%72 = load double, double* %15
%73 = call double @acos(double %72)
%74 = fmul double %71, %73
ret double %74
}

LLVM IR (-O2):
define double @grib_nearest_distance(double %0, double %1, double %2, double %3, double %4) {
%6 = fmul double %2, 0x3FF921FB54442D18
%7 = fdiv double %6, 9.000000e+01
%8 = fmul double %4, 0x3FF921FB54442D18
%9 = fdiv double %8, 9.000000e+01
%10 = insertelement <2 x double> poison, double %3, i64 0
%11 = insertelement <2 x double> %10, double %1, i64 1
%12 = fcmp ult <2 x double> %11, <double 3.600000e+02, double 3.600000e+02>
%13 = fadd <2 x double> %11, <double -3.600000e+02, double -3.600000e+02>
%14 = select <2 x i1> %12, <2 x double> %11, <2 x double> %13
%15 = fmul <2 x double> %14, <double 0x3FF921FB54442D18, double 0x3FF921FB54442D18>
%16 = fdiv <2 x double> %15, <double 9.000000e+01, double 9.000000e+01>
%17 = tail call double @sin(double %7)
%18 = tail call double @sin(double %9)
%19 = tail call double @cos(double %7)
%20 = tail call double @cos(double %9)
%21 = fmul double %19, %20
%22 = shufflevector <2 x double> %16, <2 x double> poison, <2 x i32> <i32 1, i32 undef>
%23 = fsub <2 x double> %16, %22
%24 = extractelement <2 x double> %23, i64 0
%25 = tail call double @cos(double %24)
%26 = fmul double %21, %25
%27 = tail call double @llvm.fmuladd.f64(double %17, double %18, double %26)
%28 = fcmp ogt double %27, 1.000000e+00
%29 = fcmp olt double %27, -1.000000e+00
%30 = or i1 %28, %29
%31 = fptosi double %27 to i32
%32 = sitofp i32 %31 to double
%33 = select i1 %30, double %32, double %27
%34 = tail call double @acos(double %33)
%35 = fmul double %34, %0
ret double %35
}