Program: 132819
Project: 132819
Buggy source:
/** 
 * Handle the general foreach loop where we are looping over array
 * @param context
 * @param loop
 * @throws UserException
 * @throws UndefinedTypeException
 */
private void foreachArray(Context context,ForeachLoop loop) throws UserException, UndefinedTypeException {
  Var arrayVar=exprWalker.eval(context,loop.getArrayVarTree(),loop.findArrayType(context),false,null);
  VariableUsageInfo bodyVU=loop.getBody().checkedGetVariableUsage();
  List<Var> writtenVars=new ArrayList<Var>();
  summariseBranchVariableUsage(context,Arrays.asList(bodyVU),writtenVars);
  for (  Var v : writtenVars) {
    if (v.equals(arrayVar)) {
      throw new STCRuntimeError("String_Node_Str" + v + "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str");
    }
  }
  FunctionContext fc=context.getFunctionContext();
  int loopNum=fc.getCounterVal("String_Node_Str");
  Var realArray;
  Context outsideLoopContext;
  if (Types.isContainerRef(arrayVar)) {
    backend.startWaitStatement(fc.getFunctionName() + "String_Node_Str" + loopNum,VarRepr.backendVar(arrayVar).asList(),WaitMode.WAIT_ONLY,false,false,TaskMode.LOCAL_CONTROL);
    outsideLoopContext=new LocalContext(context);
    realArray=varCreator.createTmp(outsideLoopContext,arrayVar.type().memberType(),false,true);
    exprWalker.retrieveRef(realArray,arrayVar);
  }
 else {
    assert(Types.isContainer(arrayVar));
    realArray=arrayVar;
    outsideLoopContext=context;
  }
  backend.startWaitStatement(fc.getFunctionName() + "String_Node_Str" + loopNum,VarRepr.backendVar(realArray).asList(),WaitMode.WAIT_ONLY,false,false,TaskMode.LOCAL_CONTROL);
  loop.setupLoopBodyContext(outsideLoopContext,false,false);
  Context loopBodyContext=loop.getBodyContext();
  Var loopCountVal=loop.getLoopCountVal();
  Var backendMemberVal=VarRepr.backendVar(loop.getMemberVal());
  backend.startForeachLoop(fc.getFunctionName() + "String_Node_Str" + loopNum,VarRepr.backendVar(realArray),backendMemberVal,loopCountVal == null ? null : VarRepr.backendVar(loopCountVal),loop.getSplitDegree(),loop.getLeafDegree(),true);
  varCreator.initialiseVariable(loopBodyContext,loop.getMemberVar());
  backend.assignScalar(VarRepr.backendVar(loop.getMemberVar()),backendMemberVal.asArg());
  if (!loop.isSyncLoop()) {
    backend.startWaitStatement(fc.getFunctionName() + "String_Node_Str" + loopNum,Var.NONE,WaitMode.TASK_DISPATCH,false,false,TaskMode.CONTROL);
  }
  if (loop.getCountVarName() != null) {
    Var loopCountVar=varCreator.createVariable(loop.getBodyContext(),loop.createCountVar(context));
    exprWalker.assign(loopCountVar,loop.getLoopCountVal().asArg());
  }
  block(loopBodyContext,loop.getBody());
  if (!loop.isSyncLoop()) {
    backend.endWaitStatement();
  }
  backend.endForeachLoop();
  backend.endWaitStatement();
  if (Types.isContainerRef(arrayVar.type())) {
    backend.endWaitStatement();
  }
}

Fixed version (ground truth):
/** 
 * Handle the general foreach loop where we are looping over array
 * @param context
 * @param loop
 * @throws UserException
 * @throws UndefinedTypeException
 */
private void foreachArray(Context context,ForeachLoop loop) throws UserException, UndefinedTypeException {
  Var arrayVar=exprWalker.eval(context,loop.getArrayVarTree(),loop.findArrayType(context),false,null);
  VariableUsageInfo bodyVU=loop.getBody().checkedGetVariableUsage();
  List<Var> writtenVars=new ArrayList<Var>();
  summariseBranchVariableUsage(context,Arrays.asList(bodyVU),writtenVars);
  for (  Var v : writtenVars) {
    if (v.equals(arrayVar)) {
      throw new STCRuntimeError("String_Node_Str" + v + "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str");
    }
  }
  FunctionContext fc=context.getFunctionContext();
  int loopNum=fc.getCounterVal("String_Node_Str");
  Var realArray;
  Context outsideLoopContext;
  if (Types.isContainerRef(arrayVar)) {
    backend.startWaitStatement(fc.getFunctionName() + "String_Node_Str" + loopNum,VarRepr.backendVar(arrayVar).asList(),WaitMode.WAIT_ONLY,false,false,TaskMode.LOCAL_CONTROL);
    outsideLoopContext=new LocalContext(context);
    realArray=varCreator.createTmp(outsideLoopContext,arrayVar.type().memberType(),false,true);
    exprWalker.retrieveRef(realArray,arrayVar);
  }
 else {
    assert(Types.isContainer(arrayVar));
    realArray=arrayVar;
    outsideLoopContext=context;
  }
  backend.startWaitStatement(fc.getFunctionName() + "String_Node_Str" + loopNum,VarRepr.backendVar(realArray).asList(),WaitMode.WAIT_ONLY,false,false,TaskMode.LOCAL_CONTROL);
  loop.setupLoopBodyContext(outsideLoopContext,false,false);
  Context loopBodyContext=loop.getBodyContext();
  Var loopCountVal=loop.getLoopCountVal();
  boolean memberIsVal=(loop.getMemberVal() != null);
  Var backendIterVar=null;
  if (memberIsVal) {
    backendIterVar=VarRepr.backendVar(loop.getMemberVal());
  }
 else {
    backendIterVar=VarRepr.backendVar(loop.getMemberVar());
  }
  backend.startForeachLoop(fc.getFunctionName() + "String_Node_Str" + loopNum,VarRepr.backendVar(realArray),backendIterVar,loopCountVal == null ? null : VarRepr.backendVar(loopCountVal),loop.getSplitDegree(),loop.getLeafDegree(),true);
  if (memberIsVal) {
    varCreator.initialiseVariable(loopBodyContext,loop.getMemberVar());
    exprWalker.assign(VarRepr.backendVar(loop.getMemberVar()),backendIterVar.asArg());
  }
  if (!loop.isSyncLoop()) {
    backend.startWaitStatement(fc.getFunctionName() + "String_Node_Str" + loopNum,Var.NONE,WaitMode.TASK_DISPATCH,false,false,TaskMode.CONTROL);
  }
  if (loop.getCountVarName() != null) {
    Var loopCountVar=varCreator.createVariable(loop.getBodyContext(),loop.createCountVar(context));
    exprWalker.assign(loopCountVar,loop.getLoopCountVal().asArg());
  }
  block(loopBodyContext,loop.getBody());
  if (!loop.isSyncLoop()) {
    backend.endWaitStatement();
  }
  backend.endForeachLoop();
  backend.endWaitStatement();
  if (Types.isContainerRef(arrayVar.type())) {
    backend.endWaitStatement();
  }
}