// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x70(%rbp)
movq 0x20(%rax), %rdi
callq 0x501d20
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0x20(%rbp)
movq 0x20(%rax), %rdi
movq $-0xc, %rsi
movl $0x2, %edx
callq 0x506370
movq -0x70(%rbp), %rax
movq %rax, %rdi
addq $0xa4, %rdi
movq 0x20(%rax), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x501c20
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0x28(%rbp)
cmpl $0x0, 0xa4(%rax)
je 0x423b3f
movq -0x70(%rbp), %rax
cmpl $0x200, 0xa4(%rax)      # imm = 0x200
je 0x423b3f
movb $0x0, -0x1(%rbp)
jmp 0x424142
movq -0x70(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x20(%rbp), %rsi
xorl %edx, %edx
callq 0x506370
movq -0x70(%rbp), %rax
cmpl $0x200, 0xa4(%rax)      # imm = 0x200
jne 0x423eec
movq -0x70(%rbp), %rax
movq 0x20(%rax), %rdi
movq $-0x8, %rsi
movl $0x2, %edx
callq 0x506370
movq -0x70(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x506450
movl %eax, %ecx
movq -0x70(%rbp), %rax
movslq %ecx, %rcx
movq %rcx, -0x30(%rbp)
movq -0x18(%rbp), %rcx
movq (%rcx), %rdx
subq $0x4, %rdx
movq -0x18(%rbp), %rcx
movq %rdx, (%rcx)
movq 0x20(%rax), %rdi
movq -0x30(%rbp), %rax
addq $0x8, %rax
xorl %ecx, %ecx
movl %ecx, %esi
subq %rax, %rsi
movl $0x2, %edx
callq 0x506370
movq -0x70(%rbp), %rax
movq %rax, %rdi
addq $0x78, %rdi
movq 0x20(%rax), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x501c20
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0x28(%rbp)
movq %rax, %rdi
addq $0x7c, %rdi
movq 0x20(%rax), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x501c20
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0x28(%rbp)
movq %rax, %rdi
addq $0x80, %rdi
movq 0x20(%rax), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x501c20
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0x28(%rbp)
movq %rax, %rdi
addq $0x84, %rdi
movq 0x20(%rax), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x501c20
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0x28(%rbp)
movq %rax, %rdi
addq $0x88, %rdi
movq 0x20(%rax), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x501c20
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0x28(%rbp)
movq %rax, %rdi
addq $0x8c, %rdi
movq 0x20(%rax), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x501c20
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0x28(%rbp)
movl 0x8c(%rax), %ecx
movl %ecx, 0xb0(%rax)
movq 0x20(%rax), %rcx
leaq -0x34(%rbp), %rdi
movl $0x1, %esi
movl $0x4, %edx
callq 0x501c20
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0x28(%rbp)
movl -0x34(%rbp), %ecx
movq %rcx, 0x90(%rax)
movq 0x90(%rax), %rcx
movq %rcx, 0xb8(%rax)
movq %rax, %rdi
addq $0x98, %rdi
movq 0x20(%rax), %rcx
movl $0x1, %esi
movl $0x8, %edx
callq 0x501c20
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0x28(%rbp)
movq %rax, %rdi
addq $0xa0, %rdi
movq 0x20(%rax), %rcx
movl $0x1, %edx
movq %rdx, %rsi
callq 0x501c20
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0x28(%rbp)
movb 0xa0(%rax), %cl
xorb $-0x1, %cl
andb $0x1, %cl
movb %cl, 0xa0(%rax)
movl 0x88(%rax), %ecx
shll %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
addl $0x1, %ecx
movl %ecx, 0x58(%rax)
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
movl 0x58(%rax), %edx
shlq $0x2, %rdx
addq -0x30(%rbp), %rdx
addq $0x8, %rdx
subq %rdx, %rcx
movq %rcx, -0x40(%rbp)
movl 0x84(%rax), %ecx
shll %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
movl %ecx, 0x48(%rax)
movq -0x40(%rbp), %rcx
shrq $0x3, %rcx
movq %rcx, -0x48(%rbp)
movq 0x20(%rax), %rdi
callq 0x5066b0
movq -0x70(%rbp), %rax
movq 0x20(%rax), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x506370
movq -0x70(%rbp), %rax
movq -0x40(%rbp), %rcx
addq $0x8, %rcx
shrq $0x3, %rcx
movq %rcx, 0x38(%rax)
movq 0x38(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x42fb90
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x30(%rax)
movq 0x30(%rax), %rdi
movq -0x40(%rbp), %rdx
addq $0x8, %rdx
movq 0x20(%rax), %rcx
movl $0x1, %esi
callq 0x501c20
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x423e42
movb $0x0, -0x1(%rbp)
jmp 0x424142
movq -0x70(%rbp), %rax
movq 0x98(%rax), %rsi
addq $0x1, %rsi
movq 0x30(%rax), %rcx
movq -0x48(%rbp), %rdx
movq %rsi, (%rcx,%rdx,8)
movl 0x58(%rax), %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x42fb90
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x50(%rax)
movq 0x50(%rax), %rdi
movl 0x58(%rax), %ecx
movl %ecx, %edx
shlq $0x2, %rdx
movq 0x20(%rax), %rcx
movl $0x1, %esi
callq 0x501c20
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x423ebb
movb $0x0, -0x1(%rbp)
jmp 0x424142
movq -0x70(%rbp), %rax
movl 0x78(%rax), %ecx
subl 0x84(%rax), %ecx
shrl $0x2, %ecx
movl %ecx, 0xa8(%rax)
movl 0xa8(%rax), %ecx
addl 0x80(%rax), %ecx
movl %ecx, 0xac(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x424142
movq -0x70(%rbp), %rax
cmpl $0x0, 0xa4(%rax)
jne 0x42413c
movq -0x70(%rbp), %rax
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
subq $0x4, %rcx
shrq $0x3, %rcx
movq %rcx, 0x38(%rax)
movq 0x38(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x42fb90
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x30(%rax)
movq 0x30(%rax), %rdi
movq -0x18(%rbp), %rcx
movq (%rcx), %rdx
subq $0x4, %rdx
movq 0x20(%rax), %rcx
movl $0x1, %esi
callq 0x501c20
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x423f75
movb $0x0, -0x1(%rbp)
jmp 0x424142
movq -0x70(%rbp), %rax
movq 0x20(%rax), %rdi
movq $-0x8, %rsi
movl $0x2, %edx
callq 0x506370
movq -0x70(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x506450
movl %eax, %ecx
movq -0x70(%rbp), %rax
movslq %ecx, %rcx
movq %rcx, -0x50(%rbp)
movq -0x18(%rbp), %rcx
movq (%rcx), %rdx
subq $0x4, %rdx
movq -0x18(%rbp), %rcx
movq %rdx, (%rcx)
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
subq -0x50(%rbp), %rcx
shrq $0x3, %rcx
movq %rcx, -0x58(%rbp)
movq -0x58(%rbp), %rcx
movq %rcx, -0x60(%rbp)
movq 0x30(%rax), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq %rcx, -0x68(%rbp)
movq -0x68(%rbp), %rcx
movl %ecx, 0x78(%rax)
movq -0x68(%rbp), %rcx
shrq $0x20, %rcx
movl %ecx, 0x7c(%rax)
movq -0x58(%rbp), %rcx
addq $0x1, %rcx
movq %rcx, -0x58(%rbp)
movq 0x30(%rax), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, 0x80(%rax)
movq 0x30(%rax), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
movl %ecx, 0x84(%rax)
movq -0x58(%rbp), %rcx
addq $0x1, %rcx
movq %rcx, -0x58(%rbp)
movq 0x30(%rax), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, 0xb0(%rax)
movl 0xb0(%rax), %ecx
movl %ecx, 0x8c(%rax)
movq 0x30(%rax), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
movq %rcx, 0xb8(%rax)
movq -0x58(%rbp), %rcx
addq $0x1, %rcx
movq %rcx, -0x58(%rbp)
movq 0x30(%rax), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq %rcx, 0x98(%rax)
movq -0x58(%rbp), %rcx
addq $0x1, %rcx
movq %rcx, -0x58(%rbp)
movq 0x30(%rax), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
andq $0xf, %rcx
cmpq $0x1, %rcx
sete %cl
andb $0x1, %cl
movb %cl, 0xa0(%rax)
movb 0xa0(%rax), %cl
xorb $-0x1, %cl
andb $0x1, %cl
movb %cl, 0xa0(%rax)
movq 0x30(%rax), %rdx
movq -0x58(%rbp), %rsi
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
andq (%rdx,%rsi,8), %rcx
addq 0xb8(%rax), %rcx
movq %rcx, 0xb8(%rax)
movq 0xb8(%rax), %rcx
movq %rcx, 0x90(%rax)
movq 0x98(%rax), %rsi
addq $0x1, %rsi
movq 0x30(%rax), %rcx
movq -0x60(%rbp), %rdx
movq %rsi, (%rcx,%rdx,8)
movl 0x78(%rax), %ecx
subl 0x84(%rax), %ecx
shrl $0x2, %ecx
movl %ecx, 0xa8(%rax)
movl 0xa8(%rax), %ecx
addl 0x80(%rax), %ecx
movl %ecx, 0xac(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x424142
jmp 0x42413e
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)