// File: /workspace/github_cpp_makefile_star2_O3/ChnMasterOG[P]KmerGO2/kmc_api/kmc_file.cpp
// Function: CKMCFile::ReadParamsFrom_prefix_file_buf(unsigned long long&)
bool CKMCFile::ReadParamsFrom_prefix_file_buf(uint64 &size)
{
	size_t prev_pos = my_ftell(file_pre);
	my_fseek(file_pre, -12, SEEK_END);
	size_t result;

	result = fread(&kmc_version, sizeof(uint32), 1, file_pre);
	if (kmc_version != 0 && kmc_version != 0x200) //only this versions are supported, 0 = kmc1, 0x200 = kmc2
		return false;
	my_fseek(file_pre, prev_pos, SEEK_SET);

	if (kmc_version == 0x200)
	{
		my_fseek(file_pre, -8, SEEK_END);
		
		int64 header_offset;
		header_offset = fgetc(file_pre);
		
		size = size - 4;	//file size without the size of header_offset (and without 2 markers)

		my_fseek(file_pre, (0LL - (header_offset + 8)), SEEK_END);
		result = fread(&kmer_length, 1, sizeof(uint32), file_pre);
		result = fread(&mode, 1, sizeof(uint32), file_pre);
		result = fread(&counter_size, 1, sizeof(uint32), file_pre);
		result = fread(&lut_prefix_length, 1, sizeof(uint32), file_pre);
		result = fread(&signature_len, 1, sizeof(uint32), file_pre);
		result = fread(&min_count, 1, sizeof(uint32), file_pre);
		original_min_count = min_count;
		uint32 max_count_uint32;
		result = fread(&max_count_uint32, 1, sizeof(uint32), file_pre);
		max_count = max_count_uint32;
		original_max_count = max_count;
		result = fread(&total_kmers, 1, sizeof(uint64), file_pre);
		result = fread(&both_strands, 1, 1, file_pre);
		both_strands = !both_strands;

		signature_map_size = ((1 << (2 * signature_len)) + 1);
		uint64 lut_area_size_in_bytes = size - (signature_map_size * sizeof(uint32)+header_offset + 8);
		single_LUT_size = 1 << (2 * lut_prefix_length);
		uint64 last_data_index = lut_area_size_in_bytes / sizeof(uint64);

		rewind(file_pre);
		my_fseek(file_pre, +4, SEEK_CUR);
		prefix_file_buf_size = (lut_area_size_in_bytes + 8) / sizeof(uint64);		//reads without 4 bytes of a header_offset (and without markers)		
		prefix_file_buf = new uint64[prefix_file_buf_size];
		result = fread(prefix_file_buf, 1, (size_t)(lut_area_size_in_bytes + 8), file_pre);
		if (result == 0)
			return false;
		prefix_file_buf[last_data_index] = total_kmers + 1;

		signature_map = new uint32[signature_map_size];
		result = fread(signature_map, 1, signature_map_size * sizeof(uint32), file_pre);
		if (result == 0)
			return false;

		sufix_size = (kmer_length - lut_prefix_length) / 4;		 
	
		sufix_rec_size = sufix_size + counter_size;	

		return true;
	}
	else if (kmc_version == 0)
	{
		prefix_file_buf_size = (size - 4) / sizeof(uint64);		//reads without 4 bytes of a header_offset (and without markers)		
		prefix_file_buf = new uint64[prefix_file_buf_size];
		result = fread(prefix_file_buf, 1, (size_t)(size - 4), file_pre);
		if (result == 0)
			return false;

		my_fseek(file_pre, -8, SEEK_END);

		uint64 header_offset;
		header_offset = fgetc(file_pre);

		size = size - 4;

		uint64 header_index = (size - header_offset) / sizeof(uint64);
		uint64 last_data_index = header_index;

		uint64 d = prefix_file_buf[header_index];

		kmer_length = (uint32)d;			//- kmer's length
		mode = d >> 32;				//- mode: 0 or 1

		header_index++;
		counter_size = (uint32)prefix_file_buf[header_index];	//- the size of a counter in bytes; 
		//- for mode 0 counter_size is 1, 2, 3, or 4 (or 5, 6, 7, 8 for small k values)
		//- for mode = 1 counter_size is 4;
		lut_prefix_length = prefix_file_buf[header_index] >> 32;		//- the number of prefix's symbols cut frm kmers; 
		//- (kmer_length - lut_prefix_length) is divisible by 4

		header_index++;
		original_min_count = (uint32)prefix_file_buf[header_index];    //- the minimal number of kmer's appearances 
		min_count = original_min_count;
		original_max_count = prefix_file_buf[header_index] >> 32;      //- the maximal number of kmer's appearances
		//max_count = original_max_count;

		header_index++;
		total_kmers = prefix_file_buf[header_index];					//- the total number of kmers 

		header_index++;
		both_strands = (prefix_file_buf[header_index] & 0x000000000000000F) == 1;
		both_strands = !both_strands;

		original_max_count += prefix_file_buf[header_index] & 0xFFFFFFFF00000000;
		max_count = original_max_count;

		prefix_file_buf[last_data_index] = total_kmers + 1;

		sufix_size = (kmer_length - lut_prefix_length) / 4;

		sufix_rec_size = sufix_size + counter_size;

		return true;

	}
	return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x20(%rdi), %rdi
callq 0x4eddf0
movq %rax, %r15
movq 0x20(%rbx), %rdi
movq $-0xc, %rsi
movl $0x2, %edx
callq 0x4f25e0
leaq 0xa4(%rbx), %rdi
movq 0x20(%rbx), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x4edcf0
testl $0xfffffdff, 0xa4(%rbx) # imm = 0xFFFFFDFF
jne 0x4165ed
movq 0x20(%rbx), %rdi
xorl %ebp, %ebp
movq %r15, %rsi
xorl %edx, %edx
callq 0x4f25e0
movl 0xa4(%rbx), %eax
testl %eax, %eax
je 0x4164d8
cmpl $0x200, %eax            # imm = 0x200
jne 0x4165ef
movq 0x20(%rbx), %rdi
movq $-0x8, %r15
movq $-0x8, %rsi
movl $0x2, %edx
callq 0x4f25e0
movq 0x20(%rbx), %rdi
callq 0x4f26c0
addq $-0x4, (%r14)
movslq %eax, %r12
movq 0x20(%rbx), %rdi
subq %r12, %r15
movq %r15, %rsi
movl $0x2, %edx
callq 0x4f25e0
leaq 0x78(%rbx), %rdi
movq 0x20(%rbx), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x4edcf0
leaq 0x7c(%rbx), %rdi
movq 0x20(%rbx), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x4edcf0
leaq 0x80(%rbx), %rdi
movq 0x20(%rbx), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x4edcf0
leaq 0x84(%rbx), %rdi
movq 0x20(%rbx), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x4edcf0
leaq 0x88(%rbx), %rdi
movq 0x20(%rbx), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x4edcf0
leaq 0x8c(%rbx), %rdi
movq 0x20(%rbx), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x4edcf0
movl 0x8c(%rbx), %eax
movl %eax, 0xb0(%rbx)
movq 0x20(%rbx), %rcx
leaq 0xc(%rsp), %r15
movl $0x1, %esi
movl $0x4, %edx
movq %r15, %rdi
callq 0x4edcf0
movl (%r15), %eax
movq %rax, 0x90(%rbx)
movq %rax, 0xb8(%rbx)
leaq 0x98(%rbx), %rdi
movq 0x20(%rbx), %rcx
movl $0x1, %esi
movl $0x8, %edx
callq 0x4edcf0
leaq 0xa0(%rbx), %rdi
movq 0x20(%rbx), %rcx
movl $0x1, %esi
movl $0x1, %edx
callq 0x4edcf0
xorb $0x1, 0xa0(%rbx)
movb 0x88(%rbx), %cl
addb %cl, %cl
movl $0x1, %eax
shll %cl, %eax
movl $0x1, %edx
leal 0x1(%rax), %ecx
movl %ecx, 0x58(%rbx)
movq (%r14), %r15
leaq (%r12,%rax,4), %r12
movq %r12, %rax
movb 0x84(%rbx), %cl
addb %cl, %cl
shll %cl, %edx
negq %rax
leaq (%r15,%rax), %r14
addq $-0x4, %r14
movl %edx, 0x48(%rbx)
movq 0x20(%rbx), %rdi
callq 0x4f2920
movq 0x20(%rbx), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x4f25e0
movq %r14, %rax
shrq $0x3, %rax
movq %rax, 0x38(%rbx)
movq %r14, %rdi
andq $-0x8, %rdi
callq 0x41bd90
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rcx
movl $0x1, %esi
movq %rax, %rdi
movq %r14, %rdx
callq 0x4edcf0
testq %rax, %rax
je 0x4165ed
subq %r12, %r15
addq $-0xc, %r15
movq 0x30(%rbx), %rax
movq 0x98(%rbx), %rcx
incq %rcx
andq $-0x8, %r15
movq %rcx, (%rax,%r15)
movl 0x58(%rbx), %r14d
shlq $0x2, %r14
movq %r14, %rdi
callq 0x41bd90
movq %rax, 0x50(%rbx)
movq 0x20(%rbx), %rcx
movl $0x1, %esi
movq %rax, %rdi
movq %r14, %rdx
callq 0x4edcf0
testq %rax, %rax
je 0x4165ed
movl 0x78(%rbx), %eax
subl 0x84(%rbx), %eax
shrl $0x2, %eax
movl %eax, 0xa8(%rbx)
addl 0x80(%rbx), %eax
movl %eax, 0xac(%rbx)
jmp 0x4165e8
movq (%r14), %rdi
addq $-0x4, %rdi
movq %rdi, %rax
shrq $0x3, %rax
movq %rax, 0x38(%rbx)
andq $-0x8, %rdi
callq 0x41bd90
movq %rax, 0x30(%rbx)
movq (%r14), %rdx
addq $-0x4, %rdx
movq 0x20(%rbx), %rcx
movl $0x1, %esi
movq %rax, %rdi
callq 0x4edcf0
testq %rax, %rax
je 0x4165ed
movq 0x20(%rbx), %rdi
movq $-0x8, %rsi
movl $0x2, %edx
callq 0x4f25e0
movq 0x20(%rbx), %rdi
callq 0x4f26c0
cltq
movq (%r14), %rcx
leaq -0x4(%rcx), %rdx
movq %rdx, (%r14)
negq %rax
addq %rax, %rcx
addq $-0x4, %rcx
movq 0x30(%rbx), %rsi
andq $-0x8, %rcx
movq (%rsi,%rcx), %rax
movq %rax, 0x78(%rbx)
movq 0x8(%rsi,%rcx), %rdx
movq %rdx, 0x80(%rbx)
movq %rdx, %rdi
shrq $0x20, %rdi
movq 0x10(%rsi,%rcx), %r8
movl %r8d, 0xb0(%rbx)
movl %r8d, 0x8c(%rbx)
shrq $0x20, %r8
movq %r8, 0xb8(%rbx)
movq 0x18(%rsi,%rcx), %r9
movq %r9, 0x98(%rbx)
movq 0x20(%rsi,%rcx), %r10
movl %r10d, %r11d
andl $0xf, %r11d
cmpl $0x1, %r11d
setne 0xa0(%rbx)
movabsq $-0x100000000, %r11     # imm = 0xFFFFFFFF00000000
andq %r10, %r11
orq %r8, %r11
movq %r11, 0xb8(%rbx)
movq %r11, 0x90(%rbx)
incq %r9
movq %r9, (%rsi,%rcx)
subl %edi, %eax
shrl $0x2, %eax
movl %eax, 0xa8(%rbx)
addl %edx, %eax
movl %eax, 0xac(%rbx)
movb $0x1, %bpl
jmp 0x4165ef
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq