#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);
double 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 (fabs(diff) <= min_tol)
          flag = 1;
      } else
        x = x;
    }
  }
  return x;
}


x86 assembly (clang, -O0):
<sqrt>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  0xef2(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  0xed9(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1158 <sqrt+0x58>
jp     1158 <sqrt+0x58>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    1246 <sqrt+0x146>
movl   $0x1,-0x2c(%rbp)
cmpl   $0x14,-0x2c(%rbp)
jge    1241 <sqrt+0x141>
cmpl   $0x0,-0x30(%rbp)
jne    1224 <sqrt+0x124>
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  0xe67(%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),%xmm1
movaps 0xe1b(%rip),%xmm0
pand   %xmm0,%xmm1
movsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     121f <sqrt+0x11f>
movl   $0x1,-0x30(%rbp)
jmp    122e <sqrt+0x12e>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    1233 <sqrt+0x133>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    115f <sqrt+0x5f>
jmp    1246 <sqrt+0x146>
movsd  -0x10(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqrt>:
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1111 <sqrt+0x11>
jp     1111 <sqrt+0x11>
movapd %xmm1,%xmm0
ret
movapd %xmm0,%xmm4
divsd  0xee3(%rip),%xmm4
xor    %ecx,%ecx
mov    $0x13,%eax
movapd 0xee4(%rip),%xmm2
movsd  0xed4(%rip),%xmm3
jmp    114d <sqrt+0x4d>
cs nopw 0x0(%rax,%rax,1)
movapd %xmm4,%xmm1
movapd %xmm1,%xmm4
add    $0xffffffff,%eax
je     110c <sqrt+0xc>
mov    %ecx,%edx
mov    $0x1,%ecx
test   %edx,%edx
jne    1140 <sqrt+0x40>
movapd %xmm4,%xmm5
mulsd  %xmm4,%xmm5
movapd %xmm0,%xmm1
subsd  %xmm5,%xmm1
movapd %xmm4,%xmm5
addsd  %xmm4,%xmm5
divsd  %xmm5,%xmm1
addsd  %xmm4,%xmm1
movapd %xmm1,%xmm4
mulsd  %xmm1,%xmm4
movapd %xmm0,%xmm5
subsd  %xmm4,%xmm5
andpd  %xmm2,%xmm5
ucomisd %xmm5,%xmm3
jae    1144 <sqrt+0x44>
xor    %ecx,%ecx
jmp    1144 <sqrt+0x44>

LLVM IR (-O0):
define double @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 @llvm.fabs.f64(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 @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 @llvm.fabs.f64(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
}