I've been trying the find the code in Vanitygen that does checks whether the generated address starts with the expected pattern.
Vanitygen repo: https://github.com/samr7/vanitygen
What I currently know:
vg_output_match_console is called when the generated address starts with the expected pattern.
vg_exec_context_calc_address generates the address as a byte array, which is probably used.
get_prefix_ranges finds the range of the bignum of the address we want to generate, such that all bignums in that range start with the expected prefix.
vg_encode_address generates the address in Base58, however is run only at the end.
vg_prefix_avl_search seems to be an important part (that does what?)