C-style strings need a null character at the end to signify their termination. Things tend to go awry when there isn't one - functions that deal with c-strings will usually keep going until they hit a null character.
In this case, however, atol stops at the first non-numeric character. When the lines marked as '2' are uncommented, atol includes them because it hasn't found a null character or a non-numerical character yet.
Why c-strings need a null character? A c-string is simply a pointer to a location in memory. That pointer, when passed to a function, tells nothing about what lies ahead. The pointer is then treated as a stream of characters. A null character has to be included to signify that the function has reached the end of the "string."
As for the implementation, here is a (very) rough and inefficient, but completely standards-compliant version of atol. I made this just to show you what logic is going on behind the scenes here - don't use this as a replacement for atol. Also, the actual implementation of atol (and other functions like it) are most likely written in assembly by hand for performance reasons.