#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 abs(double);
double pow(double, int);
double sqrt_newton2(double num, double precision) {
  double lo = 0.0;
  double hi = num;
  double prev = 0.0;
  double mid = (lo + hi) / 2.0;
  while (abs(mid - prev) > precision) {
    if (pow(mid, 2) > num)
      hi = mid;
    else
      lo = mid;
    prev = mid;
    mid = (lo + hi) / 2.0;
  }
  return mid;
}


x86 assembly (clang, -O0):
<sqrt_newton2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x18(%rbp),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  0xea2(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
call   1030 <abs@plt>
ucomisd -0x10(%rbp),%xmm0
jbe    11de <sqrt_newton2+0xbe>
movsd  -0x30(%rbp),%xmm0
mov    $0x2,%edi
call   1040 <pow@plt>
ucomisd -0x8(%rbp),%xmm0
jbe    11aa <sqrt_newton2+0x8a>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    11b4 <sqrt_newton2+0x94>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x18(%rbp),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  0xe30(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    1167 <sqrt_newton2+0x47>
movsd  -0x30(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqrt_newton2>:
sub    $0x58,%rsp
movsd  %xmm1,0x18(%rsp)
movapd %xmm0,%xmm2
xorpd  %xmm1,%xmm1
movsd  %xmm0,0x10(%rsp)
xorpd  %xmm0,%xmm0
movapd %xmm0,0x30(%rsp)
addsd  %xmm1,%xmm2
mulsd  0xeb2(%rip),%xmm2
movapd %xmm2,0x20(%rsp)
movapd %xmm2,%xmm0
call   1030 <abs@plt>
ucomisd 0x18(%rsp),%xmm0
jbe    1211 <sqrt_newton2+0xf1>
movsd  0x10(%rsp),%xmm2
movapd 0x20(%rsp),%xmm0
jmp    11d6 <sqrt_newton2+0xb6>
nopw   0x0(%rax,%rax,1)
movsd  %xmm3,0x8(%rsp)
cmpltsd %xmm0,%xmm1
movapd 0x30(%rsp),%xmm0
andpd  %xmm1,%xmm0
andnpd %xmm2,%xmm1
orpd   %xmm1,%xmm0
movapd %xmm0,0x30(%rsp)
addsd  %xmm3,%xmm0
mulsd  0xe51(%rip),%xmm0
movapd %xmm0,0x40(%rsp)
subsd  %xmm2,%xmm0
call   1030 <abs@plt>
movapd 0x40(%rsp),%xmm1
ucomisd 0x18(%rsp),%xmm0
movapd %xmm1,%xmm0
movsd  0x8(%rsp),%xmm2
jbe    1217 <sqrt_newton2+0xf7>
movsd  %xmm2,0x8(%rsp)
movapd %xmm0,0x20(%rsp)
mov    $0x2,%edi
call   1040 <pow@plt>
movapd 0x20(%rsp),%xmm2
movsd  0x10(%rsp),%xmm1
ucomisd %xmm1,%xmm0
movapd %xmm2,%xmm3
ja     1180 <sqrt_newton2+0x60>
movsd  0x8(%rsp),%xmm3
jmp    1180 <sqrt_newton2+0x60>
movapd 0x20(%rsp),%xmm1
movapd %xmm1,%xmm0
add    $0x58,%rsp
ret

LLVM IR (-O0):
define double @sqrt_newton2(double %0, double %1) {
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
store double %0, double* %3
store double %1, double* %4
store double 0.000000e+00, double* %5
%9 = load double, double* %3
store double %9, double* %6
store double 0.000000e+00, double* %7
%10 = load double, double* %5
%11 = load double, double* %6
%12 = fadd double %10, %11
%13 = fdiv double %12, 2.000000e+00
store double %13, double* %8
br label %14
14: ; preds = %30, %2
%15 = load double, double* %8
%16 = load double, double* %7
%17 = fsub double %15, %16
%18 = call double @abs(double %17)
%19 = load double, double* %4
%20 = fcmp ogt double %18, %19
br i1 %20, label %21, label %36
21: ; preds = %14
%22 = load double, double* %8
%23 = call double @pow(double %22, i32 2)
%24 = load double, double* %3
%25 = fcmp ogt double %23, %24
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = load double, double* %8
store double %27, double* %6
br label %30
28: ; preds = %21
%29 = load double, double* %8
store double %29, double* %5
br label %30
30: ; preds = %28, %26
%31 = load double, double* %8
store double %31, double* %7
%32 = load double, double* %5
%33 = load double, double* %6
%34 = fadd double %32, %33
%35 = fdiv double %34, 2.000000e+00
store double %35, double* %8
br label %14
36: ; preds = %14
%37 = load double, double* %8
ret double %37
}

LLVM IR (-O2):
define double @sqrt_newton2(double %0, double %1) {
%3 = fadd double %0, 0.000000e+00
%4 = fmul double %3, 5.000000e-01
%5 = tail call double @abs(double %4)
%6 = fcmp ogt double %5, %1
br i1 %6, label %7, label %20
7: ; preds = %2, %7
%8 = phi double [ %16, %7 ], [ %4, %2 ]
%9 = phi double [ %14, %7 ], [ %0, %2 ]
%10 = phi double [ %13, %7 ], [ 0.000000e+00, %2 ]
%11 = tail call double @pow(double %8, i32 2)
%12 = fcmp ogt double %11, %0
%13 = select i1 %12, double %10, double %8
%14 = select i1 %12, double %8, double %9
%15 = fadd double %13, %14
%16 = fmul double %15, 5.000000e-01
%17 = fsub double %16, %8
%18 = tail call double @abs(double %17)
%19 = fcmp ogt double %18, %1
br i1 %19, label %7, label %20
20: ; preds = %7, %2
%21 = phi double [ %4, %2 ], [ %16, %7 ]
ret double %21
}