#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(double);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, double *);
int main(void) {
  double x, last_x, a;
  printf("input a real number:");
  scanf("%lf", &a);
  x = 1;
  do {
    last_x = x;
    x = 1.0 / 2.0 * (x + a / x);
  } while (fabs(x - last_x) > 10e-5);
  printf("sqrt(%lg) = %lg\n", a, x);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xed2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xed9(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <scanf@plt>
movsd  0xe9b(%rip),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
divsd  -0x10(%rbp),%xmm0
addsd  %xmm0,%xmm1
movsd  0xe79(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
call   1040 <fabs@plt>
cvtsi2sd %eax,%xmm0
movsd  0xe5d(%rip),%xmm1
ucomisd %xmm1,%xmm0
ja     116a <main+0x3a>
movsd  -0x20(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
lea    0xe63(%rip),%rdi
mov    $0x2,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0xed5(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xedc(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1050 <scanf@plt>
movsd  0xea5(%rip),%xmm1
nopl   0x0(%rax,%rax,1)
movsd  0x8(%rsp),%xmm0
divsd  %xmm1,%xmm0
addsd  %xmm1,%xmm0
mulsd  0xe92(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
subsd  %xmm1,%xmm0
call   1040 <fabs@plt>
movsd  0x10(%rsp),%xmm1
test   %eax,%eax
jg     1160 <main+0x30>
movsd  0x8(%rsp),%xmm0
lea    0xe8d(%rip),%rdi
mov    $0x2,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"input a real number:\00"
@.str.1 = [4 x i8] c"%lf\00"
@.str.2 = [17 x i8] c"sqrt(%lg) = %lg\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca double
%3 = alloca double
%4 = alloca double
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 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* %4)
store double 1.000000e+00, double* %2
br label %7
7: ; preds = %15, %0
%8 = load double, double* %2
store double %8, double* %3
%9 = load double, double* %2
%10 = load double, double* %4
%11 = load double, double* %2
%12 = fdiv double %10, %11
%13 = fadd double %9, %12
%14 = fmul double 5.000000e-01, %13
store double %14, double* %2
br label %15
15: ; preds = %7
%16 = load double, double* %2
%17 = load double, double* %3
%18 = fsub double %16, %17
%19 = call i32 @fabs(double %18)
%20 = sitofp i32 %19 to double
%21 = fcmp ogt double %20, 1.000000e-04
br i1 %21, label %7, label %22
22: ; preds = %15
%23 = load double, double* %4
%24 = load double, double* %2
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), double %23, double %24)
ret i32 0
}

LLVM IR (-O2):
@.str = [21 x i8] c"input a real number:\00"
@.str.1 = [4 x i8] c"%lf\00"
@.str.2 = [17 x i8] c"sqrt(%lg) = %lg\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 ([21 x i8], [21 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 %10, %6
%12 = call i32 @fabs(double %11)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %5, label %14
14: ; preds = %5
%15 = load double, double* %1
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), double %15, double %10)
ret i32 0
}