double xsq_to_stddev(xsq, k, n, approx)
double xsq, k, n;
int *approx;
{
  double stddev, v, wsq, w, a, keff, twologp, u;
  xsq = xsq + k / n;
  v = k - 1.0;
  wsq = xsq - v - v * log(xsq / v);
  if (wsq < 0.0)
    wsq = 0.0;
  w = sqrt(wsq);
  a = sqrt(2.0 / v) / 3.0;
  if (xsq <= v) {
    stddev = -w + a;
  } else {
    keff = k / (1.0 + xsq / n) * (1.0 - exp(-(xsq + n) / k));
    twologp =
        (2.0 * n - 1.0 - 2.0 * keff) * log(k / keff) - 2.0 * keff + 1.837877;
    if (twologp < 2.83)
      twologp = 1000.0;
    u = sqrt(twologp - log(twologp - 1.837877) - 1.837877);
    stddev = (((u) < (w + a)) ? (u) : (w + a));
  }
  if (fabs(stddev) > n / sqrt(2.0 * v)) {
    *approx = 1;
  } else {
    *approx = 0;
  }
  return (stddev);
}


x86 assembly (clang, -O0):
<xsq_to_stddev>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movsd  %xmm2,-0x18(%rbp)
mov    %rdi,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
divsd  -0x18(%rbp),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xe8d(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x8(%rbp),%xmm0
subsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x70(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x68(%rbp)
movsd  -0x8(%rbp),%xmm0
divsd  -0x30(%rbp),%xmm0
call   1040 <log@plt>
movsd  -0x70(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x68(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x38(%rbp),%xmm0
jbe    11eb <xsq_to_stddev+0xbb>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
call   1050 <sqrt@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  0xe0e(%rip),%xmm0
divsd  -0x30(%rbp),%xmm0
call   1050 <sqrt@plt>
movsd  0xdf4(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x8(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     125c <xsq_to_stddev+0x12c>
movsd  -0x40(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
addsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    13f5 <xsq_to_stddev+0x2c5>
movsd  -0x10(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm2
divsd  -0x18(%rbp),%xmm2
movsd  0xd8d(%rip),%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x80(%rbp)
movsd  -0x8(%rbp),%xmm0
addsd  -0x18(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
divsd  -0x10(%rbp),%xmm0
call   1030 <exp@plt>
movaps %xmm0,%xmm2
movsd  -0x80(%rbp),%xmm0
movsd  0xd45(%rip),%xmm1
subsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x18(%rbp),%xmm0
addsd  %xmm0,%xmm0
movsd  0xd4f(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  -0x50(%rbp),%xmm1
addsd  %xmm1,%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x78(%rbp)
movsd  -0x10(%rbp),%xmm0
divsd  -0x50(%rbp),%xmm0
call   1040 <log@plt>
movaps %xmm0,%xmm2
movsd  -0x78(%rbp),%xmm0
movsd  0xd02(%rip),%xmm1
mulsd  -0x50(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  0xce6(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  0xccd(%rip),%xmm0
ucomisd -0x58(%rbp),%xmm0
jbe    1363 <xsq_to_stddev+0x233>
movsd  0xcd2(%rip),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x88(%rbp)
ff
movsd  -0x58(%rbp),%xmm0
movsd  0xca3(%rip),%xmm1
subsd  %xmm1,%xmm0
call   1040 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x88(%rbp),%xmm0
ff
subsd  %xmm1,%xmm0
movsd  0xc83(%rip),%xmm1
subsd  %xmm1,%xmm0
call   1050 <sqrt@plt>
movsd  %xmm0,-0x60(%rbp)
movsd  -0x60(%rbp),%xmm1
movsd  -0x40(%rbp),%xmm0
addsd  -0x48(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    13d6 <xsq_to_stddev+0x2a6>
movsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x90(%rbp)
ff
jmp    13e8 <xsq_to_stddev+0x2b8>
movsd  -0x40(%rbp),%xmm0
addsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x90(%rbp)
ff
movsd  -0x90(%rbp),%xmm0
ff
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movaps 0xc3f(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x98(%rbp)
ff
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0xa0(%rbp)
ff
movsd  0xbee(%rip),%xmm0
mulsd  -0x30(%rbp),%xmm0
call   1050 <sqrt@plt>
movsd  -0xa0(%rbp),%xmm1
ff
movaps %xmm0,%xmm2
movsd  -0x98(%rbp),%xmm0
ff
divsd  %xmm2,%xmm1
ucomisd %xmm1,%xmm0
jbe    145c <xsq_to_stddev+0x32c>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
jmp    1466 <xsq_to_stddev+0x336>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movsd  -0x28(%rbp),%xmm0
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xsq_to_stddev>:
push   %rbx
sub    $0x50,%rsp
mov    %rdi,%rbx
movapd %xmm1,%xmm3
movsd  %xmm2,0x18(%rsp)
divsd  %xmm2,%xmm3
addsd  %xmm0,%xmm3
movsd  0xeae(%rip),%xmm2
movsd  %xmm1,0x20(%rsp)
addsd  %xmm1,%xmm2
movsd  %xmm2,0x30(%rsp)
movapd %xmm3,%xmm0
subsd  %xmm2,%xmm0
movsd  %xmm0,(%rsp)
movapd %xmm3,0x40(%rsp)
movapd %xmm3,%xmm0
divsd  %xmm2,%xmm0
call   1040 <log@plt>
movsd  0x30(%rsp),%xmm1
mulsd  %xmm1,%xmm0
movsd  (%rsp),%xmm3
subsd  %xmm0,%xmm3
xorpd  %xmm2,%xmm2
xorpd  %xmm0,%xmm0
maxsd  %xmm3,%xmm0
ucomisd %xmm2,%xmm0
jb     11ad <xsq_to_stddev+0x7d>
sqrtsd %xmm0,%xmm0
jmp    11bc <xsq_to_stddev+0x8c>
call   1050 <sqrt@plt>
xorpd  %xmm2,%xmm2
movsd  0x30(%rsp),%xmm1
movsd  %xmm0,(%rsp)
movsd  0xe3f(%rip),%xmm0
divsd  %xmm1,%xmm0
ucomisd %xmm2,%xmm0
jb     11d9 <xsq_to_stddev+0xa9>
sqrtsd %xmm0,%xmm0
jmp    11e4 <xsq_to_stddev+0xb4>
call   1050 <sqrt@plt>
movsd  0x30(%rsp),%xmm1
movapd 0x40(%rsp),%xmm2
divsd  0xe1e(%rip),%xmm0
ucomisd %xmm2,%xmm1
jae    1305 <xsq_to_stddev+0x1d5>
movsd  %xmm0,0x40(%rsp)
movapd %xmm2,%xmm0
movsd  0x18(%rsp),%xmm3
divsd  %xmm3,%xmm0
addsd  0xe00(%rip),%xmm0
movsd  0x20(%rsp),%xmm1
movapd %xmm1,%xmm4
divsd  %xmm0,%xmm4
movsd  %xmm4,0x10(%rsp)
addsd  %xmm3,%xmm2
xorpd  0xe18(%rip),%xmm2
divsd  %xmm1,%xmm2
movapd %xmm2,%xmm0
call   1030 <exp@plt>
movsd  0xdcb(%rip),%xmm1
subsd  %xmm0,%xmm1
mulsd  0x10(%rsp),%xmm1
movsd  0x18(%rsp),%xmm2
addsd  %xmm2,%xmm2
addsd  0xd97(%rip),%xmm2
movsd  0x20(%rsp),%xmm0
divsd  %xmm1,%xmm0
mulsd  0xda5(%rip),%xmm1
movsd  %xmm1,0x20(%rsp)
addsd  %xmm1,%xmm2
movsd  %xmm2,0x10(%rsp)
call   1040 <log@plt>
mulsd  0x10(%rsp),%xmm0
addsd  0x20(%rsp),%xmm0
addsd  0xd84(%rip),%xmm0
movapd %xmm0,%xmm1
cmpltsd 0xd7f(%rip),%xmm1
movsd  0xd7f(%rip),%xmm2
andpd  %xmm1,%xmm2
andnpd %xmm0,%xmm1
orpd   %xmm1,%xmm2
movapd %xmm2,0x20(%rsp)
movapd %xmm2,%xmm0
addsd  0xd69(%rip),%xmm0
call   1040 <log@plt>
movapd 0x20(%rsp),%xmm1
subsd  %xmm0,%xmm1
addsd  0xd52(%rip),%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
movapd %xmm1,%xmm0
jb     1310 <xsq_to_stddev+0x1e0>
xorps  %xmm2,%xmm2
sqrtsd %xmm0,%xmm2
jmp    1319 <xsq_to_stddev+0x1e9>
subsd  (%rsp),%xmm0
movapd %xmm0,%xmm2
jmp    132e <xsq_to_stddev+0x1fe>
call   1050 <sqrt@plt>
movapd %xmm0,%xmm2
movsd  (%rsp),%xmm0
addsd  0x40(%rsp),%xmm0
minsd  %xmm0,%xmm2
movsd  0x30(%rsp),%xmm1
movapd 0xd2a(%rip),%xmm3
andpd  %xmm2,%xmm3
addsd  %xmm1,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
jb     1351 <xsq_to_stddev+0x221>
xorps  %xmm0,%xmm0
sqrtsd %xmm1,%xmm0
jmp    1370 <xsq_to_stddev+0x240>
movapd %xmm1,%xmm0
movapd %xmm2,0x30(%rsp)
movapd %xmm3,(%rsp)
call   1050 <sqrt@plt>
movapd (%rsp),%xmm3
movapd 0x30(%rsp),%xmm2
movsd  0x18(%rsp),%xmm1
divsd  %xmm0,%xmm1
xor    %eax,%eax
ucomisd %xmm1,%xmm3
seta   %al
mov    %eax,(%rbx)
movapd %xmm2,%xmm0
add    $0x50,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define double @xsq_to_stddev(double %0, double %1, double %2, i32* %3) {
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca i32*
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca double
%16 = alloca double
store double %0, double* %5
store double %1, double* %6
store double %2, double* %7
store i32* %3, i32** %8
%17 = load double, double* %5
%18 = load double, double* %6
%19 = load double, double* %7
%20 = fdiv double %18, %19
%21 = fadd double %17, %20
store double %21, double* %5
%22 = load double, double* %6
%23 = fsub double %22, 1.000000e+00
store double %23, double* %10
%24 = load double, double* %5
%25 = load double, double* %10
%26 = fsub double %24, %25
%27 = load double, double* %10
%28 = load double, double* %5
%29 = load double, double* %10
%30 = fdiv double %28, %29
%31 = call double @log(double %30)
%32 = fneg double %27
%33 = call double @llvm.fmuladd.f64(double %32, double %31, double %26)
store double %33, double* %11
%34 = load double, double* %11
%35 = fcmp olt double %34, 0.000000e+00
br i1 %35, label %36, label %37
36: ; preds = %4
store double 0.000000e+00, double* %11
br label %37
37: ; preds = %36, %4
%38 = load double, double* %11
%39 = call double @sqrt(double %38)
store double %39, double* %12
%40 = load double, double* %10
%41 = fdiv double 2.000000e+00, %40
%42 = call double @sqrt(double %41)
%43 = fdiv double %42, 3.000000e+00
store double %43, double* %13
%44 = load double, double* %5
%45 = load double, double* %10
%46 = fcmp ole double %44, %45
br i1 %46, label %47, label %52
47: ; preds = %37
%48 = load double, double* %12
%49 = fneg double %48
%50 = load double, double* %13
%51 = fadd double %49, %50
store double %51, double* %9
br label %105
52: ; preds = %37
%53 = load double, double* %6
%54 = load double, double* %5
%55 = load double, double* %7
%56 = fdiv double %54, %55
%57 = fadd double 1.000000e+00, %56
%58 = fdiv double %53, %57
%59 = load double, double* %5
%60 = load double, double* %7
%61 = fadd double %59, %60
%62 = fneg double %61
%63 = load double, double* %6
%64 = fdiv double %62, %63
%65 = call double @exp(double %64)
%66 = fsub double 1.000000e+00, %65
%67 = fmul double %58, %66
store double %67, double* %14
%68 = load double, double* %7
%69 = call double @llvm.fmuladd.f64(double 2.000000e+00, double %68, double -1.000000e+00)
%70 = load double, double* %14
%71 = call double @llvm.fmuladd.f64(double -2.000000e+00, double %70, double %69)
%72 = load double, double* %6
%73 = load double, double* %14
%74 = fdiv double %72, %73
%75 = call double @log(double %74)
%76 = load double, double* %14
%77 = fmul double 2.000000e+00, %76
%78 = fneg double %77
%79 = call double @llvm.fmuladd.f64(double %71, double %75, double %78)
%80 = fadd double %79, 0x3FFD67F1B6912125
store double %80, double* %15
%81 = load double, double* %15
%82 = fcmp olt double %81, 2.830000e+00
br i1 %82, label %83, label %84
83: ; preds = %52
store double 1.000000e+03, double* %15
br label %84
84: ; preds = %83, %52
%85 = load double, double* %15
%86 = load double, double* %15
%87 = fsub double %86, 0x3FFD67F1B6912125
%88 = call double @log(double %87)
%89 = fsub double %85, %88
%90 = fsub double %89, 0x3FFD67F1B6912125
%91 = call double @sqrt(double %90)
store double %91, double* %16
%92 = load double, double* %16
%93 = load double, double* %12
%94 = load double, double* %13
%95 = fadd double %93, %94
%96 = fcmp olt double %92, %95
br i1 %96, label %97, label %99
97: ; preds = %84
%98 = load double, double* %16
br label %103
99: ; preds = %84
%100 = load double, double* %12
%101 = load double, double* %13
%102 = fadd double %100, %101
br label %103
103: ; preds = %99, %97
%104 = phi double [ %98, %97 ], [ %102, %99 ]
store double %104, double* %9
br label %105
105: ; preds = %103, %47
%106 = load double, double* %9
%107 = call double @llvm.fabs.f64(double %106)
%108 = load double, double* %7
%109 = load double, double* %10
%110 = fmul double 2.000000e+00, %109
%111 = call double @sqrt(double %110)
%112 = fdiv double %108, %111
%113 = fcmp ogt double %107, %112
br i1 %113, label %114, label %116
114: ; preds = %105
%115 = load i32*, i32** %8
store i32 1, i32* %115
br label %118
116: ; preds = %105
%117 = load i32*, i32** %8
store i32 0, i32* %117
br label %118
118: ; preds = %116, %114
%119 = load double, double* %9
ret double %119
}

LLVM IR (-O2):
define double @xsq_to_stddev(double %0, double %1, double %2, i32* nocapture writeonly %3) {
%5 = fdiv double %1, %2
%6 = fadd double %5, %0
%7 = fadd double %1, -1.000000e+00
%8 = fsub double %6, %7
%9 = fdiv double %6, %7
%10 = tail call double @log(double %9)
%11 = fneg double %7
%12 = tail call double @llvm.fmuladd.f64(double %11, double %10, double %8)
%13 = fcmp olt double %12, 0.000000e+00
%14 = select i1 %13, double 0.000000e+00, double %12
%15 = tail call double @sqrt(double %14)
%16 = fdiv double 2.000000e+00, %7
%17 = tail call double @sqrt(double %16)
%18 = fdiv double %17, 3.000000e+00
%19 = fcmp ugt double %6, %7
br i1 %19, label %22, label %20
20: ; preds = %4
%21 = fsub double %18, %15
br label %49
22: ; preds = %4
%23 = fdiv double %6, %2
%24 = fadd double %23, 1.000000e+00
%25 = fdiv double %1, %24
%26 = fadd double %6, %2
%27 = fneg double %26
%28 = fdiv double %27, %1
%29 = tail call double @exp(double %28)
%30 = fsub double 1.000000e+00, %29
%31 = fmul double %25, %30
%32 = tail call double @llvm.fmuladd.f64(double %2, double 2.000000e+00, double -1.000000e+00)
%33 = tail call double @llvm.fmuladd.f64(double %31, double -2.000000e+00, double %32)
%34 = fdiv double %1, %31
%35 = tail call double @log(double %34)
%36 = fmul double %31, -2.000000e+00
%37 = tail call double @llvm.fmuladd.f64(double %33, double %35, double %36)
%38 = fadd double %37, 0x3FFD67F1B6912125
%39 = fcmp olt double %38, 2.830000e+00
%40 = select i1 %39, double 1.000000e+03, double %38
%41 = fadd double %40, 0xBFFD67F1B6912125
%42 = tail call double @log(double %41)
%43 = fsub double %40, %42
%44 = fadd double %43, 0xBFFD67F1B6912125
%45 = tail call double @sqrt(double %44)
%46 = fadd double %15, %18
%47 = fcmp olt double %45, %46
%48 = select i1 %47, double %45, double %46
br label %49
49: ; preds = %22, %20
%50 = phi double [ %21, %20 ], [ %48, %22 ]
%51 = tail call double @llvm.fabs.f64(double %50)
%52 = fmul double %7, 2.000000e+00
%53 = tail call double @sqrt(double %52)
%54 = fdiv double %2, %53
%55 = fcmp ogt double %51, %54
%56 = zext i1 %55 to i32
store i32 %56, i32* %3
ret double %50
}