Working commented code is good, but that's not what this game thread is about. Let me show what I had in mind. I'll use C for these examples.

In Snippets #8-#11, we're asked to __guess__ what function the code implements, or at least in what application it arises.

#8 is from real code. #9-#11 have had names degraded to make the purpose less obvious.

Snippet #8:

Code:

retval = -EAGAIN;
if (atomic_read(&p->real_cred->user->processes) >=
task_rlimit(p, RLIMIT_NPROC)) {
if (p->real_cred->user != INIT_USER &&
!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN))
goto bad_fork_free;
}
current->flags &= ~PF_NPROC_EXCEEDED;

Snippet #9:

Code:

double foo(double to[2], double fro[2])
{
double z = cos(to[0]) * cos(fro[1] - to[1]);
double y = sin(to[0]);
y = - sqrt(y*y + z*z) * cos(atan2(y, z) - fro[0]);
return 6371 * (3.14159 / 2 + atan2(y, sqrt(1 - y*y)));
}

Snippet #10:

Code:

unsigned long m = ~0;
printf("%d\n", (int)((m)/((m)%255+1)/255%255*8 + 7-100/((m)%255+14)));

Snippet #11:

Code:

struct { double x, y; } A[4];
#define B(a,b,q) (A[a].q - A[b].q)
#define C(a,b,c) (B(a,b,x) * B(c,a,y) > B(c,a,x) * B(a,b,y))
#define DD (1 ^ C(0,1,2) ^ C(1,2,3) ^ C(2,3,0) ^ C(3,0,1))

The next Snippet is the opposite. We're told what the snippet does and asked to write it.

I still remember when someone showed me the solution 52 years ago. I thought it was neat!

I suppose it is well known.

Snippet 12:

Code:

/* y = lastbit(x) should return
* 00000000000010000 when x is
* 01101110110010000
* or more generally
* 0000000010000... when z is
* xxxxxxxx10000... where 'x' means don't care.
*
* Note that either
* x ^= lastbit(x);
* or
* x &= ~lastbit(x);
* will delete the right-most bit from x.
*/
int lastbit(int y);