
#include <core.h>

struct Herbivore {
  bool isRuminant;
};
struct Carnivore {
  int numteeth;
};

@tagged union Animal {
    struct Herbivore@ h;
    struct Carnivore@ c;
};

typedef union Animal animal;

/* count the total number of teeth among the carnivores
 * in the given array.
 */
int count_teeth(animal ? serengeti) {
  int total = 0;
  int i; 
  if (!serengeti) return 0;
  for (i = 0; i < numelts(serengeti); i++) {
    if (tagcheck(serengeti[i].c))
      total += serengeti[i].c->numteeth;
  }
  return total;
}
