Program: 167060
Project: 167060
Buggy source:
protected void combineSteps(Traversal.Admin<?,?> traversal,List<Step> steps,ListIterator<Step> stepIterator){
  SqlgStep sqlgStep=null;
  Step previous=null;
  ReplacedStep<?,?> lastReplacedStep=null;
  int pathCount=0;
  boolean alreadyReplacedGraphStep=false;
  boolean repeatStepAdded=false;
  int repeatStepsAdded=0;
  while (stepIterator.hasNext()) {
    Step step=stepIterator.next();
    if (step instanceof RepeatStep) {
      if (unoptimizableRepeat(steps,stepIterator.previousIndex())) {
        this.logger.debug("String_Node_Str" + traversal.toString() + "String_Node_Str"+ ExceptionUtils.getStackTrace(new Throwable()));
        return;
      }
      repeatStepsAdded=0;
      repeatStepAdded=false;
      RepeatStep repeatStep=(RepeatStep)step;
      List<Traversal.Admin<?,?>> repeatTraversals=repeatStep.getGlobalChildren();
      Traversal.Admin admin=repeatTraversals.get(0);
      List<Step> repeatStepInternalVertexSteps=admin.getSteps();
      LoopTraversal loopTraversal;
      long numberOfLoops;
      loopTraversal=(LoopTraversal)repeatStep.getUntilTraversal();
      numberOfLoops=loopTraversal.getMaxLoops();
      for (int i=0; i < numberOfLoops; i++) {
        for (        Step internalVertexStep : repeatStepInternalVertexSteps) {
          if (internalVertexStep instanceof RepeatStep.RepeatEndStep) {
            break;
          }
          internalVertexStep.setPreviousStep(repeatStep.getPreviousStep());
          stepIterator.add(internalVertexStep);
          stepIterator.previous();
          stepIterator.next();
          repeatStepAdded=true;
          repeatStepsAdded++;
        }
      }
      traversal.removeStep(repeatStep);
      for (int i=0; i < repeatStepsAdded; i++) {
        stepIterator.previous();
      }
    }
 else {
      if (isReplaceableStep(step.getClass(),alreadyReplacedGraphStep)) {
        boolean emit=false;
        boolean emitFirst=false;
        boolean untilFirst=false;
        if (repeatStepsAdded > 0) {
          repeatStepsAdded--;
          RepeatStep repeatStep=(RepeatStep)step.getTraversal().getParent();
          emit=repeatStep.getEmitTraversal() != null;
          emitFirst=repeatStep.emitFirst;
          untilFirst=repeatStep.untilFirst;
        }
        pathCount++;
        ReplacedStep replacedStep=ReplacedStep.from(this.sqlgGraph.getSchemaManager(),(AbstractStep)step,pathCount);
        if (emit) {
          List<ReplacedStep> previousReplacedSteps=sqlgStep.getReplacedSteps();
          ReplacedStep previousReplacedStep;
          if (emitFirst) {
            previousReplacedStep=previousReplacedSteps.get(previousReplacedSteps.size() - 1);
            pathCount--;
          }
 else {
            previousReplacedStep=replacedStep;
          }
          previousReplacedStep.setEmit(true);
          previousReplacedStep.setUntilFirst(untilFirst);
          previousReplacedStep.setEmitFirst(emitFirst);
          previousReplacedStep.addLabel((pathCount) + BaseSqlgStrategy.EMIT_LABEL_SUFFIX + BaseSqlgStrategy.SQLG_PATH_FAKE_LABEL);
          previousReplacedStep.getLabels().remove((pathCount) + BaseSqlgStrategy.PATH_LABEL_SUFFIX + BaseSqlgStrategy.SQLG_PATH_FAKE_LABEL);
          if (emitFirst) {
            pathCount++;
          }
        }
        if (replacedStep.getLabels().isEmpty()) {
          boolean precedesPathStep=precedesPathOrTreeStep(steps,stepIterator.nextIndex());
          if (precedesPathStep) {
            replacedStep.addLabel(pathCount + BaseSqlgStrategy.PATH_LABEL_SUFFIX + BaseSqlgStrategy.SQLG_PATH_FAKE_LABEL);
          }
        }
        if (previous == null) {
          sqlgStep=constructSqlgStep(traversal,step);
          alreadyReplacedGraphStep=alreadyReplacedGraphStep || step instanceof GraphStep;
          sqlgStep.addReplacedStep(replacedStep);
          handleFirstReplacedStep(step,sqlgStep,traversal);
          collectHasSteps(stepIterator,traversal,replacedStep,pathCount);
        }
 else {
          sqlgStep.addReplacedStep(replacedStep);
          if (!repeatStepAdded) {
            traversal.removeStep(step);
          }
          collectHasSteps(stepIterator,traversal,replacedStep,pathCount);
        }
        previous=step;
        lastReplacedStep=replacedStep;
      }
 else {
        if (lastReplacedStep != null && steps.stream().anyMatch(s -> s instanceof OrderGlobalStep)) {
          doLastEntry(step,stepIterator,traversal,lastReplacedStep,sqlgStep);
        }
        break;
      }
    }
  }
}

