public static Surrogate getInstanceSurrogate(Stella_Object instanceref) {
    if (instanceref == null) {
      return (null);
    }
    else if (Stella_Object.isaP(instanceref, Logic.SGT_STELLA_SURROGATE)) {
      if (((Surrogate)(instanceref)).surrogateValue == null) {
        return (null);
      }
      else {
        return (((Surrogate)(instanceref)));
      }
    }
    else {
      { Stella_Object instance = Logic.getInstance(instanceref);

        if (instance == null) {
          return (null);
        }
        { Surrogate testValue000 = Stella_Object.safePrimaryType(instance);

          if (Surrogate.subtypeOfP(testValue000, Logic.SGT_LOGIC_LOGIC_OBJECT)) {
            { LogicObject instance000 = ((LogicObject)(instance));

              if (instance000 != null) {
                return (instance000.surrogateValueInverse);
              }
              else {
                return (null);
              }
            }
          }
          else if (Surrogate.subtypeOfP(testValue000, Logic.SGT_STELLA_THING)) {
            { Thing instance000 = ((Thing)(instance));

              if (instance000 != null) {
                return (instance000.surrogateValueInverse);
              }
              else {
                return (null);
              }
            }
          }
          else {
            { OutputStringStream stream000 = OutputStringStream.newOutputStringStream();

              stream000.nativeStream.print("`" + testValue000 + "' is not a valid case option");
              throw ((StellaException)(StellaException.newStellaException(stream000.theStringReader()).fillInStackTrace()));
            }
          }
        }
      }
    }
  }