Program: 132835
Project: 132835
Buggy source:
/** 
 * In cases where variable is closed upon starting loop iterations, switch to passing as value
 * @param logger
 * @param loop
 */
private void optimizeLoop(Logger logger,Loop loop){
  List<BlockingVar> closedVars=loop.closedLoopVars();
  if (closedVars.isEmpty()) {
    return;
  }
  Block outerBlock=loop.parent();
  ListIterator<Statement> outerInsertPoint=outerBlock.statementIterator();
  List<Statement> outerFetches=new ArrayList<Statement>();
  List<Var> outerFetched=new ArrayList<Var>();
  LoopInstructions insts=loop.findInstructions();
  Block innerBlock=insts.continueInstBlock;
  ListIterator<Statement> innerInsertPoint=findLoopContinueInsertPoint(insts.continueInst,innerBlock);
  List<Statement> innerFetches=new ArrayList<Statement>();
  List<Var> innerFetched=new ArrayList<Var>();
  for (  BlockingVar closedVar : closedVars) {
    if (replaceLoopVarWithVal(closedVar.var)) {
      Var init=loop.getInitVal(closedVar.var).getVar();
      Var subsequent=loop.getUpdateVal(closedVar.var).getVar();
      Var outerFetchedV=fetchLoopVar(init,closedVar.recursive,outerBlock,outerFetches);
      Var innerFetchedV=fetchLoopVar(subsequent,closedVar.recursive,innerBlock,innerFetches);
      outerFetched.add(outerFetchedV);
      innerFetched.add(innerFetchedV);
      placeAndClear(outerInsertPoint,outerFetches);
      placeAndClear(innerInsertPoint,innerFetches);
      Var oldLoopVar=closedVar.var;
      Var newLoopVar=new Var(outerFetchedV.type(),OptUtil.optVPrefix(outerBlock,oldLoopVar),outerFetchedV.storage(),DefType.LOCAL_COMPILER,VarProvenance.valueOf(oldLoopVar));
      boolean blockOn=Types.canWaitForFinalize(newLoopVar) && loop.isBlocking(oldLoopVar);
      loop.replaceLoopVar(oldLoopVar,newLoopVar,outerFetchedV.asArg(),innerFetchedV.asArg(),blockOn);
      loop.getLoopBody().addVariable(oldLoopVar);
      loop.getLoopBody().addInstructionFront(ICInstructions.futureSet(oldLoopVar,newLoopVar.asArg()));
    }
  }
}

Fixed version (ground truth):
/** 
 * In cases where variable is closed upon starting loop iterations, switch to passing as value
 * @param logger
 * @param loop
 */
private void optimizeLoop(Logger logger,Loop loop){
  List<BlockingVar> closedVars=loop.closedLoopVars();
  if (closedVars.isEmpty()) {
    return;
  }
  Block outerBlock=loop.parent();
  ListIterator<Statement> outerInsertPoint=outerBlock.statementEndIterator();
  List<Statement> outerFetches=new ArrayList<Statement>();
  List<Var> outerFetched=new ArrayList<Var>();
  LoopInstructions insts=loop.findInstructions();
  Block innerBlock=insts.continueInstBlock;
  ListIterator<Statement> innerInsertPoint=findLoopContinueInsertPoint(insts.continueInst,innerBlock);
  List<Statement> innerFetches=new ArrayList<Statement>();
  List<Var> innerFetched=new ArrayList<Var>();
  for (  BlockingVar closedVar : closedVars) {
    if (replaceLoopVarWithVal(closedVar.var)) {
      Var init=loop.getInitVal(closedVar.var).getVar();
      Var subsequent=loop.getUpdateVal(closedVar.var).getVar();
      Var outerFetchedV=fetchLoopVar(init,closedVar.recursive,outerBlock,outerFetches);
      Var innerFetchedV=fetchLoopVar(subsequent,closedVar.recursive,innerBlock,innerFetches);
      outerFetched.add(outerFetchedV);
      innerFetched.add(innerFetchedV);
      placeAndClear(outerInsertPoint,outerFetches);
      placeAndClear(innerInsertPoint,innerFetches);
      Var oldLoopVar=closedVar.var;
      Var newLoopVar=new Var(outerFetchedV.type(),OptUtil.optVPrefix(outerBlock,oldLoopVar),outerFetchedV.storage(),DefType.LOCAL_COMPILER,VarProvenance.valueOf(oldLoopVar));
      boolean blockOn=Types.canWaitForFinalize(newLoopVar) && loop.isBlocking(oldLoopVar);
      loop.replaceLoopVar(oldLoopVar,newLoopVar,outerFetchedV.asArg(),innerFetchedV.asArg(),blockOn);
      loop.getLoopBody().addVariable(oldLoopVar);
      loop.getLoopBody().addInstructionFront(ICInstructions.futureSet(oldLoopVar,newLoopVar.asArg()));
    }
  }
}