#include <perms.h>

                                                                //      1  %begin
#line 1 "regression-bugs-tmp/nsqrt.imp"
int main(int argc, char **argv) {
  _imp_initialise(argc, argv);
                                                                //      2  
                                                                //      3  %constreal M SQRT2 = 1.4
#line 3 "regression-bugs-tmp/nsqrt.imp"
const float MSQRT2 = 1.4;
                                                                //      4  
                                                                //      5  %real %fn sqrt(%long %real X)
#line 5 "regression-bugs-tmp/nsqrt.imp"
float SQRT( double X )
#line 5 "regression-bugs-tmp/nsqrt.imp"
{
                                                                //      6  
                                                                //      7    %integer %fn isqrt(%integer X)
#line 7 "regression-bugs-tmp/nsqrt.imp"
int ISQRT( int X )
#line 7 "regression-bugs-tmp/nsqrt.imp"
{
                                                                //      8      %integer k = 1, result = 1
#line 8 "regression-bugs-tmp/nsqrt.imp"
int K;
#line 8 "regression-bugs-tmp/nsqrt.imp"
K = 1;
#line 8 "regression-bugs-tmp/nsqrt.imp"
int RESULT;
#line 8 "regression-bugs-tmp/nsqrt.imp"
RESULT = 1;
                                                                //      9      k = k+1 %and result = k*k %while result <= X
#line 9 "regression-bugs-tmp/nsqrt.imp"
L_0002:;
#line 9 "regression-bugs-tmp/nsqrt.imp"
if (((RESULT) > (X))) goto L_0003;
#line 9 "regression-bugs-tmp/nsqrt.imp"
K = (((int)(K)) + (1));
#line 9 "regression-bugs-tmp/nsqrt.imp"
RESULT = (((int)(K)) * (K));
#line 9 "regression-bugs-tmp/nsqrt.imp"
goto L_0002;
#line 9 "regression-bugs-tmp/nsqrt.imp"
L_0003:;
                                                                //     10      %result = k-1
#line 10 "regression-bugs-tmp/nsqrt.imp"
/* Remove %on %event handler here if present for this block */
return (((int)(K)) - (1));
                                                                //     11    %end
#line 11 "regression-bugs-tmp/nsqrt.imp"
} // End of block ISQRT at level 3
                                                                //     12    
                                                                //     13    %integer I
#line 13 "regression-bugs-tmp/nsqrt.imp"
int I;
                                                                //     14    %long %real A prev, A
#line 14 "regression-bugs-tmp/nsqrt.imp"
double APREV;
#line 14 "regression-bugs-tmp/nsqrt.imp"
double A;
                                                                //     15    %long %real epsilon = X / 100000000.0
#line 15 "regression-bugs-tmp/nsqrt.imp"
double EPSILON;
#line 15 "regression-bugs-tmp/nsqrt.imp"
EPSILON = (double)((double)((double)(X)) / (double)((double)(100000000.0)));
                                                                //     16    %result = M SQRT2 %if X = 2.0
#line 16 "regression-bugs-tmp/nsqrt.imp"
if (((X) != (2.0))) goto L_0005;
#line 16 "regression-bugs-tmp/nsqrt.imp"
/* Remove %on %event handler here if present for this block */
return MSQRT2;
#line 16 "regression-bugs-tmp/nsqrt.imp"
L_0005:;
                                                                //     17    I = isqrt(INT PT(X))
#line 17 "regression-bugs-tmp/nsqrt.imp"
I = ISQRT(_imp_INTPT(X));
                                                                //     18    %result = I %if X = I*I { exact root }
#line 18 "regression-bugs-tmp/nsqrt.imp"
if (((X) != ((((int)(I)) * (I))))) goto L_0006;
#line 18 "regression-bugs-tmp/nsqrt.imp"
/* Remove %on %event handler here if present for this block */
return I;
#line 18 "regression-bugs-tmp/nsqrt.imp"
L_0006:;
                                                                //     19    ! Newton's method: A{n} = (A{n-1} + X/A{n-1})/2
                                                                //     20    A prev = I ; A = A PREV
#line 20 "regression-bugs-tmp/nsqrt.imp"
APREV = I;
#line 20 "regression-bugs-tmp/nsqrt.imp"
A = APREV;
                                                                //     21    %cycle
#line 21 "regression-bugs-tmp/nsqrt.imp"
L_0007:;
                                                                //     22      A prev = A
#line 22 "regression-bugs-tmp/nsqrt.imp"
APREV = A;
                                                                //     23      A = (A + X/A)/2.0
#line 23 "regression-bugs-tmp/nsqrt.imp"
A = (double)((double)((((A) + (((double)((double)((double)(X)) / (double)((double)(A)))))))) / (double)((double)(2.0)));
                                                                //     24    %repeat %until A prev - epsilon <= A <= A prev + epsilon
#line 24 "regression-bugs-tmp/nsqrt.imp"
if (((((((double)(APREV)) - (EPSILON))) > (A))) || (((A) <= ((((double)(APREV)) + (EPSILON)))))) goto L_0008;
#line 24 "regression-bugs-tmp/nsqrt.imp"
L_0009:;
#line 24 "regression-bugs-tmp/nsqrt.imp"
goto L_0007;
#line 24 "regression-bugs-tmp/nsqrt.imp"
L_0008:;
                                                                //     25    %result = A
#line 25 "regression-bugs-tmp/nsqrt.imp"
/* Remove %on %event handler here if present for this block */
return A;
                                                                //     26  %end
#line 26 "regression-bugs-tmp/nsqrt.imp"
} // End of block SQRT at level 2
                                                                //     27  
                                                                //     28  %endofprogram
#line 28 "regression-bugs-tmp/nsqrt.imp"
/* Remove %on %event handler here if present for this block */
return 0;
} // End of block _imp_main at level 1
#line 28 "regression-bugs-tmp/nsqrt.imp"
// End of file
