double sqrt(double b) {
  double x = 1;
  int step = 0;
  while ((x * x - b < -0.000000000000001 || x * x - b > 0.000000000000001) &&
         step < 50) {
    x = (b / x + x) / 2.0;
    step++;
  }
  return x;
}


x86 assembly (clang, -O0):
<sqrt>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x8(%rbp)
movsd  0xeef(%rip),%xmm0
movsd  %xmm0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movsd  -0x10(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm2
movsd  -0x8(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movsd  0xeb5(%rip),%xmm0
ucomisd %xmm1,%xmm0
ja     11a2 <sqrt+0xa2>
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
xor    %eax,%eax
movsd  0xe7b(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x15(%rbp)
jbe    11ac <sqrt+0xac>
cmpl   $0x32,-0x14(%rbp)
setl   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    11bc <sqrt+0xbc>
jmp    11ea <sqrt+0xea>
movsd  -0x8(%rbp),%xmm0
divsd  -0x10(%rbp),%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  0xe45(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    111d <sqrt+0x1d>
movsd  -0x10(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqrt>:
movapd 0xef8(%rip),%xmm2
xorpd  %xmm0,%xmm2
xor    %eax,%eax
movsd  0xefa(%rip),%xmm1
movsd  0xefa(%rip),%xmm3
movsd  0xf02(%rip),%xmm4
movsd  0xef2(%rip),%xmm5
jmp    114c <sqrt+0x4c>
cmp    $0x31,%eax
ja     1169 <sqrt+0x69>
movapd %xmm0,%xmm6
divsd  %xmm1,%xmm6
addsd  %xmm1,%xmm6
mulsd  %xmm4,%xmm6
add    $0x1,%eax
movapd %xmm6,%xmm1
movapd %xmm1,%xmm6
mulsd  %xmm1,%xmm6
addsd  %xmm2,%xmm6
ucomisd %xmm6,%xmm3
ja     1130 <sqrt+0x30>
ucomisd %xmm5,%xmm6
jbe    1169 <sqrt+0x69>
cmp    $0x32,%eax
jb     1135 <sqrt+0x35>
movapd %xmm1,%xmm0
ret

LLVM IR (-O0):
define double @sqrt(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca i32
store double %0, double* %2
store double 1.000000e+00, double* %3
store i32 0, i32* %4
br label %5
5: ; preds = %24, %1
%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)
%11 = fcmp olt double %10, -1.000000e-15
br i1 %11, label %19, label %12
12: ; preds = %5
%13 = load double, double* %3
%14 = load double, double* %3
%15 = load double, double* %2
%16 = fneg double %15
%17 = call double @llvm.fmuladd.f64(double %13, double %14, double %16)
%18 = fcmp ogt double %17, 1.000000e-15
br i1 %18, label %19, label %22
19: ; preds = %12, %5
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 50
br label %22
22: ; preds = %19, %12
%23 = phi i1 [ false, %12 ], [ %21, %19 ]
br i1 %23, label %24, label %33
24: ; preds = %22
%25 = load double, double* %2
%26 = load double, double* %3
%27 = fdiv double %25, %26
%28 = load double, double* %3
%29 = fadd double %27, %28
%30 = fdiv double %29, 2.000000e+00
store double %30, double* %3
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
store i32 %32, i32* %4
br label %5
33: ; preds = %22
%34 = load double, double* %3
ret double %34
}

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