public static Stella_Object parseQueryAndOptions(Cons queryAoptions, Object [] MV_returnarray) {
    if ((queryAoptions == null) ||
        (queryAoptions == Stella.NIL)) {
      { Stella_Object _return_temp = null;

        MV_returnarray[0] = null;
        MV_returnarray[1] = PropertyList.newPropertyList();
        return (_return_temp);
      }
    }
    { Stella_Object firstarg = queryAoptions.value;
      Stella_Object variables = null;
      Stella_Object proposition = null;
      PropertyList options = PropertyList.newPropertyList();
      boolean handlenonmatchingvariablesP = false;
      List freevariables = null;
      Cons declaredvariables = null;

      { Surrogate testValue000 = Stella_Object.safePrimaryType(firstarg);

        if (Surrogate.subtypeOfIntegerP(testValue000)) {
          { IntegerWrapper firstarg000 = ((IntegerWrapper)(firstarg));

            options.insertAt(Logic.KWD_HOW_MANY, firstarg000);
            queryAoptions = queryAoptions.rest;
          }
        }
        else if (Surrogate.subtypeOfSymbolP(testValue000)) {
          { Symbol firstarg000 = ((Symbol)(firstarg));

            if (Stella.stringEqualP(firstarg000.symbolName, "ALL")) {
              options.insertAt(Logic.KWD_HOW_MANY, IntegerWrapper.wrapInteger(Stella.NULL_INTEGER));
              queryAoptions = queryAoptions.rest;
            }
            else if (Stella.stringEqualP(firstarg000.symbolName, "BEST")) {
              options.insertAt(Logic.KWD_HOW_MANY, IntegerWrapper.wrapInteger(Stella.NULL_INTEGER));
              options.insertAt(Logic.KWD_SORT_BY, Logic.KWD_SCORE);
              queryAoptions = queryAoptions.rest;
            }
          }
        }
        else if (Surrogate.subtypeOfKeywordP(testValue000)) {
          { Keyword firstarg000 = ((Keyword)(firstarg));

            if (Stella.stringEqualP(firstarg000.symbolName, "ALL")) {
              options.insertAt(Logic.KWD_HOW_MANY, IntegerWrapper.wrapInteger(Stella.NULL_INTEGER));
              queryAoptions = queryAoptions.rest;
            }
            else if (Stella.stringEqualP(firstarg000.symbolName, "BEST")) {
              options.insertAt(Logic.KWD_HOW_MANY, IntegerWrapper.wrapInteger(Stella.NULL_INTEGER));
              options.insertAt(Logic.KWD_SORT_BY, Logic.KWD_SCORE);
              queryAoptions = queryAoptions.rest;
            }
          }
        }
        else {
        }
      }
      if (!Stella_Object.keywordP(queryAoptions.value)) {
        variables = queryAoptions.value;
        queryAoptions = queryAoptions.rest;
      }
      if (!Stella_Object.keywordP(queryAoptions.value)) {
        proposition = queryAoptions.value;
        queryAoptions = queryAoptions.rest;
      }
      if ((variables != null) &&
          ((proposition == null) &&
           (!Logic.kifVariableDeclarationP(variables)))) {
        proposition = variables;
        variables = null;
      }
      if (Stella_Object.consP(proposition)) {
        freevariables = List.newList();
        if (variables == null) {
          declaredvariables = Stella.NIL;
        }
        else if (!Stella_Object.consP(variables)) {
          declaredvariables = Cons.cons(variables, Stella.NIL);
        }
        else {
          declaredvariables = ((Cons)(variables));
        }
        declaredvariables = Logic.extractVariablesFromDeclarations(declaredvariables);
        Logic.collectUndeclaredVariables(proposition, Stella.NIL, freevariables);
        if ((declaredvariables == Stella.NIL) &&
            freevariables.nonEmptyP()) {
          if (freevariables.length() == 1) {
            variables = ((Symbol)(freevariables.first()));
          }
          else {
            variables = freevariables.theConsList;
          }
        }
        else if ((declaredvariables == Stella.NIL) &&
            freevariables.emptyP()) {
        }
        else if (!freevariables.theConsList.equivalentSetsP(declaredvariables)) {
          handlenonmatchingvariablesP = true;
        }
      }
      while (!(queryAoptions == Stella.NIL)) {
        if (!Stella_Object.keywordP(queryAoptions.value)) {
          { OutputStringStream stream000 = OutputStringStream.newOutputStringStream();

            { Object old$PrintreadablyP$000 = Stella.$PRINTREADABLYp$.get();

              try {
                Native.setBooleanSpecial(Stella.$PRINTREADABLYp$, true);
                stream000.nativeStream.println("PARSING ERROR: Illegal query option: `" + queryAoptions.value + "'.");
                Logic.helpSignalPropositionError(stream000, Logic.KWD_ERROR);

              } finally {
                Stella.$PRINTREADABLYp$.set(old$PrintreadablyP$000);
              }
            }
            throw ((ParsingError)(ParsingError.newParsingError(stream000.theStringReader()).fillInStackTrace()));
          }
        }
        if (queryAoptions.rest.value == null) {
          { OutputStringStream stream001 = OutputStringStream.newOutputStringStream();

            { Object old$PrintreadablyP$001 = Stella.$PRINTREADABLYp$.get();

              try {
                Native.setBooleanSpecial(Stella.$PRINTREADABLYp$, true);
                stream001.nativeStream.println("PARSING ERROR: Missing value for option `" + queryAoptions.value + "'.");
                Logic.helpSignalPropositionError(stream001, Logic.KWD_ERROR);

              } finally {
                Stella.$PRINTREADABLYp$.set(old$PrintreadablyP$001);
              }
            }
            throw ((ParsingError)(ParsingError.newParsingError(stream001.theStringReader()).fillInStackTrace()));
          }
        }
        options.insertAt(queryAoptions.value, queryAoptions.rest.value);
        queryAoptions = queryAoptions.rest.rest;
      }
      if (handlenonmatchingvariablesP) {
        { Stella_Object temp000 = options.lookup(Logic.KWD_CHECK_VARIABLESp);

          if (BooleanWrapper.coerceWrappedBooleanToBoolean(Stella_Object.coerceToBoolean(((temp000 != null) ? temp000 : Stella.FALSE_WRAPPER)))) {
            { OutputStringStream stream002 = OutputStringStream.newOutputStringStream();

              { Object old$PrintreadablyP$002 = Stella.$PRINTREADABLYp$.get();

                try {
                  Native.setBooleanSpecial(Stella.$PRINTREADABLYp$, true);
                  {
                    stream002.nativeStream.println("PARSING ERROR: Declared query variables `" + declaredvariables + "' do not match");
                    stream002.nativeStream.println("   the free variables `" + freevariables.theConsList + "' referenced in the query body.");
                    stream002.nativeStream.println("   To simply use the free variables, omit the declaration.");
                  }
;
                  Logic.helpSignalPropositionError(stream002, Logic.KWD_ERROR);

                } finally {
                  Stella.$PRINTREADABLYp$.set(old$PrintreadablyP$002);
                }
              }
              throw ((ParsingError)(ParsingError.newParsingError(stream002.theStringReader()).fillInStackTrace()));
            }
          }
          else {
            proposition = Cons.list$(Cons.cons(Logic.SYM_STELLA_EXISTS, Cons.cons(freevariables.theConsList.difference(declaredvariables), Cons.cons(Cons.cons(proposition, Stella.NIL), Stella.NIL))));
          }
        }
      }
      { Stella_Object _return_temp = variables;

        MV_returnarray[0] = proposition;
        MV_returnarray[1] = options;
        return (_return_temp);
      }
    }
  }