double sqrt(double x) {
  double guess = 1.0;
  double tmp;
  while (1) {
    tmp = guess * guess - x;
    tmp = (tmp < 0) ? 0 - tmp : tmp;
    if (tmp < 0.001)
      return guess;
    guess = (guess + x / guess) / 2;
  }
}


x86 assembly (clang, -O0):
<sqrt>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x8(%rbp)
movsd  0xeef(%rip),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  -0x10(%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,-0x18(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    1169 <sqrt+0x69>
xorps  %xmm0,%xmm0
subsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    1173 <sqrt+0x73>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xe83(%rip),%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    1197 <sqrt+0x97>
movsd  -0x10(%rbp),%xmm0
pop    %rbp
ret
movsd  -0x10(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm1
divsd  -0x10(%rbp),%xmm1
addsd  %xmm1,%xmm0
movsd  0xe5e(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    1116 <sqrt+0x16>

x86 assembly (clang, -O2):
<sqrt>:
movsd  0xf08(%rip),%xmm1
movapd %xmm1,%xmm3
subsd  %xmm0,%xmm3
xorpd  %xmm8,%xmm8
xorpd  %xmm4,%xmm4
subsd  %xmm3,%xmm4
movapd %xmm3,%xmm5
cmpltsd %xmm8,%xmm5
andpd  %xmm5,%xmm4
andnpd %xmm3,%xmm5
orpd   %xmm4,%xmm5
movsd  0xedd(%rip),%xmm3
ucomisd %xmm5,%xmm3
ja     11a4 <sqrt+0xa4>
movapd 0xeb7(%rip),%xmm4
xorpd  %xmm0,%xmm4
movsd  0xebb(%rip),%xmm6
movsd  0xec3(%rip),%xmm5
nopl   (%rax)
movapd %xmm0,%xmm1
divsd  %xmm6,%xmm1
addsd  %xmm6,%xmm1
mulsd  %xmm5,%xmm1
movapd %xmm1,%xmm2
mulsd  %xmm1,%xmm2
addsd  %xmm4,%xmm2
xorpd  %xmm7,%xmm7
subsd  %xmm2,%xmm7
movapd %xmm2,%xmm6
cmpltsd %xmm8,%xmm6
andpd  %xmm6,%xmm7
andnpd %xmm2,%xmm6
orpd   %xmm7,%xmm6
ucomisd %xmm6,%xmm3
movapd %xmm1,%xmm6
jbe    1160 <sqrt+0x60>
movapd %xmm1,%xmm0
ret

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

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