2009年9月17日木曜日

gcc=?ISO-2022-JP?B?GyRCOEA4bDNIRCUbKEI=?=

#include <sys/types.h>
#include <unistd.h>
#include <limits.h>

#define HW1 "Hello, world!\n"
#define HW2 "Hello, world!\nHello, world!\n"

#define PRINTIT(args...) printf(args)

inline long
getnum(int argc, char *argv[]) __attribute__((always_inline));

int
main(int argc, char *argv[])
{
__label__ retok, retfailed;
int num = getnum(argc, argv);
char buf[strlen(HW1)*num + 1];
typeof(&buf[0]) formstr(char *buf, int n) {
buf[0] = '\0';
char *hw;
switch (n & 1) {
case 1 ... 1:
hw = HW1;
break;
case 0 ... 0:
n >>= 1;
hw = HW2;
break;
}
while (n-- > 0)
strcat(buf, hw);
return buf;
}
void *addr = &&retok;
char *cp = formstr(buf, num) ? : ({addr = &&retfailed; "Failed\n";});
PRINTIT(cp);
goto *addr;

retfailed:
return -1;
retok:
return 0;
}

long
getnum(int argc, char *argv[])
{
return ({long ret = 1;
if (argc > 1) {
ret = strtol(argv[1], (char**)NULL, 10);
if (ret < 0 || ret == LONG_MAX || ret == LONG_MIN)
ret = 0;
}
ret;});
}
http://www-cms.phys.s.u-tokyo.ac.jp/~naoki/CIPINTRO/index.html