real_main() {
  eval("source", source());
  eval("sink", sink());
}


source() {
  int i, out_index=0;

  for (i = 5; i < LIMIT; i += 2) out("seg", 3, out_index++, i);
  out("seg", 3, out_index, 0);
}


sink() {
  int    in_index=0, num, pipe_seg(), prime=3, prime_count=2;
  while(1) {
    in("seg", prime, in_index++, ? num);
    if (!num) break;
    if (num % prime) {
      ++prime_count;
      if (num*num < LIMIT) {
	eval("pipe seg", pipe_seg(prime, num, in_index));
	prime = num;
	in_index = 0;
      }
    }
  }
  printf("count: %d.\n", prime_count);
}


pipe_seg(prime, next, in_index) {
  int num, out_index=0;
  while(1) {
    in("seg", prime, in_index++, ? num);
    if (!num) break;
    if (num % prime) out("seg", next, out_index++, num);
  }
  out("seg", next, out_index, num);
}

/* eof- */