function [R,U,T,B] = ilabinfo(N,O)
% [R,U,T,B] = ilabinfo(name[,utt])   Return info for an ilabfile
%    Open an ICSI labels file ("ilab") named <name>.  Return the 
%    number of utterances in U, and the total number of rows in R.  
%    If <utt> is 
%    specified, R is the number of rows in that utterance only, and T
%    is the row number where it starts (zero otherwise).
%    B is the number of bits per label
% 2003-03-12 dpwe@ee.columbia.edu after pfinfo

if nargin < 2
	O = -1;
end

fid = fopen(N,'r','b');
if fid < 0
	error(['couldnt open ilabfile ', N]);
end

% Read through the ilab header
magic = fread(fid, 1, 'int32');
if magic ~= 1229734210
  fclose(fid)
  error(['ilab file has bad magic (', num2str(magic), ')']);
end
vers = fread(fid, 1, 'int32');
if vers ~= 19990304
  disp(['ilab file has weird version (', num2str(vers), ')']);
end
dataofs = fread(fid, 1, 'int32');
indxofs = fread(fid, 1, 'int32');
B  = fread(fid, 1, 'int32');
U  = fread(fid, 1, 'int32');
R  = fread(fid, 1, 'int32');

T = 0;

if O >= 0
	% Read the sent_table_data to get data on one seg
	% seek to the sentence table data
	if indxofs <= 0
	  fclose(fid);
	  error('specific segment info requested, but ilab has no index');
	end
	fseek(fid, indxofs, -1);
	% read the row index for each utterance
	tab = fread(fid, inf, 'int32');
	if (size(tab,1) ~= (2*U))
	  fclose(fid);
	  error([num2str(U), ' sentences but got ', num2str(size(tab,1)), ' index entries']);
	end
	% return data for requested utterance
	T = tab(1+O);
	R = tab(1+O+U);
end

fclose(fid);

		
