[comp.lang.c] Creme de la Creme of UGLY CODE

jep@oink.UUCP (James E. Prior) (12/13/87)

Recently someone posted a piece of ugly code, which many
people revelled in commenting on.  Dey ain't seen nut'in!

Don Libes of Micro/Systems Journal holds an annual
International Obfuscated C Code Contest.  I've seen
some real gems from it the last couple of years.

My all time favorite piece of bad code was in 1986 from 
Lennart Augustsson (seismo!mcvax!enea!chalmers!augustss)
is as follows:

#define p struct c
#define q struct b
#define h a->a
#define i a->b
#define e i->c
#define o a=(*b->a)(b->b,b->c)
#define s return a;}q*
#define n (d,b)p*b;{q*a;p*c;
#define z(t)(t*)malloc(sizeof(t))
q{int a;p{q*(*a)();int b;p*c;}*b;};q*u n a=z(q);h=d;i=z(p);i->a=u;i->b=d+1;s
v n c=b;do o,b=i;while(!(h%d));i=c;i->a=v;i->b=d;e=b;s
w n o;c=i;i=b;i->a=w;e=z(p);e->a=v;e->b=h;e->c=c;s
t n for(;;)o,main(-h),b=i;}main(b){p*a;if(b>0)a=z(p),h=w,a->c=z(p),a->c->a=u,a->c->b=2,t(0,a);putchar(b?main(b/2),-b%2+'0':10);}

Don Libes's comments: This is really obscure.  I recommend running this
first, then renaming the functions more descriptively.

My own comments.  This program has several levels of obfuscation.  Most
obfuscation attempts are just the inverse of cb.  That is, they use many
misleading #defines, and cram it on a few lines.  L.A.'s gem goes far
beyond that.  Once you have run the code through cpp and cb, you have
a program that looks almost simple.  That's the most beatiful part of
L.A.'s obfuscation: It looks simple, but it isn't.  The function calling
sequence is quite ingenius.  

Some newer compilers either crash or get confused about the meaning of
certain identifiers.  There *is* enough information from the context 
in which they are used, to properly derive the proper meaning.  I've
seen some compilers compile it as is.  If your compiler can't derive 
the proper meaning of all the identifiers, then you'll have to change 
some of them.  Be careful if you do.

You can learn much about the C language from this and other obfuscated
programs.

-- 
Jim Prior    jep@oink.UUCP    {ihnp4|cbosgd}!n8emr!oink!jep

decot@hpisod2.HP.COM (Dave Decot) (12/17/87)

> Don Libes of Micro/Systems Journal holds an annual
> International Obfuscated C Code Contest.  I've seen
> some real gems from it the last couple of years.

I seem to remember this contest was originally run by someone named
"chongo@nsc".  Is this the same person?

Dave Decot
"Former 2nd place winner"
hpda!decot

farren@gethen.UUCP (Michael J. Farren) (12/18/87)

In article <209@oink.UUCP> jep@oink.UUCP (James E. Prior) writes:
>Don Libes of Micro/Systems Journal holds an annual
>International Obfuscated C Code Contest.  I've seen
>some real gems from it the last couple of years.

Doesn't someone on the net (chongo@amdcad?) run an Annual Obfuscated
C Contest?  Are these two different things?  Or is someone 'borrowing'
someone else's work?  Enqueueing minds want to know...

-- 
Michael J. Farren             | "INVESTIGATE your point of view, don't just 
{ucbvax, uunet, hoptoad}!     | dogmatize it!  Reflect on it and re-evaluate
        unisoft!gethen!farren | it.  You may want to change your mind someday."
gethen!farren@lll-winken.arpa |     Tom Reingold, from alt.flame 

jep@oink.UUCP (James E. Prior) (12/25/87)

In article <462@gethen.UUCP> farren@gethen.UUCP (Michael J. Farren) writes:
>In article <209@oink.UUCP> jep@oink.UUCP (James E. Prior) writes:
>>Don Libes of Micro/Systems Journal holds an annual
>>International Obfuscated C Code Contest.
>
>Doesn't someone on the net (chongo@amdcad?) run an Annual Obfuscated
>C Contest?  Are these two different things?  Or is someone 'borrowing'
>someone else's work?  Enqueueing minds want to know...

I went back and read the beginning of the articles more carefully.  The
contest I read about was the International Obfuscated C Code Contest.
It is run by Landon Noll of Amdahl and Larry Bassel of National
Semiconductor.  Both USENET and U.S.MAIL address were given to submit
entries to.  For the most recent contest, decwrl!amdahl!chongo was the
USENET address given, so apparently there is only one contest.
-- 
Jim Prior    jep@oink.UUCP    {ihnp4|cbosgd}!n8emr!oink!jep