Program: 132680
Project: 132680
Buggy source:
/** 
 * Helper function to insert element into bag, selecting appropriate backend implementation.
 * @param context
 * @param bag
 * @param elem
 * @return
 * @throws UserException
 * @throws UndefinedTypeException
 */
private Var backendBagAppend(Context context,Var bag,Var elem) throws UserException {
  Var stmtResultVar=elem;
  Type elemType=Types.containerElemType(bag);
  boolean bagRef=Types.isBagRef(bag);
  boolean elemRef=Types.isRefTo(elem,elemType);
  boolean openWait1=bagRef || elemRef;
  if (openWait1) {
    String waitName=context.getFunctionContext().constructName("String_Node_Str");
    List<Var> waitVars=new ArrayList<Var>(2);
    if (bagRef) {
      waitVars.add(bag);
    }
    if (elemRef) {
      waitVars.add(elem);
    }
    backend.startWaitStatement(waitName,VarRepr.backendVars(waitVars),WaitMode.WAIT_ONLY,false,false,ExecTarget.nonDispatchedAny());
    if (bagRef) {
      Var derefedBag=varCreator.createTmpAlias(context,Types.retrievedType(bag));
      exprWalker.retrieveRef(VarRepr.backendVar(derefedBag),VarRepr.backendVar(bag),true);
      bag=derefedBag;
    }
    if (elemRef) {
      stmtResultVar=elem;
      Var derefedElem=varCreator.createTmpAlias(context,elemType);
      exprWalker.retrieveRef(VarRepr.backendVar(derefedElem),VarRepr.backendVar(elem),true);
      elem=derefedElem;
    }
  }
  Var elemVal;
  boolean openWait2=!VarRepr.storeRefInContainer(elem);
  if (openWait2) {
    String waitName=context.getFunctionContext().constructName("String_Node_Str");
    backend.startWaitStatement(waitName,VarRepr.backendVars(elem),WaitMode.WAIT_ONLY,false,false,ExecTarget.nonDispatchedAny());
    elemVal=exprWalker.retrieveToVar(context,elem);
  }
 else {
    elemVal=elem;
  }
  backend.bagInsert(VarRepr.backendVar(bag),VarRepr.backendArg(elemVal));
  if (openWait2) {
    backend.endWaitStatement();
  }
  if (openWait1) {
    backend.endWaitStatement();
  }
  return stmtResultVar;
}

Fixed version (ground truth):
/** 
 * Helper function to insert element into bag, selecting appropriate backend implementation.
 * @param context
 * @param bag
 * @param elem
 * @return
 * @throws UserException
 * @throws UndefinedTypeException
 */
private Var backendBagAppend(Context context,Var bag,Var elem) throws UserException {
  Var stmtResultVar=elem;
  Type elemType=Types.containerElemType(bag);
  boolean bagRef=Types.isBagRef(bag);
  assert(!bagRef || Types.isRef(bag,true));
  boolean elemRef=Types.isRefTo(elem,elemType);
  boolean openWait1=bagRef || elemRef;
  if (openWait1) {
    String waitName=context.getFunctionContext().constructName("String_Node_Str");
    List<Var> waitVars=new ArrayList<Var>(2);
    if (bagRef) {
      waitVars.add(bag);
    }
    if (elemRef) {
      waitVars.add(elem);
    }
    backend.startWaitStatement(waitName,VarRepr.backendVars(waitVars),WaitMode.WAIT_ONLY,false,false,ExecTarget.nonDispatchedAny());
    if (bagRef) {
      Var derefedBag=varCreator.createTmpAlias(context,Types.retrievedType(bag));
      exprWalker.retrieveRef(VarRepr.backendVar(derefedBag),VarRepr.backendVar(bag),true);
      bag=derefedBag;
    }
    if (elemRef) {
      stmtResultVar=elem;
      Var derefedElem=varCreator.createTmpAlias(context,elemType);
      exprWalker.retrieveRef(VarRepr.backendVar(derefedElem),VarRepr.backendVar(elem),false);
      elem=derefedElem;
    }
  }
  Var elemVal;
  boolean openWait2=!VarRepr.storeRefInContainer(elem);
  if (openWait2) {
    String waitName=context.getFunctionContext().constructName("String_Node_Str");
    backend.startWaitStatement(waitName,VarRepr.backendVars(elem),WaitMode.WAIT_ONLY,false,false,ExecTarget.nonDispatchedAny());
    elemVal=exprWalker.retrieveToVar(context,elem);
  }
 else {
    elemVal=elem;
  }
  backend.bagInsert(VarRepr.backendVar(bag),VarRepr.backendArg(elemVal));
  if (openWait2) {
    backend.endWaitStatement();
  }
  if (openWait1) {
    backend.endWaitStatement();
  }
  return stmtResultVar;
}