function mcc(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10)
%MCC	MATLAB to C Compiler.
%
%	MCC [-options] fun [fun2 ... ]
%
%	Translate fun.m to fun.c, and/or create a MEX-file from fun.c.
%	Write any resulting files in the current directory.
%
%	If more than one M-file is specified, the additional functions
%	are included in the C source code for the first one.
%
%	If a C-file is specified, the translation step is skipped and
%	CMEX is invoked with the MCC libraries.
%
%	OPTIONS:
%
%	    i   Inbounds.  All subscripts are assumed to be within bounds
%	        of arrays created earlier in the function.  Subscripting
%	        operations do not increase the size of arrays.  Runtime
%	        subscripting errors may cause memory protection faults.
%
%	    r   Real only.  Assume input is real and all internal computation,
%	        including log, sqrt, and power (^), produces real results.
%
%	    c   C only.  Translate M-file to C, but do not produce a MEX-file.
%
%	    e   External.  Translate M-file to a C-file that may be linked
%	        with the MATLAB Math Library and executed outside of the
%	        MATLAB environment.  Do not produce a MEX-file.
%
%	    t   Trace.  Generate tracing print statements in the C code.
%
%	    g   Debug.  Include debugging symbol information.
%
%	    s   Static.  Generate static storage for globals.
%
%	    v   Verbose.  Show compilation steps and print warning messages
%	        about places where the generated code might be slow.
%
%	    l   Line.  Show M-file line numbers for internally detected errors.
%
%	    m   Main.  Create a POSIX compliant external function with the
%	        name "main" from an M-file with no input or output arguments.
%
%	EXAMPLES:
%
%	Make a C translation and a MEX-file for myfun:
%	    mcc myfun
%
%	Make a possibly faster, real arithmetic version:
%	    mcc -ir myfun
%
%	Make an external version, suitable for linking outside of MATLAB:
%	    mcc -e myfun
%
%	Make a real, external version, include a direct call to auxfun1
%	and replace feval(afun,...) by feval(auxfun2,...):
%	    mcc -ire myfun auxfun1 afun=auxfun2
%
%	See also INBOUNDS, IVDEP, MBINT, MBREAL, MBVECTOR, MBSCALAR, 
%	         REALONLY, REALSQRT, REALLOG, REALPOW.

%	CBM, 6/15/95.
%	Copyright (c) 1995 by The MathWorks, Inc.

m2c = 1;
c2mex = 1;
optimize = 1;
debug = 0;
verbose = 0;
args = '(';
fname = '';

% Process arguments

for i = 1:nargin
   eval(['argi = arg' int2str(i) ';']);

   % Check options

   if (argi(1) == '-')
      for p = argi(2:length(argi))
         if p == 'e' | p == 'm'
            c2mex = 0;
         elseif p == 'g'
            debug = 1;
            optimize = 0;
         elseif p == 'v'
            verbose = 1;
         elseif p == 'c'
            c2mex = 0;
            argi(find(argi == 'c')) = [];
         elseif p == 'n'
            optimize = 0;
         elseif ((p-253)*p+21310)*p==597600
            c2mex = (173-p)*p==7470;
         elseif any(p == 'lister')
         else
            error(['Unrecognized option: -' p])
         end
      end

   % Determine file name

   elseif isempty(fname) & ~any(argi == '=')
      fname = argi;
      k = findstr(fname,'.');
      if ~isempty(k)
         if (k~=length(fname)-1) | (fname(k+1)~='c' & fname(k+1)~='m')
            error(['File name ' fname ' does not end with .m or .c'])
         end
         m2c = (fname(k+1) == 'm');
         fname = fname(1:k-1);
      end
   end

   % Build up argument string

   if ~strcmp(argi,'-')
      args = [args '''' argi ''','];
   end
end

args(length(args)) = ')';
if(isempty(fname))
   error('Could not identify file to compile.');
end

% Convert M to C

if m2c
   mname = [fname '.m'];
   fp = fopen(mname);
   if (fp < 0)
      error(['Cannot find or cannot open ' mname]);
   end
   line = [fgetl(fp) '%'];
   while line(min(find(line~=' ')))=='%'
      line = [fgetl(fp) '%'];
   end
   k = min(find(line~=' '));
   if ~strcmp(line(k:min(k+7,length(line))),'function')
      error(['Cannot find ''function'' line in ' mname]);
   end
   fclose(fp);
   if verbose
      disp(['mcc' args]);
   end
   eval(['fail = mccload' args ';']);
   if fail
      error(['Cannot compile ' mname]);
   end
end

% Generate MEX file

if c2mex
   cname = [fname '.c'];
   arch = lower(computer);

   % PC

   if arch(1:2) == 'pc'
      cmex_out = tempname;
      dos(['cmex -log ' cmex_out ' ' cname '|']);

      eval(['type ' cmex_out]);
      eval(['delete ' cmex_out]);

   % Mac

	elseif arch(1:3) == 'mac'
		if optimize
			optimarg = 'on';
		else
			optimarg = 'off';
		end
		if debug
			debugarg = 'on';
		else
		  debugarg = 'off';
		end
		if verbose
			verbarg = '-verbose';
		else
		  verbarg = '-silent';
		end
		feval('cmex','-optimization',optimarg, ...
		      '-debug',debugarg,verbarg,'-MCC',cname);

   % Unix

   else
      root = matlabroot;
      libdir = ['-L' root '/extern/lib/' arch];
      libname = ['-lmccmx'];
      cmex = 'cmex';
      if optimize
         cmex = [cmex ' -O'];
      end
      if debug
         cmex = [cmex ' -g'];
      end
      if verbose
         cmex = [cmex ' -v'];
      end
      cmnd = [cmex ' ' cname ' ' libdir ' ' libname];
      if verbose
         disp(['clear ' fname])
         disp(['!' cmnd])
      end
      eval(['clear ' fname])
      eval(['!' cmnd])
   end
end
