#include "../../../../../lib/stubs.h"

int main (void)
{
  // XXX infile originally at most MAXLINE long per call to mime_fromqp

  char outfile[BASE_SZ]; // originally MAXLINE
    // originally a function argument **ooutfile; this function modified
    // caller's pointer into outbut buffer
  char *outp = outfile;

  int c1, c2;

  // number of chars copied from infile into outfile; reset when
  // continuation sequence "=\n" is read
  int nchar = 0;

  while ((c1 = nondet_char ()) != EOS)
  {
    if (c1 == '=')
    {
      // malformed: early EOS
      if ((c1 = nondet_char ()) == EOS)
	// in Zitser, these breaks actually return to the caller where the
	// pointer into outfile is reset before this is called again
	break; 

      // =\n: continuation; signal to caller it's ok to pass in more infile
      // BAD: forgot to reset outp
      if (c1 == '\n')
      {
	nchar = 0;
	continue;
      }
      else
      {
	// convert, e.g., "=5c" to int

	// malformed: early EOF
	if ((c2 = nondet_char ()) == EOS)
	  break;

	nchar++;
	if (nchar > BASE_SZ)
	  break;

	/* BAD */
	*outp++ = c1;
      }
    }
    else
    {
      // regular character, copy verbatim

      nchar++;
      if (nchar > BASE_SZ)
	break;

      /* BAD */
      *outp++ = c1;

      if (c1 == '\n')
	break;
    }
  }

  /* BAD */
  *outp++ = EOS;
  return 0;
}
