#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 msqrt(double n, double eps) {
  int max = 1000000000;
  double x = n;
  while (fabs(x * x - n) > eps && max > 0) {
    max--;
    x = 0.5 * (x + (n / x));
  }
  return x;
}


x86 assembly (clang, -O0):
<msqrt>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movl   $0x3b9aca00,-0x14(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
movsd  -0x8(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movaps 0xeac(%rip),%xmm1
pand   %xmm1,%xmm0
xor    %eax,%eax
ucomisd -0x10(%rbp),%xmm0
mov    %al,-0x21(%rbp)
jbe    1172 <msqrt+0x72>
cmpl   $0x0,-0x14(%rbp)
setg   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1182 <msqrt+0x82>
jmp    11b4 <msqrt+0xb4>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
movsd  -0x20(%rbp),%xmm1
movsd  -0x8(%rbp),%xmm0
divsd  -0x20(%rbp),%xmm0
addsd  %xmm0,%xmm1
movsd  0xe6a(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    111f <msqrt+0x1f>
movsd  -0x20(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<msqrt>:
movapd %xmm0,%xmm2
mulsd  %xmm0,%xmm2
subsd  %xmm0,%xmm2
andpd  0xefc(%rip),%xmm2
ucomisd %xmm1,%xmm2
jbe    1174 <msqrt+0x74>
movapd 0xede(%rip),%xmm3
xorpd  %xmm0,%xmm3
mov    $0x3b9aca00,%eax
movsd  0xeed(%rip),%xmm4
movapd 0xed5(%rip),%xmm5
movapd %xmm0,%xmm2
nop
movapd %xmm0,%xmm6
divsd  %xmm2,%xmm6
addsd  %xmm6,%xmm2
mulsd  %xmm4,%xmm2
movapd %xmm2,%xmm6
mulsd  %xmm2,%xmm6
addsd  %xmm3,%xmm6
andpd  %xmm5,%xmm6
ucomisd %xmm1,%xmm6
jbe    1170 <msqrt+0x70>
lea    -0x1(%rax),%ecx
cmp    $0x1,%eax
mov    %ecx,%eax
ja     1140 <msqrt+0x40>
movapd %xmm2,%xmm0
ret

LLVM IR (-O0):
define double @msqrt(double %0, double %1) {
%3 = alloca double
%4 = alloca double
%5 = alloca i32
%6 = alloca double
store double %0, double* %3
store double %1, double* %4
store i32 1000000000, i32* %5
%7 = load double, double* %3
store double %7, double* %6
br label %8
8: ; preds = %22, %2
%9 = load double, double* %6
%10 = load double, double* %6
%11 = load double, double* %3
%12 = fneg double %11
%13 = call double @llvm.fmuladd.f64(double %9, double %10, double %12)
%14 = call double @llvm.fabs.f64(double %13)
%15 = load double, double* %4
%16 = fcmp ogt double %14, %15
br i1 %16, label %17, label %20
17: ; preds = %8
%18 = load i32, i32* %5
%19 = icmp sgt i32 %18, 0
br label %20
20: ; preds = %17, %8
%21 = phi i1 [ false, %8 ], [ %19, %17 ]
br i1 %21, label %22, label %31
22: ; preds = %20
%23 = load i32, i32* %5
%24 = add nsw i32 %23, -1
store i32 %24, i32* %5
%25 = load double, double* %6
%26 = load double, double* %3
%27 = load double, double* %6
%28 = fdiv double %26, %27
%29 = fadd double %25, %28
%30 = fmul double 5.000000e-01, %29
store double %30, double* %6
br label %8
31: ; preds = %20
%32 = load double, double* %6
ret double %32
}

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