#include <perms.h>
static const _imp_string Snl = _imp_str_literal("\n");
int _imp_mainep(int _imp_argc, char **_imp_argv) {
  static const int True = 1;
  static const int False = 0;
  static const int Astream = 1;
  static const int Bstream = 2;
  static _imp_string Afile;
  static _imp_string Bfile;
  static _imp_string Pr;
  static _imp_string Left;
  static _imp_string Right;
  static _imp_string File[2 /*65:66*/];
  static int Base[2 /*65:66*/];
  static int Acount;
  static int Bcount;
  static int Anum;
  static int Bnum;
  static const int Maxlines = 512;
  static const int Maxlinewidth = 128;
  unsigned char Aa[65536 /*1:65536*/];
  unsigned char Bb[65536 /*1:65536*/];
  int Inputended(void) {
    int Ch;
    if (_imp_on_event(9)) return (True);
    Ch = Nextsymbol();
    return (False);
  }
  void Rdline(_imp_string * S) {
    int Ch;
    *S = _imp_str_literal("");
    for (;;) {
      Readsymbol(Ch);
      if (Ch == Nl || *Length(*S) == Maxlinewidth) break;
      *S = _imp_join(*S, Tostring(Ch));
    }
  }
  unsigned char *A(int I, int J) { return (&Aa[((Base['A'] + I) & (Maxlines - 1)) * Maxlinewidth + J]); }
  unsigned char *B(int I, int J) { return (&Bb[((Base['B'] + I) & (Maxlines - 1)) * Maxlinewidth + J]); }
  void Refill(void X(int I, int J), int N, int *Count) {
    int I;
    int J;
    int Ch;
    for (I = Maxlines + 1 - N; I <= Maxlines; I++) {
      do {
        if (Inputended() == True) return;
        Readsymbol(Ch);
      } while (Ch == Nl);
      for (J = 1; J <= Maxlinewidth - 1; J++) {
        if (Ch == Nl || Ch < 32) break;
        X = Ch;
        if (Inputended() == True) return;
        Readsymbol(Ch);
      }
      X = Nl;
      *Count = *Count + 1;
    }
  }
  void Print(void X(int I, int J), int N, int Flag) {
    int I;
    int J;
    for (I = 1; I <= N; I++) {
      Printstring(_imp_join(_imp_str_literal("\""), _imp_join(File[Flag], _imp_str_literal("\","))));
      Write(Base[Flag] + I + 1, 5);
      Printstring(_imp_str_literal(": "));
      J = 0;
      for (;;) {
        J++;
        if (X == Nl) break;
        Printsymbol(X);
        if (J == 57) {
          Newline();
          Spaces(20);
          Printsymbol(':');
        }
      }
      Newline();
    }
  }
  int Linesmatch(int Aline, int Bline) {
    int J;
    if (Aline > Acount || Bline > Bcount) {
      if (Aline > Acount && Bline > Bcount) return (True);
      return (False);
    }
    for (J = 1; J <= Maxlinewidth; J++) {
      if (*A(Aline, J) != *B(Bline, J)) return (False);
      if (*A(Aline, J) == Nl) return (True);
    }
    return (True);
  }
  void Findmatch(int *Al, int *Bl) {
    int I;
    int J;
    for (I = 1; I <= Maxlines; I++) {
      if (Linesmatch(I, I) == True) {
        *Al = I;
        *Bl = I;
        return;
      }
      if (I > 1)
        for (J = 1; J <= I - 1; J++) {
          if (Linesmatch(I, J) == True) {
            *Al = I;
            *Bl = J;
            return;
          }
          if (Linesmatch(J, I) == True) {
            *Al = J;
            *Bl = I;
            return;
          }
        }
    }
    *Al = Maxlines + 1;
    *Bl = Maxlines + 1;
  }
  int Fnamelen;
  Anum = Maxlines;
  Bnum = Maxlines;
  Base['A'] = 0;
  Base['B'] = 0;
  Acount = 0;
  Bcount = 0;
  if (_imp_resolve(Cliparam(), NULL, _imp_str_literal("-h"), NULL)) {
    Printstring(_imp_join(_imp_str_literal("Syntax: compare oldfile newfile"), Snl));
    exit(0);
  }
  Pr = _imp_str_literal("New file: ");
  if (!_imp_resolve(Cliparam(), Afile, _imp_str_literal(" "), Bfile)) {
    Afile = Cliparam();
    if (_imp_strcmp(Afile, _imp_str_literal("")) != 0) {
      Bfile = _imp_str_literal("");
      Pr = _imp_str_literal("Against:  ");
    } else {
      Afile = _imp_str_literal("");
      Bfile = _imp_str_literal("");
    }
  }
  while (_imp_resolve(Afile, Left, _imp_str_literal(" "), Right)) Afile = _imp_join(Left, Right);
  while (_imp_resolve(Bfile, Left, _imp_str_literal(" "), Right)) Bfile = _imp_join(Left, Right);
  File['A'] = Afile;
  File['B'] = Bfile;
  Prompt(_imp_str_literal("Old file: "));
  while (_imp_strcmp(Afile, _imp_str_literal("")) == 0) Rdline(Afile);
  Prompt(Pr);
  while (_imp_strcmp(Bfile, _imp_str_literal("")) == 0) Rdline(Bfile);
  Fnamelen = *Length(Afile);
  if (Fnamelen < *Length(Bfile)) Fnamelen = *Length(Bfile);
  if (Fnamelen > 11) Fnamelen = 11;
  Openinput(Astream, Afile);
  Openinput(Bstream, Bfile);
  Afile = _imp_join(_imp_str_literal("           "), Afile);
  Bfile = _imp_join(_imp_str_literal("           "), Bfile);
  Afile = Substring(Afile, *Length(Afile) - 10, *Length(Afile));
  Bfile = Substring(Bfile, *Length(Bfile) - 10, *Length(Bfile));
  for (;;) {
    Selectinput(Astream);
    Refill(*A(), Anum, Acount);
    Selectinput(Bstream);
    Refill(*B(), Bnum, Bcount);
    if (Acount <= 0 && Bcount <= 0) break;
    Findmatch(Anum, Bnum);
    if (Anum != 1 || Bnum != 1) {
      Print(*A(), Anum - 1, 'A');
      Print(*B(), Bnum - 1, 'B');
      Printstring(_imp_join(_imp_str_literal("---------------------"), Snl));
      if (Anum > Maxlines) {
        Printstring(_imp_str_literal("*** Mismatch too large"));
        Newline();
        exit(0);
      }
    }
    Base['A'] = Base['A'] + Anum;
    Base['B'] = Base['B'] + Bnum;
    Acount -= Anum;
    Bcount -= Bnum;
  }
  exit(0);
  return (1);
}
