#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(double);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, double *);
int main(void) {
  double x, y = 1, avg;
  printf("Enter a positive number: ");
  scanf("%lf", &x);
  for (;;) {
    avg = (y + x / y) / 2;
    if (fabs(y - avg) < (.00001f * avg)) {
      printf("Square root: %lf\n", y);
      break;
    }
    y = avg;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movsd  0xeb9(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
lea    0xec5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xed1(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <scanf@plt>
movsd  -0x18(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
divsd  -0x18(%rbp),%xmm1
addsd  %xmm1,%xmm0
movsd  0xe8b(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
call   1040 <fabs@plt>
cvtss2sd %xmm0,%xmm1
movsd  0xe5f(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11d0 <main+0xa0>
movsd  -0x18(%rbp),%xmm0
lea    0xe72(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
jmp    11df <main+0xaf>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    116a <main+0x3a>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0xedd(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xee9(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1050 <scanf@plt>
movsd  0xea5(%rip),%xmm2
nopl   0x0(%rax,%rax,1)
movapd %xmm2,%xmm0
movsd  0x10(%rsp),%xmm1
divsd  %xmm2,%xmm1
addsd  %xmm2,%xmm1
mulsd  0xe8e(%rip),%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  %xmm2,(%rsp)
subsd  %xmm1,%xmm0
call   1040 <fabs@plt>
movsd  0x8(%rsp),%xmm2
cvtss2sd %xmm0,%xmm0
movapd %xmm2,%xmm1
mulsd  0xe6c(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    1160 <main+0x30>
lea    0xe85(%rip),%rdi
movsd  (%rsp),%xmm0
mov    $0x1,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Enter a positive number: \00"
@.str.1 = [4 x i8] c"%lf\00"
@.str.2 = [18 x i8] c"Square root: %lf\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca double
%3 = alloca double
%4 = alloca double
store i32 0, i32* %1
store double 1.000000e+00, double* %3
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double* %2)
br label %7
7: ; preds = %25, %0
%8 = load double, double* %3
%9 = load double, double* %2
%10 = load double, double* %3
%11 = fdiv double %9, %10
%12 = fadd double %8, %11
%13 = fdiv double %12, 2.000000e+00
store double %13, double* %4
%14 = load double, double* %3
%15 = load double, double* %4
%16 = fsub double %14, %15
%17 = call float @fabs(double %16)
%18 = fpext float %17 to double
%19 = load double, double* %4
%20 = fmul double 0x3EE4F8B580000000, %19
%21 = fcmp olt double %18, %20
br i1 %21, label %22, label %25
22: ; preds = %7
%23 = load double, double* %3
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), double %23)
br label %27
25: ; preds = %7
%26 = load double, double* %4
store double %26, double* %3
br label %7
27: ; preds = %22
ret i32 0
}

LLVM IR (-O2):
@.str = [26 x i8] c"Enter a positive number: \00"
@.str.1 = [4 x i8] c"%lf\00"
@.str.2 = [18 x i8] c"Square root: %lf\0A\00"
define i32 @main() {
%1 = alloca double
%2 = bitcast double* %1 to i8*
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double* %1)
br label %5
5: ; preds = %5, %0
%6 = phi double [ 1.000000e+00, %0 ], [ %10, %5 ]
%7 = load double, double* %1
%8 = fdiv double %7, %6
%9 = fadd double %6, %8
%10 = fmul double %9, 5.000000e-01
%11 = fsub double %6, %10
%12 = call float @fabs(double %11)
%13 = fpext float %12 to double
%14 = fmul double %10, 0x3EE4F8B580000000
%15 = fcmp ogt double %14, %13
br i1 %15, label %16, label %5
16: ; preds = %5
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), double %6)
ret i32 0
}