Program: 33953
Project: 33953
Buggy source:
public QueryResponse getQueryResponse(){
  long totalHits=0;
  long returnedHits=0;
  for (  SegmentQueryResult sr : segmentQueryResults) {
    totalHits+=sr.getTotalHits();
    returnedHits+=sr.getReturnedHits();
  }
  QueryResponse.Builder builder=QueryResponse.newBuilder();
  builder.setTotalHits(totalHits);
  resultsSize=Math.min(amount,(int)returnedHits);
  results=new ArrayList<ScoredResult>(resultsSize);
  Map<String,ScoredResult[]> lastIndexResultMap=new HashMap<String,ScoredResult[]>();
  for (  String indexName : indexToSegmentResponseMap.keySet()) {
    int numberOfSegments=usedIndexMap.get(indexName).getNumberOfSegments();
    lastIndexResultMap.put(indexName,new ScoredResult[numberOfSegments]);
  }
  for (  LastIndexResult lir : lastResult.getLastIndexResultList()) {
    ScoredResult[] lastForSegmentArr=lastIndexResultMap.get(lir.getIndexName());
    for (    ScoredResult sr : lir.getLastForSegmentList()) {
      lastForSegmentArr[sr.getSegment()]=sr;
    }
  }
  while (results.size() < resultsSize && !isShort) {
    float maxScore=Float.MIN_VALUE;
    SegmentQueryResult maxResult=null;
    boolean anyEmpty=false;
    for (    SegmentQueryResult sr : segmentQueryResults) {
      if (!sr.hasNext() && sr.moreAvailable()) {
        anyEmpty=true;
        break;
      }
    }
    if (!anyEmpty) {
      for (      SegmentQueryResult sr : segmentQueryResults) {
        if (sr.hasNext()) {
          ScoredResult sd=sr.previewNext();
          if (sd != null) {
            if (sd.getScore() > maxScore) {
              maxScore=sd.getScore();
              maxResult=sr;
            }
 else             if (sr.getIndex() < maxResult.getIndex()) {
              if (maxResult.getIndexName().equals(sd.getIndexName())) {
                double segmentTolerance=usedIndexMap.get(maxResult.getIndexName()).getSegmentTolerance();
                double diff=(Math.abs(sd.getScore() - maxScore));
                if (diff < segmentTolerance) {
                  maxScore=sd.getScore();
                  maxResult=sr;
                }
              }
            }
          }
        }
      }
    }
    if (maxResult != null) {
      ScoredResult max=maxResult.next();
      ScoredResult[] lastForSegmentArr=lastIndexResultMap.get(max.getIndexName());
      lastForSegmentArr[max.getSegment()]=max;
      results.add(max);
    }
 else {
      isShort=true;
    }
  }
  builder.addAllResults(results);
  LastResult.Builder newLastResultBuilder=LastResult.newBuilder();
  for (  String indexName : lastIndexResultMap.keySet()) {
    ScoredResult[] lastForSegmentArr=lastIndexResultMap.get(indexName);
    int numberOfSegments=usedIndexMap.get(indexName).getNumberOfSegments();
    List<ScoredResult> indexList=new ArrayList<ScoredResult>();
    for (int i=0; i < numberOfSegments; i++) {
      if (lastForSegmentArr[i] != null) {
        indexList.add(lastForSegmentArr[i]);
      }
    }
    if (!indexList.isEmpty()) {
      LastIndexResult lastIndexResult=LastIndexResult.newBuilder().addAllLastForSegment(indexList).setIndexName(indexName).build();
      newLastResultBuilder.addLastIndexResult(lastIndexResult);
    }
  }
  builder.setLastResult(newLastResultBuilder.build());
  return builder.build();
}

Fixed version (ground truth):
public QueryResponse getQueryResponse(){
  long totalHits=0;
  long returnedHits=0;
  for (  SegmentQueryResult sr : segmentQueryResults) {
    totalHits+=sr.getTotalHits();
    returnedHits+=sr.getReturnedHits();
  }
  QueryResponse.Builder builder=QueryResponse.newBuilder();
  builder.setTotalHits(totalHits);
  resultsSize=Math.min(amount,(int)returnedHits);
  results=new ArrayList<ScoredResult>(resultsSize);
  Map<String,ScoredResult[]> lastIndexResultMap=new HashMap<String,ScoredResult[]>();
  for (  String indexName : indexToSegmentResponseMap.keySet()) {
    int numberOfSegments=usedIndexMap.get(indexName).getNumberOfSegments();
    lastIndexResultMap.put(indexName,new ScoredResult[numberOfSegments]);
  }
  for (  LastIndexResult lir : lastResult.getLastIndexResultList()) {
    ScoredResult[] lastForSegmentArr=lastIndexResultMap.get(lir.getIndexName());
    for (    ScoredResult sr : lir.getLastForSegmentList()) {
      lastForSegmentArr[sr.getSegment()]=sr;
    }
  }
  while (results.size() < resultsSize && !isShort) {
    float maxScore=Float.MIN_VALUE;
    SegmentQueryResult maxResult=null;
    boolean anyEmpty=false;
    for (    SegmentQueryResult sr : segmentQueryResults) {
      if (!sr.hasNext() && sr.moreAvailable()) {
        anyEmpty=true;
        break;
      }
    }
    if (!anyEmpty) {
      for (      SegmentQueryResult sr : segmentQueryResults) {
        if (sr.hasNext()) {
          ScoredResult sd=sr.previewNext();
          if (sd != null) {
            boolean notNearEnd=((sr.getIndex() + 2) < sr.getReturnedHits());
            if (maxResult == null || ((sd.getScore() > maxScore) && notNearEnd)) {
              maxScore=sd.getScore();
              maxResult=sr;
            }
 else             if (sr.getIndex() < maxResult.getIndex()) {
              if (maxResult.getIndexName().equals(sd.getIndexName())) {
                double segmentTolerance=usedIndexMap.get(maxResult.getIndexName()).getSegmentTolerance();
                double diff=(Math.abs(sd.getScore() - maxScore));
                if (diff < segmentTolerance) {
                  maxScore=sd.getScore();
                  maxResult=sr;
                }
              }
            }
          }
        }
      }
    }
    if (maxResult != null) {
      ScoredResult max=maxResult.next();
      ScoredResult[] lastForSegmentArr=lastIndexResultMap.get(max.getIndexName());
      lastForSegmentArr[max.getSegment()]=max;
      results.add(max);
    }
 else {
      isShort=true;
    }
  }
  builder.addAllResults(results);
  LastResult.Builder newLastResultBuilder=LastResult.newBuilder();
  for (  String indexName : lastIndexResultMap.keySet()) {
    ScoredResult[] lastForSegmentArr=lastIndexResultMap.get(indexName);
    int numberOfSegments=usedIndexMap.get(indexName).getNumberOfSegments();
    List<ScoredResult> indexList=new ArrayList<ScoredResult>();
    for (int i=0; i < numberOfSegments; i++) {
      if (lastForSegmentArr[i] != null) {
        indexList.add(lastForSegmentArr[i]);
      }
    }
    if (!indexList.isEmpty()) {
      LastIndexResult lastIndexResult=LastIndexResult.newBuilder().addAllLastForSegment(indexList).setIndexName(indexName).build();
      newLastResultBuilder.addLastIndexResult(lastIndexResult);
    }
  }
  builder.setLastResult(newLastResultBuilder.build());
  return builder.build();
}