What is the algorithm used in cornerSubPix?

Could someone explain to me how this function works? From the documentation it says that the function uses the fact that the gradient of a the corner should be perpendicular any adjacent point in the nearby area.

In practice how is that solved? Particularly within the tight loop, what is being created here? A matrix? It looks like the gradient of every point in the window is being calculated, but that seems to be different from what the documentation is saying is happening.

What do the variables a, b, c, bb1 and bb2 refer to?