#include <perms.h>

//          Edinburgh IMP77 Compiler - Version 8.4
//
//     1  %systemintegerfn stoi(%string(255) S)

extern int STOI(_imp_string255 S) {
  _imp_enter(STOI);

  //     2  %integer i,k

  int I = 0xFEFEFEFE;
  int K = 0xFEFEFEFE;

  //     3  %integer sign=0, val=0

  int SIGN = 0xFEFEFEFE;
  SIGN = 0;
  int VAL = 0xFEFEFEFE;
  VAL = 0;

  //     4    i = 0

  I = 0;

  //     5    %while i < length(s) %cycle

L_0001:;
  if ((I) >= (*_imp_LENGTH((_imp_string255 *)&S))) goto L_0002;

    //     6      i = i+1;  k = charno(s,i)

  I = I + 1;
  K = *_imp_CHARNO((_imp_string255 *)&S, I);

  //     7      %continue %if k <= ' '

  if ((K) > (32)) goto L_0003;
  goto L_0004;
L_0003:;

//     8      %if k = '-' %start

  if ((K) != (45)) goto L_0005;

    //     9        sign = 1

  SIGN = 1;

  //    10      %else %if '0' <= k <= '9'

  goto L_0006;
L_0005:;
  {
    int tmp;
    if ((48) > (tmp = K)) goto L_0007;
    // Double-sided test on K (Part II)
    if (tmp > (57)) goto L_0007;
  }

  //    11        val = val<<3+val+val+k-'0'

  VAL = (((int)VAL) << (((int)3) & 31)) + VAL + VAL + K - 48;

  //    12      %else

  goto L_0006;
L_0007:;

//    13        %signal 4, 1, k{, "Non-numeric character"}

  _imp_signal(4, 1, K, "");

  //    14      %finish

L_0006:;

//    15    %repeat

L_0004:;
  goto L_0001;
L_0002:;

//    16    %result = val %if sign = 0

  if ((SIGN) != (0)) goto L_0008;
  _imp_result(VAL);
L_0008:;

//    17    %result = -val

  _imp_result(-VAL);

  //    18  %end

  _imp_begin_backtrace(STOI);
  _imp_diagf("\n\nArguments:\n\n");
  _imp_diagf("S = \"%.*s\"\n", S.cstr.len, S.cstr.s);
  _imp_diagf("\nLocal Variables:\n\n");
  _imp_bt_int(I);
  _imp_bt_int(K);
  _imp_bt_int(SIGN);
  _imp_bt_int(VAL);
  _imp_end_backtrace();
} /* end of STOI */

//    19
//    20  %endoffile