int CompareDoubles(const double *a, const double *b) {
  if (!a && !b) {
    return 0;
  }
  if (!a) {
    return -1;
  }
  if (!b) {
    return 1;
  }
  double n1 = *a;
  double n2 = *b;
  if (n1 > n2) {
    return 1;
  } else if (n1 < n2) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<CompareDoubles>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    112e <CompareDoubles+0x2e>
cmpq   $0x0,-0x18(%rbp)
jne    112e <CompareDoubles+0x2e>
movl   $0x0,-0x4(%rbp)
jmp    11be <CompareDoubles+0xbe>
cmpq   $0x0,-0x10(%rbp)
jne    1145 <CompareDoubles+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <CompareDoubles+0xbe>
cmpq   $0x0,-0x18(%rbp)
jne    115c <CompareDoubles+0x5c>
movl   $0x1,-0x4(%rbp)
jmp    11be <CompareDoubles+0xbe>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
ucomisd -0x28(%rbp),%xmm0
jbe    1192 <CompareDoubles+0x92>
movl   $0x1,-0x4(%rbp)
jmp    11be <CompareDoubles+0xbe>
movsd  -0x20(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11b2 <CompareDoubles+0xb2>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <CompareDoubles+0xbe>
jmp    11b7 <CompareDoubles+0xb7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<CompareDoubles>:
xor    %ecx,%ecx
mov    %rdi,%rax
or     %rsi,%rax
setne  %cl
neg    %ecx
mov    $0x1,%eax
test   %rdi,%rdi
cmove  %ecx,%eax
je     113e <CompareDoubles+0x3e>
test   %rsi,%rsi
je     113e <CompareDoubles+0x3e>
movsd  (%rdi),%xmm0
movsd  (%rsi),%xmm1
xor    %ecx,%ecx
ucomisd %xmm0,%xmm1
seta   %cl
neg    %ecx
ucomisd %xmm1,%xmm0
mov    $0x1,%eax
cmovbe %ecx,%eax
ret

LLVM IR (-O0):
define i32 @CompareDoubles(double* %0, double* %1) {
%3 = alloca i32
%4 = alloca double*
%5 = alloca double*
%6 = alloca double
%7 = alloca double
store double* %0, double** %4
store double* %1, double** %5
%8 = load double*, double** %4
%9 = icmp ne double* %8, null
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = load double*, double** %5
%12 = icmp ne double* %11, null
br i1 %12, label %14, label %13
13: ; preds = %10
store i32 0, i32* %3
br label %38
14: ; preds = %10, %2
%15 = load double*, double** %4
%16 = icmp ne double* %15, null
br i1 %16, label %18, label %17
17: ; preds = %14
store i32 -1, i32* %3
br label %38
18: ; preds = %14
%19 = load double*, double** %5
%20 = icmp ne double* %19, null
br i1 %20, label %22, label %21
21: ; preds = %18
store i32 1, i32* %3
br label %38
22: ; preds = %18
%23 = load double*, double** %4
%24 = load double, double* %23
store double %24, double* %6
%25 = load double*, double** %5
%26 = load double, double* %25
store double %26, double* %7
%27 = load double, double* %6
%28 = load double, double* %7
%29 = fcmp ogt double %27, %28
br i1 %29, label %30, label %31
30: ; preds = %22
store i32 1, i32* %3
br label %38
31: ; preds = %22
%32 = load double, double* %6
%33 = load double, double* %7
%34 = fcmp olt double %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 -1, i32* %3
br label %38
36: ; preds = %31
br label %37
37: ; preds = %36
store i32 0, i32* %3
br label %38
38: ; preds = %37, %35, %30, %21, %17, %13
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
define i32 @CompareDoubles(double* %0, double* %1) {
%3 = icmp ne double* %0, null
%4 = icmp ne double* %1, null
%5 = or i1 %3, %4
%6 = sext i1 %5 to i32
%7 = and i1 %3, %4
%8 = select i1 %3, i32 1, i32 %6
br i1 %7, label %9, label %16
9: ; preds = %2
%10 = load double, double* %0
%11 = load double, double* %1
%12 = fcmp ogt double %10, %11
%13 = fcmp olt double %10, %11
%14 = sext i1 %13 to i32
%15 = select i1 %12, i32 1, i32 %14
br label %16
16: ; preds = %2, %9
%17 = phi i32 [ %15, %9 ], [ %8, %2 ]
ret i32 %17
}