Copy of main.c, to be adapted for use in R.