// File: /workspace/github_cpp_makefile_star2_O3/giovannarosone[P]BCR_LCP_GSA/BCRexternalBWT.cpp
// Function: BCRexternalBWT::rankManySymbolsFilePartial(_IO_FILE&, unsigned long*, unsigned long, unsigned char*)
dataTypeNChar BCRexternalBWT::rankManySymbolsFilePartial(FILE & InFileBWT, dataTypeNChar *counters, dataTypeNChar toRead, uchar *foundSymbol)
{
	dataTypeNChar numchar, cont=0;  //cont is the number of symbols already read!
	uchar *buffer = new uchar[SIZEBUFFER];

	//it reads toRead symbols from the fp file (Partial BWT)
	while (toRead > 0) {            //((numchar!=0) && (toRead > 0)) {
		if (toRead <= SIZEBUFFER) {    //Read toRead characters
			numchar = fread(buffer,sizeof(uchar),toRead,&InFileBWT);
			assert(numchar == toRead); // we should always read/write the same number of characters
			*foundSymbol = buffer[numchar-1];     //The symbol of the sequence k.  It is the symbol in the last position in the partial BWT that we have read.

			//#if BCR_SET_ALN_RH ==0    //LEFT ALIGN
			//	if (*foundSymbol == TERMINATE_CHAR)
			//		std::cerr << "rankManySymbolsFilePartial: WARNING --> Rank toRead=" << (unsigned int)toRead << " foundSymbol= " << (unsigned int)(*foundSymbol) << std::endl;			
				//#else   //RIGHT ALIGN
				//	std::cerr << "--> Rank toRead=" << (unsigned int)toRead << " foundSymbol= " << (unsigned int)(*foundSymbol) << std::endl;
			//#endif	
		}
		else {   //Read sizebuffer characters
			numchar = fread(buffer,sizeof(uchar),SIZEBUFFER,&InFileBWT);
			assert(numchar == SIZEBUFFER); // we should always read/write the same number of characters
		}

		//For each symbol in the buffer, it updates the number of occurrences into counters
		for (dataTypeNChar r=0; r<numchar; r++)
			counters[alpha[(unsigned int)buffer[r]]]++;    //increment the number of letter symbol into counters


		cont   += numchar;  //number of read symbols
		toRead -= numchar;  //number of remaining symbols to read
		if ((numchar == 0) && (toRead > 0)) {  //it means that we have read 0 character, but there are still toRead characters to read
			std::cerr << "rankManySymbolsFilePartial: read 0 character, but there are still " << toRead << " characters to read  " << std::endl;
			exit (EXIT_FAILURE);
		}
	}
	delete [] buffer;

	return cont;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, 0x10(%rsp)
movq %rcx, %r13
movq %rdx, %r12
movq %rsi, 0x8(%rsp)
movq %rdi, %rbx
movl $0x400, %edi            # imm = 0x400
callq 0x3040
movq %rax, %r14
xorl %ebp, %ebp
testq %r13, %r13
je 0x4452
cmpq $0x400, %r13            # imm = 0x400
ja 0x440d
movl $0x1, %esi
movq %r14, %rdi
movq %r13, %rdx
movq 0x8(%rsp), %rcx
callq 0x3390
cmpq %r13, %rax
jne 0x446c
movb -0x1(%r14,%r13), %al
movq 0x10(%rsp), %rcx
movb %al, (%rcx)
movq %r13, %r15
jmp 0x4432
movl $0x400, %r15d           # imm = 0x400
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
movq 0x8(%rsp), %rcx
callq 0x3390
cmpq $0x400, %rax            # imm = 0x400
jne 0x448b
xorl %eax, %eax
movzbl (%r14,%rax), %ecx
movzbl 0x48(%rbx,%rcx), %ecx
incq (%r12,%rcx,8)
incq %rax
cmpq %rax, %r15
jne 0x4434
addq %r15, %rbp
subq %r15, %r13
jne 0x43d9
movq %r14, %rdi
callq 0x3330
movq %rbp, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x805d(%rip), %rdi      # 0xc4d0
leaq 0x7f63(%rip), %rsi      # 0xc3dd
leaq 0x8061(%rip), %rcx      # 0xc4e2
movl $0xf4, %edx
callq 0x31a0
leaq 0x80aa(%rip), %rdi      # 0xc53c
leaq 0x7f44(%rip), %rsi      # 0xc3dd
leaq 0x8042(%rip), %rcx      # 0xc4e2
movl $0x100, %edx            # imm = 0x100
callq 0x31a0