double cosTheta(double xi, double g) {
  double ans;
  if (g == 0.0) {
    ans = 2 * xi - 1;
  } else {
    double temp = (1.0 - g * g) / (1.0 - g + 2.0 * g * xi);
    ans = 1.0 / (2.0 * g) * (1.0 + g * g - temp * temp);
  }
  return (ans);
}


x86 assembly (clang, -O0):
<cosTheta>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1145 <cosTheta+0x45>
jp     1145 <cosTheta+0x45>
movsd  -0x8(%rbp),%xmm0
addsd  %xmm0,%xmm0
movsd  0xed9(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    120b <cosTheta+0x10b>
movsd  -0x10(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm1,%xmm0
movsd  0xe8e(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  0xe82(%rip),%xmm2
subsd  -0x10(%rbp),%xmm2
movsd  0xe7d(%rip),%xmm1
mulsd  -0x10(%rbp),%xmm1
movsd  -0x8(%rbp),%xmm3
mulsd  %xmm3,%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xe5a(%rip),%xmm1
mulsd  -0x10(%rbp),%xmm1
movsd  0xe45(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  -0x10(%rbp),%xmm2
movsd  -0x10(%rbp),%xmm1
mulsd  %xmm1,%xmm2
movsd  0xe2b(%rip),%xmm1
addsd  %xmm1,%xmm2
movsd  -0x20(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm3
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm3,%xmm1
addsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<cosTheta>:
xorpd  %xmm2,%xmm2
ucomisd %xmm2,%xmm1
jne    1119 <cosTheta+0x19>
jp     1119 <cosTheta+0x19>
addsd  %xmm0,%xmm0
addsd  0xef0(%rip),%xmm0
ret
movapd %xmm1,%xmm2
mulsd  %xmm1,%xmm2
movsd  0xed7(%rip),%xmm3
movapd %xmm3,%xmm4
subsd  %xmm2,%xmm4
movapd %xmm3,%xmm5
subsd  %xmm1,%xmm5
addsd  %xmm1,%xmm1
mulsd  %xmm1,%xmm0
addsd  %xmm5,%xmm0
divsd  %xmm0,%xmm4
addsd  %xmm3,%xmm2
divsd  %xmm1,%xmm3
mulsd  %xmm4,%xmm4
subsd  %xmm4,%xmm2
mulsd  %xmm3,%xmm2
movapd %xmm2,%xmm0
ret

LLVM IR (-O0):
define double @cosTheta(double %0, double %1) {
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca double
store double %0, double* %3
store double %1, double* %4
%7 = load double, double* %4
%8 = fcmp oeq double %7, 0.000000e+00
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load double, double* %3
%11 = call double @llvm.fmuladd.f64(double 2.000000e+00, double %10, double -1.000000e+00)
store double %11, double* %5
br label %35
12: ; preds = %2
%13 = load double, double* %4
%14 = load double, double* %4
%15 = fneg double %13
%16 = call double @llvm.fmuladd.f64(double %15, double %14, double 1.000000e+00)
%17 = load double, double* %4
%18 = fsub double 1.000000e+00, %17
%19 = load double, double* %4
%20 = fmul double 2.000000e+00, %19
%21 = load double, double* %3
%22 = call double @llvm.fmuladd.f64(double %20, double %21, double %18)
%23 = fdiv double %16, %22
store double %23, double* %6
%24 = load double, double* %4
%25 = fmul double 2.000000e+00, %24
%26 = fdiv double 1.000000e+00, %25
%27 = load double, double* %4
%28 = load double, double* %4
%29 = call double @llvm.fmuladd.f64(double %27, double %28, double 1.000000e+00)
%30 = load double, double* %6
%31 = load double, double* %6
%32 = fneg double %30
%33 = call double @llvm.fmuladd.f64(double %32, double %31, double %29)
%34 = fmul double %26, %33
store double %34, double* %5
br label %35
35: ; preds = %12, %9
%36 = load double, double* %5
ret double %36
}

LLVM IR (-O2):
define double @cosTheta(double %0, double %1) {
%3 = fcmp oeq double %1, 0.000000e+00
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call double @llvm.fmuladd.f64(double %0, double 2.000000e+00, double -1.000000e+00)
br label %18
6: ; preds = %2
%7 = fneg double %1
%8 = tail call double @llvm.fmuladd.f64(double %7, double %1, double 1.000000e+00)
%9 = fsub double 1.000000e+00, %1
%10 = fmul double %1, 2.000000e+00
%11 = tail call double @llvm.fmuladd.f64(double %10, double %0, double %9)
%12 = fdiv double %8, %11
%13 = fdiv double 1.000000e+00, %10
%14 = tail call double @llvm.fmuladd.f64(double %1, double %1, double 1.000000e+00)
%15 = fneg double %12
%16 = tail call double @llvm.fmuladd.f64(double %15, double %12, double %14)
%17 = fmul double %13, %16
br label %18
18: ; preds = %6, %4
%19 = phi double [ %5, %4 ], [ %17, %6 ]
ret double %19
}