#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 *, ...);
int main() {
  int number = 0;
  double guess = 0.0;
  int numSteps = 0;
  double squareAprox = 0.0;
  int i = 0;
  printf("Enter the integer you wish to find the square root of: ");
  scanf(" %d", &number);
  printf("Enter your first guess and number of steps: ");
  scanf(" %lf %d", &guess, &numSteps);
  i = 1;
  while (i <= numSteps) {
    guess = (guess + number / guess) * (1 / 2.0);
    squareAprox = guess;
    ++i;
  }
  printf("The square root of %d is approximately %0.5lf", number, squareAprox);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
lea    0xead(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf08(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
lea    0xec5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xee4(%rip),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
mov    $0x0,%al
call   1040 <scanf@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     11e7 <main+0xc7>
movsd  -0x10(%rbp),%xmm0
cvtsi2sdl -0x8(%rbp),%xmm1
divsd  -0x10(%rbp),%xmm1
addsd  %xmm1,%xmm0
movsd  0xe3a(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    119f <main+0x7f>
mov    -0x8(%rbp),%esi
movsd  -0x20(%rbp),%xmm0
lea    0xe7f(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
movl   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
movl   $0x0,0xc(%rsp)
lea    0xec4(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xf1f(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
lea    0xedb(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xefa(%rip),%rdi
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0xc(%rsp),%eax
test   %eax,%eax
jle    11f5 <main+0xd5>
movsd  0x10(%rsp),%xmm0
mov    0x8(%rsp),%esi
cvtsi2sd %esi,%xmm1
cmp    $0x1,%eax
je     11d5 <main+0xb5>
mov    %eax,%ecx
and    $0xfffffffe,%ecx
movsd  0xe54(%rip),%xmm2
nopl   0x0(%rax)
movapd %xmm1,%xmm3
divsd  %xmm0,%xmm3
addsd  %xmm0,%xmm3
mulsd  %xmm2,%xmm3
movapd %xmm1,%xmm0
divsd  %xmm3,%xmm0
addsd  %xmm3,%xmm0
mulsd  %xmm2,%xmm0
add    $0xfffffffe,%ecx
jne    11b0 <main+0x90>
test   $0x1,%al
je     11ed <main+0xcd>
divsd  %xmm0,%xmm1
addsd  %xmm0,%xmm1
mulsd  0xe17(%rip),%xmm1
movapd %xmm1,%xmm0
movsd  %xmm0,0x10(%rsp)
jmp    11fd <main+0xdd>
xorpd  %xmm0,%xmm0
mov    0x8(%rsp),%esi
lea    0xe71(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
@.str = [56 x i8] c"Enter the integer you wish to find the square root of: \00"
@.str.1 = [4 x i8] c" %d\00"
@.str.2 = [45 x i8] c"Enter your first guess and number of steps: \00"
@.str.3 = [8 x i8] c" %lf %d\00"
@.str.4 = [46 x i8] c"The square root of %d is approximately %0.5lf\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca double
%4 = alloca i32
%5 = alloca double
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store double 0.000000e+00, double* %3
store i32 0, i32* %4
store double 0.000000e+00, double* %5
store i32 0, i32* %6
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), double* %3, i32* %4)
store i32 1, i32* %6
br label %11
11: ; preds = %15, %0
%12 = load i32, i32* %6
%13 = load i32, i32* %4
%14 = icmp sle i32 %12, %13
br i1 %14, label %15, label %26
15: ; preds = %11
%16 = load double, double* %3
%17 = load i32, i32* %2
%18 = sitofp i32 %17 to double
%19 = load double, double* %3
%20 = fdiv double %18, %19
%21 = fadd double %16, %20
%22 = fmul double %21, 5.000000e-01
store double %22, double* %3
%23 = load double, double* %3
store double %23, double* %5
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
store i32 %25, i32* %6
br label %11
26: ; preds = %11
%27 = load i32, i32* %2
%28 = load double, double* %5
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i32 %27, double %28)
ret i32 0
}

LLVM IR (-O2):
@.str = [56 x i8] c"Enter the integer you wish to find the square root of: \00"
@.str.1 = [4 x i8] c" %d\00"
@.str.2 = [45 x i8] c"Enter your first guess and number of steps: \00"
@.str.3 = [8 x i8] c" %lf %d\00"
@.str.4 = [46 x i8] c"The square root of %d is approximately %0.5lf\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca double
%3 = alloca i32
%4 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%5 = bitcast double* %2 to i8*
store double 0.000000e+00, double* %2
%6 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), double* %2, i32* %3)
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = load i32, i32* %1
br label %44
15: ; preds = %0
%16 = load double, double* %2
%17 = load i32, i32* %1
%18 = sitofp i32 %17 to double
%19 = and i32 %11, 1
%20 = icmp eq i32 %11, 1
br i1 %20, label %34, label %21
21: ; preds = %15
%22 = and i32 %11, -2
br label %23
23: ; preds = %23, %21
%24 = phi double [ %16, %21 ], [ %31, %23 ]
%25 = phi i32 [ 0, %21 ], [ %32, %23 ]
%26 = fdiv double %18, %24
%27 = fadd double %24, %26
%28 = fmul double %27, 5.000000e-01
%29 = fdiv double %18, %28
%30 = fadd double %28, %29
%31 = fmul double %30, 5.000000e-01
%32 = add i32 %25, 2
%33 = icmp eq i32 %32, %22
br i1 %33, label %34, label %23
34: ; preds = %23, %15
%35 = phi double [ undef, %15 ], [ %31, %23 ]
%36 = phi double [ %16, %15 ], [ %31, %23 ]
%37 = icmp eq i32 %19, 0
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = fdiv double %18, %36
%40 = fadd double %36, %39
%41 = fmul double %40, 5.000000e-01
br label %42
42: ; preds = %34, %38
%43 = phi double [ %35, %34 ], [ %41, %38 ]
store double %43, double* %2
br label %44
44: ; preds = %13, %42
%45 = phi i32 [ %17, %42 ], [ %14, %13 ]
%46 = phi double [ %43, %42 ], [ 0.000000e+00, %13 ]
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i32 %45, double %46)
ret i32 0
}