Each implementation of gcd is the mirror image of the other.
In the second example, if the variables a and b were swapped, you would get the first version. Hence these gcd implementations are equivalent. The only thing that changes, is the order of a and b.
This algorithms is know as Euclidean division. From Wikipedia:
At every step k, the Euclidean algorithm computes a quotient qk and remainder rk from two numbers rk-1 and rk-2
rk-2 = qk rk-1 + rk
where the rk is non-negative and is strictly less than the absolute value of rk-1. The theorem which underlies the definition of the Euclidean division ensures that such a quotient and remainder always exist and are unique.
In Euclid's original version of the algorithm, the quotient and remainder are found by repeated subtraction; that is, rk-1 is subtracted from rk-2 repeatedly until the remainder rk is smaller than rk-1. After that rk and rk-1 are exchanged and the process is iterated. Euclidean division reduces all the steps between two exchanges into a single step, which is thus more efficient. Moreover, the quotients are not needed, thus one may replace Euclidean division by the modulo operation, which gives only the remainder. Thus the iteration of the Euclidean algorithm becomes simply
rk = rk-2 mod rk-1
See also