Program: 101491
Project: 101491
Buggy source:
/** 
 * Convert us to a binary representation. There are some distinctly endianist happenings here, but that is OK because we are reading the stuff we write here just below.
 * @param ref The Passage to convert
 * @return a byte array
 */
static byte[] toBinaryRepresentation(Passage ref){
  int maxOrdinal=ref.getVersification().maximumOrdinal();
  int verses=ref.countVerses();
  int ranges=ref.countRanges(RestrictionType.NONE);
  int bitwise_size=maxOrdinal / 8;
  int ranged_size=(ranges * 4) + 1;
  int distinct_size=(verses * 2) + 1;
  if (bitwise_size <= ranged_size && bitwise_size <= distinct_size) {
    int array_size=binarySize(AbstractPassage.METHOD_COUNT) + (maxOrdinal / 8) + 1;
    byte[] buffer=new byte[array_size];
    int index=0;
    index+=toBinary(buffer,index,AbstractPassage.BITWISE,AbstractPassage.METHOD_COUNT);
    for (    Key aKey : ref) {
      Verse verse=(Verse)aKey;
      int ord=verse.getOrdinal();
      int idx0=(ord / 8) + index;
      int bit=(ord % 8) - 1;
      buffer[idx0]|=1 << bit;
    }
    return buffer;
  }
 else   if (distinct_size <= ranged_size) {
    int array_size=binarySize(AbstractPassage.METHOD_COUNT) + binarySize(maxOrdinal) + (verses * binarySize(maxOrdinal));
    byte[] buffer=new byte[array_size];
    int index=0;
    index+=toBinary(buffer,index,AbstractPassage.DISTINCT,AbstractPassage.METHOD_COUNT);
    index+=toBinary(buffer,index,verses,maxOrdinal);
    for (    Key aKey : ref) {
      Verse verse=(Verse)aKey;
      int ord=verse.getOrdinal();
      index+=toBinary(buffer,index,ord,maxOrdinal);
    }
    return buffer;
  }
 else {
    int array_size=binarySize(AbstractPassage.METHOD_COUNT) + binarySize(maxOrdinal / 2) + (2 * ranges * binarySize(maxOrdinal));
    byte[] buffer=new byte[array_size];
    int index=0;
    index+=toBinary(buffer,index,AbstractPassage.RANGED,AbstractPassage.METHOD_COUNT);
    index+=toBinary(buffer,index,ranges,maxOrdinal / 2);
    Iterator<Key> it=ref.rangeIterator(RestrictionType.NONE);
    while (it.hasNext()) {
      VerseRange range=(VerseRange)it.next();
      index+=toBinary(buffer,index,range.getStart().getOrdinal(),maxOrdinal);
      index+=toBinary(buffer,index,range.getCardinality(),maxOrdinal);
    }
    return buffer;
  }
}

Fixed version (ground truth):
/** 
 * Convert us to a binary representation. There are some distinctly endianist happenings here, but that is OK because we are reading the stuff we write here just below.
 * @param ref The Passage to convert
 * @return a byte array
 */
static byte[] toBinaryRepresentation(Passage ref){
  Versification v11n=ref.getVersification();
  int maxOrdinal=v11n.maximumOrdinal();
  int verses=ref.countVerses();
  int ranges=ref.countRanges(RestrictionType.NONE);
  int bitwise_size=maxOrdinal / 8;
  int ranged_size=(ranges * 4) + 1;
  int distinct_size=(verses * 2) + 1;
  if (bitwise_size <= ranged_size && bitwise_size <= distinct_size) {
    int array_size=binarySize(AbstractPassage.METHOD_COUNT) + (maxOrdinal / 8) + 1;
    byte[] buffer=new byte[array_size];
    int index=0;
    index+=toBinary(buffer,index,AbstractPassage.BITWISE,AbstractPassage.METHOD_COUNT);
    for (    Key aKey : ref) {
      Verse verse=(Verse)aKey;
      int ord=v11n.getOrdinal(verse);
      int idx0=(ord / 8) + index;
      int bit=(ord % 8) - 1;
      buffer[idx0]|=1 << bit;
    }
    return buffer;
  }
 else   if (distinct_size <= ranged_size) {
    int array_size=binarySize(AbstractPassage.METHOD_COUNT) + binarySize(maxOrdinal) + (verses * binarySize(maxOrdinal));
    byte[] buffer=new byte[array_size];
    int index=0;
    index+=toBinary(buffer,index,AbstractPassage.DISTINCT,AbstractPassage.METHOD_COUNT);
    index+=toBinary(buffer,index,verses,maxOrdinal);
    for (    Key aKey : ref) {
      Verse verse=(Verse)aKey;
      int ord=v11n.getOrdinal(verse);
      index+=toBinary(buffer,index,ord,maxOrdinal);
    }
    return buffer;
  }
 else {
    int array_size=binarySize(AbstractPassage.METHOD_COUNT) + binarySize(maxOrdinal / 2) + (2 * ranges * binarySize(maxOrdinal));
    byte[] buffer=new byte[array_size];
    int index=0;
    index+=toBinary(buffer,index,AbstractPassage.RANGED,AbstractPassage.METHOD_COUNT);
    index+=toBinary(buffer,index,ranges,maxOrdinal / 2);
    Iterator<Key> it=ref.rangeIterator(RestrictionType.NONE);
    while (it.hasNext()) {
      VerseRange range=(VerseRange)it.next();
      index+=toBinary(buffer,index,v11n.getOrdinal(range.getStart()),maxOrdinal);
      index+=toBinary(buffer,index,range.getCardinality(),maxOrdinal);
    }
    return buffer;
  }
}