// File: chhylp123[P]hifiasm/Process_Read.cpp
// Function: load_All_reads(All_reads*, char*)
int load_All_reads(All_reads* r, char* read_file_name)
{
    char* index_name = (char*)malloc(strlen(read_file_name)+15);
    sprintf(index_name, "%s.bin", read_file_name);
    FILE* fp = fopen(index_name, "r");
	if (!fp) {
		free(index_name);
        return 0;
    }
	int local_adapterLen;
	int f_flag;
    f_flag = fread(&local_adapterLen, sizeof(local_adapterLen), 1, fp);
    if(local_adapterLen != asm_opt.adapterLen)
    {
        fprintf(stderr, "the adapterLen of index is: %d, but the adapterLen set by user is: %d\n", 
        local_adapterLen, asm_opt.adapterLen);
		exit(1);
    }
    f_flag += fread(&r->index_size, sizeof(r->index_size), 1, fp);
	f_flag += fread(&r->name_index_size, sizeof(r->name_index_size), 1, fp);
	f_flag += fread(&r->total_reads, sizeof(r->total_reads), 1, fp);
	f_flag += fread(&r->total_reads_bases, sizeof(r->total_reads_bases), 1, fp);
	f_flag += fread(&r->total_name_length, sizeof(r->total_name_length), 1, fp);

	uint64_t i = 0;
	uint64_t zero = 0;
	r->N_site = (uint64_t**)malloc(sizeof(uint64_t*)*r->total_reads);
	for (i = 0; i < r->total_reads; i++)
	{
		f_flag += fread(&zero, sizeof(zero), 1, fp);

		if (zero)
		{
			r->N_site[i] = (uint64_t*)malloc(sizeof(uint64_t)*(zero + 1));
			r->N_site[i][0] = zero;
			if (r->N_site[i][0])
			{
				f_flag += fread(r->N_site[i]+1, sizeof(r->N_site[i][0]), r->N_site[i][0], fp);
			}
		}
		else
		{
			r->N_site[i] = NULL;
		}
	}

	r->read_length = (uint64_t*)malloc(sizeof(uint64_t)*r->total_reads);
	f_flag += fread(r->read_length, sizeof(uint64_t), r->total_reads, fp);

	r->read_size = (uint64_t*)malloc(sizeof(uint64_t)*r->total_reads);
	memcpy (r->read_size, r->read_length, sizeof(uint64_t)*r->total_reads);

	r->read_sperate = (uint8_t**)malloc(sizeof(uint8_t*)*r->total_reads);
	for (i = 0; i < r->total_reads; i++)
    {
		r->read_sperate[i] = (uint8_t*)malloc(sizeof(uint8_t)*(r->read_length[i]/4+1));
		f_flag += fread(r->read_sperate[i], sizeof(uint8_t), r->read_length[i]/4+1, fp);
	}


	r->name = (char*)malloc(sizeof(char)*r->total_name_length);
	f_flag += fread(r->name, sizeof(char), r->total_name_length, fp);

	r->name_index = (uint64_t*)malloc(sizeof(uint64_t)*r->name_index_size);
	f_flag += fread(r->name_index, sizeof(uint64_t), r->name_index_size, fp);

	/****************************may have bugs********************************/
	r->trio_flag = (uint8_t*)malloc(sizeof(uint8_t)*r->total_reads);
	f_flag += fread(r->trio_flag, sizeof(uint8_t), r->total_reads, fp);
	f_flag += fread(&(asm_opt.hom_cov), sizeof(asm_opt.hom_cov), 1, fp);
    f_flag += fread(&(asm_opt.het_cov), sizeof(asm_opt.het_cov), 1, fp);
	/****************************may have bugs********************************/

	r->cigars = (Compressed_Cigar_record*)malloc(sizeof(Compressed_Cigar_record)*r->total_reads);
	r->second_round_cigar = (Compressed_Cigar_record*)malloc(sizeof(Compressed_Cigar_record)*r->total_reads);
	for (i = 0; i < r->total_reads; i++)
	{
		r->second_round_cigar[i].size = r->cigars[i].size = 0;
		r->second_round_cigar[i].length = r->cigars[i].length = 0;
		r->second_round_cigar[i].record = r->cigars[i].record = NULL;

		r->second_round_cigar[i].lost_base_size = r->cigars[i].lost_base_size = 0;
		r->second_round_cigar[i].lost_base_length = r->cigars[i].lost_base_length = 0;
		r->second_round_cigar[i].lost_base = r->cigars[i].lost_base = NULL;
	}
	///r->pb_regions = NULL;

	uint64_t mm = 0;
	if (!feof(fp)) {
		if((fread(&mm, sizeof(mm), 1, fp)) && (mm == 1)) {
			MALLOC(r->rsc, r->total_reads);
			for (i = 0; i < r->total_reads; i++) {
				MALLOC(r->rsc[i], (r->read_length[i]/sc_bn) + ((r->read_length[i]%sc_bn)?1:0));
				f_flag += fread(r->rsc[i], sizeof(uint8_t), (r->read_length[i]/sc_bn) + ((r->read_length[i]%sc_bn)?1:0), fp);
			}
		}
	}



    free(index_name);    
    fclose(fp);
    fprintf(stderr, "Reads has been loaded.\n");

	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x20a0
movq %rax, %rdi
addq $0xf, %rdi
callq 0x2270
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x379a42(%rip), %rsi    # 0x38141a
movb $0x0, %al
callq 0x2050
movq -0x20(%rbp), %rdi
leaq 0x3889ec(%rip), %rsi    # 0x3903d6
callq 0x2200
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x7a0f
movq -0x20(%rbp), %rdi
callq 0x2210
movl $0x0, -0x4(%rbp)
jmp 0x81c2
movq -0x28(%rbp), %rcx
leaq -0x2c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x22e0
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
leaq 0x3bcdc5(%rip), %rcx    # 0x3c47f8
cmpl 0x100(%rcx), %eax
je 0x7a6d
movq 0x3bc59e(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movl -0x2c(%rbp), %edx
leaq 0x3bcda9(%rip), %rax    # 0x3c47f8
movl 0x100(%rax), %ecx
leaq 0x3799de(%rip), %rsi    # 0x38143a
movb $0x0, %al
callq 0x22a0
movl $0x1, %edi
callq 0x2240
movq -0x10(%rbp), %rdi
addq $0x38, %rdi
movq -0x28(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x30(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
movq -0x28(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x30(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
addq $0x50, %rdi
movq -0x28(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x30(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
movq -0x28(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x30(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
addq $0x60, %rdi
movq -0x28(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x30(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
shlq $0x3, %rdi
callq 0x2270
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jae 0x7c5f
movq -0x28(%rbp), %rcx
leaq -0x40(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x30(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x30(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x7c39
movq -0x40(%rbp), %rdi
addq $0x1, %rdi
shlq $0x3, %rdi
callq 0x2270
movq %rax, %rdx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq $0x0, (%rax)
je 0x7c37
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
addq $0x8, %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rdx
movq -0x28(%rbp), %rcx
movl $0x8, %esi
callq 0x22e0
movq %rax, %rcx
movslq -0x30(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x30(%rbp)
jmp 0x7c4c
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
jmp 0x7c4e
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x7b68
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
shlq $0x3, %rdi
callq 0x2270
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdx
movq -0x28(%rbp), %rcx
movl $0x8, %esi
callq 0x22e0
movq %rax, %rcx
movslq -0x30(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
shlq $0x3, %rdi
callq 0x2270
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdx
shlq $0x3, %rdx
callq 0x2180
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
shlq $0x3, %rdi
callq 0x2270
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jae 0x7da1
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
shrq $0x2, %rdi
addq $0x1, %rdi
shlq $0x0, %rdi
callq 0x2270
movq %rax, %rdx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shrq $0x2, %rdx
addq $0x1, %rdx
movq -0x28(%rbp), %rcx
movl $0x1, %esi
callq 0x22e0
movq %rax, %rcx
movslq -0x30(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x30(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x7d07
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
shlq $0x0, %rdi
callq 0x2270
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdx
movq -0x28(%rbp), %rcx
movl $0x1, %esi
callq 0x22e0
movq %rax, %rcx
movslq -0x30(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
shlq $0x3, %rdi
callq 0x2270
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdx
movq -0x28(%rbp), %rcx
movl $0x8, %esi
callq 0x22e0
movq %rax, %rcx
movslq -0x30(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
shlq $0x0, %rdi
callq 0x2270
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdx
movq -0x28(%rbp), %rcx
movl $0x1, %esi
callq 0x22e0
movq %rax, %rcx
movslq -0x30(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x30(%rbp)
movq -0x28(%rbp), %rcx
leaq 0x3bc977(%rip), %rdi    # 0x3c47f8
addq $0xd0, %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x30(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x30(%rbp)
movq -0x28(%rbp), %rcx
leaq 0x3bc949(%rip), %rdi    # 0x3c47f8
addq $0xd4, %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x30(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
imulq $0x28, 0x50(%rax), %rdi
callq 0x2270
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x10(%rbp), %rax
imulq $0x28, 0x50(%rax), %rdi
callq 0x2270
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x70(%rax)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jae 0x8045
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl $0x0, 0xc(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl $0x0, 0xc(%rax)
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl $0x0, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl $0x0, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movq $0x0, (%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movq $0x0, (%rax)
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl $0x0, 0x1c(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl $0x0, 0x1c(%rax)
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl $0x0, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl $0x0, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movq $0x0, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movq $0x0, 0x10(%rax)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x7f0c
movq $0x0, -0x48(%rbp)
movq -0x28(%rbp), %rdi
callq 0x2310
cmpl $0x0, %eax
jne 0x8191
movq -0x28(%rbp), %rcx
leaq -0x48(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x22e0
cmpq $0x0, %rax
je 0x818f
cmpq $0x1, -0x48(%rbp)
jne 0x818f
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
shlq $0x3, %rdi
callq 0x2270
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jae 0x818d
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
shrq %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
andq $0x1, %rdx
xorl %eax, %eax
movl $0x1, %ecx
cmpq $0x0, %rdx
cmovnel %ecx, %eax
cltq
addq %rax, %rdi
shlq $0x0, %rdi
callq 0x2270
movq %rax, %rdx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x38(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shrq %rdx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
andq $0x1, %rsi
xorl %eax, %eax
movl $0x1, %ecx
cmpq $0x0, %rsi
cmovnel %ecx, %eax
cltq
addq %rax, %rdx
movq -0x28(%rbp), %rcx
movl $0x1, %esi
callq 0x22e0
movq %rax, %rcx
movslq -0x30(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x30(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x80af
jmp 0x818f
jmp 0x8191
movq -0x20(%rbp), %rdi
callq 0x2210
movq -0x28(%rbp), %rdi
callq 0x21a0
movq 0x3bbe36(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x3792cd(%rip), %rsi    # 0x381481
movb $0x0, %al
callq 0x22a0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)