float xdnd_sqrt(float x) {
  float last_ans, ans = 2, a;
  if (x <= 0.0)
    return 0.0;
  do {
    last_ans = ans;
    ans = (ans + x / ans) / 2;
    a = (ans - last_ans) / ans;
    if (a < 0.0)
      a = (-a);
  } while (a > 0.001);
  return ans;
}


x86 assembly (clang, -O0):
<xdnd_sqrt>:
push   %rbp
mov    %rsp,%rbp
movss  %xmm0,-0x8(%rbp)
movss  0xeef(%rip),%xmm0
movss  %xmm0,-0x10(%rbp)
movss  -0x8(%rbp),%xmm0
cvtss2sd %xmm0,%xmm1
xorps  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jb     1139 <xdnd_sqrt+0x39>
xorps  %xmm0,%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    11d7 <xdnd_sqrt+0xd7>
jmp    113e <xdnd_sqrt+0x3e>
movss  -0x10(%rbp),%xmm0
movss  %xmm0,-0xc(%rbp)
movss  -0x10(%rbp),%xmm0
movss  -0x8(%rbp),%xmm1
divss  -0x10(%rbp),%xmm1
addss  %xmm1,%xmm0
movss  0xe9d(%rip),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x10(%rbp)
movss  -0x10(%rbp),%xmm0
subss  -0xc(%rbp),%xmm0
divss  -0x10(%rbp),%xmm0
movss  %xmm0,-0x14(%rbp)
movss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm1
xorps  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    11ad <xdnd_sqrt+0xad>
movss  -0x14(%rbp),%xmm0
movd   %xmm0,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm0
movss  %xmm0,-0x14(%rbp)
jmp    11b2 <xdnd_sqrt+0xb2>
movss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe45(%rip),%xmm1
ucomisd %xmm1,%xmm0
ja     113e <xdnd_sqrt+0x3e>
movss  -0x10(%rbp),%xmm0
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<xdnd_sqrt>:
xorps  %xmm1,%xmm1
ucomiss %xmm0,%xmm1
jae    1172 <xdnd_sqrt+0x72>
movss  0xef0(%rip),%xmm7
movss  0xeeb(%rip),%xmm8
movaps 0xef0(%rip),%xmm3
xorps  %xmm4,%xmm4
movsd  0xef5(%rip),%xmm5
nopl   0x0(%rax,%rax,1)
movaps %xmm0,%xmm1
divss  %xmm7,%xmm1
addss  %xmm7,%xmm1
mulss  %xmm8,%xmm1
movaps %xmm1,%xmm6
subss  %xmm7,%xmm6
divss  %xmm1,%xmm6
movaps %xmm6,%xmm2
xorps  %xmm3,%xmm2
movaps %xmm6,%xmm7
cmpltss %xmm4,%xmm7
andps  %xmm7,%xmm2
andnps %xmm6,%xmm7
orps   %xmm2,%xmm7
xorps  %xmm2,%xmm2
cvtss2sd %xmm7,%xmm2
ucomisd %xmm5,%xmm2
movaps %xmm1,%xmm7
ja     1130 <xdnd_sqrt+0x30>
movaps %xmm1,%xmm0
ret

LLVM IR (-O0):
define float @xdnd_sqrt(float %0) {
%2 = alloca float
%3 = alloca float
%4 = alloca float
%5 = alloca float
%6 = alloca float
store float %0, float* %3
store float 2.000000e+00, float* %5
%7 = load float, float* %3
%8 = fpext float %7 to double
%9 = fcmp ole double %8, 0.000000e+00
br i1 %9, label %10, label %11
10: ; preds = %1
store float 0.000000e+00, float* %2
br label %38
11: ; preds = %1
br label %12
12: ; preds = %32, %11
%13 = load float, float* %5
store float %13, float* %4
%14 = load float, float* %5
%15 = load float, float* %3
%16 = load float, float* %5
%17 = fdiv float %15, %16
%18 = fadd float %14, %17
%19 = fdiv float %18, 2.000000e+00
store float %19, float* %5
%20 = load float, float* %5
%21 = load float, float* %4
%22 = fsub float %20, %21
%23 = load float, float* %5
%24 = fdiv float %22, %23
store float %24, float* %6
%25 = load float, float* %6
%26 = fpext float %25 to double
%27 = fcmp olt double %26, 0.000000e+00
br i1 %27, label %28, label %31
28: ; preds = %12
%29 = load float, float* %6
%30 = fneg float %29
store float %30, float* %6
br label %31
31: ; preds = %28, %12
br label %32
32: ; preds = %31
%33 = load float, float* %6
%34 = fpext float %33 to double
%35 = fcmp ogt double %34, 1.000000e-03
br i1 %35, label %12, label %36
36: ; preds = %32
%37 = load float, float* %5
store float %37, float* %2
br label %38
38: ; preds = %36, %10
%39 = load float, float* %2
ret float %39
}

LLVM IR (-O2):
define float @xdnd_sqrt(float %0) {
%2 = fcmp ugt float %0, 0.000000e+00
br i1 %2, label %3, label %15
3: ; preds = %1, %3
%4 = phi float [ %7, %3 ], [ 2.000000e+00, %1 ]
%5 = fdiv float %0, %4
%6 = fadd float %4, %5
%7 = fmul float %6, 5.000000e-01
%8 = fsub float %7, %4
%9 = fdiv float %8, %7
%10 = fcmp olt float %9, 0.000000e+00
%11 = fneg float %9
%12 = select i1 %10, float %11, float %9
%13 = fpext float %12 to double
%14 = fcmp ogt double %13, 1.000000e-03
br i1 %14, label %3, label %15
15: ; preds = %3, %1
%16 = phi float [ 0.000000e+00, %1 ], [ %7, %3 ]
ret float %16
}