Program: 27971
Project: 27971
Buggy source:
/** 
 * Remove useless switches and cases.
 */
private Node tryOptimizeSwitch(Node n){
  Preconditions.checkState(n.isSwitch());
  Node defaultCase=tryOptimizeDefaultCase(n);
  if (defaultCase == null) {
    Node cond=n.getFirstChild(), prev=null, next=null, cur;
    for (cur=cond.getNext(); cur != null; cur=next) {
      next=cur.getNext();
      if (!mayHaveSideEffects(cur.getFirstChild()) && isUselessCase(cur,prev)) {
        removeCase(n,cur);
      }
 else {
        prev=cur;
      }
    }
    if (NodeUtil.isLiteralValue(cond,false)) {
      Node caseLabel;
      TernaryValue caseMatches=TernaryValue.TRUE;
      for (cur=cond.getNext(); cur != null; cur=next) {
        next=cur.getNext();
        caseLabel=cur.getFirstChild();
        caseMatches=PeepholeFoldConstants.evaluateComparison(Token.SHEQ,cond,caseLabel);
        if (caseMatches == TernaryValue.TRUE) {
          break;
        }
 else         if (caseMatches == TernaryValue.UNKNOWN) {
          break;
        }
 else {
          n.removeChild(cur);
          reportCodeChange();
        }
      }
      if (caseMatches != TernaryValue.UNKNOWN) {
        Node block, lastStm;
        while (cur != null) {
          block=cur.getLastChild();
          lastStm=block.getLastChild();
          cur=cur.getNext();
          if (lastStm.isBreak()) {
            block.removeChild(lastStm);
            reportCodeChange();
            break;
          }
        }
        for (; cur != null; cur=next) {
          next=cur.getNext();
          n.removeChild(cur);
          reportCodeChange();
        }
        cur=cond.getNext();
        if (cur != null && cur.getNext() == null) {
          block=cur.getLastChild();
          if (!(NodeUtil.containsType(block,Token.BREAK,NodeUtil.MATCH_NOT_FUNCTION))) {
            cur.removeChild(block);
            n.getParent().replaceChild(n,block);
            reportCodeChange();
            return block;
          }
        }
      }
    }
  }
  if (n.hasOneChild()) {
    Node condition=n.removeFirstChild();
    Node replacement=IR.exprResult(condition).srcref(n);
    n.getParent().replaceChild(n,replacement);
    reportCodeChange();
    return replacement;
  }
  return null;
}

Fixed version (ground truth):
/** 
 * Remove useless switches and cases.
 */
private Node tryOptimizeSwitch(Node n){
  Preconditions.checkState(n.isSwitch());
  Node defaultCase=tryOptimizeDefaultCase(n);
  if (defaultCase == null) {
    Node cond=n.getFirstChild(), prev=null, next=null, cur;
    for (cur=cond.getNext(); cur != null; cur=next) {
      next=cur.getNext();
      if (!mayHaveSideEffects(cur.getFirstChild()) && isUselessCase(cur,prev)) {
        removeCase(n,cur);
      }
 else {
        prev=cur;
      }
    }
    if (NodeUtil.isLiteralValue(cond,false)) {
      Node caseLabel;
      TernaryValue caseMatches=TernaryValue.TRUE;
      for (cur=cond.getNext(); cur != null; cur=next) {
        next=cur.getNext();
        caseLabel=cur.getFirstChild();
        caseMatches=PeepholeFoldConstants.evaluateComparison(Token.SHEQ,cond,caseLabel);
        if (caseMatches == TernaryValue.TRUE) {
          break;
        }
 else         if (caseMatches == TernaryValue.UNKNOWN) {
          break;
        }
 else {
          n.removeChild(cur);
          reportCodeChange();
        }
      }
      if (caseMatches != TernaryValue.UNKNOWN) {
        Node block, lastStm;
        while (cur != null) {
          block=cur.getLastChild();
          lastStm=block.getLastChild();
          cur=cur.getNext();
          if (lastStm != null && lastStm.isBreak()) {
            block.removeChild(lastStm);
            reportCodeChange();
            break;
          }
        }
        for (; cur != null; cur=next) {
          next=cur.getNext();
          n.removeChild(cur);
          reportCodeChange();
        }
        cur=cond.getNext();
        if (cur != null && cur.getNext() == null) {
          block=cur.getLastChild();
          if (!(NodeUtil.containsType(block,Token.BREAK,NodeUtil.MATCH_NOT_FUNCTION))) {
            cur.removeChild(block);
            n.getParent().replaceChild(n,block);
            reportCodeChange();
            return block;
          }
        }
      }
    }
  }
  if (n.hasOneChild()) {
    Node condition=n.removeFirstChild();
    Node replacement=IR.exprResult(condition).srcref(n);
    n.getParent().replaceChild(n,replacement);
    reportCodeChange();
    return replacement;
  }
  return null;
}