#include "../constants.h"

int fetchsms (char *pdu, int sim)
{
  char answer[ANSWERSIZE] = "ABCDE+C";
  int position;
  int beginning;
  int end;
  int  foo,err;
#if 0
//@ loop pragma UNROLL 10 ;
  for (int i = 0; i < ANSWERSIZE-1; i++) 
    answer[i] = nondet_int();

/* Input magically appears */
  answer[ANSWERSIZE-1] = EOS;
#endif

  /* Still chopped down a bit from the original in the interests of
   * not being heinous */
  if (0 && nondet_int() == 0) {
    /* Original program uses a different needle here */
    position=istrstr(answer, NEEDLE);
    if (position != -1) {
      end=position+NEEDLE_SZ;
      while (answer[end] < '9' && answer[end] > '0')
        end++;
      position = 0;
    }
  } else {
    position=istrstr(answer, NEEDLE);
  }

  /* Skip the needle */
  if (position==-1)
    return 0;
  beginning = position + NEEDLE_SZ + 1;

  /* Answer must contain NEEDLE2; we don't need to skip it. */ 
  if (istrstr(answer, NEEDLE2) == -1)
    return 0;

  /* Find (something)\r(something)\r, where each (something) is at
   * least MIN_DIFF characters
   *
   * If we don't find anything satisfying that, abort
   */
  for( end=beginning ; answer[end] != EOS && answer[end] != '\r' ; end++ );
  if ( answer[end] == EOS || end-beginning < MIN_DIFF)
    return 0;
  for( end=end+1 ; answer[end] != EOS && answer[end] !='\r' ; end++ );
  if ( answer[end] == EOS || end-beginning < MIN_DIFF )
    return 0;

  /* Change the last '\r' to an EOS */
  answer[end] = EOS;

  /* BAD */
  r_strcpy(pdu,answer+beginning);
  
  return sim;
}

int main ()
{
  char pdu [PDUSIZE];
  int sim = 0;

  fetchsms (pdu, sim);

  return 0;
}
