double umath_double_sqrt(double a) {
  int i;
  double x;
  x = a;
  for (i = 1; (i <= 30) &&
              (((x * x - a) > 0 ? (x * x - a) : (-(x * x - a))) > 0.00001);
       i++)
    x = (x + a / x) / 2;
  return x;
}


x86 assembly (clang, -O0):
<umath_double_sqrt>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x1,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x1e,-0xc(%rbp)
mov    %al,-0x19(%rbp)
jg     11fd <umath_double_sqrt+0xfd>
movsd  -0x18(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
movsd  -0x8(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    119c <umath_double_sqrt+0x9c>
movsd  -0x18(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
movsd  -0x8(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    11e6 <umath_double_sqrt+0xe6>
movsd  -0x18(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
movsd  -0x8(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  0xe0d(%rip),%xmm1
ucomisd %xmm1,%xmm0
seta   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    120d <umath_double_sqrt+0x10d>
jmp    123f <umath_double_sqrt+0x13f>
movsd  -0x18(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm1
divsd  -0x18(%rbp),%xmm1
addsd  %xmm1,%xmm0
movsd  0xde0(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    111a <umath_double_sqrt+0x1a>
movsd  -0x18(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<umath_double_sqrt>:
movapd 0xef8(%rip),%xmm2
xorpd  %xmm0,%xmm2
mov    $0x1e,%eax
movapd 0xee6(%rip),%xmm8
movsd  0xeee(%rip),%xmm4
movsd  0xeee(%rip),%xmm5
movapd %xmm0,%xmm6
xchg   %ax,%ax
movapd %xmm6,%xmm1
mulsd  %xmm6,%xmm1
addsd  %xmm2,%xmm1
movapd %xmm1,%xmm7
xorpd  %xmm8,%xmm7
xorpd  %xmm3,%xmm3
cmpltsd %xmm1,%xmm3
andpd  %xmm3,%xmm1
andnpd %xmm7,%xmm3
orpd   %xmm1,%xmm3
ucomisd %xmm4,%xmm3
jbe    117e <umath_double_sqrt+0x7e>
movapd %xmm0,%xmm1
divsd  %xmm6,%xmm1
addsd  %xmm6,%xmm1
mulsd  %xmm5,%xmm1
movapd %xmm1,%xmm6
add    $0xffffffff,%eax
jne    1130 <umath_double_sqrt+0x30>
movapd %xmm1,%xmm0
ret
movapd %xmm6,%xmm0
ret

LLVM IR (-O0):
define double @umath_double_sqrt(double %0) {
%2 = alloca double
%3 = alloca i32
%4 = alloca double
store double %0, double* %2
%5 = load double, double* %2
store double %5, double* %4
store i32 1, i32* %3
br label %6
6: ; preds = %41, %1
%7 = load i32, i32* %3
%8 = icmp sle i32 %7, 30
br i1 %8, label %9, label %32
9: ; preds = %6
%10 = load double, double* %4
%11 = load double, double* %4
%12 = load double, double* %2
%13 = fneg double %12
%14 = call double @llvm.fmuladd.f64(double %10, double %11, double %13)
%15 = fcmp ogt double %14, 0.000000e+00
br i1 %15, label %16, label %22
16: ; preds = %9
%17 = load double, double* %4
%18 = load double, double* %4
%19 = load double, double* %2
%20 = fneg double %19
%21 = call double @llvm.fmuladd.f64(double %17, double %18, double %20)
br label %29
22: ; preds = %9
%23 = load double, double* %4
%24 = load double, double* %4
%25 = load double, double* %2
%26 = fneg double %25
%27 = call double @llvm.fmuladd.f64(double %23, double %24, double %26)
%28 = fneg double %27
br label %29
29: ; preds = %22, %16
%30 = phi double [ %21, %16 ], [ %28, %22 ]
%31 = fcmp ogt double %30, 1.000000e-05
br label %32
32: ; preds = %29, %6
%33 = phi i1 [ false, %6 ], [ %31, %29 ]
br i1 %33, label %34, label %44
34: ; preds = %32
%35 = load double, double* %4
%36 = load double, double* %2
%37 = load double, double* %4
%38 = fdiv double %36, %37
%39 = fadd double %35, %38
%40 = fdiv double %39, 2.000000e+00
store double %40, double* %4
br label %41
41: ; preds = %34
%42 = load i32, i32* %3
%43 = add nsw i32 %42, 1
store i32 %43, i32* %3
br label %6
44: ; preds = %32
%45 = load double, double* %4
ret double %45
}

LLVM IR (-O2):
define double @umath_double_sqrt(double %0) {
%2 = fneg double %0
br label %3
3: ; preds = %1, %11
%4 = phi double [ %0, %1 ], [ %14, %11 ]
%5 = phi i32 [ 1, %1 ], [ %15, %11 ]
%6 = tail call double @llvm.fmuladd.f64(double %4, double %4, double %2)
%7 = fcmp ogt double %6, 0.000000e+00
%8 = fneg double %6
%9 = select i1 %7, double %6, double %8
%10 = fcmp ogt double %9, 1.000000e-05
br i1 %10, label %11, label %17
11: ; preds = %3
%12 = fdiv double %0, %4
%13 = fadd double %4, %12
%14 = fmul double %13, 5.000000e-01
%15 = add nuw nsw i32 %5, 1
%16 = icmp eq i32 %15, 31
br i1 %16, label %17, label %3
17: ; preds = %11, %3
%18 = phi double [ %14, %11 ], [ %4, %3 ]
ret double %18
}