double sqrt(double n) {
  double s = n / 2;
  if (s < 0.0)
    return 0.0 / 0;
  if (s == 0.0)
    return 0.0;
  for (double t = 0.0; t = (s + n / s) / 2, s != t; s = t)
    ;
  return s;
}


x86 assembly (clang, -O0):
<sqrt>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xeea(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    113f <sqrt+0x3f>
movsd  0xed3(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c9 <sqrt+0xc9>
movsd  -0x18(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1164 <sqrt+0x64>
jp     1164 <sqrt+0x64>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c9 <sqrt+0xc9>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
divsd  -0x18(%rbp),%xmm1
addsd  %xmm1,%xmm0
movsd  0xe79(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
ucomisd -0x20(%rbp),%xmm0
jne    11ab <sqrt+0xab>
jp     11ab <sqrt+0xab>
jmp    11bf <sqrt+0xbf>
jmp    11b0 <sqrt+0xb0>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    116c <sqrt+0x6c>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqrt>:
movsd  0xef8(%rip),%xmm2
mulsd  %xmm0,%xmm2
xorpd  %xmm1,%xmm1
ucomisd %xmm2,%xmm1
jbe    111f <sqrt+0x1f>
movsd  0xeea(%rip),%xmm0
ret
ucomisd %xmm1,%xmm2
jne    1127 <sqrt+0x27>
jnp    114c <sqrt+0x4c>
movsd  0xed1(%rip),%xmm3
nop
movapd %xmm2,%xmm1
movapd %xmm0,%xmm2
divsd  %xmm1,%xmm2
addsd  %xmm1,%xmm2
mulsd  %xmm3,%xmm2
ucomisd %xmm2,%xmm1
jne    1130 <sqrt+0x30>
jp     1130 <sqrt+0x30>
movapd %xmm1,%xmm0
ret

LLVM IR (-O0):
define double @sqrt(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca double
%5 = alloca double
store double %0, double* %3
%6 = load double, double* %3
%7 = fdiv double %6, 2.000000e+00
store double %7, double* %4
%8 = load double, double* %4
%9 = fcmp olt double %8, 0.000000e+00
br i1 %9, label %10, label %11
10: ; preds = %1
store double 0x7FF8000000000000, double* %2
br label %31
11: ; preds = %1
%12 = load double, double* %4
%13 = fcmp oeq double %12, 0.000000e+00
br i1 %13, label %14, label %15
14: ; preds = %11
store double 0.000000e+00, double* %2
br label %31
15: ; preds = %11
store double 0.000000e+00, double* %5
br label %16
16: ; preds = %27, %15
%17 = load double, double* %4
%18 = load double, double* %3
%19 = load double, double* %4
%20 = fdiv double %18, %19
%21 = fadd double %17, %20
%22 = fdiv double %21, 2.000000e+00
store double %22, double* %5
%23 = load double, double* %4
%24 = load double, double* %5
%25 = fcmp une double %23, %24
br i1 %25, label %26, label %29
26: ; preds = %16
br label %27
27: ; preds = %26
%28 = load double, double* %5
store double %28, double* %4
br label %16
29: ; preds = %16
%30 = load double, double* %4
store double %30, double* %2
br label %31
31: ; preds = %29, %14, %10
%32 = load double, double* %2
ret double %32
}

LLVM IR (-O2):
define double @sqrt(double %0) {
%2 = fmul double %0, 5.000000e-01
%3 = fcmp olt double %2, 0.000000e+00
br i1 %3, label %12, label %4
4: ; preds = %1
%5 = fcmp oeq double %2, 0.000000e+00
br i1 %5, label %12, label %6
6: ; preds = %4, %6
%7 = phi double [ %10, %6 ], [ %2, %4 ]
%8 = fdiv double %0, %7
%9 = fadd double %7, %8
%10 = fmul double %9, 5.000000e-01
%11 = fcmp une double %7, %10
br i1 %11, label %6, label %12
12: ; preds = %6, %4, %1
%13 = phi double [ 0x7FF8000000000000, %1 ], [ 0.000000e+00, %4 ], [ %7, %6 ]
ret double %13
}