Fixed version (ground truth):
protected void combineSteps(Traversal.Admin<?,?> traversal,List<Step> steps,ListIterator<Step> stepIterator){
  SqlgStep sqlgStep=null;
  Step previous=null;
  ReplacedStep<?,?> lastReplacedStep=null;
  int pathCount=0;
  boolean alreadyReplacedGraphStep=false;
  boolean repeatStepAdded=false;
  int repeatStepsAdded=0;
  while (stepIterator.hasNext()) {
    Step step=stepIterator.next();
    if (step instanceof RepeatStep) {
      if (unoptimizableRepeat(steps,stepIterator.previousIndex())) {
        this.logger.debug("String_Node_Str" + traversal.toString() + "String_Node_Str"+ ExceptionUtils.getStackTrace(new Throwable()));
        return;
      }
      repeatStepsAdded=0;
      repeatStepAdded=false;
      RepeatStep repeatStep=(RepeatStep)step;
      List<Traversal.Admin<?,?>> repeatTraversals=repeatStep.getGlobalChildren();
      Traversal.Admin admin=repeatTraversals.get(0);
      List<Step> repeatStepInternalVertexSteps=admin.getSteps();
      LoopTraversal loopTraversal;
      long numberOfLoops;
      loopTraversal=(LoopTraversal)repeatStep.getUntilTraversal();
      numberOfLoops=loopTraversal.getMaxLoops();
      for (int i=0; i < numberOfLoops; i++) {
        for (        Step internalVertexStep : repeatStepInternalVertexSteps) {
          if (internalVertexStep instanceof RepeatStep.RepeatEndStep) {
            break;
          }
          internalVertexStep.setPreviousStep(repeatStep.getPreviousStep());
          stepIterator.add(internalVertexStep);
          stepIterator.previous();
          stepIterator.next();
          repeatStepAdded=true;
          repeatStepsAdded++;
        }
      }
      traversal.removeStep(repeatStep);
      for (int i=0; i < repeatStepsAdded; i++) {
        stepIterator.previous();
      }
    }
 else {
      if (isReplaceableStep(step.getClass(),alreadyReplacedGraphStep)) {
        boolean emit=false;
        boolean emitFirst=false;
        boolean untilFirst=false;
        if (repeatStepsAdded > 0) {
          repeatStepsAdded--;
          RepeatStep repeatStep=(RepeatStep)step.getTraversal().getParent();
          emit=repeatStep.getEmitTraversal() != null;
          emitFirst=repeatStep.emitFirst;
          untilFirst=repeatStep.untilFirst;
        }
 else {
          repeatStepAdded=false;
        }
        pathCount++;
        ReplacedStep replacedStep=ReplacedStep.from(this.sqlgGraph.getSchemaManager(),(AbstractStep)step,pathCount);
        if (emit) {
          List<ReplacedStep> previousReplacedSteps=sqlgStep.getReplacedSteps();
          ReplacedStep previousReplacedStep;
          if (emitFirst) {
            previousReplacedStep=previousReplacedSteps.get(previousReplacedSteps.size() - 1);
            pathCount--;
          }
 else {
            previousReplacedStep=replacedStep;
          }
          previousReplacedStep.setEmit(true);
          previousReplacedStep.setUntilFirst(untilFirst);
          previousReplacedStep.setEmitFirst(emitFirst);
          previousReplacedStep.addLabel((pathCount) + BaseSqlgStrategy.EMIT_LABEL_SUFFIX + BaseSqlgStrategy.SQLG_PATH_FAKE_LABEL);
          previousReplacedStep.getLabels().remove((pathCount) + BaseSqlgStrategy.PATH_LABEL_SUFFIX + BaseSqlgStrategy.SQLG_PATH_FAKE_LABEL);
          if (emitFirst) {
            pathCount++;
          }
        }
        if (replacedStep.getLabels().isEmpty()) {
          boolean precedesPathStep=precedesPathOrTreeStep(steps,stepIterator.nextIndex());
          if (precedesPathStep) {
            replacedStep.addLabel(pathCount + BaseSqlgStrategy.PATH_LABEL_SUFFIX + BaseSqlgStrategy.SQLG_PATH_FAKE_LABEL);
          }
        }
        if (previous == null) {
          sqlgStep=constructSqlgStep(traversal,step);
          alreadyReplacedGraphStep=alreadyReplacedGraphStep || step instanceof GraphStep;
          sqlgStep.addReplacedStep(replacedStep);
          handleFirstReplacedStep(step,sqlgStep,traversal);
          collectHasSteps(stepIterator,traversal,replacedStep,pathCount);
        }
 else {
          sqlgStep.addReplacedStep(replacedStep);
          if (!repeatStepAdded) {
            traversal.removeStep(step);
          }
          collectHasSteps(stepIterator,traversal,replacedStep,pathCount);
        }
        previous=step;
        lastReplacedStep=replacedStep;
      }
 else {
        if (lastReplacedStep != null && steps.stream().anyMatch(s -> s instanceof OrderGlobalStep)) {
          doLastEntry(step,stepIterator,traversal,lastReplacedStep,sqlgStep);
        }
        break;
      }
    }
  }
}