#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 fabs(double);
double my_sqrt(double x) {
  double result = 0;
  double resultNew = 1;
  if (x < 0) {
    return 0.0 / 0.0;
  }
  if (x < 1e-12) {
    return 0;
  }
  while (fabs(resultNew - result) > 1e-12 * fabs(resultNew)) {
    result = resultNew;
    resultNew = 0.5 * (x / result + result);
  }
  return resultNew;
}


x86 assembly (clang, -O0):
<my_sqrt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xed3(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    1152 <my_sqrt+0x42>
movsd  0xed0(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f1 <my_sqrt+0xe1>
movsd  0xeae(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    1172 <my_sqrt+0x62>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f1 <my_sqrt+0xe1>
jmp    1177 <my_sqrt+0x67>
movsd  -0x20(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
call   1030 <fabs@plt>
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1030 <fabs@plt>
movsd  -0x28(%rbp),%xmm0
cvtsi2sd %eax,%xmm2
movsd  0xe5e(%rip),%xmm1
mulsd  %xmm2,%xmm1
ucomisd %xmm1,%xmm0
jbe    11e7 <my_sqrt+0xd7>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm1
divsd  -0x18(%rbp),%xmm1
addsd  -0x18(%rbp),%xmm1
movsd  0xe37(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    1177 <my_sqrt+0x67>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_sqrt>:
sub    $0x18,%rsp
xorpd  %xmm2,%xmm2
ucomisd %xmm0,%xmm2
jbe    112b <my_sqrt+0x1b>
movsd  0xeda(%rip),%xmm2
jmp    11ff <my_sqrt+0xef>
movsd  0xed5(%rip),%xmm1
ucomisd %xmm0,%xmm1
movsd  %xmm0,0x8(%rsp)
ja     11ff <my_sqrt+0xef>
movsd  0xec5(%rip),%xmm0
call   1030 <fabs@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,(%rsp)
movsd  0xeac(%rip),%xmm0
call   1030 <fabs@plt>
movsd  0xe9f(%rip),%xmm2
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
mulsd  0xe88(%rip),%xmm0
movsd  (%rsp),%xmm1
ucomisd %xmm0,%xmm1
jbe    11ff <my_sqrt+0xef>
movsd  0xe7d(%rip),%xmm1
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  0x8(%rsp),%xmm0
divsd  %xmm1,%xmm0
addsd  %xmm1,%xmm0
mulsd  0xe62(%rip),%xmm0
movsd  %xmm0,(%rsp)
subsd  %xmm1,%xmm0
call   1030 <fabs@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x10(%rsp)
movsd  (%rsp),%xmm0
call   1030 <fabs@plt>
movsd  (%rsp),%xmm2
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
mulsd  0xe19(%rip),%xmm0
movsd  0x10(%rsp),%xmm1
ucomisd %xmm0,%xmm1
movapd %xmm2,%xmm1
ja     11a0 <my_sqrt+0x90>
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
store double 0.000000e+00, double* %4
store double 1.000000e+00, double* %5
%6 = load double, double* %3
%7 = fcmp olt double %6, 0.000000e+00
br i1 %7, label %8, label %9
8: ; preds = %1
store double 0x7FF8000000000000, double* %2
br label %35
9: ; preds = %1
%10 = load double, double* %3
%11 = fcmp olt double %10, 0x3D719799812DEA11
br i1 %11, label %12, label %13
12: ; preds = %9
store double 0.000000e+00, double* %2
br label %35
13: ; preds = %9
br label %14
14: ; preds = %25, %13
%15 = load double, double* %5
%16 = load double, double* %4
%17 = fsub double %15, %16
%18 = call i32 @fabs(double %17)
%19 = sitofp i32 %18 to double
%20 = load double, double* %5
%21 = call i32 @fabs(double %20)
%22 = sitofp i32 %21 to double
%23 = fmul double 0x3D719799812DEA11, %22
%24 = fcmp ogt double %19, %23
br i1 %24, label %25, label %33
25: ; preds = %14
%26 = load double, double* %5
store double %26, double* %4
%27 = load double, double* %3
%28 = load double, double* %4
%29 = fdiv double %27, %28
%30 = load double, double* %4
%31 = fadd double %29, %30
%32 = fmul double 5.000000e-01, %31
store double %32, double* %5
br label %14
33: ; preds = %14
%34 = load double, double* %5
store double %34, double* %2
br label %35
35: ; preds = %33, %12, %8
%36 = load double, double* %2
ret double %36
}

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