// File: /workspace/github_cpp_makefile_star2_O2/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, %r12
movq %rdx, %r15
movq %rsi, 0x8(%rsp)
movq %rdi, %r13
movl $0x400, %edi            # imm = 0x400
callq 0x3040
movq %rax, %rbx
xorl %ebp, %ebp
testq %r12, %r12
je 0x3e68
cmpq $0x400, %r12            # imm = 0x400
ja 0x3e23
movq %rbx, %rdi
pushq $0x1
popq %rsi
movq %r12, %rdx
movq 0x8(%rsp), %rcx
callq 0x3390
cmpq %r12, %rax
jne 0x3e82
movb -0x1(%rbx,%r12), %al
movq 0x10(%rsp), %rcx
movb %al, (%rcx)
movq %r12, %r14
jmp 0x3e46
movl $0x400, %r14d           # imm = 0x400
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
pushq $0x1
popq %rsi
movq 0x8(%rsp), %rcx
callq 0x3390
cmpq $0x400, %rax            # imm = 0x400
jne 0x3ea1
xorl %eax, %eax
cmpq %rax, %r14
je 0x3e60
movzbl (%rbx,%rax), %ecx
movzbl 0x48(%r13,%rcx), %ecx
incq (%r15,%rcx,8)
incq %rax
jmp 0x3e48
addq %r14, %rbp
subq %r14, %r12
jmp 0x3dec
movq %rbx, %rdi
callq 0x3310
movq %rbp, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x661f(%rip), %rdi      # 0xa4a8
leaq 0x6525(%rip), %rsi      # 0xa3b5
leaq 0x6623(%rip), %rcx      # 0xa4ba
movl $0xf4, %edx
callq 0x3190
leaq 0x666c(%rip), %rdi      # 0xa514
leaq 0x6506(%rip), %rsi      # 0xa3b5
leaq 0x6604(%rip), %rcx      # 0xa4ba
movl $0x100, %edx            # imm = 0x100
callq 0x3190