#!/bin/sh

# Adjust the following for your gcc 4 setup. Only 'cc'
# is used later.
#
mf="/usr/local/gcc-mudflap"
cc="$mf/bin/i686-pc-linux-gnu-gcc"
export LD_LIBRARY_PATH="$mf/lib"

prog="$HOME/zz/zz41"

cat >$prog.c <<EOF
/* demonstrate missed violations
*/

#include <stdio.h>
#include <pthread.h>


static void *
test (void *arg)
{
	char		x[1];
	static char	y[1];

	return (x[1] == y[1] ? "1" : "2");	/* two violations here */
}

static void
threaded_test (void)
{
	pthread_t	h;

	pthread_create (&h, NULL, test, NULL);
	fprintf (stderr, "%s(%d) created\n",
		__FILE__, __LINE__);
	fflush (stderr);

	pthread_join (h, NULL);
	fprintf (stderr, "%s(%d) joined\n",
		__FILE__, __LINE__);
	fflush (stderr);
}

int
main (int argc, char **argv)
{
	fprintf (stderr, "%s(%d) testing in main\n",
		__FILE__, __LINE__);
	fflush (stderr);
	test (NULL);		/* this provokes two violations (correct) */

	fprintf (stderr, "%s(%d) testing in thread\n",
		__FILE__, __LINE__);
	fflush (stderr);
	threaded_test ();	/* this provokes one violation (incorrect) */

	fprintf (stderr, "%s(%d) test done\n",
		__FILE__, __LINE__);
	fflush (stderr);

	return (0);
}
EOF

 MUDFLAP_OPTIONS=""
#MUDFLAP_OPTIONS="$MUDFLAP_OPTIONS -help"
 MUDFLAP_OPTIONS="$MUDFLAP_OPTIONS -trace-calls"
#MUDFLAP_OPTIONS="$MUDFLAP_OPTIONS -verbose-trace"
 MUDFLAP_OPTIONS="$MUDFLAP_OPTIONS -verbose-violations"
 MUDFLAP_OPTIONS="$MUDFLAP_OPTIONS -backtrace=30"
#MUDFLAP_OPTIONS="$MUDFLAP_OPTIONS -check-initialization"
export MUDFLAP_OPTIONS

opts="-v -g -O0 -fmudflap -fmudflapth -lmudflapth -lpthread"

test -f $prog && rm $prog
$cc $opts -Wall -o $prog $prog.c >$prog.out 2>&1 || exit 1
echo "`date` running program"
$prog 2>>$prog.out
echo "`date` done"
