while (pos < max_len - 1)
pos last position is max_len - 2. It doenst go to the last position in buffer[max_len]