#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 fabs(float);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, float *);
int main() {
  float fVal, fNextGuess, fLastGuess = 1.0f, fDiff;
  printf("\nEnter a value whose square root has to be calculated\n");
  scanf("%f", &fVal);
  do {
    fNextGuess = 0.5 * (fLastGuess + (fVal / fLastGuess));
    fDiff = fabs(fNextGuess - fLastGuess);
    fLastGuess = fNextGuess;
  } while (fDiff > 0.0001);
  printf("\nSquare root of %g = %g\n", fVal, fNextGuess);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movss  0xeb9(%rip),%xmm0
movss  %xmm0,-0x10(%rbp)
lea    0xec5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeee(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <scanf@plt>
movss  -0x10(%rbp),%xmm0
movss  -0x8(%rbp),%xmm1
divss  -0x10(%rbp),%xmm1
addss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm1
movsd  0xe7f(%rip),%xmm0
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0xc(%rbp)
movss  -0xc(%rbp),%xmm0
subss  -0x10(%rbp),%xmm0
call   1040 <fabs@plt>
movss  %xmm0,-0x14(%rbp)
movss  -0xc(%rbp),%xmm0
movss  %xmm0,-0x10(%rbp)
movss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe4b(%rip),%xmm1
ucomisd %xmm1,%xmm0
ja     116a <main+0x3a>
movss  -0x8(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movss  -0xc(%rbp),%xmm1
cvtss2sd %xmm1,%xmm1
lea    0xe6a(%rip),%rdi
mov    $0x2,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xf0c(%rip),%rdi
call   1030 <puts@plt>
lea    0xee4(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1050 <scanf@plt>
movss  0xeaa(%rip),%xmm1
movss  0x4(%rsp),%xmm0
movss  0xea0(%rip),%xmm2
movaps 0xea5(%rip),%xmm3
movsd  0xead(%rip),%xmm4
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movaps %xmm1,%xmm5
movaps %xmm0,%xmm1
divss  %xmm5,%xmm1
addss  %xmm5,%xmm1
mulss  %xmm2,%xmm1
movaps %xmm1,%xmm6
subss  %xmm5,%xmm6
andps  %xmm3,%xmm6
xorps  %xmm5,%xmm5
cvtss2sd %xmm6,%xmm5
ucomisd %xmm4,%xmm5
ja     1180 <main+0x50>
cvtss2sd %xmm0,%xmm0
cvtss2sd %xmm1,%xmm1
lea    0xe73(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [55 x i8] c"\0AEnter a value whose square root has to be calculated\0A\00"
@.str.1 = [3 x i8] c"%f\00"
@.str.2 = [25 x i8] c"\0ASquare root of %g = %g\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca float
%3 = alloca float
%4 = alloca float
%5 = alloca float
store i32 0, i32* %1
store float 1.000000e+00, float* %4
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), float* %2)
br label %8
8: ; preds = %22, %0
%9 = load float, float* %4
%10 = load float, float* %2
%11 = load float, float* %4
%12 = fdiv float %10, %11
%13 = fadd float %9, %12
%14 = fpext float %13 to double
%15 = fmul double 5.000000e-01, %14
%16 = fptrunc double %15 to float
store float %16, float* %3
%17 = load float, float* %3
%18 = load float, float* %4
%19 = fsub float %17, %18
%20 = call float @fabs(float %19)
store float %20, float* %5
%21 = load float, float* %3
store float %21, float* %4
br label %22
22: ; preds = %8
%23 = load float, float* %5
%24 = fpext float %23 to double
%25 = fcmp ogt double %24, 1.000000e-04
br i1 %25, label %8, label %26
26: ; preds = %22
%27 = load float, float* %2
%28 = fpext float %27 to double
%29 = load float, float* %3
%30 = fpext float %29 to double
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), double %28, double %30)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%f\00"
@.str.2 = [25 x i8] c"\0ASquare root of %g = %g\0A\00"
@str = [54 x i8] c"\0AEnter a value whose square root has to be calculated\00"
define i32 @main() {
%1 = alloca float
%2 = bitcast float* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str, i64 0, i64 0))
%4 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), float* %1)
%5 = load float, float* %1
br label %6
6: ; preds = %6, %0
%7 = phi float [ 1.000000e+00, %0 ], [ %10, %6 ]
%8 = fdiv float %5, %7
%9 = fadd float %7, %8
%10 = fmul float %9, 5.000000e-01
%11 = fsub float %10, %7
%12 = call float @llvm.fabs.f32(float %11)
%13 = fpext float %12 to double
%14 = fcmp ogt double %13, 1.000000e-04
br i1 %14, label %6, label %15
15: ; preds = %6
%16 = fpext float %5 to double
%17 = fpext float %10 to double
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), double %16, double %17)
ret i32 0
}