#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);
int floor(double);
int Newtonsqrt(double x) {
  double x1 = 1;
  double x2 = x1 / 2.0 + x / 2.0 / x1;
  while (fabs(x2 - x1) > 0.1) {
    x1 = x2;
    x2 = x1 / 2.0 + x / 2.0 / x1;
  }
  return floor(x1);
}


x86 assembly (clang, -O0):
<Newtonsqrt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  0xee3(%rip),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xec9(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  -0x8(%rbp),%xmm1
movsd  0xeb8(%rip),%xmm2
divsd  %xmm2,%xmm1
divsd  -0x10(%rbp),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
movaps 0xeb5(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  0xe99(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11c0 <Newtonsqrt+0xb0>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xe68(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  -0x8(%rbp),%xmm1
movsd  0xe57(%rip),%xmm2
divsd  %xmm2,%xmm1
divsd  -0x10(%rbp),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    115a <Newtonsqrt+0x4a>
movsd  -0x10(%rbp),%xmm0
call   1030 <floor@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Newtonsqrt>:
movsd  0xee8(%rip),%xmm2
mulsd  %xmm2,%xmm0
movapd %xmm0,%xmm3
addsd  %xmm2,%xmm3
movsd  0xedc(%rip),%xmm1
addsd  %xmm3,%xmm1
andpd  0xee8(%rip),%xmm1
ucomisd 0xed8(%rip),%xmm1
jbe    1193 <Newtonsqrt+0x83>
movapd 0xed6(%rip),%xmm4
movsd  0xec6(%rip),%xmm5
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movapd %xmm3,%xmm1
movapd %xmm3,%xmm6
mulsd  %xmm2,%xmm6
movapd %xmm0,%xmm3
divsd  %xmm1,%xmm3
addsd  %xmm6,%xmm3
movapd %xmm3,%xmm6
subsd  %xmm1,%xmm6
andpd  %xmm4,%xmm6
ucomisd %xmm5,%xmm6
ja     1160 <Newtonsqrt+0x50>
movapd %xmm1,%xmm0
jmp    1030 <floor@plt>
movsd  0xe75(%rip),%xmm0
jmp    1030 <floor@plt>

LLVM IR (-O0):
define i32 @Newtonsqrt(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca double
store double %0, double* %2
store double 1.000000e+00, double* %3
%5 = load double, double* %3
%6 = fdiv double %5, 2.000000e+00
%7 = load double, double* %2
%8 = fdiv double %7, 2.000000e+00
%9 = load double, double* %3
%10 = fdiv double %8, %9
%11 = fadd double %6, %10
store double %11, double* %4
br label %12
12: ; preds = %18, %1
%13 = load double, double* %4
%14 = load double, double* %3
%15 = fsub double %13, %14
%16 = call double @llvm.fabs.f64(double %15)
%17 = fcmp ogt double %16, 1.000000e-01
br i1 %17, label %18, label %27
18: ; preds = %12
%19 = load double, double* %4
store double %19, double* %3
%20 = load double, double* %3
%21 = fdiv double %20, 2.000000e+00
%22 = load double, double* %2
%23 = fdiv double %22, 2.000000e+00
%24 = load double, double* %3
%25 = fdiv double %23, %24
%26 = fadd double %21, %25
store double %26, double* %4
br label %12
27: ; preds = %12
%28 = load double, double* %3
%29 = call i32 @floor(double %28)
ret i32 %29
}

LLVM IR (-O2):
define i32 @Newtonsqrt(double %0) {
%2 = fmul double %0, 5.000000e-01
%3 = fadd double %2, 5.000000e-01
%4 = fadd double %3, -1.000000e+00
%5 = tail call double @llvm.fabs.f64(double %4)
%6 = fcmp ogt double %5, 1.000000e-01
br i1 %6, label %7, label %15
7: ; preds = %1, %7
%8 = phi double [ %11, %7 ], [ %3, %1 ]
%9 = fmul double %8, 5.000000e-01
%10 = fdiv double %2, %8
%11 = fadd double %9, %10
%12 = fsub double %11, %8
%13 = tail call double @llvm.fabs.f64(double %12)
%14 = fcmp ogt double %13, 1.000000e-01
br i1 %14, label %7, label %15
15: ; preds = %7, %1
%16 = phi double [ 1.000000e+00, %1 ], [ %8, %7 ]
%17 = tail call i32 @floor(double %16)
ret i32 %17
}