#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
static void groom(int n, unsigned char a){
  unsigned char g[8]; int i, u;
  g[0]=8; g[1]=AF_INET; g[2]=0x11; g[3]=0x22;
  g[4]=a; g[5]=2; g[6]=3; g[7]=4;
  for(i=0;i<n;i++){ u=socket(AF_INET,SOCK_DGRAM,0); bind(u,(struct sockaddr*)g,8); close(u);}
}
int main(void){
  unsigned char sa[8];
  int p, st, i, s, r, flips = 0;
  memset(sa,0,sizeof sa);
  sa[0]=2; sa[1]=AF_INET;
  for (p = 0; p < 48; p++) {
    if (fork() == 0) {
      int f = 0;
      for (i = 0; i < 8000; i++) {
        groom(4,224);
        s = socket(AF_INET,SOCK_STREAM,0);
        r = bind(s,(struct sockaddr*)sa,2);
        close(s);
        if (r == 0 || errno == 47) f++;
      }
      _exit(f ? 1 : 0);
    }
  }
  while (wait(&st) > 0)
    if (WEXITSTATUS(st) == 1) flips++;
  printf("procs with >=1 EAFNOSUPPORT flip: %d/48\n", flips);
  return 0;}
