[comp.unix.ultrix] Ultrix 2.0 C optimizer bug

tp@td2cad.UUCP (t patterson) (05/04/88)

/*
following program demonstrates a bug in C optimizer found in Ultrix 2.0.
it does not occur under UTS on IBM mainframe, on 4.3 BSD on a VAX, under
SunOS 3.2 (or maybe 3.4), nor under Bell Technologies System V on Intel 301 box.
does anybody know if Ultrix 2.2 fixes this?

(we have filed an SPR on it; no reply from DEC; we filed thru DSIN ... hahaha
...at least we got a receipt that the report was mailed properly; I presume
the whole affair is over one of those old 4.2 BSD optimizer bugs that DEC has 
never fixed)

Don't trust your C programs! (or use gcc!)
*/
/*
 * $Compile: cc -O -o opttest opttest.c
 *
 * From myu@tdcad Wed Apr  6 08:41:56 1988
 * Subject: simple c program for testing -O in cc
 *
 *  if the following program is compiled with -O option, it will give
 * results different from when it is compiled without that option.
 *
 *  if you dis-assemble the object file, it's pretty obvious that the
 * optimizer is producing incorrect code -- it goofs up a branch statement
 * so it always passes over one of the printf statements.
 *
 * the program can be made to run correctly by compiling with "-DHUMOR"
 * or by tinkering withthe array c in various ways.
 *
 * this problem was discovered when someone finally noticed that a
 * piece of production code was producing wildly incorrect results.
 */
#include <stdio.h>

int c[9] = { 2, 2, 1, 5, 20, 18, 20, 17, 8 };

main()
{
	int id, b, a;

	id = 4;
	b = 0;
	if ( (id != 8) && (id != 9) )  {
		b = c[ id ];
	} else {
		printf("id is either 8 or 9\n");
	}

#ifdef HUMOR
	a = 10; 	/* if you compile with "-DHUMOR", it's OK	*/
#endif
	if( b == 20 ) {
		printf("This statement skipped with optimizer; b=%d\n", b);
	}

	printf("the last statement: id=%d b=%d\n",id,b);
} 

--
  ..tp.. t patterson			path: {ihnp4,uunet}!wucs1!td2cad!tp
					      OR {pyramid,hoptoad}!td2cad!tp
domain: tp%td2cad.intel.com@relay.cs.net      OR decwrl!intelca!mipos3!td2cad!tp