#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 */
int /*<<< orphan*/ EDOM;
double INFINITY;
double NAN;
long double __sqrt_approx(double);
int /*<<< orphan*/ errno;
scalar_t__ isinf(double);
scalar_t__ isnan(double);
double sqrt(double x) {
  long double y, y1;
  if (x < 0.0) {
    errno = EDOM;
    return NAN;
  }
  if (isnan(x))
    return NAN;
  if (isinf(x))
    return INFINITY;
  if (x == 0.0)
    return 0.0;
  y = __sqrt_approx(x);
  y = 0.5 * (y + x / y);
  y = 0.5 * (y + x / y);
  y = 0.5 * (y + x / y);
  y = 0.5 * (y + x / y);
  if (y * y < x - 1.0 || y * y > x + 1.0) {
    y1 = -1.0;
    while (y != y1) {
      y1 = y;
      y = 0.5 * (y + x / y);
    }
  }
  return y;
}


x86 assembly (clang, -O0):
<sqrt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    1172 <sqrt+0x42>
mov    0x2e86(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6d(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e94(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12ef <sqrt+0x1bf>
movsd  -0x10(%rbp),%xmm0
call   1040 <isnan@plt>
cmp    $0x0,%rax
je     119b <sqrt+0x6b>
mov    0x2e6b(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12ef <sqrt+0x1bf>
movsd  -0x10(%rbp),%xmm0
call   1050 <isinf@plt>
cmp    $0x0,%rax
je     11c4 <sqrt+0x94>
mov    0x2e2a(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12ef <sqrt+0x1bf>
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11e9 <sqrt+0xb9>
jp     11e9 <sqrt+0xb9>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12ef <sqrt+0x1bf>
movsd  -0x10(%rbp),%xmm0
call   1030 <__sqrt_approx@plt>
fstpt  -0x20(%rbp)
fldt   -0x20(%rbp)
fldl   -0x10(%rbp)
fdiv   %st(1),%st
faddp  %st,%st(1)
flds   0xdfa(%rip)
fmul   %st,%st(1)
fxch   %st(1)
fstpt  -0x20(%rbp)
fldt   -0x20(%rbp)
fldl   -0x10(%rbp)
fdiv   %st(1),%st
faddp  %st,%st(1)
fmul   %st(1),%st
fstpt  -0x20(%rbp)
fldt   -0x20(%rbp)
fldl   -0x10(%rbp)
fdiv   %st(1),%st
faddp  %st,%st(1)
fmul   %st(1),%st
fstpt  -0x20(%rbp)
fldt   -0x20(%rbp)
fldl   -0x10(%rbp)
fdiv   %st(1),%st
faddp  %st,%st(1)
fmulp  %st,%st(1)
fstpt  -0x20(%rbp)
fldt   -0x20(%rbp)
fmul   %st,%st(0)
movsd  -0x10(%rbp),%xmm0
movsd  0xdbc(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
fldl   -0x38(%rbp)
fucomip %st(1),%st
fstp   %st(0)
ja     1296 <sqrt+0x166>
jmp    1267 <sqrt+0x137>
fldt   -0x20(%rbp)
fmul   %st,%st(0)
movsd  -0x10(%rbp),%xmm0
movsd  0xd97(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
fldl   -0x40(%rbp)
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jbe    12df <sqrt+0x1af>
jmp    1296 <sqrt+0x166>
fld1
fchs
fstpt  -0x30(%rbp)
fldt   -0x20(%rbp)
fldt   -0x30(%rbp)
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jne    12ba <sqrt+0x18a>
jp     12ba <sqrt+0x18a>
jmp    12da <sqrt+0x1aa>
fldt   -0x20(%rbp)
fstpt  -0x30(%rbp)
fldt   -0x20(%rbp)
fldl   -0x10(%rbp)
fdiv   %st(1),%st
faddp  %st,%st(1)
flds   0xd30(%rip)
fmulp  %st,%st(1)
fstpt  -0x20(%rbp)
jmp    129d <sqrt+0x16d>
jmp    12df <sqrt+0x1af>
fldt   -0x20(%rbp)
fstpl  -0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqrt>:
sub    $0x28,%rsp
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jbe    1152 <sqrt+0x22>
mov    0x2e93(%rip),%rax
mov    (%rax),%eax
mov    0x2e7a(%rip),%rcx
mov    %eax,(%rcx)
jmp    1161 <sqrt+0x31>
movsd  %xmm0,(%rsp)
call   1040 <isnan@plt>
test   %rax,%rax
je     1171 <sqrt+0x41>
mov    0x2e90(%rip),%rax
movsd  (%rax),%xmm0
add    $0x28,%rsp
ret
movsd  (%rsp),%xmm0
call   1050 <isinf@plt>
test   %rax,%rax
je     1189 <sqrt+0x59>
mov    0x2e59(%rip),%rax
jmp    1168 <sqrt+0x38>
xorpd  %xmm0,%xmm0
movsd  (%rsp),%xmm1
ucomisd %xmm0,%xmm1
jne    119e <sqrt+0x6e>
jnp    1276 <sqrt+0x146>
movapd %xmm1,%xmm0
call   1030 <__sqrt_approx@plt>
movsd  (%rsp),%xmm1
movsd  %xmm1,0x20(%rsp)
fldl   0x20(%rsp)
fld    %st(0)
fdiv   %st(2),%st
faddp  %st,%st(2)
flds   0xe3e(%rip)
fmul   %st,%st(2)
fld    %st(1)
fdiv   %st(3),%st
faddp  %st,%st(3)
fmul   %st,%st(2)
fld    %st(1)
fdiv   %st(3),%st
faddp  %st,%st(3)
fmul   %st,%st(2)
fld    %st(1)
fdiv   %st(3),%st
faddp  %st,%st(3)
fmulp  %st,%st(2)
fld    %st(1)
fmul   %st(2),%st
movsd  0xe20(%rip),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,0x18(%rsp)
fldl   0x18(%rsp)
fucomip %st(1),%st
jbe    120a <sqrt+0xda>
fstp   %st(0)
fld1
fchs
fxch   %st(2)
fucomi %st(2),%st
jne    1236 <sqrt+0x106>
jp     1236 <sqrt+0x106>
jmp    1268 <sqrt+0x138>
addsd  0xdfe(%rip),%xmm1
movsd  %xmm1,0x10(%rsp)
fldl   0x10(%rsp)
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jbe    125a <sqrt+0x12a>
fld1
fchs
fxch   %st(2)
fucomi %st(2),%st
fstp   %st(2)
fldz
fxch   %st(2)
jne    1236 <sqrt+0x106>
jnp    125e <sqrt+0x12e>
fstp   %st(2)
fldz
fxch   %st(2)
nopl   0x0(%rax)
fstp   %st(2)
fld    %st(0)
fdiv   %st(2),%st
fadd   %st(2),%st
fmuls  0xdb2(%rip)
fucomi %st(2),%st
fstp   %st(2)
fld    %st(1)
jne    1240 <sqrt+0x110>
jp     1240 <sqrt+0x110>
jmp    1268 <sqrt+0x138>
fstp   %st(0)
jmp    1262 <sqrt+0x132>
fstp   %st(1)
fstp   %st(1)
fldz
fldz
fxch   %st(1)
fstp   %st(0)
fstp   %st(0)
fstpl  0x8(%rsp)
movsd  0x8(%rsp),%xmm0
add    $0x28,%rsp
ret

LLVM IR (-O0):
@EDOM = global i32 0
@errno = global i32 0
@NAN = global double 0.000000e+00
@INFINITY = global double 0.000000e+00
define double @sqrt(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca x86_fp80
%5 = alloca x86_fp80
store double %0, double* %3
%6 = load double, double* %3
%7 = fcmp olt double %6, 0.000000e+00
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @EDOM
store i32 %9, i32* @errno
%10 = load double, double* @NAN
store double %10, double* %2
br label %91
11: ; preds = %1
%12 = load double, double* %3
%13 = call i64 @isnan(double %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = load double, double* @NAN
store double %16, double* %2
br label %91
17: ; preds = %11
%18 = load double, double* %3
%19 = call i64 @isinf(double %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = load double, double* @INFINITY
store double %22, double* %2
br label %91
23: ; preds = %17
%24 = load double, double* %3
%25 = fcmp oeq double %24, 0.000000e+00
br i1 %25, label %26, label %27
26: ; preds = %23
store double 0.000000e+00, double* %2
br label %91
27: ; preds = %23
%28 = load double, double* %3
%29 = call x86_fp80 @__sqrt_approx(double %28)
store x86_fp80 %29, x86_fp80* %4
%30 = load x86_fp80, x86_fp80* %4
%31 = load double, double* %3
%32 = fpext double %31 to x86_fp80
%33 = load x86_fp80, x86_fp80* %4
%34 = fdiv x86_fp80 %32, %33
%35 = fadd x86_fp80 %30, %34
%36 = fmul x86_fp80 0xK3FFE8000000000000000, %35
store x86_fp80 %36, x86_fp80* %4
%37 = load x86_fp80, x86_fp80* %4
%38 = load double, double* %3
%39 = fpext double %38 to x86_fp80
%40 = load x86_fp80, x86_fp80* %4
%41 = fdiv x86_fp80 %39, %40
%42 = fadd x86_fp80 %37, %41
%43 = fmul x86_fp80 0xK3FFE8000000000000000, %42
store x86_fp80 %43, x86_fp80* %4
%44 = load x86_fp80, x86_fp80* %4
%45 = load double, double* %3
%46 = fpext double %45 to x86_fp80
%47 = load x86_fp80, x86_fp80* %4
%48 = fdiv x86_fp80 %46, %47
%49 = fadd x86_fp80 %44, %48
%50 = fmul x86_fp80 0xK3FFE8000000000000000, %49
store x86_fp80 %50, x86_fp80* %4
%51 = load x86_fp80, x86_fp80* %4
%52 = load double, double* %3
%53 = fpext double %52 to x86_fp80
%54 = load x86_fp80, x86_fp80* %4
%55 = fdiv x86_fp80 %53, %54
%56 = fadd x86_fp80 %51, %55
%57 = fmul x86_fp80 0xK3FFE8000000000000000, %56
store x86_fp80 %57, x86_fp80* %4
%58 = load x86_fp80, x86_fp80* %4
%59 = load x86_fp80, x86_fp80* %4
%60 = fmul x86_fp80 %58, %59
%61 = load double, double* %3
%62 = fsub double %61, 1.000000e+00
%63 = fpext double %62 to x86_fp80
%64 = fcmp olt x86_fp80 %60, %63
br i1 %64, label %73, label %65
65: ; preds = %27
%66 = load x86_fp80, x86_fp80* %4
%67 = load x86_fp80, x86_fp80* %4
%68 = fmul x86_fp80 %66, %67
%69 = load double, double* %3
%70 = fadd double %69, 1.000000e+00
%71 = fpext double %70 to x86_fp80
%72 = fcmp ogt x86_fp80 %68, %71
br i1 %72, label %73, label %88
73: ; preds = %65, %27
store x86_fp80 0xKBFFF8000000000000000, x86_fp80* %5
br label %74
74: ; preds = %78, %73
%75 = load x86_fp80, x86_fp80* %4
%76 = load x86_fp80, x86_fp80* %5
%77 = fcmp une x86_fp80 %75, %76
br i1 %77, label %78, label %87
78: ; preds = %74
%79 = load x86_fp80, x86_fp80* %4
store x86_fp80 %79, x86_fp80* %5
%80 = load x86_fp80, x86_fp80* %4
%81 = load double, double* %3
%82 = fpext double %81 to x86_fp80
%83 = load x86_fp80, x86_fp80* %4
%84 = fdiv x86_fp80 %82, %83
%85 = fadd x86_fp80 %80, %84
%86 = fmul x86_fp80 0xK3FFE8000000000000000, %85
store x86_fp80 %86, x86_fp80* %4
br label %74
87: ; preds = %74
br label %88
88: ; preds = %87, %65
%89 = load x86_fp80, x86_fp80* %4
%90 = fptrunc x86_fp80 %89 to double
store double %90, double* %2
br label %91
91: ; preds = %88, %26, %21, %15, %8
%92 = load double, double* %2
ret double %92
}

LLVM IR (-O2):
@EDOM = global i32 0
@errno = global i32 0
@NAN = global double 0.000000e+00
@INFINITY = global double 0.000000e+00
define double @sqrt(double %0) {
%2 = fcmp olt double %0, 0.000000e+00
br i1 %2, label %3, label %6
3: ; preds = %1
%4 = load i32, i32* @EDOM
store i32 %4, i32* @errno
%5 = load double, double* @NAN
br label %55
6: ; preds = %1
%7 = tail call i64 @isnan(double %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = load double, double* @NAN
br label %55
11: ; preds = %6
%12 = tail call i64 @isinf(double %0)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = load double, double* @INFINITY
br label %55
16: ; preds = %11
%17 = fcmp oeq double %0, 0.000000e+00
br i1 %17, label %55, label %18
18: ; preds = %16
%19 = tail call x86_fp80 @__sqrt_approx(double %0)
%20 = fpext double %0 to x86_fp80
%21 = fdiv x86_fp80 %20, %19
%22 = fadd x86_fp80 %19, %21
%23 = fmul x86_fp80 %22, 0xK3FFE8000000000000000
%24 = fdiv x86_fp80 %20, %23
%25 = fadd x86_fp80 %23, %24
%26 = fmul x86_fp80 %25, 0xK3FFE8000000000000000
%27 = fdiv x86_fp80 %20, %26
%28 = fadd x86_fp80 %26, %27
%29 = fmul x86_fp80 %28, 0xK3FFE8000000000000000
%30 = fdiv x86_fp80 %20, %29
%31 = fadd x86_fp80 %29, %30
%32 = fmul x86_fp80 %31, 0xK3FFE8000000000000000
%33 = fmul x86_fp80 %32, %32
%34 = fadd double %0, -1.000000e+00
%35 = fpext double %34 to x86_fp80
%36 = fcmp olt x86_fp80 %33, %35
br i1 %36, label %43, label %37
37: ; preds = %18
%38 = fadd double %0, 1.000000e+00
%39 = fpext double %38 to x86_fp80
%40 = fcmp ogt x86_fp80 %33, %39
%41 = fcmp une x86_fp80 %32, 0xKBFFF8000000000000000
%42 = and i1 %40, %41
br i1 %42, label %45, label %52
43: ; preds = %18
%44 = fcmp une x86_fp80 %32, 0xKBFFF8000000000000000
br i1 %44, label %45, label %52
45: ; preds = %43, %37
br label %46
46: ; preds = %45, %46
%47 = phi x86_fp80 [ %50, %46 ], [ %32, %45 ]
%48 = fdiv x86_fp80 %20, %47
%49 = fadd x86_fp80 %47, %48
%50 = fmul x86_fp80 %49, 0xK3FFE8000000000000000
%51 = fcmp une x86_fp80 %50, %47
br i1 %51, label %46, label %52
52: ; preds = %46, %43, %37
%53 = phi x86_fp80 [ %32, %37 ], [ 0xKBFFF8000000000000000, %43 ], [ %50, %46 ]
%54 = fptrunc x86_fp80 %53 to double
br label %55
55: ; preds = %16, %52, %14, %9, %3
%56 = phi double [ %5, %3 ], [ %10, %9 ], [ %15, %14 ], [ %54, %52 ], [ 0.000000e+00, %16 ]
ret double %56
}