#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 asin(double);
double cos(double);
double sin(double);
double sqrt(double);
double distance(double lat1, double lon1, double lat2, double lon2) {
  double dlon = lon1 - lon2;
  double dlat = lat1 - lat2;
  double a1, a2, a, sa, c, d;
  if (dlon == 0 && dlat == 0)
    return 0;
  a1 = sin(dlat / 2);
  a2 = sin(dlon / 2);
  a = (a1 * a1) + cos(lat1) * cos(lat2) * a2 * a2;
  sa = sqrt(a);
  if (sa <= 1.0) {
    c = 2 * asin(sa);
  } else {
    c = 2 * asin(1.0);
  }
  d = 6378.137 * c;
  return (d);
}


x86 assembly (clang, -O0):
<distance>:
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  -0x18(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x30(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11ba <distance+0x7a>
jp     11ba <distance+0x7a>
movsd  -0x38(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11ba <distance+0x7a>
jp     11ba <distance+0x7a>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12bc <distance+0x17c>
movsd  -0x38(%rbp),%xmm0
movsd  0xe41(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1030 <sin@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  0xe26(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1030 <sin@plt>
movsd  %xmm0,-0x48(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x70(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x78(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1050 <cos@plt>
movsd  %xmm0,-0x80(%rbp)
movsd  -0x20(%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  -0x48(%rbp),%xmm1
mulsd  -0x48(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
call   1060 <sqrt@plt>
movsd  %xmm0,-0x58(%rbp)
movsd  0xd9f(%rip),%xmm0
ucomisd -0x58(%rbp),%xmm0
jb     1287 <distance+0x147>
movsd  -0x58(%rbp),%xmm0
call   1040 <asin@plt>
shl    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x60(%rbp)
jmp    12a0 <distance+0x160>
movsd  0xd71(%rip),%xmm0
call   1040 <asin@plt>
shl    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  0xd68(%rip),%xmm0
mulsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x68(%rbp)
movsd  -0x68(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<distance>:
movapd %xmm0,%xmm4
subsd  %xmm3,%xmm1
subsd  %xmm2,%xmm0
xorpd  %xmm5,%xmm5
ucomisd %xmm5,%xmm0
jne    1169 <distance+0x29>
jp     1169 <distance+0x29>
xorpd  %xmm3,%xmm3
ucomisd %xmm3,%xmm1
jne    1169 <distance+0x29>
jp     1169 <distance+0x29>
movapd %xmm5,%xmm0
ret
sub    $0x28,%rsp
mulsd  0xe8b(%rip),%xmm0
movsd  %xmm2,0x18(%rsp)
movsd  %xmm4,0x8(%rsp)
movsd  %xmm1,0x10(%rsp)
call   1030 <sin@plt>
movsd  %xmm0,0x20(%rsp)
movsd  0x10(%rsp),%xmm0
mulsd  0xe60(%rip),%xmm0
call   1030 <sin@plt>
movsd  %xmm0,0x10(%rsp)
movsd  0x8(%rsp),%xmm0
call   1050 <cos@plt>
movsd  %xmm0,0x8(%rsp)
movsd  0x18(%rsp),%xmm0
call   1050 <cos@plt>
mulsd  0x8(%rsp),%xmm0
movsd  0x10(%rsp),%xmm1
mulsd  %xmm1,%xmm0
mulsd  %xmm1,%xmm0
movsd  0x20(%rsp),%xmm1
mulsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
movapd %xmm1,%xmm0
jb     11fd <distance+0xbd>
sqrtsd %xmm0,%xmm0
jmp    1202 <distance+0xc2>
call   1060 <sqrt@plt>
minsd  0xdfe(%rip),%xmm0
call   1040 <asin@plt>
add    %eax,%eax
xorps  %xmm5,%xmm5
cvtsi2sd %eax,%xmm5
mulsd  0xdf0(%rip),%xmm5
add    $0x28,%rsp
movapd %xmm5,%xmm0
ret

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

LLVM IR (-O2):
define double @distance(double %0, double %1, double %2, double %3) {
%5 = fsub double %1, %3
%6 = fsub double %0, %2
%7 = fcmp oeq double %5, 0.000000e+00
%8 = fcmp oeq double %6, 0.000000e+00
%9 = and i1 %8, %7
br i1 %9, label %28, label %10
10: ; preds = %4
%11 = fmul double %6, 5.000000e-01
%12 = tail call double @sin(double %11)
%13 = fmul double %5, 5.000000e-01
%14 = tail call double @sin(double %13)
%15 = tail call double @cos(double %0)
%16 = tail call double @cos(double %2)
%17 = fmul double %15, %16
%18 = fmul double %14, %17
%19 = fmul double %14, %18
%20 = tail call double @llvm.fmuladd.f64(double %12, double %12, double %19)
%21 = tail call double @sqrt(double %20)
%22 = fcmp ole double %21, 1.000000e+00
%23 = select i1 %22, double %21, double 1.000000e+00
%24 = tail call i32 @asin(double %23)
%25 = shl nsw i32 %24, 1
%26 = sitofp i32 %25 to double
%27 = fmul double %26, 0x40B8EA23126E978D
br label %28
28: ; preds = %4, %10
%29 = phi double [ %27, %10 ], [ 0.000000e+00, %4 ]
ret double %29
}