Program: 72986
Project: 72986
Buggy source:
/** 
 * Generate code and append it to the given string buffer. Write the code to the directory specified by the codeDirectory parameter.  The file name is a sanitized version of the model name with a suffix that is based on last package name of the <i>generatorPackage</i> parameter.  Thus if the <i>codeDirectory</i> is <code>$HOME</code>, the name of the model is <code>Foo</code> and the <i>generatorPackage</i> is <code>ptolemy.codegen.c</code>, then the file that is written will be <code>$HOME/Foo.c</code> This method is the main entry point. We do not call the super method because it is too different from it. For instance, there is not only one file generated, but a few.
 * @param code The given string buffer.
 * @return The return value of the last subprocess that was executed.or -1 if no commands were executed.
 * @exception KernelException If the target file cannot be overwrittenor write-to-file throw any exception.
 */
protected int _generateCode(StringBuffer code) throws KernelException {
  long startTime=new Date().getTime();
  long overallStartTime=startTime;
  _reset();
  _sanitizedModelName=CodeGeneratorAdapter.generateName(_model);
  NamedObj container=getContainer();
  Director director=null;
  if (container instanceof CompositeActor)   director=((CompositeActor)container).getDirector();
  if (container instanceof ptolemy.cg.lib.CompiledCompositeActor) {
    _sanitizedModelName=((ptolemy.cg.lib.CompiledCompositeActor)container).getSanitizedName();
  }
  boolean inlineValue=((BooleanToken)inline.getToken()).booleanValue();
  if (inlineValue && director != null && director instanceof DEDirector) {
    throw new IllegalActionException("String_Node_Str");
  }
  _analyzeTypeConversions();
  startTime=_printTimeAndMemory(startTime,"String_Node_Str");
  _addActorIncludeDirectories();
  _addActorLibraries();
  _setupAdapter();
  String preinitializeCode=_generatePreinitializeCode();
  String preinitializeMethodEntryCode=_generatePreinitializeMethodEntryCode();
  String preinitializeMethodBodyCode=_generatePreinitializeMethodBodyCode();
  String preinitializeMethodExitCode=_generatePreinitializeMethodExitCode();
  String preinitializeProcedureName=_generatePreinitializeMethodProcedureName();
  String initializeCode=generateInitializeCode();
  String bodyCode=_generateBodyCode();
  String mainEntryCode=generateMainEntryCode();
  String mainExitCode=generateMainExitCode();
  String initializeEntryCode=generateInitializeEntryCode();
  String initializeExitCode=generateInitializeExitCode();
  String initializeProcedureName=generateInitializeProcedureName();
  String wrapupEntryCode=generateWrapupEntryCode();
  String wrapupExitCode=generateWrapupExitCode();
  String wrapupProcedureName=generateWrapupProcedureName();
  String fireFunctionCode=null;
  String[] actorsCode=new String[0];
  if (!inlineValue) {
    if (director != null && director instanceof DEDirector) {
      ptolemy.cg.adapter.generic.program.procedural.adapters.ptolemy.domains.de.kernel.DEDirector directorAdapter=(ptolemy.cg.adapter.generic.program.procedural.adapters.ptolemy.domains.de.kernel.DEDirector)getAdapter(((DEDirector)director));
      actorsCode=directorAdapter.generateActorCode();
    }
 else {
      actorsCode=new String[0];
      fireFunctionCode=generateFireFunctionCode();
    }
  }
  String wrapupCode=generateWrapupCode();
  String closingEntryCode=generateClosingEntryCode();
  String closingExitCode=generateClosingExitCode();
  String variableInitCode=generateVariableInitialization();
  String sharedCode=_generateSharedCode();
  String declareSharedCode=_generateDeclareSharedCode();
  String[] typeResolutionCode=generateTypeConvertCodeCandH();
  List<String> variableDeclareCode=_splitVariableDeclaration("String_Node_Str",generateVariableDeclaration());
  String includeFiles=_generateIncludeFiles();
  startTime=_printTimeAndMemory(startTime,"String_Node_Str");
  StringBuffer codeMainH=new StringBuffer();
  StringBuffer codeTypesH=new StringBuffer();
  StringBuffer codeTypesC=new StringBuffer();
  codeTypesH.append("String_Node_Str" + _eol + "String_Node_Str"+ _eol);
  code.append(generateCopyright());
  code.append(generatePackageStatement());
  code.append(variableDeclareCode.get(0));
  codeMainH.append("String_Node_Str" + _eol + "String_Node_Str"+ _eol);
  if (director != null && director instanceof DEDirector) {
    codeMainH.append("String_Node_Str" + _eol);
    codeMainH.append("String_Node_Str" + _eol);
    codeMainH.append("String_Node_Str" + _eol);
    codeMainH.append("String_Node_Str" + _eol);
    codeMainH.append("String_Node_Str" + _eol);
    codeMainH.append("String_Node_Str" + _eol);
  }
  codeMainH.append("String_Node_Str" + _eol);
  codeMainH.append(includeFiles);
  code.append("String_Node_Str" + _eol);
  includeFiles=null;
  code.append(comment("String_Node_Str"));
  codeTypesH.append(typeResolutionCode[0]);
  typeResolutionCode[0]=null;
  codeTypesH.append(comment("String_Node_Str"));
  codeTypesC.append(typeResolutionCode[1]);
  typeResolutionCode[1]=null;
  codeTypesC.append(comment("String_Node_Str"));
  codeTypesC.append(sharedCode);
  sharedCode=null;
  codeTypesH.append(declareSharedCode);
  declareSharedCode=null;
  codeTypesH.append(comment("String_Node_Str"));
  codeMainH.append(_writeVariableDeclarations(variableDeclareCode));
  codeMainH.append(comment("String_Node_Str"));
  codeMainH.append(preinitializeCode);
  preinitializeCode=null;
  codeMainH.append(comment("String_Node_Str"));
  codeMainH.append(comment("String_Node_Str"));
  String[] splitPreinitializeMethodBodyCode=_splitBody("String_Node_Str",preinitializeMethodBodyCode);
  codeMainH.append(comment("String_Node_Str"));
  codeMainH.append(splitPreinitializeMethodBodyCode[0]);
  splitPreinitializeMethodBodyCode[0]=null;
  codeMainH.append(comment("String_Node_Str"));
  codeMainH.append(preinitializeMethodEntryCode);
  codeMainH.append(splitPreinitializeMethodBodyCode[1]);
  splitPreinitializeMethodBodyCode[1]=null;
  codeMainH.append(preinitializeMethodExitCode);
  codeMainH.append(_eol + "String_Node_Str");
  if (!inlineValue && fireFunctionCode != null) {
    code.append(comment("String_Node_Str"));
    code.append(fireFunctionCode);
    fireFunctionCode=null;
    code.append(comment("String_Node_Str"));
  }
  String[] splitVariableInitCode=_splitBody("String_Node_Str",variableInitCode);
  code.append(comment("String_Node_Str"));
  code.append(splitVariableInitCode[0] + "String_Node_Str");
  splitVariableInitCode[0]=null;
  code.append(comment("String_Node_Str"));
  String[] splitInitializeCode=_splitBody("String_Node_Str",initializeCode);
  code.append(comment("String_Node_Str"));
  code.append(splitInitializeCode[0]);
  splitInitializeCode[0]=null;
  code.append(comment("String_Node_Str"));
  code.append(comment("String_Node_Str"));
  code.append(initializeEntryCode);
  code.append(comment("String_Node_Str"));
  code.append(comment("String_Node_Str"));
  code.append(splitVariableInitCode[1]);
  splitVariableInitCode[1]=null;
  code.append(comment("String_Node_Str"));
  code.append(comment("String_Node_Str"));
  code.append(splitInitializeCode[1]);
  splitInitializeCode[1]=null;
  code.append(comment("String_Node_Str"));
  code.append(comment("String_Node_Str"));
  code.append(initializeExitCode);
  String[] splitWrapupCode=_splitBody("String_Node_Str",wrapupCode);
  code.append(splitWrapupCode[0]);
  splitWrapupCode[0]=null;
  code.append(wrapupEntryCode);
  code.append(splitWrapupCode[1]);
  splitWrapupCode[1]=null;
  code.append(wrapupExitCode);
  code.append(mainEntryCode);
  if (_isTopLevel()) {
    if (containsCode(preinitializeMethodBodyCode)) {
      code.append(preinitializeProcedureName);
    }
    if (containsCode(variableInitCode) || containsCode(initializeCode)) {
      code.append(initializeProcedureName);
    }
  }
  code.append(bodyCode);
  String directorySrc="String_Node_Str";
  directorySrc+=codeDirectory.stringValue();
  if (!directorySrc.endsWith("String_Node_Str"))   directorySrc+="String_Node_Str";
  new File(directorySrc).mkdirs();
  directorySrc+="String_Node_Str";
  new File(directorySrc).mkdirs();
  _actorsToInclude=new LinkedList<String>();
  for (int i=0; i < actorsCode.length; i+=3) {
    _actorsToInclude.add(actorsCode[i]);
    StringBuffer actorCode=new StringBuffer();
    actorCode.append(actorsCode[i + 1]);
    super._writeCodeFileName(actorCode,"String_Node_Str" + actorsCode[i] + "String_Node_Str",true,false);
    StringBuffer actorCodeH=new StringBuffer();
    actorCodeH.append(actorsCode[i + 2]);
    super._writeCodeFileName(actorCodeH,"String_Node_Str" + actorsCode[i] + "String_Node_Str",true,false);
  }
  bodyCode=null;
  if (_isTopLevel()) {
    if (containsCode(closingEntryCode)) {
      code.append(closingEntryCode);
    }
    if (containsCode(wrapupCode)) {
      code.append(wrapupProcedureName);
    }
    code.append(closingExitCode);
  }
  code.append(mainExitCode);
  codeTypesH.append("String_Node_Str");
  if (_executeCommands == null) {
    _executeCommands=new StreamExec();
  }
  startTime=_printTimeAndMemory(startTime,"String_Node_Str");
  code=_finalPassOverCode(code);
  startTime=_printTimeAndMemory(startTime,"String_Node_Str");
  super._writeCode(code);
  super._writeCodeFileName(codeMainH,"String_Node_Str",true,false);
  super._writeCodeFileName(codeTypesH,"String_Node_Str",true,false);
  super._writeCodeFileName(codeTypesC,"String_Node_Str",true,false);
  if (director != null && director instanceof DEDirector) {
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
  }
  code=null;
  _printTimeAndMemory(startTime,"String_Node_Str");
  _writeMakefile();
  _printTimeAndMemory(overallStartTime,"String_Node_Str");
  return _executeCommands();
}

