dimanche 11 novembre 2018

Why is glibc's __random_r assigning variables it immediately overwrites?

I was looking for the source for glibc's rand() function, which an answer here links to.

Following the link, I'm puzzled about the code for the __random_r() TYPE_0 branch:

int32_t val = state[0];
val = ((state[0] * 1103515245) + 12345) & 0x7fffffff;
state[0] = val;
*result = val;

What is the point of the val variable, getting assigned and then immediately overwritten? The random_data struct that holds state is nothing unusual.

As one would expect, compiling with -O2 on godbolt gives the same code if you just eliminate val. Is there a known reason for this pattern?

(The only thing I can think of is "makes it slightly easier to see the state in some more basic debuggers when doing unoptimized builds".)




Aucun commentaire:

Enregistrer un commentaire