#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);
float my_sqrt(float val) {
  float x = val / 10;
  float 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
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
movss  0xee6(%rip),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x8(%rbp)
movsd  0xec5(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movss  -0x4(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jne    116b <my_sqrt+0x5b>
jp     116b <my_sqrt+0x5b>
xorps  %xmm0,%xmm0
movss  %xmm0,-0x8(%rbp)
jmp    1256 <my_sqrt+0x146>
movl   $0x1,-0x24(%rbp)
cmpl   $0x14,-0x24(%rbp)
jge    1251 <my_sqrt+0x141>
cmpl   $0x0,-0x28(%rbp)
jne    1234 <my_sqrt+0x124>
movss  -0x4(%rbp),%xmm1
movss  -0x8(%rbp),%xmm0
movss  -0x8(%rbp),%xmm2
movd   %xmm0,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm0
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movss  -0x8(%rbp),%xmm1
cvtss2sd %xmm1,%xmm2
movsd  0xe49(%rip),%xmm1
mulsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0xc(%rbp)
movss  -0x8(%rbp),%xmm0
addss  -0xc(%rbp),%xmm0
movss  %xmm0,-0x8(%rbp)
movss  -0x4(%rbp),%xmm1
movss  -0x8(%rbp),%xmm0
movss  -0x8(%rbp),%xmm2
movd   %xmm0,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm0
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1030 <my_fabs@plt>
movaps %xmm0,%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     122f <my_sqrt+0x11f>
movl   $0x1,-0x28(%rbp)
jmp    123e <my_sqrt+0x12e>
movss  -0x8(%rbp),%xmm0
movss  %xmm0,-0x8(%rbp)
jmp    1243 <my_sqrt+0x133>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1172 <my_sqrt+0x62>
jmp    1256 <my_sqrt+0x146>
movss  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_sqrt>:
push   %rbp
push   %rbx
push   %rax
xorps  %xmm1,%xmm1
movss  %xmm0,(%rsp)
ucomiss %xmm1,%xmm0
jne    112c <my_sqrt+0x1c>
jp     112c <my_sqrt+0x1c>
movaps %xmm1,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
movss  (%rsp),%xmm0
divss  0xec7(%rip),%xmm0
xor    %ebp,%ebp
mov    $0x13,%ebx
jmp    115e <my_sqrt+0x4e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movss  0x4(%rsp),%xmm1
movaps %xmm1,%xmm0
add    $0xffffffff,%ebx
je     1122 <my_sqrt+0x12>
mov    %ebp,%eax
mov    $0x1,%ebp
test   %eax,%eax
je     1170 <my_sqrt+0x60>
movaps %xmm0,%xmm1
jmp    1156 <my_sqrt+0x46>
xchg   %ax,%ax
movaps %xmm0,%xmm1
mulss  %xmm0,%xmm1
movss  (%rsp),%xmm3
movaps %xmm3,%xmm2
subss  %xmm1,%xmm2
xorps  %xmm1,%xmm1
cvtss2sd %xmm2,%xmm1
xorps  %xmm2,%xmm2
cvtss2sd %xmm0,%xmm2
addsd  %xmm2,%xmm2
divsd  %xmm2,%xmm1
cvtsd2ss %xmm1,%xmm1
addss  %xmm0,%xmm1
movss  %xmm1,0x4(%rsp)
movaps %xmm1,%xmm0
mulss  %xmm1,%xmm0
movaps %xmm3,%xmm1
subss  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtss2sd %xmm1,%xmm0
call   1030 <my_fabs@plt>
movsd  0xe3f(%rip),%xmm1
ucomisd %xmm0,%xmm1
jae    1150 <my_sqrt+0x40>
xor    %ebp,%ebp
jmp    1150 <my_sqrt+0x40>

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

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