#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(scalar_t__);
scalar_t__ pow(float, int);
float mySqrt(int x) {
  if (x == 0)
    return 0;
  float a = x;
  float prev = -1;
  while (fabs(pow(a, 2) - (float)x) > 1e-4) {
    a = (a + (float)x / a) / 2.0;
    if (prev == a) {
      break;
    }
    prev = a;
  }
  return a;
}


x86 assembly (clang, -O0):
<mySqrt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1142 <mySqrt+0x22>
xorps  %xmm0,%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    11f5 <mySqrt+0xd5>
cvtsi2ssl -0x8(%rbp),%xmm0
movss  %xmm0,-0xc(%rbp)
movss  0xeac(%rip),%xmm0
movss  %xmm0,-0x10(%rbp)
movss  -0xc(%rbp),%xmm0
mov    $0x2,%edi
call   1040 <pow@plt>
cvtsi2ss %rax,%xmm0
cvtsi2ssl -0x8(%rbp),%xmm1
subss  %xmm1,%xmm0
cvttss2si %xmm0,%rdi
call   1030 <fabs@plt>
cvtsi2sd %eax,%xmm0
movsd  0xe7c(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11eb <mySqrt+0xcb>
movss  -0xc(%rbp),%xmm0
cvtsi2ssl -0x8(%rbp),%xmm1
divss  -0xc(%rbp),%xmm1
addss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe5b(%rip),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0xc(%rbp)
movss  -0x10(%rbp),%xmm0
ucomiss -0xc(%rbp),%xmm0
jne    11dc <mySqrt+0xbc>
jp     11dc <mySqrt+0xbc>
jmp    11eb <mySqrt+0xcb>
movss  -0xc(%rbp),%xmm0
movss  %xmm0,-0x10(%rbp)
jmp    1159 <mySqrt+0x39>
movss  -0xc(%rbp),%xmm0
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mySqrt>:
sub    $0x18,%rsp
test   %edi,%edi
je     11a9 <mySqrt+0x89>
cvtsi2ss %edi,%xmm2
movss  0xec8(%rip),%xmm1
movss  %xmm2,0x10(%rsp)
xchg   %ax,%ax
movss  %xmm1,0x14(%rsp)
movss  %xmm2,0xc(%rsp)
movaps %xmm2,%xmm0
mov    $0x2,%edi
call   1040 <pow@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
subss  0x10(%rsp),%xmm0
cvttss2si %xmm0,%rdi
call   1030 <fabs@plt>
test   %eax,%eax
jle    11b1 <mySqrt+0x91>
movss  0x10(%rsp),%xmm0
movss  0xc(%rsp),%xmm1
divss  %xmm1,%xmm0
addss  %xmm1,%xmm0
mulss  0xe73(%rip),%xmm0
movss  0x14(%rsp),%xmm1
ucomiss %xmm0,%xmm1
movaps %xmm0,%xmm2
movaps %xmm0,%xmm1
jne    1140 <mySqrt+0x20>
jp     1140 <mySqrt+0x20>
add    $0x18,%rsp
ret
xorps  %xmm0,%xmm0
add    $0x18,%rsp
ret
movss  0xc(%rsp),%xmm0
add    $0x18,%rsp
ret

LLVM IR (-O0):
define float @mySqrt(i32 %0) {
%2 = alloca float
%3 = alloca i32
%4 = alloca float
%5 = alloca float
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store float 0.000000e+00, float* %2
br label %41
9: ; preds = %1
%10 = load i32, i32* %3
%11 = sitofp i32 %10 to float
store float %11, float* %4
store float -1.000000e+00, float* %5
br label %12
12: ; preds = %37, %9
%13 = load float, float* %4
%14 = call i64 @pow(float %13, i32 2)
%15 = sitofp i64 %14 to float
%16 = load i32, i32* %3
%17 = sitofp i32 %16 to float
%18 = fsub float %15, %17
%19 = fptosi float %18 to i64
%20 = call i32 @fabs(i64 %19)
%21 = sitofp i32 %20 to double
%22 = fcmp ogt double %21, 1.000000e-04
br i1 %22, label %23, label %39
23: ; preds = %12
%24 = load float, float* %4
%25 = load i32, i32* %3
%26 = sitofp i32 %25 to float
%27 = load float, float* %4
%28 = fdiv float %26, %27
%29 = fadd float %24, %28
%30 = fpext float %29 to double
%31 = fdiv double %30, 2.000000e+00
%32 = fptrunc double %31 to float
store float %32, float* %4
%33 = load float, float* %5
%34 = load float, float* %4
%35 = fcmp oeq float %33, %34
br i1 %35, label %36, label %37
36: ; preds = %23
br label %39
37: ; preds = %23
%38 = load float, float* %4
store float %38, float* %5
br label %12
39: ; preds = %36, %12
%40 = load float, float* %4
store float %40, float* %2
br label %41
41: ; preds = %39, %8
%42 = load float, float* %2
ret float %42
}

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