#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 */
float const abs_v(float);
int /*<<< orphan*/ printf(char *, float);
float sqrt_v(float x) {
  const float epsilon = 0.001f;
  float guess = 1.0f;
  while (abs_v(guess * guess - x) >= epsilon) {
    printf("Guess is: %.5f\n", guess);
    guess = (x / guess + guess) / 2.0f;
  }
  return guess;
}


x86 assembly (clang, -O0):
<sqrt_v>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movss  %xmm0,-0x4(%rbp)
movss  0xecf(%rip),%xmm0
movss  %xmm0,-0x8(%rbp)
movss  0xebe(%rip),%xmm0
movss  %xmm0,-0xc(%rbp)
movss  -0xc(%rbp),%xmm0
movss  -0xc(%rbp),%xmm2
movss  -0x4(%rbp),%xmm1
movd   %xmm1,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
call   1040 <abs_v@plt>
movss  0xe8c(%rip),%xmm1
ucomiss %xmm1,%xmm0
jb     11b7 <sqrt_v+0x97>
movss  -0xc(%rbp),%xmm0
lea    0xe7f(%rip),%rdi
call   1030 <printf@plt>
movss  -0x4(%rbp),%xmm0
divss  -0xc(%rbp),%xmm0
addss  -0xc(%rbp),%xmm0
movss  0xe5f(%rip),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0xc(%rbp)
jmp    1147 <sqrt_v+0x27>
movss  -0xc(%rbp),%xmm0
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqrt_v>:
push   %rbx
sub    $0x30,%rsp
movaps %xmm0,%xmm1
movss  0xee0(%rip),%xmm0
movaps %xmm0,(%rsp)
movaps %xmm1,0x10(%rsp)
subss  %xmm1,%xmm0
call   1040 <abs_v@plt>
ucomiss 0xecb(%rip),%xmm0
jb     11b7 <sqrt_v+0x97>
movaps 0xeae(%rip),%xmm0
xorps  0x10(%rsp),%xmm0
movaps %xmm0,0x20(%rsp)
movss  0xeac(%rip),%xmm0
lea    0xeb1(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
movss  %xmm0,(%rsp)
call   1030 <printf@plt>
movaps 0x10(%rsp),%xmm0
movss  (%rsp),%xmm1
divss  %xmm1,%xmm0
addss  %xmm1,%xmm0
mulss  0xe81(%rip),%xmm0
movaps %xmm0,(%rsp)
mulss  %xmm0,%xmm0
addss  0x20(%rsp),%xmm0
call   1040 <abs_v@plt>
ucomiss 0xe63(%rip),%xmm0
movaps (%rsp),%xmm0
jae    1170 <sqrt_v+0x50>
movaps (%rsp),%xmm0
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Guess is: %.5f\0A\00"
define float @sqrt_v(float %0) {
%2 = alloca float
%3 = alloca float
%4 = alloca float
store float %0, float* %2
store float 0x3F50624DE0000000, float* %3
store float 1.000000e+00, float* %4
br label %5
5: ; preds = %13, %1
%6 = load float, float* %4
%7 = load float, float* %4
%8 = load float, float* %2
%9 = fneg float %8
%10 = call float @llvm.fmuladd.f32(float %6, float %7, float %9)
%11 = call float @abs_v(float %10)
%12 = fcmp oge float %11, 0x3F50624DE0000000
br i1 %12, label %13, label %22
13: ; preds = %5
%14 = load float, float* %4
%15 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), float %14)
%16 = load float, float* %2
%17 = load float, float* %4
%18 = fdiv float %16, %17
%19 = load float, float* %4
%20 = fadd float %18, %19
%21 = fdiv float %20, 2.000000e+00
store float %21, float* %4
br label %5
22: ; preds = %5
%23 = load float, float* %4
ret float %23
}

LLVM IR (-O2):
@.str = [16 x i8] c"Guess is: %.5f\0A\00"
define float @sqrt_v(float %0) {
%2 = fneg float %0
%3 = fsub float 1.000000e+00, %0
%4 = tail call float @abs_v(float %3)
%5 = fcmp ult float %4, 0x3F50624DE0000000
br i1 %5, label %15, label %6
6: ; preds = %1, %6
%7 = phi float [ %11, %6 ], [ 1.000000e+00, %1 ]
%8 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), float %7)
%9 = fdiv float %0, %7
%10 = fadd float %7, %9
%11 = fmul float %10, 5.000000e-01
%12 = tail call float @llvm.fmuladd.f32(float %11, float %11, float %2)
%13 = tail call float @abs_v(float %12)
%14 = fcmp ult float %13, 0x3F50624DE0000000
br i1 %14, label %15, label %6
15: ; preds = %6, %1
%16 = phi float [ 1.000000e+00, %1 ], [ %11, %6 ]
ret float %16
}