Program: 132867
Project: 132867
Buggy source:
/** 
 * Handle foreach loop as special case where we want to increment <# of iterations> * <needed increments inside loop> before loop. This function will work out if it can pull out increments from foreach loop body
 */
private void postProcessForeachLoop(Continuation c){
  AbstractForeachLoop loop=(AbstractForeachLoop)c;
  Counters<Var> readIncrs=new Counters<Var>();
  Counters<Var> writeIncrs=new Counters<Var>();
  if (loop.isAsync()) {
    for (    PassedVar v : loop.getPassedVars()) {
      if (!v.writeOnly && RefCounting.hasReadRefCount(v.var)) {
        readIncrs.increment(v.var);
      }
    }
    for (    Var v : loop.getKeepOpenVars()) {
      if (RefCounting.hasWriteRefCount(v)) {
        writeIncrs.increment(v);
      }
    }
  }
  Block loopBody=loop.getLoopBody();
  ListIterator<Statement> it=loopBody.statementIterator();
  while (it.hasNext()) {
    Statement stmt=it.next();
    if (stmt.type() != StatementType.INSTRUCTION || !RefCountOp.isIncrement(stmt.instruction().op)) {
      break;
    }
    Instruction inst=stmt.instruction();
    Arg amount=RefCountOp.getRCAmount(inst);
    Var var=RefCountOp.getRCTarget(inst);
    RefCountType rcType=RefCountOp.getRCType(inst.op);
    if (amount.isIntVal() && RCUtil.definedOutsideCont(loop,loopBody,var)) {
      if (rcType == RefCountType.READERS) {
        readIncrs.add(var,amount.getIntLit());
      }
 else {
        assert(rcType == RefCountType.WRITERS);
        writeIncrs.add(var,amount.getIntLit());
      }
      it.remove();
    }
  }
  for (  Entry<Var,Long> read : readIncrs.entries()) {
    loop.addStartIncrement(new RefCount(read.getKey(),RefCountType.READERS,Arg.createIntLit(read.getValue())));
  }
  for (  Entry<Var,Long> write : writeIncrs.entries()) {
    loop.addStartIncrement(new RefCount(write.getKey(),RefCountType.WRITERS,Arg.createIntLit(write.getValue())));
  }
}

Fixed version (ground truth):
/** 
 * Handle foreach loop as special case where we want to increment <# of iterations> * <needed increments inside loop> before loop. This function will work out if it can pull out increments from foreach loop body
 */
private void postProcessForeachLoop(Continuation c){
  AbstractForeachLoop loop=(AbstractForeachLoop)c;
  Counters<Var> readIncrs=new Counters<Var>();
  Counters<Var> writeIncrs=new Counters<Var>();
  if (loop.isAsync()) {
    for (    PassedVar v : loop.getAllPassedVars()) {
      if (!v.writeOnly && RefCounting.hasReadRefCount(v.var)) {
        readIncrs.increment(v.var);
      }
    }
    for (    Var v : loop.getKeepOpenVars()) {
      if (RefCounting.hasWriteRefCount(v)) {
        writeIncrs.increment(v);
      }
    }
  }
  Block loopBody=loop.getLoopBody();
  ListIterator<Statement> it=loopBody.statementIterator();
  while (it.hasNext()) {
    Statement stmt=it.next();
    if (stmt.type() != StatementType.INSTRUCTION || !RefCountOp.isIncrement(stmt.instruction().op)) {
      break;
    }
    Instruction inst=stmt.instruction();
    Arg amount=RefCountOp.getRCAmount(inst);
    Var var=RefCountOp.getRCTarget(inst);
    RefCountType rcType=RefCountOp.getRCType(inst.op);
    if (amount.isIntVal() && RCUtil.definedOutsideCont(loop,loopBody,var)) {
      if (rcType == RefCountType.READERS) {
        readIncrs.add(var,amount.getIntLit());
      }
 else {
        assert(rcType == RefCountType.WRITERS);
        writeIncrs.add(var,amount.getIntLit());
      }
      it.remove();
    }
  }
  for (  Entry<Var,Long> read : readIncrs.entries()) {
    loop.addStartIncrement(new RefCount(read.getKey(),RefCountType.READERS,Arg.createIntLit(read.getValue())));
  }
  for (  Entry<Var,Long> write : writeIncrs.entries()) {
    loop.addStartIncrement(new RefCount(write.getKey(),RefCountType.WRITERS,Arg.createIntLit(write.getValue())));
  }
}