// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Process_Read.cpp
// Function: append_All_reads(All_reads*, char*, unsigned int)
int append_All_reads(All_reads* r, char *idx, uint32_t id)
{
	char *gfa_name = NULL; MALLOC(gfa_name, (strlen(idx)+100));
	FILE *fp = NULL, *fpo = NULL;

	sprintf(gfa_name, "%s.ec%u.bin", idx, id);
    fp = fopen(gfa_name, "r");
	if (!fp) {free(gfa_name); return 0;}

	sprintf(gfa_name, "%s.ovlp%u.source.bin", idx, id);
	fpo = fopen(gfa_name, "r");
	if (!fpo) {free(gfa_name); fclose(fp); return 0;}
	free(gfa_name);

	int local_adapterLen, 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);
    }
	uint64_t index_size0, name_index_size0, total_reads0, total_reads_bases0, total_name_length0;
	index_size0 = r->index_size; 
	total_reads0 = r->total_reads; 
	total_reads_bases0 = r->total_reads_bases;
	total_name_length0 = r->total_name_length;
	name_index_size0 = ((total_reads0)?(total_reads0+1):(0));///r->name_index_size;

    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);

	r->index_size += index_size0;
	r->name_index_size += name_index_size0; if(name_index_size0) r->name_index_size--;
	r->total_reads += total_reads0;
	r->total_reads_bases += total_reads_bases0;
	r->total_name_length += total_name_length0;


	uint64_t i = 0, zero = 0, k;
	REALLOC(r->N_site, r->total_reads);

	for (i = total_reads0; 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;
		}
	}

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

	REALLOC(r->read_size, r->total_reads);
	memcpy (r->read_size + total_reads0, r->read_length + total_reads0, sizeof(uint64_t)*(r->total_reads-total_reads0));

	REALLOC(r->read_sperate, r->total_reads);
	for (i = total_reads0; 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);
	}


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

	REALLOC(r->name_index, r->name_index_size); uint64_t sft = 0;
	if(name_index_size0) sft = r->name_index[--name_index_size0];
	// fprintf(stderr, "name_index_size0::%lu, total_reads0::%lu, sft::%lu\n", name_index_size0, total_reads0, sft);
	f_flag += fread(r->name_index + name_index_size0, sizeof(uint64_t), r->name_index_size-name_index_size0, fp);
	if(sft) {
		for (i = name_index_size0; i < r->name_index_size; i++) r->name_index[i] += sft;
	}

	/****************************may have bugs********************************/
	REALLOC(r->trio_flag, r->total_reads);
	f_flag += fread(r->trio_flag + total_reads0, sizeof(uint8_t), r->total_reads - total_reads0, fp);

	int hom_cov0 = asm_opt.hom_cov, het_cov0 = asm_opt.het_cov;
	f_flag += fread(&(asm_opt.hom_cov), sizeof(asm_opt.hom_cov), 1, fp); asm_opt.hom_cov += hom_cov0;
    f_flag += fread(&(asm_opt.het_cov), sizeof(asm_opt.het_cov), 1, fp); asm_opt.het_cov += het_cov0;
	/****************************may have bugs********************************/

	REALLOC(r->cigars, r->total_reads);
	REALLOC(r->second_round_cigar, r->total_reads);
	for (i = total_reads0; 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;

	REALLOC(r->paf, r->total_reads);
	memset(r->paf+total_reads0, 0, sizeof((*(r->paf)))*(r->total_reads - total_reads0));
	long long n_read; f_flag = fread(&n_read, sizeof(n_read), 1, fpo); ma_hit_t t;
	for (i = total_reads0; i < r->total_reads; i++) {
		f_flag += fread(&(r->paf[i].is_fully_corrected), sizeof(r->paf[i].is_fully_corrected), 1, fpo);
        f_flag += fread(&(r->paf[i].is_abnormal), sizeof(r->paf[i].is_abnormal), 1, fpo);
        f_flag += fread(&(r->paf[i].length), sizeof(r->paf[i].length), 1, fpo);

        if(r->paf[i].length == 0) continue;
        for (k = 0; k < r->paf[i].length; k++) read_ma(&t, fpo);
		r->paf[i].length = 0;
	}

	REALLOC(r->reverse_paf, r->total_reads); 
	memset(r->reverse_paf+total_reads0, 0, sizeof((*(r->reverse_paf)))*(r->total_reads - total_reads0));

    fclose(fp); fclose(fpo);
    fprintf(stderr, "Reads has been loaded.\n");
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %edx, %r15d
movq %rsi, %r12
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x2090
leaq 0x64(%rax), %rdi
callq 0x2280
movq %rax, %r14
leaq 0x1dc4ef(%rip), %rsi    # 0x1e26ca
xorl %ebp, %ebp
movq %rax, %rdi
movq %r12, %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2050
leaq 0x1eb4c3(%rip), %rsi    # 0x1f16b7
movq %r14, %rdi
callq 0x2210
movq %rax, (%rsp)
testq %rax, %rax
je 0x6248
leaq 0x1dc4ca(%rip), %rsi    # 0x1e26d6
xorl %ebp, %ebp
movq %r14, %rdi
movq %r12, %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2050
leaq 0x1eb492(%rip), %rsi    # 0x1f16b7
movq %r14, %rdi
callq 0x2210
movq %rax, %r15
movq %r14, %rdi
callq 0x2220
testq %r15, %r15
jne 0x6264
movq (%rsp), %rdi
callq 0x2190
jmp 0x6250
movq %r14, %rdi
callq 0x2220
movl %ebp, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x54(%rsp), %r14
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
movq (%rsp), %r13
movq %r13, %rcx
callq 0x22f0
movl (%r14), %edx
leaq 0x24d4d0(%rip), %rax    # 0x253758
movl 0x100(%rax), %ecx
cmpl %ecx, %edx
jne 0x67d3
leaq 0x38(%rbx), %rdi
leaq 0x50(%rbx), %rax
movq %rax, 0x10(%rsp)
leaq 0x58(%rbx), %rax
movq %rax, 0x40(%rsp)
leaq 0x60(%rbx), %rax
movq %rax, 0x48(%rsp)
movq 0x60(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0x48(%rbx), %r12
movups 0x50(%rbx), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0x38(%rbx), %rax
movq %rax, 0x18(%rsp)
movq 0x50(%rbx), %r14
leaq 0x1(%r14), %rbp
testq %r14, %r14
cmoveq %r14, %rbp
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
callq 0x22f0
movq %r12, 0x30(%rsp)
movq %r12, %rdi
pushq $0x8
popq %r12
movq %r12, %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
callq 0x22f0
movq 0x10(%rsp), %rdi
movq %r12, %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
callq 0x22f0
movq 0x40(%rsp), %rdi
movq %r12, %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
callq 0x22f0
movq 0x48(%rsp), %rdi
movq %r12, %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
callq 0x22f0
movq 0x18(%rsp), %rax
addq %rax, 0x38(%rbx)
xorl %eax, %eax
movq %rbp, 0x18(%rsp)
subq $0x1, %rbp
cmovaeq %rbp, %rax
addq %rax, 0x48(%rbx)
movdqu 0x50(%rbx), %xmm0
paddq 0x20(%rsp), %xmm0
movdqu %xmm0, 0x50(%rbx)
movq 0x8(%rsp), %rax
addq %rax, 0x60(%rbx)
leaq 0x38(%rsp), %rax
andq $0x0, (%rax)
movq (%rbx), %rdi
movq %xmm0, %rsi
shlq $0x3, %rsi
callq 0x2370
movq %rax, (%rbx)
movq %r14, 0x20(%rsp)
movq (%rsp), %rbp
movq 0x10(%rsp), %rax
movq (%rax), %rsi
cmpq %rsi, %r14
jae 0x640c
leaq 0x38(%rsp), %rdi
movq %r12, %rsi
pushq $0x1
popq %rdx
movq %rbp, %rcx
callq 0x22f0
movq 0x38(%rsp), %r13
testq %r13, %r13
je 0x63fb
leaq 0x8(,%r13,8), %rdi
callq 0x2280
movq (%rbx), %rcx
movq %rax, (%rcx,%r14,8)
movq (%rbx), %rax
movq (%rax,%r14,8), %rdi
movq %r13, (%rdi)
addq $0x8, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x22f0
jmp 0x6403
movq (%rbx), %rax
andq $0x0, (%rax,%r14,8)
incq %r14
pushq $0x8
popq %r12
jmp 0x639f
movq 0x18(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x20(%rsp), %r12
leaq (%rax,%r12,8), %rdi
leaq (,%r12,8), %r14
movq 0x50(%rbx), %rdx
subq %r12, %rdx
pushq $0x8
popq %rsi
movq (%rsp), %rbp
movq %rbp, %rcx
callq 0x22f0
movq 0x20(%rbx), %rdi
movq 0x50(%rbx), %rsi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x20(%rbx)
leaq (%rax,%r12,8), %rdi
addq 0x18(%rbx), %r14
movq 0x50(%rbx), %r13
movq %r13, %rdx
subq %r12, %rdx
shlq $0x3, %rdx
movq %r14, %rsi
callq 0x2160
movq 0x10(%rbx), %rdi
shlq $0x3, %r13
movq %r13, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
pushq $0x1
popq %r14
movq %r12, %r13
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %rax
cmpq (%rax), %r13
jae 0x64e4
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %rdi
shrq $0x2, %rdi
incq %rdi
callq 0x2280
movq 0x10(%rbx), %rcx
movq %rax, (%rcx,%r13,8)
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rax,%r13,8), %rdi
movq (%rcx,%r13,8), %rdx
shrq $0x2, %rdx
incq %rdx
movq %r14, %rsi
movq %rbp, %rcx
callq 0x22f0
incq %r13
jmp 0x6497
movq 0x8(%rbx), %rdi
movq 0x60(%rbx), %rsi
callq 0x2370
movq %rax, 0x8(%rbx)
movq 0x8(%rsp), %rcx
leaq (%rax,%rcx), %rdi
movq 0x60(%rbx), %rdx
subq %rcx, %rdx
pushq $0x1
popq %rsi
movq %rbp, %rcx
callq 0x22f0
movq 0x40(%rbx), %rdi
movq 0x48(%rbx), %rsi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x40(%rbx)
testq %r12, %r12
jne 0x6532
xorl %r12d, %r12d
xorl %r14d, %r14d
jmp 0x653a
movq -0x8(%rax,%r12,8), %r14
decq %r12
movq 0x30(%rsp), %r13
leaq (%rax,%r12,8), %rdi
movq (%r13), %rdx
subq %r12, %rdx
pushq $0x8
popq %rsi
movq %rbp, %rcx
callq 0x22f0
testq %r14, %r14
jne 0x6664
movq 0x28(%rbx), %rdi
movq 0x50(%rbx), %rsi
callq 0x2370
movq %rax, 0x28(%rbx)
movq 0x20(%rsp), %r14
leaq (%rax,%r14), %rdi
movq 0x50(%rbx), %rdx
subq %r14, %rdx
pushq $0x1
popq %rsi
movq %rbp, %rcx
callq 0x22f0
leaq 0x24d1c7(%rip), %r13    # 0x253758
leaq 0xd0(%r13), %rdi
leaq 0xd4(%r13), %rbp
movl 0xd0(%r13), %r12d
movl 0xd4(%r13), %eax
movl %eax, 0x8(%rsp)
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq (%rsp), %rcx
callq 0x22f0
addl %r12d, 0xd0(%r13)
movq %rbp, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq (%rsp), %rcx
callq 0x22f0
movl 0x8(%rsp), %eax
addl %eax, 0xd4(%r13)
movq 0x68(%rbx), %rdi
imulq $0x28, 0x50(%rbx), %rsi
callq 0x2370
movq %rax, 0x68(%rbx)
movq 0x70(%rbx), %rdi
imulq $0x28, 0x50(%rbx), %rsi
callq 0x2370
movq %rax, 0x70(%rbx)
movq 0x50(%rbx), %rcx
imulq $0x28, %r14, %rdx
addq $0x1c, %rdx
cmpq %rcx, %r14
jae 0x667b
movq 0x68(%rbx), %rdi
andl $0x0, -0x10(%rdi,%rdx)
andl $0x0, -0x10(%rax,%rdx)
andl $0x0, -0x14(%rdi,%rdx)
andl $0x0, -0x14(%rax,%rdx)
andq $0x0, -0x1c(%rdi,%rdx)
andq $0x0, -0x1c(%rax,%rdx)
andl $0x0, (%rdi,%rdx)
andl $0x0, (%rax,%rdx)
andl $0x0, -0x4(%rdi,%rdx)
andl $0x0, -0x4(%rax,%rdx)
andq $0x0, -0xc(%rdi,%rdx)
andq $0x0, -0xc(%rax,%rdx)
incq %r14
addq $0x28, %rdx
jmp 0x6614
cmpq (%r13), %r12
jae 0x655e
movq 0x40(%rbx), %rax
addq %r14, (%rax,%r12,8)
incq %r12
jmp 0x6664
movq 0x78(%rbx), %rdi
imulq $0x18, %rcx, %rsi
callq 0x2370
movq %rax, 0x78(%rbx)
movq 0x20(%rsp), %r12
imulq $0x18, %r12, %rcx
movq %rcx, 0x8(%rsp)
leaq (%rax,%rcx), %rdi
movq 0x50(%rbx), %rax
subq %r12, %rax
imulq $0x18, %rax, %rdx
xorl %esi, %esi
callq 0x20e0
leaq 0x58(%rsp), %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x22f0
leaq 0x60(%rsp), %r14
movq 0x10(%rsp), %rax
movq (%rax), %rax
cmpq %rax, %r12
jae 0x676b
movq 0x78(%rbx), %rax
imulq $0x18, %r12, %r13
leaq (%rax,%r13), %rdi
addq $0x10, %rdi
pushq $0x1
popq %rbp
movq %rbp, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x22f0
movq 0x78(%rbx), %rax
leaq (%rax,%r13), %rdi
addq $0x11, %rdi
movq %rbp, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x22f0
movq 0x78(%rbx), %rax
leaq (%rax,%r13), %rdi
addq $0xc, %rdi
pushq $0x4
popq %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x22f0
movq 0x78(%rbx), %rax
movl 0xc(%rax,%r13), %ecx
testl %ecx, %ecx
je 0x6763
xorl %ebp, %ebp
movl %ecx, %ecx
cmpq %rcx, %rbp
jae 0x675d
movq %r14, %rdi
movq %r15, %rsi
callq 0x6043
incq %rbp
movq 0x78(%rbx), %rax
movl 0xc(%rax,%r13), %ecx
jmp 0x673d
andl $0x0, 0xc(%rax,%r13)
incq %r12
jmp 0x66c8
movq 0x80(%rbx), %rdi
imulq $0x18, %rax, %rsi
callq 0x2370
movq %rax, 0x80(%rbx)
movq 0x8(%rsp), %rdi
addq %rax, %rdi
movq 0x50(%rbx), %rax
subq 0x20(%rsp), %rax
imulq $0x18, %rax, %rdx
xorl %esi, %esi
callq 0x20e0
movq (%rsp), %rdi
callq 0x2190
movq %r15, %rdi
callq 0x2190
movq 0x24c82a(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x1dbef2(%rip), %rdi    # 0x1e26b2
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
pushq $0x1
popq %rbp
jmp 0x6250
movq 0x24c806(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x1dbe87(%rip), %rsi    # 0x1e266b
xorl %eax, %eax
callq 0x22b0
pushq $0x1
popq %rdi
callq 0x2250