def CompactBlocks(blocks):
  """Compacts adjacent code blocks.

  In many cases 2 adjacent blocks can be merged into one. This allows
  to do some further processing on those blocks.

  Args:
    blocks: [(offset1, size), ...]

  Returns:
    A list with the same structure as the input with adjacent blocks
    merged.  However, the last block (which is always assumed to have
    a zero size) is never merged.  For example, the input
    [(0, 2), (2, 8), (10, 5), (15, 0)]
    will produce the output [(0, 15), (15, 0)].
  """
  if len(blocks) == 1:
    return blocks
  result = [blocks[0]]
  for block in blocks[1:-1]:
    last_start, last_len = result[-1]
    curr_start, curr_len = block
    if last_start + last_len == curr_start:
      result[-1] = last_start, last_len + curr_len
    else:
      result.append(block)
  result.append(blocks[-1])
  return result