According to this answer, blocks in blk*.dat files and rev*.dat files don't necessarily appear in the same order, nor are either files necessarily in monotonic order, and blocks not part of the active chain may or may not have undo data available. The only guarantee is that the undo data (if available) for a block in a blk*.dat file will be stored in its matching rev*.dat file.
Given these limitations, how can one reliably collate the block entries in a blk*.dat, rev*.dat pair?