#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
double fabs(double);
double my_sqrt(double x) {
  if (x == 0)
    return 0;
  double result = 1;
  for (;;) {
    double oldResult = result;
    result = (result + (long double)x / result) / 2;
    double step = fabs(result - oldResult);
    if (step < 0.000004 * result) {
      break;
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<my_sqrt>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    112e <my_sqrt+0x2e>
jp     112e <my_sqrt+0x2e>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11ae <my_sqrt+0xae>
movsd  0xeca(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
fldl   -0x18(%rbp)
fldl   -0x10(%rbp)
fdiv   %st(1),%st
faddp  %st,%st(1)
flds   0xecb(%rip)
fdivrp %st,%st(1)
fstpl  -0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
movaps 0xe9b(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm1
movsd  0xe7d(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    119f <my_sqrt+0x9f>
jmp    11a4 <my_sqrt+0xa4>
jmp    113b <my_sqrt+0x3b>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_sqrt>:
movapd %xmm0,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
fldz
jne    1112 <my_sqrt+0x12>
jnp    117e <my_sqrt+0x7e>
fstp   %st(0)
movsd  %xmm1,-0x8(%rsp)
movsd  0xede(%rip),%xmm3
fldl   -0x8(%rsp)
movapd 0xef2(%rip),%xmm1
movsd  0xed2(%rip),%xmm2
cs nopw 0x0(%rax,%rax,1)
movsd  %xmm3,-0x10(%rsp)
fldl   -0x10(%rsp)
fld    %st(1)
fdiv   %st(1),%st
faddp  %st,%st(1)
fmuls  0xeba(%rip)
fstpl  -0x18(%rsp)
movsd  -0x18(%rsp),%xmm0
movapd %xmm0,%xmm4
subsd  %xmm3,%xmm4
andpd  %xmm1,%xmm4
movapd %xmm0,%xmm3
mulsd  %xmm2,%xmm3
ucomisd %xmm4,%xmm3
movapd %xmm0,%xmm3
jbe    1140 <my_sqrt+0x40>
fstp   %st(0)
ret

LLVM IR (-O0):
define double @my_sqrt(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca double
store double %0, double* %3
%7 = load double, double* %3
%8 = fcmp oeq double %7, 0.000000e+00
br i1 %8, label %9, label %10
9: ; preds = %1
store double 0.000000e+00, double* %2
br label %35
10: ; preds = %1
store double 1.000000e+00, double* %4
br label %11
11: ; preds = %32, %10
%12 = load double, double* %4
store double %12, double* %5
%13 = load double, double* %4
%14 = fpext double %13 to x86_fp80
%15 = load double, double* %3
%16 = fpext double %15 to x86_fp80
%17 = load double, double* %4
%18 = fpext double %17 to x86_fp80
%19 = fdiv x86_fp80 %16, %18
%20 = fadd x86_fp80 %14, %19
%21 = fdiv x86_fp80 %20, 0xK40008000000000000000
%22 = fptrunc x86_fp80 %21 to double
store double %22, double* %4
%23 = load double, double* %4
%24 = load double, double* %5
%25 = fsub double %23, %24
%26 = call double @llvm.fabs.f64(double %25)
store double %26, double* %6
%27 = load double, double* %6
%28 = load double, double* %4
%29 = fmul double 4.000000e-06, %28
%30 = fcmp olt double %27, %29
br i1 %30, label %31, label %32
31: ; preds = %11
br label %33
32: ; preds = %11
br label %11
33: ; preds = %31
%34 = load double, double* %4
store double %34, double* %2
br label %35
35: ; preds = %33, %9
%36 = load double, double* %2
ret double %36
}

LLVM IR (-O2):
define double @my_sqrt(double %0) {
%2 = fcmp oeq double %0, 0.000000e+00
br i1 %2, label %16, label %3
3: ; preds = %1
%4 = fpext double %0 to x86_fp80
br label %5
5: ; preds = %3, %5
%6 = phi double [ %11, %5 ], [ 1.000000e+00, %3 ]
%7 = fpext double %6 to x86_fp80
%8 = fdiv x86_fp80 %4, %7
%9 = fadd x86_fp80 %8, %7
%10 = fmul x86_fp80 %9, 0xK3FFE8000000000000000
%11 = fptrunc x86_fp80 %10 to double
%12 = fsub double %11, %6
%13 = tail call double @llvm.fabs.f64(double %12)
%14 = fmul double %11, 4.000000e-06
%15 = fcmp olt double %13, %14
br i1 %15, label %16, label %5
16: ; preds = %5, %1
%17 = phi double [ 0.000000e+00, %1 ], [ %11, %5 ]
ret double %17
}