/* test108.c == bitfield, struct pass/ret, compound expr, nested func, setjmp */

#include <setjmp.h>
int printf(const char *fmt, ...);
jmp_buf jb;

typedef struct _xx {
	float f;
	unsigned b1 : 7;
	unsigned b2 : 4;
	signed	b3 : 9;
	unsigned b4 : 5;
	signed b5 : 24;
	unsigned b6 : 8;
	int i;
	struct {
		unsigned char b1 : 2;
		unsigned char b2 : 4;
	} s;
	int j;
} XX, *PXX;
XX xx = {2.537, 33, 11, -234, 21, -99876, 111, 99999,{1,7},'w'};

int func2(int (*pfunc)())
{
	return pfunc();
}
short func1(int x)
{
int y = x - 1;
int nfunc()
{
	return ({int ss; ss = ( x < 2) ? x+100 : (y <= 2) ? x+200 : x+300; ss;});
}
int z = 0;
	switch(x)
	{
		case 1:
			z = nfunc();
			break;
		case 2:
			z = func1(y);
			break;
		case 3:
			z = func1(nfunc());
			break;
		case 4:
			z = func1(func1(nfunc()));
			break;
		case 5:
			longjmp(jb, 1);
	}
	return printf("%d:%d ", x, z) + func2(nfunc);
}
XX func(int x, char c, XX xx, XX yy, PXX pxx)
{
	printf("f=%8.4f b1=%d b2=%d b3=%d b4=%d b5=%d b6=%d i=%d bb1=%d bb2=%d j=%d\n",
		xx.f, xx.b1, xx.b2, xx.b3, xx.b4, xx.b5, xx.b6, xx.i, xx.s.b1, xx.s.b2, xx.j);
	printf("f=%8.4f b1=%d b2=%d b3=%d b4=%d b5=%d b6=%d i=%d bb1=%d bb2=%d j=%d\n",
		yy.f, yy.b1, yy.b2, yy.b3, yy.b4, yy.b5, yy.b6, yy.i, yy.s.b1,yy.s.b2,yy.j);
	printf("f=%8.4f b1=%d b2=%d b3=%d b4=%d b5=%d b6=%d i=%d bb1=%d bb2=%d j=%d\n",
		pxx->f, pxx->b1, pxx->b2, pxx->b3, pxx->b4, pxx->b5, pxx->b6, pxx->i, pxx->s.b1, pxx->s.b2, pxx->j);
	printf("x=%d c=%c\n", x, c);
	return *pxx;
}
int main()
{
char padit[8] = {0,0,'w',0,0,0,0,0};
XX yy = {2.537, 33, 11, -234, 21, -99876, 111, 99999,{1,7}, padit[2]};
PXX pxx = &xx;
XX zz;

	if(setjmp(jb))
	{/* print expected values */
		printf("\n1:101 1:101 2:107 203:0 3:509 304:0 610:0 4:916\n");
		printf("SETJMP CALLED\n");
		return 0;
	}
	padit[1] = padit[2];
	zz  = func(-1, padit[1], xx, yy, pxx);
	printf("f=%8.4f b1=%d b2=%d b3=%d b4=%d b5=%d b6=%d i=%d bb1=%d bb2=%d j=%d\n",
		xx.f, xx.b1, xx.b2, xx.b3, xx.b4, xx.b5, xx.b6, xx.i, xx.s.b1, xx.s.b2,xx.j);
	printf("f=%8.4f b1=%d b2=%d b3=%d b4=%d b5=%d b6=%d i=%d bb1=%d bb2=%d j=%d\n",
		yy.f, yy.b1, yy.b2, yy.b3, yy.b4, yy.b5, yy.b6, yy.i, yy.s.b1, yy.s.b2, yy.j);
	printf("f=%8.4f b1=%d b2=%d b3=%d b4=%d b5=%d b6=%d i=%d bb1=%d bb2=%d j=%d\n",
		pxx->f, pxx->b1, pxx->b2, pxx->b3, pxx->b4, pxx->b5, pxx->b6, pxx->i, pxx->s.b1, pxx->s.b2, pxx->j);
	printf("f=%8.4f b1=%d b2=%d b3=%d b4=%d b5=%d b6=%d i=%d bb1=%d bb2=%d j=%d\n",
		zz.f, zz.b1, zz.b2, zz.b3, zz.b4, zz.b5, zz.b6, zz.i, zz.s.b1, zz.s.b2,zz.j);
	func1(1);
	func1(2);
	func1(3);
	func1(4);
	func1(5);
	return 0;
}
