#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 DBL_DIG;
int /*<<< orphan*/ EDOM;
int /*<<< orphan*/ errno;
double const fabs(double);
double pow(double, double);
double NewtonSquareRoot(double num) {
  if (num < 0) {
    errno = EDOM;
    return 0.0;
  }
  const double EPSILON = 1.0 * pow(10.0, -1.0 * (DBL_DIG - 1));
  double guess = num / 2.0;
  while (fabs(guess * guess - num) > EPSILON) {
    guess = (guess + num / guess) / 2.0;
  }
  return guess;
}


x86 assembly (clang, -O0):
<NewtonSquareRoot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    115a <NewtonSquareRoot+0x3a>
mov    0x2ea6(%rip),%rax
mov    (%rax),%ecx
mov    0x2e85(%rip),%rax
mov    %ecx,(%rax)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    121e <NewtonSquareRoot+0xfe>
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  0xea6(%rip),%xmm1
mulsd  %xmm0,%xmm1
movsd  0xe92(%rip),%xmm0
call   1040 <pow@plt>
movaps %xmm0,%xmm1
movsd  0xe7a(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xe5c(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
movsd  -0x10(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1030 <fabs@plt>
ucomisd -0x18(%rbp),%xmm0
jbe    1214 <NewtonSquareRoot+0xf4>
movsd  -0x20(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
divsd  -0x20(%rbp),%xmm1
addsd  %xmm1,%xmm0
movsd  0xdfa(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    11ad <NewtonSquareRoot+0x8d>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NewtonSquareRoot>:
sub    $0x18,%rsp
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jbe    1135 <NewtonSquareRoot+0x25>
mov    0x2ec3(%rip),%rax
mov    (%rax),%eax
mov    0x2ea2(%rip),%rcx
mov    %eax,(%rcx)
jmp    11e0 <NewtonSquareRoot+0xd0>
movapd %xmm0,%xmm2
mov    0x2ea0(%rip),%rax
mov    (%rax),%eax
add    $0xffffffff,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
xorpd  0xeac(%rip),%xmm1
movsd  0xec4(%rip),%xmm0
movapd %xmm2,(%rsp)
call   1030 <pow@plt>
movapd (%rsp),%xmm5
movsd  0xeb5(%rip),%xmm2
mulsd  %xmm5,%xmm2
movapd %xmm2,%xmm1
mulsd  %xmm2,%xmm1
subsd  %xmm5,%xmm1
andpd  0xe85(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    11dc <NewtonSquareRoot+0xcc>
movapd 0xe67(%rip),%xmm6
xorpd  %xmm5,%xmm6
movsd  0xe83(%rip),%xmm3
movapd 0xe63(%rip),%xmm4
nopl   (%rax)
movapd %xmm5,%xmm1
divsd  %xmm2,%xmm1
addsd  %xmm2,%xmm1
mulsd  %xmm3,%xmm1
movapd %xmm1,%xmm2
mulsd  %xmm1,%xmm2
addsd  %xmm6,%xmm2
andpd  %xmm4,%xmm2
ucomisd %xmm0,%xmm2
movapd %xmm1,%xmm2
ja     11b0 <NewtonSquareRoot+0xa0>
jmp    11e0 <NewtonSquareRoot+0xd0>
movapd %xmm2,%xmm1
movapd %xmm1,%xmm0
add    $0x18,%rsp
ret

LLVM IR (-O0):
@EDOM = global i32 0
@errno = global i32 0
@DBL_DIG = global i32 0
define double @NewtonSquareRoot(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 = fcmp olt double %6, 0.000000e+00
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = load i32, i32* @EDOM
store i32 %9, i32* @errno
store double 0.000000e+00, double* %2
br label %37
10: ; preds = %1
%11 = load i32, i32* @DBL_DIG
%12 = sub nsw i32 %11, 1
%13 = sitofp i32 %12 to double
%14 = fmul double -1.000000e+00, %13
%15 = call double @pow(double 1.000000e+01, double %14)
%16 = fmul double 1.000000e+00, %15
store double %16, double* %4
%17 = load double, double* %3
%18 = fdiv double %17, 2.000000e+00
store double %18, double* %5
br label %19
19: ; preds = %28, %10
%20 = load double, double* %5
%21 = load double, double* %5
%22 = load double, double* %3
%23 = fneg double %22
%24 = call double @llvm.fmuladd.f64(double %20, double %21, double %23)
%25 = call double @fabs(double %24)
%26 = load double, double* %4
%27 = fcmp ogt double %25, %26
br i1 %27, label %28, label %35
28: ; preds = %19
%29 = load double, double* %5
%30 = load double, double* %3
%31 = load double, double* %5
%32 = fdiv double %30, %31
%33 = fadd double %29, %32
%34 = fdiv double %33, 2.000000e+00
store double %34, double* %5
br label %19
35: ; preds = %19
%36 = load double, double* %5
store double %36, double* %2
br label %37
37: ; preds = %35, %8
%38 = load double, double* %2
ret double %38
}

LLVM IR (-O2):
@EDOM = global i32 0
@errno = global i32 0
@DBL_DIG = global i32 0
define double @NewtonSquareRoot(double %0) {
%2 = fcmp olt double %0, 0.000000e+00
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = load i32, i32* @EDOM
store i32 %4, i32* @errno
br label %24
5: ; preds = %1
%6 = load i32, i32* @DBL_DIG
%7 = add nsw i32 %6, -1
%8 = sitofp i32 %7 to double
%9 = fneg double %8
%10 = tail call double @pow(double 1.000000e+01, double %9)
%11 = fneg double %0
%12 = fmul double %0, 5.000000e-01
%13 = tail call double @llvm.fmuladd.f64(double %12, double %12, double %11)
%14 = tail call double @llvm.fabs.f64(double %13)
%15 = fcmp ogt double %14, %10
br i1 %15, label %16, label %24
16: ; preds = %5, %16
%17 = phi double [ %20, %16 ], [ %12, %5 ]
%18 = fdiv double %0, %17
%19 = fadd double %17, %18
%20 = fmul double %19, 5.000000e-01
%21 = tail call double @llvm.fmuladd.f64(double %20, double %20, double %11)
%22 = tail call double @llvm.fabs.f64(double %21)
%23 = fcmp ogt double %22, %10
br i1 %23, label %16, label %24
24: ; preds = %16, %5, %3
%25 = phi double [ 0.000000e+00, %3 ], [ %12, %5 ], [ %20, %16 ]
ret double %25
}