Well, the best guide is probably our own code with "top" being the most straightforward example.
The top project would have been an interesting pointer if the source code for top included a call to proc_listallpids. Which is not the case.
No, that's not what's going on. Both of those pids will always be returned by proc_listallpids because they're "special" within the system.
You probably have misread what I wrote. I'm not telling that they won't be returned. As I mentioned, the proc_listallpids API returns a list of the existing pids in a decreasing order. So in theory, the last pid should be 0 or 1 depending on whether you consider that the kernel is a process. But in practice, the API can end up listing pids after pid 0. And it's not just padding. Those are the pids that are invalid.
Sorry for the time you spent explaining why launchd is special, I already knew about all this stuff.