public static Cons applyCachedRetrieve(Cons variables, Cons querybody, Cons inputbindings, Stella_Object options, Symbol cacheid, Object [] MV_returnarray) {
    { QueryIterator queryiterator = null;
      int bindingscount = 0;
      Cons outputbindings = Stella.NIL;
      Cons truthvalues = Stella.NIL;
      Cons justifications = Stella.NIL;
      boolean forcesingletontuplesP = false;

      { ConsIterator it = inputbindings.allocateIterator();

        while (it.nextP()) {
          if (it.value != null) {
            bindingscount = bindingscount + 1;
          }
        }
      }
      queryiterator = Logic.makeCachedQuery(variables, querybody, inputbindings, options, cacheid);
      if (queryiterator == null) {
        { Cons _return_temp = Stella.NIL;

          MV_returnarray[0] = Stella.NIL;
          MV_returnarray[1] = Stella.NIL;
          return (_return_temp);
        }
      }
      if (Stella_Object.eqlP(queryiterator.options.lookup(Logic.KWD_SINGLETONSp), Stella.FALSE_WRAPPER) ||
          Stella_Object.eqlP(queryiterator.options.lookup(Logic.KWD_ATOMIC_SINGLETONSp), Stella.FALSE_WRAPPER)) {
        forcesingletontuplesP = true;
      }
      { Object old$Queryiterator$000 = Logic.$QUERYITERATOR$.get();
        Object old$ReversepolarityP$000 = Logic.$REVERSEPOLARITYp$.get();

        try {
          Native.setSpecial(Logic.$QUERYITERATOR$, queryiterator);
          Native.setBooleanSpecial(Logic.$REVERSEPOLARITYp$, false);
          while (queryiterator.nextP()) {
          }

        } finally {
          Logic.$REVERSEPOLARITYp$.set(old$ReversepolarityP$000);
          Logic.$QUERYITERATOR$.set(old$Queryiterator$000);
        }
      }
      { Stella_Object tuple = null;
        boolean atomicsingletonsP = (inputbindings.length() == (bindingscount + 1)) &&
            (!forcesingletontuplesP);

        { QuerySolution s = null;
          DictionaryIterator iter000 = ((DictionaryIterator)(queryiterator.solutions.allocateIterator()));

          while (iter000.nextP()) {
            s = ((QuerySolution)(iter000.value));
            if (atomicsingletonsP) {
              { Stella_Object v = null;
                Vector vector000 = s.bindings;
                int index000 = 0;
                int length000 = vector000.length();
                Stella_Object b = null;
                Cons iter001 = inputbindings;

                for (;(index000 < length000) &&
                          (!(iter001 == Stella.NIL)); index000 = index000 + 1, iter001 = iter001.rest) {
                  v = (vector000.theArray)[index000];
                  b = iter001.value;
                  if (b == null) {
                    tuple = v;
                  }
                }
              }
            }
            else {
              { Cons values = Stella.NIL;

                { Stella_Object v = null;
                  Vector vector001 = s.bindings;
                  int index001 = 0;
                  int length001 = vector001.length();
                  Stella_Object b = null;
                  Cons iter002 = inputbindings;
                  Cons collect000 = null;

                  for (;(index001 < length001) &&
                            (!(iter002 == Stella.NIL)); index001 = index001 + 1, iter002 = iter002.rest) {
                    v = (vector001.theArray)[index001];
                    b = iter002.value;
                    if (b == null) {
                      if (collect000 == null) {
                        {
                          collect000 = Cons.cons(v, Stella.NIL);
                          if (values == Stella.NIL) {
                            values = collect000;
                          }
                          else {
                            Cons.addConsToEndOfConsList(values, collect000);
                          }
                        }
                      }
                      else {
                        {
                          collect000.rest = Cons.cons(v, Stella.NIL);
                          collect000 = collect000.rest;
                        }
                      }
                    }
                  }
                }
                tuple = values;
              }
            }
            outputbindings = Cons.cons(tuple, outputbindings);
            truthvalues = Cons.cons(s.truthValue, truthvalues);
            if (((Boolean)(Logic.$RECORD_JUSTIFICATIONSp$.get())).booleanValue()) {
              justifications = Cons.cons(s.justification, justifications);
            }
          }
        }
      }
      QueryIterator.freeCachedQuery(queryiterator, cacheid);
      { Cons _return_temp = outputbindings.reverse();

        MV_returnarray[0] = truthvalues.reverse();
        MV_returnarray[1] = justifications.reverse();
        return (_return_temp);
      }
    }
  }