Program: 133229
Project: 133229
Buggy source:
/** 
 * Handle the special case of a foreach loop where we are looping over range specified by two or three integer parameters
 * @param context
 * @param loop
 * @throws UserException
 * @throws UndefinedTypeException
 */
private void foreachRange(Context context,ForeachLoop loop) throws UserException {
  ArrayRange range=ArrayRange.fromAST(context,loop.getArrayVarTree());
  range.typeCheck(context);
  Var start=exprWalker.eval(context,range.getStart(),Types.F_INT,false,null);
  Var end=exprWalker.eval(context,range.getEnd(),Types.F_INT,false,null);
  Var step;
  if (range.getStep() != null) {
    step=exprWalker.eval(context,range.getStep(),Types.F_INT,false,null);
  }
 else {
    step=varCreator.createTmp(context,Types.F_INT);
    backend.assignInt(step,Arg.createIntLit(1));
  }
  ArrayList<Var> usedVariables=new ArrayList<Var>();
  ArrayList<Var> keepOpenVars=new ArrayList<Var>();
  VariableUsageInfo bodyVU=loop.getBody().checkedGetVariableUsage();
  summariseBranchVariableUsage(context,Arrays.asList(bodyVU),usedVariables,keepOpenVars);
  FunctionContext fc=context.getFunctionContext();
  int loopNum=fc.getCounterVal("String_Node_Str");
  ArrayList<Var> waitUsedVariables=new ArrayList<Var>(usedVariables);
  waitUsedVariables.addAll(Arrays.asList(start,end,step));
  backend.startWaitStatement("String_Node_Str" + loopNum,Arrays.asList(start,end,step),waitUsedVariables,keepOpenVars,WaitMode.DATA_ONLY,TaskMode.LOCAL);
  Context waitContext=new LocalContext(context);
  Var startVal=varCreator.fetchValueOf(waitContext,start);
  Var endVal=varCreator.fetchValueOf(waitContext,end);
  Var stepVal=varCreator.fetchValueOf(waitContext,step);
  Context bodyContext=loop.setupLoopBodyContext(waitContext);
  Var memberVal=varCreator.createValueOfVar(bodyContext,loop.getMemberVar(),false);
  Var counterVal=loop.getLoopCountVal();
  backend.startRangeLoop("String_Node_Str" + loopNum,memberVal,counterVal,Arg.createVar(startVal),Arg.createVar(endVal),Arg.createVar(stepVal),usedVariables,keepOpenVars,loop.getDesiredUnroll(),loop.getSplitDegree());
  List<Var> waitUsedVars=null;
  if (!loop.isSyncLoop()) {
    waitUsedVars=new ArrayList<Var>(usedVariables);
    waitUsedVars.add(memberVal);
    backend.startWaitStatement("String_Node_Str" + loopNum,Collections.<Var>emptyList(),waitUsedVars,keepOpenVars,WaitMode.TASK_DISPATCH,TaskMode.CONTROL);
  }
  varCreator.initialiseVariable(bodyContext,loop.getMemberVar());
  backend.assignInt(loop.getMemberVar(),Arg.createVar(memberVal));
  if (loop.getCountVarName() != null) {
    Var loopCountVar=varCreator.createVariable(bodyContext,Types.F_INT,loop.getCountVarName(),VarStorage.STACK,DefType.LOCAL_USER,null);
    backend.assignInt(loopCountVar,Arg.createVar(counterVal));
  }
  block(bodyContext,loop.getBody());
  if (!loop.isSyncLoop()) {
    backend.endWaitStatement(waitUsedVars,keepOpenVars);
  }
  backend.endRangeLoop(usedVariables,keepOpenVars,loop.getSplitDegree());
  backend.endWaitStatement(usedVariables,keepOpenVars);
}

Fixed version (ground truth):
/** 
 * Handle the special case of a foreach loop where we are looping over range specified by two or three integer parameters
 * @param context
 * @param loop
 * @throws UserException
 * @throws UndefinedTypeException
 */
private void foreachRange(Context context,ForeachLoop loop) throws UserException {
  ArrayRange range=ArrayRange.fromAST(context,loop.getArrayVarTree());
  range.typeCheck(context);
  Var start=exprWalker.eval(context,range.getStart(),Types.F_INT,false,null);
  Var end=exprWalker.eval(context,range.getEnd(),Types.F_INT,false,null);
  Var step;
  if (range.getStep() != null) {
    step=exprWalker.eval(context,range.getStep(),Types.F_INT,false,null);
  }
 else {
    step=varCreator.createTmp(context,Types.F_INT);
    backend.assignInt(step,Arg.createIntLit(1));
  }
  ArrayList<Var> usedVariables=new ArrayList<Var>();
  ArrayList<Var> keepOpenVars=new ArrayList<Var>();
  VariableUsageInfo bodyVU=loop.getBody().checkedGetVariableUsage();
  summariseBranchVariableUsage(context,Arrays.asList(bodyVU),usedVariables,keepOpenVars);
  FunctionContext fc=context.getFunctionContext();
  int loopNum=fc.getCounterVal("String_Node_Str");
  ArrayList<Var> waitUsedVariables=new ArrayList<Var>(usedVariables);
  waitUsedVariables.addAll(Arrays.asList(start,end,step));
  backend.startWaitStatement(fc.getFunctionName() + "String_Node_Str" + loopNum,Arrays.asList(start,end,step),waitUsedVariables,keepOpenVars,WaitMode.DATA_ONLY,TaskMode.LOCAL);
  Context waitContext=new LocalContext(context);
  Var startVal=varCreator.fetchValueOf(waitContext,start);
  Var endVal=varCreator.fetchValueOf(waitContext,end);
  Var stepVal=varCreator.fetchValueOf(waitContext,step);
  Context bodyContext=loop.setupLoopBodyContext(waitContext);
  Var memberVal=varCreator.createValueOfVar(bodyContext,loop.getMemberVar(),false);
  Var counterVal=loop.getLoopCountVal();
  backend.startRangeLoop(fc.getFunctionName() + "String_Node_Str" + loopNum,memberVal,counterVal,Arg.createVar(startVal),Arg.createVar(endVal),Arg.createVar(stepVal),usedVariables,keepOpenVars,loop.getDesiredUnroll(),loop.getSplitDegree());
  List<Var> waitUsedVars=null;
  if (!loop.isSyncLoop()) {
    waitUsedVars=new ArrayList<Var>(usedVariables);
    waitUsedVars.add(memberVal);
    backend.startWaitStatement(fc.getFunctionName() + "String_Node_Str" + loopNum,Collections.<Var>emptyList(),waitUsedVars,keepOpenVars,WaitMode.TASK_DISPATCH,TaskMode.CONTROL);
  }
  varCreator.initialiseVariable(bodyContext,loop.getMemberVar());
  backend.assignInt(loop.getMemberVar(),Arg.createVar(memberVal));
  if (loop.getCountVarName() != null) {
    Var loopCountVar=varCreator.createVariable(bodyContext,Types.F_INT,loop.getCountVarName(),VarStorage.STACK,DefType.LOCAL_USER,null);
    backend.assignInt(loopCountVar,Arg.createVar(counterVal));
  }
  block(bodyContext,loop.getBody());
  if (!loop.isSyncLoop()) {
    backend.endWaitStatement(waitUsedVars,keepOpenVars);
  }
  backend.endRangeLoop(usedVariables,keepOpenVars,loop.getSplitDegree());
  backend.endWaitStatement(usedVariables,keepOpenVars);
}