Fixed version (ground truth):
/** 
 * Generate code and append it to the given string buffer. Write the code to the directory specified by the codeDirectory parameter.  The file name is a sanitized version of the model name with a suffix that is based on last package name of the <i>generatorPackage</i> parameter.  Thus if the <i>codeDirectory</i> is <code>$HOME</code>, the name of the model is <code>Foo</code> and the <i>generatorPackage</i> is <code>ptolemy.codegen.c</code>, then the file that is written will be <code>$HOME/Foo.c</code> This method is the main entry point. We do not call the super method because it is too different from it. For instance, there is not only one file generated, but a few.
 * @param code The given string buffer.
 * @return The return value of the last subprocess that was executed.or -1 if no commands were executed.
 * @exception KernelException If the target file cannot be overwrittenor write-to-file throw any exception.
 */
protected int _generateCode(StringBuffer code) throws KernelException {
  long startTime=new Date().getTime();
  long overallStartTime=startTime;
  _reset();
  _sanitizedModelName=CodeGeneratorAdapter.generateName(_model);
  NamedObj container=getContainer();
  Director director=null;
  if (container instanceof CompositeActor)   director=((CompositeActor)container).getDirector();
  if (container instanceof ptolemy.cg.lib.CompiledCompositeActor) {
    _sanitizedModelName=((ptolemy.cg.lib.CompiledCompositeActor)container).getSanitizedName();
  }
  Attribute generateEmbeddedCode=getAttribute("String_Node_Str");
  if (generateEmbeddedCode instanceof Parameter) {
    ((Parameter)generateEmbeddedCode).setExpression("String_Node_Str");
  }
  boolean inlineValue=((BooleanToken)inline.getToken()).booleanValue();
  if (inlineValue && director != null && director instanceof DEDirector) {
    throw new IllegalActionException("String_Node_Str");
  }
  _analyzeTypeConversions();
  startTime=_printTimeAndMemory(startTime,"String_Node_Str");
  _addActorIncludeDirectories();
  _addActorLibraries();
  _setupAdapter();
  String preinitializeCode=_generatePreinitializeCode();
  String preinitializeMethodEntryCode=_generatePreinitializeMethodEntryCode();
  String preinitializeMethodBodyCode=_generatePreinitializeMethodBodyCode();
  String preinitializeMethodExitCode=_generatePreinitializeMethodExitCode();
  String preinitializeProcedureName=_generatePreinitializeMethodProcedureName();
  String initializeCode=generateInitializeCode();
  String bodyCode=_generateBodyCode();
  String mainEntryCode=generateMainEntryCode();
  String mainExitCode=generateMainExitCode();
  String initializeEntryCode=generateInitializeEntryCode();
  String initializeExitCode=generateInitializeExitCode();
  String initializeProcedureName=generateInitializeProcedureName();
  String wrapupEntryCode=generateWrapupEntryCode();
  String wrapupExitCode=generateWrapupExitCode();
  String wrapupProcedureName=generateWrapupProcedureName();
  String fireFunctionCode=null;
  String[] actorsCode=new String[0];
  if (!inlineValue) {
    if (director != null && director instanceof DEDirector) {
      ptolemy.cg.adapter.generic.program.procedural.adapters.ptolemy.domains.de.kernel.DEDirector directorAdapter=(ptolemy.cg.adapter.generic.program.procedural.adapters.ptolemy.domains.de.kernel.DEDirector)getAdapter(((DEDirector)director));
      actorsCode=directorAdapter.generateActorCode();
    }
 else {
      actorsCode=new String[0];
      fireFunctionCode=generateFireFunctionCode();
    }
  }
  String wrapupCode=generateWrapupCode();
  String closingEntryCode=generateClosingEntryCode();
  String closingExitCode=generateClosingExitCode();
  String variableInitCode=generateVariableInitialization();
  String sharedCode=_generateSharedCode();
  String declareSharedCode=_generateDeclareSharedCode();
  String[] typeResolutionCode=generateTypeConvertCodeCandH();
  List<String> variableDeclareCode=_splitVariableDeclaration("String_Node_Str",generateVariableDeclaration());
  String includeFiles=_generateIncludeFiles();
  startTime=_printTimeAndMemory(startTime,"String_Node_Str");
  StringBuffer codeMainH=new StringBuffer();
  StringBuffer codeTypesH=new StringBuffer();
  StringBuffer codeTypesC=new StringBuffer();
  codeTypesH.append("String_Node_Str" + _eol + "String_Node_Str"+ _eol);
  code.append(generateCopyright());
  code.append(generatePackageStatement());
  code.append(variableDeclareCode.get(0));
  codeMainH.append("String_Node_Str" + _eol + "String_Node_Str"+ _eol);
  if (director != null && director instanceof DEDirector) {
    codeMainH.append("String_Node_Str" + _eol);
    codeMainH.append("String_Node_Str" + _eol);
    codeMainH.append("String_Node_Str" + _eol);
    codeMainH.append("String_Node_Str" + _eol);
    codeMainH.append("String_Node_Str" + _eol);
    codeMainH.append("String_Node_Str" + _eol);
  }
  codeMainH.append("String_Node_Str" + _eol);
  codeMainH.append(includeFiles);
  code.append("String_Node_Str" + _eol);
  includeFiles=null;
  code.append(comment("String_Node_Str"));
  codeTypesH.append(typeResolutionCode[0]);
  typeResolutionCode[0]=null;
  codeTypesH.append(comment("String_Node_Str"));
  codeTypesC.append(typeResolutionCode[1]);
  typeResolutionCode[1]=null;
  codeTypesC.append(comment("String_Node_Str"));
  codeTypesC.append(sharedCode);
  sharedCode=null;
  codeTypesH.append(declareSharedCode);
  declareSharedCode=null;
  codeTypesH.append(comment("String_Node_Str"));
  codeMainH.append(_writeVariableDeclarations(variableDeclareCode));
  codeMainH.append(comment("String_Node_Str"));
  codeMainH.append(preinitializeCode);
  preinitializeCode=null;
  codeMainH.append(comment("String_Node_Str"));
  codeMainH.append(comment("String_Node_Str"));
  String[] splitPreinitializeMethodBodyCode=_splitBody("String_Node_Str",preinitializeMethodBodyCode);
  codeMainH.append(comment("String_Node_Str"));
  codeMainH.append(splitPreinitializeMethodBodyCode[0]);
  splitPreinitializeMethodBodyCode[0]=null;
  codeMainH.append(comment("String_Node_Str"));
  codeMainH.append(preinitializeMethodEntryCode);
  codeMainH.append(splitPreinitializeMethodBodyCode[1]);
  splitPreinitializeMethodBodyCode[1]=null;
  codeMainH.append(preinitializeMethodExitCode);
  codeMainH.append(_eol + "String_Node_Str");
  if (!inlineValue && fireFunctionCode != null) {
    code.append(comment("String_Node_Str"));
    code.append(fireFunctionCode);
    fireFunctionCode=null;
    code.append(comment("String_Node_Str"));
  }
  String[] splitVariableInitCode=_splitBody("String_Node_Str",variableInitCode);
  code.append(comment("String_Node_Str"));
  code.append(splitVariableInitCode[0] + "String_Node_Str");
  splitVariableInitCode[0]=null;
  code.append(comment("String_Node_Str"));
  String[] splitInitializeCode=_splitBody("String_Node_Str",initializeCode);
  code.append(comment("String_Node_Str"));
  code.append(splitInitializeCode[0]);
  splitInitializeCode[0]=null;
  code.append(comment("String_Node_Str"));
  code.append(comment("String_Node_Str"));
  code.append(initializeEntryCode);
  code.append(comment("String_Node_Str"));
  code.append(comment("String_Node_Str"));
  code.append(splitVariableInitCode[1]);
  splitVariableInitCode[1]=null;
  code.append(comment("String_Node_Str"));
  code.append(comment("String_Node_Str"));
  code.append(splitInitializeCode[1]);
  splitInitializeCode[1]=null;
  code.append(comment("String_Node_Str"));
  code.append(comment("String_Node_Str"));
  code.append(initializeExitCode);
  String[] splitWrapupCode=_splitBody("String_Node_Str",wrapupCode);
  code.append(splitWrapupCode[0]);
  splitWrapupCode[0]=null;
  code.append(wrapupEntryCode);
  code.append(splitWrapupCode[1]);
  splitWrapupCode[1]=null;
  code.append(wrapupExitCode);
  code.append(mainEntryCode);
  if (_isTopLevel()) {
    if (containsCode(preinitializeMethodBodyCode)) {
      code.append(preinitializeProcedureName);
    }
    if (containsCode(variableInitCode) || containsCode(initializeCode)) {
      code.append(initializeProcedureName);
    }
  }
  code.append(bodyCode);
  String directorySrc="String_Node_Str";
  directorySrc+=codeDirectory.stringValue();
  if (!directorySrc.endsWith("String_Node_Str"))   directorySrc+="String_Node_Str";
  new File(directorySrc).mkdirs();
  directorySrc+="String_Node_Str";
  new File(directorySrc).mkdirs();
  _actorsToInclude=new LinkedList<String>();
  for (int i=0; i < actorsCode.length; i+=3) {
    _actorsToInclude.add(actorsCode[i]);
    StringBuffer actorCode=new StringBuffer();
    actorCode.append(actorsCode[i + 1]);
    super._writeCodeFileName(actorCode,"String_Node_Str" + actorsCode[i] + "String_Node_Str",true,false);
    StringBuffer actorCodeH=new StringBuffer();
    actorCodeH.append(actorsCode[i + 2]);
    super._writeCodeFileName(actorCodeH,"String_Node_Str" + actorsCode[i] + "String_Node_Str",true,false);
  }
  bodyCode=null;
  if (_isTopLevel()) {
    if (containsCode(closingEntryCode)) {
      code.append(closingEntryCode);
    }
    if (containsCode(wrapupCode)) {
      code.append(wrapupProcedureName);
    }
    code.append(closingExitCode);
  }
  code.append(mainExitCode);
  codeTypesH.append("String_Node_Str");
  if (_executeCommands == null) {
    _executeCommands=new StreamExec();
  }
  startTime=_printTimeAndMemory(startTime,"String_Node_Str");
  code=_finalPassOverCode(code);
  startTime=_printTimeAndMemory(startTime,"String_Node_Str");
  super._writeCode(code);
  super._writeCodeFileName(codeMainH,"String_Node_Str",true,false);
  super._writeCodeFileName(codeTypesH,"String_Node_Str",true,false);
  super._writeCodeFileName(codeTypesC,"String_Node_Str",true,false);
  if (director != null && director instanceof DEDirector) {
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
    _copyCFileTosrc("String_Node_Str");
  }
  code=null;
  _printTimeAndMemory(startTime,"String_Node_Str");
  _writeMakefile();
  _printTimeAndMemory(overallStartTime,"String_Node_Str");
  return _executeCommands();
}