#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 my_abs(double);
double my_sqrt(double x) {
  if (x == 0) {
    return 0;
  }
  if (x < 0) {
    return -1;
  }
  double result = 1;
  double cache = 0;
  while (my_abs(cache - result) > 1e-18 * result) {
    cache = result;
    result = ((x / result) + result) / 2;
  }
  return result;
}


x86 assembly (clang, -O0):
<my_sqrt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1142 <my_sqrt+0x32>
jp     1142 <my_sqrt+0x32>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11da <my_sqrt+0xca>
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    1162 <my_sqrt+0x52>
movsd  0xec0(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11da <my_sqrt+0xca>
movsd  0xe96(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
call   1030 <my_abs@plt>
cvtsi2sd %eax,%xmm0
movsd  0xe76(%rip),%xmm1
mulsd  -0x18(%rbp),%xmm1
ucomisd %xmm1,%xmm0
jbe    11d0 <my_sqrt+0xc0>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
divsd  -0x18(%rbp),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  0xe4e(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    1177 <my_sqrt+0x67>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_sqrt>:
sub    $0x18,%rsp
xorpd  %xmm2,%xmm2
ucomisd %xmm2,%xmm0
jne    1124 <my_sqrt+0x14>
jnp    11b2 <my_sqrt+0xa2>
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jbe    1138 <my_sqrt+0x28>
movsd  0xeca(%rip),%xmm2
jmp    11b2 <my_sqrt+0xa2>
movsd  %xmm0,0x8(%rsp)
movsd  0xeba(%rip),%xmm0
call   1030 <my_abs@plt>
test   %eax,%eax
jle    11aa <my_sqrt+0x9a>
movsd  0xeb1(%rip),%xmm0
movsd  0x8(%rsp),%xmm1
nopl   (%rax)
divsd  %xmm0,%xmm1
addsd  %xmm0,%xmm1
mulsd  0xea0(%rip),%xmm1
movsd  %xmm1,0x10(%rsp)
subsd  %xmm1,%xmm0
call   1030 <my_abs@plt>
movsd  0x10(%rsp),%xmm2
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movapd %xmm2,%xmm1
mulsd  0xe80(%rip),%xmm1
ucomisd %xmm1,%xmm0
movsd  0x8(%rsp),%xmm1
movapd %xmm2,%xmm0
ja     1160 <my_sqrt+0x50>
jmp    11b2 <my_sqrt+0xa2>
movsd  0xe56(%rip),%xmm2
movapd %xmm2,%xmm0
add    $0x18,%rsp
ret

LLVM IR (-O0):
define double @my_sqrt(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 oeq double %6, 0.000000e+00
br i1 %7, label %8, label %9
8: ; preds = %1
store double 0.000000e+00, double* %2
br label %33
9: ; preds = %1
%10 = load double, double* %3
%11 = fcmp olt double %10, 0.000000e+00
br i1 %11, label %12, label %13
12: ; preds = %9
store double -1.000000e+00, double* %2
br label %33
13: ; preds = %9
store double 1.000000e+00, double* %4
store double 0.000000e+00, double* %5
br label %14
14: ; preds = %23, %13
%15 = load double, double* %5
%16 = load double, double* %4
%17 = fsub double %15, %16
%18 = call i32 @my_abs(double %17)
%19 = sitofp i32 %18 to double
%20 = load double, double* %4
%21 = fmul double 1.000000e-18, %20
%22 = fcmp ogt double %19, %21
br i1 %22, label %23, label %31
23: ; preds = %14
%24 = load double, double* %4
store double %24, double* %5
%25 = load double, double* %3
%26 = load double, double* %4
%27 = fdiv double %25, %26
%28 = load double, double* %4
%29 = fadd double %27, %28
%30 = fdiv double %29, 2.000000e+00
store double %30, double* %4
br label %14
31: ; preds = %14
%32 = load double, double* %4
store double %32, double* %2
br label %33
33: ; preds = %31, %12, %8
%34 = load double, double* %2
ret double %34
}

LLVM IR (-O2):
define double @my_sqrt(double %0) {
%2 = fcmp oeq double %0, 0.000000e+00
br i1 %2, label %18, label %3
3: ; preds = %1
%4 = fcmp olt double %0, 0.000000e+00
br i1 %4, label %18, label %5
5: ; preds = %3
%6 = tail call i32 @my_abs(double -1.000000e+00)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %5, %8
%9 = phi double [ %12, %8 ], [ 1.000000e+00, %5 ]
%10 = fdiv double %0, %9
%11 = fadd double %9, %10
%12 = fmul double %11, 5.000000e-01
%13 = fsub double %9, %12
%14 = tail call i32 @my_abs(double %13)
%15 = sitofp i32 %14 to double
%16 = fmul double %12, 1.000000e-18
%17 = fcmp olt double %16, %15
br i1 %17, label %8, label %18
18: ; preds = %8, %5, %3, %1
%19 = phi double [ 0.000000e+00, %1 ], [ -1.000000e+00, %3 ], [ 1.000000e+00, %5 ], [ %12, %8 ]
ret double %19
}