public static String substituteTemplateVariablesInString(String string, KeyValueList variables) {
    { int index = 0;
      int length = string.length();
      String result = string;
      int variableend = 0;
      String variablename = null;
      StringWrapper variablevalue = null;

      loop000 : for (;;) {
        index = Native.stringSearch(result, Stella.$MAKEFILE_TEMPLATE_VARIABLE_PREFIX$, index);
        if (index == Stella.NULL_INTEGER) {
          break loop000;
        }
        index = index + 2;
        if (index >= length) {
          {
            Stella.STANDARD_WARNING.nativeStream.println("Warning: Illegal template variable syntax in");
            Stella.STANDARD_WARNING.nativeStream.println("    `" + string + "'");
          }
;
          break loop000;
        }
        if (!(result.charAt(index) == '(')) {
          {
            Stella.STANDARD_WARNING.nativeStream.println("Warning: Illegal template variable syntax in");
            Stella.STANDARD_WARNING.nativeStream.println("    `" + string + "'");
          }
;
          continue loop000;
        }
        variableend = Native.string_position(result, ')', index);
        if (variableend == Stella.NULL_INTEGER) {
          {
            Stella.STANDARD_WARNING.nativeStream.println("Warning: Illegal template variable syntax in");
            Stella.STANDARD_WARNING.nativeStream.println("    `" + string + "'");
          }
;
          continue loop000;
        }
        index = index + 1;
        variablename = Native.string_subsequence(result, index, variableend);
        variablevalue = ((StringWrapper)(variables.lookup(StringWrapper.wrapString(variablename))));
        if (variablevalue == null) {
          Stella.STANDARD_WARNING.nativeStream.println("Warning: Undefined template variable: `" + variablename + "'");
          continue loop000;
        }
        result = Native.string_subsequence(result, 0, index - 3) + variablevalue.wrapperValue + Native.string_subsequence(result, variableend + 1, Stella.NULL_INTEGER);
        index = variableend + 1 + (result.length() - length);
        length = result.length();
      }
      return (result);
    }
  }