// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rdx, %r12
movq %rsi, 0x8(%rsp)
movq %rdi, %rbx
movl $0x400, %edi            # imm = 0x400
callq 0x3040
movq %rax, %r14
xorl %ebp, %ebp
testq %r15, %r15
je 0x444b
cmpq $0x400, %r15            # imm = 0x400
ja 0x4401
movl $0x1, %esi
movq %r14, %rdi
movq %r15, %rdx
movq 0x8(%rsp), %rcx
callq 0x3390
cmpq %r15, %rax
jne 0x4465
movq %rax, %r13
movb -0x1(%r14,%rax), %al
movq 0x10(%rsp), %rcx
movb %al, (%rcx)
jmp 0x4426
movl $0x400, %r13d           # 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 0x4484
testq %r13, %r13
je 0x4443
xorl %eax, %eax
movzbl (%r14,%rax), %ecx
movzbl 0x48(%rbx,%rcx), %ecx
incq (%r12,%rcx,8)
incq %rax
cmpq %rax, %r13
jne 0x442d
addq %r13, %rbp
subq %r13, %r15
jne 0x43cd
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 0x8064(%rip), %rdi      # 0xc4d0
leaq 0x7f6a(%rip), %rsi      # 0xc3dd
leaq 0x8068(%rip), %rcx      # 0xc4e2
movl $0xf4, %edx
callq 0x31a0
leaq 0x80b1(%rip), %rdi      # 0xc53c
leaq 0x7f4b(%rip), %rsi      # 0xc3dd
leaq 0x8049(%rip), %rcx      # 0xc4e2
movl $0x100, %edx            # imm = 0x100
callq 0x31a0
nop