Extensive documentation is provided in the form of comments.
The failure function tells exactly how many characters head start you have. The code below was translated into C from psuedo code given on page 151 of Aho, Sethi, and Ullman. Unfortunately, the C-convention of starting array indices from 0 makes the code harder to follow than it should be. When first character is received there is no proper suffix. This is the subtle part of the algorithm.