// File: 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:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %r8, 0x30(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x8(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x400, %edi            # imm = 0x400
callq 0x3040
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x38(%rsp)
jbe 0x4595
cmpq $0x400, 0x38(%rsp)      # imm = 0x400
ja 0x447b
movq 0x18(%rsp), %rdi
movq 0x38(%rsp), %rdx
movq 0x48(%rsp), %rcx
movl $0x1, %esi
callq 0x33c0
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
cmpq 0x38(%rsp), %rax
jne 0x4442
jmp 0x4461
leaq 0x14013(%rip), %rdi     # 0x1845c
leaq 0x13f19(%rip), %rsi     # 0x18369
movl $0xf4, %edx
leaq 0x14012(%rip), %rcx     # 0x1846e
callq 0x3190
movq 0x18(%rsp), %rax
movq 0x28(%rsp), %rcx
subq $0x1, %rcx
movb (%rax,%rcx), %cl
movq 0x30(%rsp), %rax
movb %cl, (%rax)
jmp 0x44c7
movq 0x18(%rsp), %rdi
movq 0x48(%rsp), %rcx
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x33c0
movq %rax, 0x28(%rsp)
cmpq $0x400, 0x28(%rsp)      # imm = 0x400
jne 0x44a6
jmp 0x44c5
leaq 0x1401b(%rip), %rdi     # 0x184c8
leaq 0x13eb5(%rip), %rsi     # 0x18369
movl $0x100, %edx            # imm = 0x100
leaq 0x13fae(%rip), %rcx     # 0x1846e
callq 0x3190
jmp 0x44c7
movq $0x0, 0x10(%rsp)
movq 0x10(%rsp), %rax
cmpq 0x28(%rsp), %rax
jae 0x4517
movq 0x8(%rsp), %rcx
movq 0x40(%rsp), %rax
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rsi
movzbl (%rdx,%rsi), %edx
movl %edx, %edx
movzbl 0x48(%rcx,%rdx), %ecx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x44d0
movq 0x28(%rsp), %rax
addq 0x20(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x28(%rsp), %rcx
movq 0x38(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x28(%rsp)
jne 0x4590
cmpq $0x0, 0x38(%rsp)
jbe 0x4590
movq 0x1baa1(%rip), %rdi     # 0x1fff0
leaq 0x13f88(%rip), %rsi     # 0x184de
callq 0x3260
movq %rax, %rdi
movq 0x38(%rsp), %rsi
callq 0x3090
movq %rax, %rdi
leaq 0x13faf(%rip), %rsi     # 0x18521
callq 0x3260
movq %rax, %rdi
movq 0x1ba3f(%rip), %rsi     # 0x1ffc0
callq 0x32a0
movl $0x1, %edi
callq 0x3300
jmp 0x43ff
movq 0x18(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x45ad
movq (%rsp), %rdi
callq 0x3330
movq 0x20(%rsp), %rax
addq $0x58, %rsp
retq
nopw (%rax,%rax)