#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 my_fabs(double);
double my_sqrt(double val) {
  double x = val / 10;
  double dx;
  double diff;
  double min_tol = 0.00001;
  int i, flag;
  flag = 0;
  if (val == 0)
    x = 0;
  else {
    for (i = 1; i < 20; i++) {
      if (!flag) {
        dx = (val - (x * x)) / (2.0 * x);
        x = x + dx;
        diff = val - (x * x);
        if (my_fabs(diff) <= min_tol)
          flag = 1;
      } else
        x = x;
    }
  }
  return (x);
}


x86 assembly (clang, -O0):
<my_sqrt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  0xede(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  0xec5(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    116c <my_sqrt+0x5c>
jp     116c <my_sqrt+0x5c>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    1257 <my_sqrt+0x147>
movl   $0x1,-0x2c(%rbp)
cmpl   $0x14,-0x2c(%rbp)
jge    1252 <my_sqrt+0x142>
cmpl   $0x0,-0x30(%rbp)
jne    1235 <my_sqrt+0x125>
movsd  -0x8(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  0xe53(%rip),%xmm1
mulsd  -0x10(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x8(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1030 <my_fabs@plt>
movaps %xmm0,%xmm1
movsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     1230 <my_sqrt+0x120>
movl   $0x1,-0x30(%rbp)
jmp    123f <my_sqrt+0x12f>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    1244 <my_sqrt+0x134>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1173 <my_sqrt+0x63>
jmp    1257 <my_sqrt+0x147>
movsd  -0x10(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_sqrt>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
xorpd  %xmm1,%xmm1
movsd  %xmm0,0x8(%rsp)
ucomisd %xmm1,%xmm0
jne    1133 <my_sqrt+0x23>
jp     1133 <my_sqrt+0x23>
movapd %xmm1,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
movsd  0x8(%rsp),%xmm0
divsd  0xebf(%rip),%xmm0
xor    %ebp,%ebp
mov    $0x13,%ebx
jmp    115f <my_sqrt+0x4f>
nopw   0x0(%rax,%rax,1)
movsd  0x10(%rsp),%xmm1
movapd %xmm1,%xmm0
add    $0xffffffff,%ebx
je     1128 <my_sqrt+0x18>
mov    %ebp,%eax
mov    $0x1,%ebp
test   %eax,%eax
je     1170 <my_sqrt+0x60>
movapd %xmm0,%xmm1
jmp    1156 <my_sqrt+0x46>
movapd %xmm0,%xmm1
mulsd  %xmm0,%xmm1
movsd  0x8(%rsp),%xmm2
movapd %xmm2,%xmm3
subsd  %xmm1,%xmm3
movapd %xmm0,%xmm1
addsd  %xmm0,%xmm1
divsd  %xmm1,%xmm3
addsd  %xmm0,%xmm3
movsd  %xmm3,0x10(%rsp)
movapd %xmm3,%xmm1
mulsd  %xmm3,%xmm1
movapd %xmm2,%xmm0
subsd  %xmm1,%xmm0
call   1030 <my_fabs@plt>
movsd  0xe4f(%rip),%xmm1
ucomisd %xmm0,%xmm1
jae    1150 <my_sqrt+0x40>
xor    %ebp,%ebp
jmp    1150 <my_sqrt+0x40>

LLVM IR (-O0):
define double @my_sqrt(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca i32
store double %0, double* %2
%9 = load double, double* %2
%10 = fdiv double %9, 1.000000e+01
store double %10, double* %3
store double 1.000000e-05, double* %6
store i32 0, i32* %8
%11 = load double, double* %2
%12 = fcmp oeq double %11, 0.000000e+00
br i1 %12, label %13, label %14
13: ; preds = %1
store double 0.000000e+00, double* %3
br label %51
14: ; preds = %1
store i32 1, i32* %7
br label %15
15: ; preds = %47, %14
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 20
br i1 %17, label %18, label %50
18: ; preds = %15
%19 = load i32, i32* %8
%20 = icmp ne i32 %19, 0
br i1 %20, label %44, label %21
21: ; preds = %18
%22 = load double, double* %2
%23 = load double, double* %3
%24 = load double, double* %3
%25 = fneg double %23
%26 = call double @llvm.fmuladd.f64(double %25, double %24, double %22)
%27 = load double, double* %3
%28 = fmul double 2.000000e+00, %27
%29 = fdiv double %26, %28
store double %29, double* %4
%30 = load double, double* %3
%31 = load double, double* %4
%32 = fadd double %30, %31
store double %32, double* %3
%33 = load double, double* %2
%34 = load double, double* %3
%35 = load double, double* %3
%36 = fneg double %34
%37 = call double @llvm.fmuladd.f64(double %36, double %35, double %33)
store double %37, double* %5
%38 = load double, double* %5
%39 = call double @my_fabs(double %38)
%40 = load double, double* %6
%41 = fcmp ole double %39, %40
br i1 %41, label %42, label %43
42: ; preds = %21
store i32 1, i32* %8
br label %43
43: ; preds = %42, %21
br label %46
44: ; preds = %18
%45 = load double, double* %3
store double %45, double* %3
br label %46
46: ; preds = %44, %43
br label %47
47: ; preds = %46
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
store i32 %49, i32* %7
br label %15
50: ; preds = %15
br label %51
51: ; preds = %50, %13
%52 = load double, double* %3
ret double %52
}

LLVM IR (-O2):
define double @my_sqrt(double %0) {
%2 = fcmp oeq double %0, 0.000000e+00
br i1 %2, label %26, label %3
3: ; preds = %1
%4 = fdiv double %0, 1.000000e+01
br label %5
5: ; preds = %3, %21
%6 = phi i32 [ 0, %3 ], [ %23, %21 ]
%7 = phi i32 [ 1, %3 ], [ %24, %21 ]
%8 = phi double [ %4, %3 ], [ %22, %21 ]
%9 = icmp eq i32 %6, 0
br i1 %9, label %10, label %21
10: ; preds = %5
%11 = fneg double %8
%12 = tail call double @llvm.fmuladd.f64(double %11, double %8, double %0)
%13 = fmul double %8, 2.000000e+00
%14 = fdiv double %12, %13
%15 = fadd double %8, %14
%16 = fneg double %15
%17 = tail call double @llvm.fmuladd.f64(double %16, double %15, double %0)
%18 = tail call double @my_fabs(double %17)
%19 = fcmp ugt double %18, 1.000000e-05
br i1 %19, label %21, label %20
20: ; preds = %10
br label %21
21: ; preds = %5, %20, %10
%22 = phi double [ %15, %20 ], [ %15, %10 ], [ %8, %5 ]
%23 = phi i32 [ 1, %20 ], [ 0, %10 ], [ 1, %5 ]
%24 = add nuw nsw i32 %7, 1
%25 = icmp eq i32 %24, 20
br i1 %25, label %26, label %5
26: ; preds = %21, %1
%27 = phi double [ 0.000000e+00, %1 ], [ %22, %21 ]
ret double %27
}