#include <perms.h>

//          Edinburgh IMP77 Compiler - Version 8.4
//
//     1  %systemstring(127)%fn rtos(%real r,%integer n,m)

extern _imp_string127 RTOS(float R, int N, int M) {
  _imp_enter(RTOS);

  //     2    %constreal pmax = 2147483647.0

  const float PMAX = 2147483647.0;

  //     3    %real y,z

  float Y = __builtin_nanf("");
  float Z = __builtin_nanf("");

  //     4    %integer i=0,l,count=0,sign

  int I = 0xFEFEFEFE;
  I = 0;
  int L = 0xFEFEFEFE;
  int COUNT = 0xFEFEFEFE;
  COUNT = 0;
  int SIGN = 0xFEFEFEFE;

  //     5    %string(127) result = ""

  _imp_string127 RESULT;
  _imp_strcpy(&RESULT, _imp_strp(_imp_str_literal("")), /*max:*/ 127);

  //     6    !
  //     7    sign = ' '

  SIGN = 32;

  //     8    sign = '-' %if r < 0

  if ((R) >= (0)) goto L_0001;
  SIGN = 45;
L_0001:;

//     9    y = |r|+0.5/10.0\{^}m;  !modulus, rounded

  Y = _imp_MOD(R) + (double)(0.5 / (_imp_REXP(10.0, M)));

  //    10    %if y > pmax %start

  if ((Y) <= (PMAX)) goto L_0002;

    //    11      count = count+1 %and y = y/10.0 %until y < 10.0

L_0003:;
  COUNT = COUNT + 1;
  Y = (double)(Y / 10.0);
  if ((Y) < (10.0)) goto L_0004;
  goto L_0003;
L_0004:;

//    12    %finish

L_0002:;

//    13    z = 1.0

  Z = 1.0;

  //    14    %cycle

L_0005:;

//    15      i = i+1;  z = z*10.0

  I = I + 1;
  Z = Z * 10.0;

  //    16    %repeat %until z > y

  if ((Z) > (Y)) goto L_0006;
  goto L_0005;
L_0006:;

//    17    result = result." " %for l = 1,1,n-i;  !l not used before here

  // %for L = 1, 1, N - I %cycle
  {
    int _final = N - I;
    if (invalid_for_loop(1, 1, _final)) _imp_signal(5, 1, 0, "Illegal cycle L = 1, 1, N - I");
    L = 0;
  L_0007:
    if (L == _final) goto L_0008;
    L += 1;
    _imp_strassign(&RESULT, 127, "%S%s", _imp_strp(RESULT), " ");
    goto L_0007;
  }

L_0008:;

//    18    result = result.tostring(sign) %unless sign = ' ' %and n <= 0

  if ((SIGN) != (32)) goto L_0009;
  if ((N) <= (0)) goto L_000A;
L_0009:;
  _imp_strassign(&RESULT, 127, "%S%S", _imp_strp(RESULT), _imp_strp(_imp_TOSTRING(SIGN)));
L_000A:;

//    19    %cycle

L_000B:;

//    20      z = z/10.0

  Z = (double)(Z / 10.0);

  //    21      l = int pt(y/z)

  L = _imp_INTPT((double)(Y / Z));

  //    22      y = y-l*z

  Y = Y - L * Z;

  //    23      result = result.tostring(l+'0')

  _imp_strassign(&RESULT, 127, "%S%S", _imp_strp(RESULT), _imp_strp(_imp_TOSTRING(L + 48)));

  //    24      i = i-1

  I = I - 1;

  //    25      %exit %if i+m <= 0

  if ((I + M) > (0)) goto L_000C;
  goto L_000D;
L_000C:;

//    26      result = result."." %if i = 0

  if ((I) != (0)) goto L_000E;
  _imp_strassign(&RESULT, 127, "%S%s", _imp_strp(RESULT), ".");
L_000E:;

//    27    %repeat

  goto L_000B;
L_000D:;

//    28    result = result."@".itos(count,0) %if count # 0

  if ((COUNT) == (0)) goto L_000F;
  _imp_strassign(&RESULT, 127, "%S%s%S", _imp_strp(RESULT), "@", _imp_strp(_imp_ITOS(COUNT, 0)));
L_000F:;

//    29    %result = result

  _imp_result(*(_imp_string127 *)_imp_strp(RESULT));

  //    30  %end;  !rtos

  _imp_begin_backtrace(RTOS);
  _imp_diagf("\n\nArguments:\n\n");
  if (__builtin_isnan(R))
    _imp_diagf("R = <UNASSIGNED REAL>\n");
  else
    _imp_diagf("R = %LE\n", (long double)R);
  _imp_diagf("N = %d\n", N);
  _imp_diagf("M = %d\n", M);
  _imp_diagf("\nLocal Variables:\n\n");
  _imp_bt_real(PMAX);
  _imp_bt_real(Y);
  _imp_bt_real(Z);
  _imp_bt_int(I);
  _imp_bt_int(L);
  _imp_bt_int(COUNT);
  _imp_bt_int(SIGN);
  _imp_diagf("RESULT = \"%.*s\"\n", RESULT.cstr.len, RESULT.cstr.s);
  _imp_end_backtrace();
} /* end of RTOS */

//    31
//    32  %endoffile