#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 /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, double *);
int main() {
  double n;
  printf("input n:");
  scanf("%lf", &n);
  double last_num = 1;
  double new_num = 1;
  do {
    last_num = new_num;
    new_num = (last_num + n / last_num) / 2.0;
  } while (last_num != new_num);
  printf("output sqrt(n)=%lf\n", new_num);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeda(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xed5(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <scanf@plt>
movsd  0xeab(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xe9e(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
divsd  -0x18(%rbp),%xmm1
addsd  %xmm1,%xmm0
movsd  0xe7c(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
ucomisd -0x20(%rbp),%xmm0
jne    1167 <main+0x47>
jp     1167 <main+0x47>
movsd  -0x20(%rbp),%xmm0
lea    0xe66(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xee8(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xee3(%rip),%rdi
mov    %rsp,%rsi
call   1040 <scanf@plt>
movsd  0xeba(%rip),%xmm0
movsd  (%rsp),%xmm1
movsd  0xeb5(%rip),%xmm2
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movapd %xmm0,%xmm3
movapd %xmm1,%xmm0
divsd  %xmm3,%xmm0
addsd  %xmm3,%xmm0
mulsd  %xmm2,%xmm0
ucomisd %xmm0,%xmm3
jne    1160 <main+0x40>
jp     1160 <main+0x40>
lea    0xe9a(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [9 x i8] c"input n:\00"
@.str.1 = [4 x i8] c"%lf\00"
@.str.2 = [20 x i8] c"output sqrt(n)=%lf\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 ([9 x i8], [9 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)
store double 1.000000e+00, double* %3
store double 1.000000e+00, double* %4
br label %7
7: ; preds = %15, %0
%8 = load double, double* %4
store double %8, double* %3
%9 = load double, double* %3
%10 = load double, double* %2
%11 = load double, double* %3
%12 = fdiv double %10, %11
%13 = fadd double %9, %12
%14 = fdiv double %13, 2.000000e+00
store double %14, double* %4
br label %15
15: ; preds = %7
%16 = load double, double* %3
%17 = load double, double* %4
%18 = fcmp une double %16, %17
br i1 %18, label %7, label %19
19: ; preds = %15
%20 = load double, double* %4
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), double %20)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"input n:\00"
@.str.1 = [4 x i8] c"%lf\00"
@.str.2 = [20 x i8] c"output sqrt(n)=%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 ([9 x i8], [9 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)
%5 = load double, double* %1
br label %6
6: ; preds = %6, %0
%7 = phi double [ 1.000000e+00, %0 ], [ %10, %6 ]
%8 = fdiv double %5, %7
%9 = fadd double %7, %8
%10 = fmul double %9, 5.000000e-01
%11 = fcmp une double %7, %10
br i1 %11, label %6, label %12
12: ; preds = %6
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), double %10)
ret i32 0
}