Program: 80481
Project: 80481
Buggy source:
private List<JcQuery> buildQueriesInt(List<ClausesPerType> clausesPerTypeList,QueryContext context,ClauseBuilderContext cbContext){
  List<JcQuery> ret=new ArrayList<JcQuery>();
  int queryIndex=-1;
  int startIdx=0;
  while (startIdx < clausesPerTypeList.size()) {
    queryIndex++;
    List<IClause> clauses=new ArrayList<IClause>();
    List<ClausesPerType> toReturn=new ArrayList<ClausesPerType>();
    List<IClause> withClauses=null;
    boolean needAddDependencies=startIdx > 0;
    boolean needWith=false;
    int i;
    for (i=startIdx; i < clausesPerTypeList.size(); i++) {
      ClausesPerType cpt=clausesPerTypeList.get(i);
      if (cpt.valid) {
        boolean startNewQueryAfterThis=false;
        if (!context.execCount) {
          if (withClauses == null)           withClauses=new ArrayList<IClause>();
          if (this.clauseBuilder.buildAddWithClause(cpt,withClauses))           needWith=true;
          if (cpt.needSkipsLimits()) {
            if (i > startIdx) {
              startIdx=i;
              break;
            }
            needWith=true;
            withClauses.add(WHERE.NOT().valueOf(cpt.node).IS_NULL());
            startIdx=i + 1;
            startNewQueryAfterThis=true;
          }
        }
        if (cpt.startCountSelectXpr) {
          if (i > startIdx) {
            startIdx=i;
            break;
          }
        }
        if (needAddDependencies) {
          cpt.addDependencyClauses(clauses);
          needAddDependencies=false;
        }
        clauses.addAll(cpt.getClauses());
        if (APIAccess.isPartOfReturn(cpt.domainObjectMatch)) {
          toReturn.add(cpt);
          QueryContext.ResultsPerType resPerType=context.addFor(cpt,context.execCount);
          context.resultsPerType.add(resPerType);
          resPerType.queryIndex=queryIndex;
        }
        if (startNewQueryAfterThis)         break;
      }
 else       context.addEmptyFor(cpt);
    }
    if (i == clausesPerTypeList.size())     startIdx=i;
    List<IClause> returnClauses=buildReturnClauses(toReturn,context.execCount);
    if (needWith)     clauses.addAll(withClauses);
    clauses.addAll(returnClauses);
    IClause[] clausesArray=clauses.toArray(new IClause[clauses.size()]);
    JcQuery query=new JcQuery();
    query.setClauses(clausesArray);
    ret.add(query);
  }
  return ret;
}

Fixed version (ground truth):
private List<JcQuery> buildQueriesInt(List<ClausesPerType> clausesPerTypeList,QueryContext context,ClauseBuilderContext cbContext){
  List<JcQuery> ret=new ArrayList<JcQuery>();
  int queryIndex=-1;
  int startIdx=0;
  while (startIdx < clausesPerTypeList.size()) {
    queryIndex++;
    List<IClause> clauses=new ArrayList<IClause>();
    List<ClausesPerType> toReturn=new ArrayList<ClausesPerType>();
    List<ClausesPerType> added=new ArrayList<ClausesPerType>();
    List<IClause> withClauses=null;
    boolean needWith=false;
    int i;
    for (i=startIdx; i < clausesPerTypeList.size(); i++) {
      ClausesPerType cpt=clausesPerTypeList.get(i);
      if (cpt.valid) {
        boolean startNewQueryAfterThis=false;
        if (!context.execCount) {
          if (withClauses == null)           withClauses=new ArrayList<IClause>();
          if (this.clauseBuilder.buildAddWithClause(cpt,withClauses))           needWith=true;
          if (cpt.needSkipsLimits()) {
            if (i > startIdx) {
              startIdx=i;
              break;
            }
            needWith=true;
            withClauses.add(WHERE.NOT().valueOf(cpt.node).IS_NULL());
            startIdx=i + 1;
            startNewQueryAfterThis=true;
          }
        }
        if (cpt.startCountSelectXpr) {
          if (i > startIdx) {
            startIdx=i;
            break;
          }
        }
        cpt.addDependencyClauses(clauses,added);
        clauses.addAll(cpt.getClauses());
        if (APIAccess.isPartOfReturn(cpt.domainObjectMatch)) {
          toReturn.add(cpt);
          QueryContext.ResultsPerType resPerType=context.addFor(cpt,context.execCount);
          context.resultsPerType.add(resPerType);
          resPerType.queryIndex=queryIndex;
        }
        if (startNewQueryAfterThis)         break;
      }
 else       context.addEmptyFor(cpt);
      added.add(cpt);
    }
    if (i == clausesPerTypeList.size())     startIdx=i;
    List<IClause> returnClauses=buildReturnClauses(toReturn,context.execCount);
    if (needWith)     clauses.addAll(withClauses);
    clauses.addAll(returnClauses);
    IClause[] clausesArray=clauses.toArray(new IClause[clauses.size()]);
    JcQuery query=new JcQuery();
    query.setClauses(clausesArray);
    ret.add(query);
  }
  return ret;
}