// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x2090
leaq 0x64(%rax), %rdi
callq 0x2280
movq %rax, %r12
leaq 0x1f1a2a(%rip), %rsi    # 0x2246ba
xorl %ebp, %ebp
movq %rax, %rdi
movq %r13, %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2050
leaq 0x2008fe(%rip), %rsi    # 0x2335a7
movq %r12, %rdi
callq 0x2210
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x32cff
leaq 0x1f1a04(%rip), %rsi    # 0x2246c6
xorl %ebp, %ebp
movq %r12, %rdi
movq %r13, %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2050
leaq 0x2008cc(%rip), %rsi    # 0x2335a7
movq %r12, %rdi
callq 0x2210
movq %rax, %r15
movq %r12, %rdi
callq 0x2220
testq %r15, %r15
jne 0x32d1b
movq 0x10(%rsp), %rdi
callq 0x2190
jmp 0x32d07
movq %r12, %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), %r12
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq 0x10(%rsp), %r14
movq %r14, %rcx
callq 0x22f0
movl (%r12), %edi
leaq 0x257a13(%rip), %rax    # 0x28a758
movl 0x100(%rax), %esi
cmpl %esi, %edi
jne 0x3331d
leaq 0x38(%rbx), %rdi
leaq 0x50(%rbx), %rbp
movq 0x38(%rbx), %rax
movq %rax, 0x38(%rsp)
movq 0x50(%rbx), %r13
leaq 0x58(%rbx), %rax
movq %rax, 0x28(%rsp)
movq 0x58(%rbx), %rax
movq %rax, 0x40(%rsp)
leaq 0x60(%rbx), %rax
movq %rax, 0x30(%rsp)
movq 0x60(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0x1(%r13), %r12
testq %r13, %r13
cmoveq %r13, %r12
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x22f0
leaq 0x48(%rbx), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rdi, 0x48(%rsp)
movq %r14, %rcx
callq 0x22f0
movl $0x8, %esi
movl $0x1, %edx
movq %rbp, 0x18(%rsp)
movq %rbp, %rdi
movq %r14, %rcx
callq 0x22f0
movl $0x8, %esi
movl $0x1, %edx
movq 0x28(%rsp), %rdi
movq %r14, %rcx
callq 0x22f0
movl $0x8, %esi
movl $0x1, %edx
movq 0x30(%rsp), %rdi
movq %r14, %rcx
callq 0x22f0
movq 0x38(%rsp), %rax
addq %rax, 0x38(%rbx)
xorl %eax, %eax
movq %r12, %rcx
subq $0x1, %rcx
cmovaeq %rcx, %rax
addq %rax, 0x48(%rbx)
movq 0x50(%rbx), %rsi
addq %r13, %rsi
movq %rsi, 0x50(%rbx)
movq 0x40(%rsp), %rax
addq %rax, 0x58(%rbx)
movq 0x8(%rsp), %rax
addq %rax, 0x60(%rbx)
movq $0x0, 0x20(%rsp)
movq (%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, (%rbx)
movq 0x50(%rbx), %rsi
movq %r13, (%rsp)
cmpq %rsi, %r13
movq %r14, %r13
jae 0x32ee5
movq (%rsp), %r14
movl $0x8, %esi
movl $0x1, %edx
leaq 0x20(%rsp), %rdi
movq %r13, %rcx
callq 0x22f0
movq %r13, %rbp
movq 0x20(%rsp), %r13
testq %r13, %r13
je 0x32ec7
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
movl $0x8, %esi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x22f0
jmp 0x32ed2
movq (%rbx), %rax
movq $0x0, (%rax,%r14,8)
movq %rbp, %r13
incq %r14
movq 0x18(%rsp), %rax
movq (%rax), %rsi
cmpq %rsi, %r14
jb 0x32e6f
movq 0x18(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x18(%rbx)
movq (%rsp), %r14
leaq (%rax,%r14,8), %rdi
leaq (,%r14,8), %rbp
movq 0x50(%rbx), %rdx
subq %r14, %rdx
movl $0x8, %esi
movq %r13, %rcx
callq 0x22f0
movq 0x20(%rbx), %rdi
movq 0x50(%rbx), %rsi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x20(%rbx)
leaq (%rax,%r14,8), %rdi
addq 0x18(%rbx), %rbp
movq 0x50(%rbx), %r13
movq %r13, %rdx
subq %r14, %rdx
shlq $0x3, %rdx
movq %rbp, %rsi
callq 0x2160
movq 0x10(%rbx), %rdi
shlq $0x3, %r13
movq %r13, %rsi
movq 0x10(%rsp), %r13
callq 0x2370
movq %rax, 0x10(%rbx)
cmpq 0x50(%rbx), %r14
jae 0x32fb9
movq (%rsp), %r14
movq 0x18(%rbx), %rax
movq (%rax,%r14,8), %rdi
shrq $0x2, %rdi
incq %rdi
callq 0x2280
movq 0x10(%rbx), %rcx
movq %rax, (%rcx,%r14,8)
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rax,%r14,8), %rdi
movq (%rcx,%r14,8), %rdx
shrq $0x2, %rdx
incq %rdx
movl $0x1, %esi
movq %r13, %rcx
callq 0x22f0
incq %r14
cmpq 0x50(%rbx), %r14
jb 0x32f70
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
movl $0x1, %esi
movq %r13, %rcx
callq 0x22f0
movq 0x40(%rbx), %rdi
movq 0x48(%rbx), %rsi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x40(%rbx)
testq %r12, %r12
movq 0x48(%rsp), %rbp
jne 0x3300e
xorl %r12d, %r12d
xorl %r14d, %r14d
jmp 0x33016
movq -0x8(%rax,%r12,8), %r14
decq %r12
leaq (%rax,%r12,8), %rdi
movq (%rbp), %rdx
subq %r12, %rdx
movl $0x8, %esi
movq %r13, %rcx
callq 0x22f0
testq %r14, %r14
je 0x3304a
cmpq (%rbp), %r12
jae 0x3304a
movq 0x40(%rbx), %rax
addq %r14, (%rax,%r12,8)
incq %r12
cmpq (%rbp), %r12
jb 0x3303d
movq 0x28(%rbx), %rdi
movq 0x50(%rbx), %rsi
callq 0x2370
movq %rax, 0x28(%rbx)
movq (%rsp), %rcx
leaq (%rax,%rcx), %rdi
movq 0x50(%rbx), %rdx
subq %rcx, %rdx
movl $0x1, %esi
movq %r13, %rcx
callq 0x22f0
leaq 0x2576da(%rip), %r14    # 0x28a758
leaq 0xd0(%r14), %rdi
movq %r13, %r12
leaq 0xd4(%r14), %r13
movl 0xd0(%r14), %ebp
movl 0xd4(%r14), %eax
movl %eax, 0x8(%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rcx
callq 0x22f0
addl %ebp, 0xd0(%r14)
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rcx
callq 0x22f0
movl 0x8(%rsp), %eax
addl %eax, 0xd4(%r14)
movq 0x50(%rbx), %rax
movq 0x68(%rbx), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
callq 0x2370
movq %rax, 0x68(%rbx)
movq 0x50(%rbx), %rax
movq 0x70(%rbx), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
callq 0x2370
movq %rax, 0x70(%rbx)
movq 0x50(%rbx), %rcx
movq %rcx, %rdx
subq (%rsp), %rdx
jbe 0x33173
movq 0x68(%rbx), %rsi
movq (%rsp), %rdi
leaq (%rdi,%rdi,4), %rdi
leaq 0x1c(,%rdi,8), %rdi
xorl %r8d, %r8d
movl %r8d, -0x10(%rsi,%rdi)
movl %r8d, -0x10(%rax,%rdi)
movl %r8d, -0x14(%rsi,%rdi)
movl %r8d, -0x14(%rax,%rdi)
movq %r8, -0x1c(%rsi,%rdi)
movq %r8, -0x1c(%rax,%rdi)
movl %r8d, (%rsi,%rdi)
movl %r8d, (%rax,%rdi)
movl %r8d, -0x4(%rsi,%rdi)
movl %r8d, -0x4(%rax,%rdi)
movq %r8, -0xc(%rsi,%rdi)
movq %r8, -0xc(%rax,%rdi)
addq $0x28, %rdi
decq %rdx
jne 0x33130
movq 0x78(%rbx), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq %rax, 0x78(%rbx)
movq (%rsp), %r14
leaq (%r14,%r14,2), %rcx
movq %rcx, 0x8(%rsp)
leaq (%rax,%rcx,8), %rdi
movq 0x50(%rbx), %rax
subq %r14, %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdx
xorl %esi, %esi
callq 0x20e0
leaq 0x58(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
movq 0x50(%rbx), %rax
cmpq %rax, %r14
jae 0x332a6
leaq 0x60(%rsp), %r13
movq (%rsp), %rbp
movq 0x78(%rbx), %rax
leaq (,%rbp,8), %rcx
leaq (%rcx,%rcx,2), %r14
leaq (%rax,%r14), %rdi
addq $0x10, %rdi
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
movq 0x78(%rbx), %rax
leaq (%rax,%r14), %rdi
addq $0x11, %rdi
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
movq 0x78(%rbx), %rax
leaq (%rax,%r14), %rdi
addq $0xc, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
movq 0x78(%rbx), %rax
cmpl $0x0, 0xc(%rax,%r14)
je 0x33292
movq 0x78(%rbx), %rax
leaq (,%rbp,2), %r14
addq %rbp, %r14
cmpl $0x0, 0xc(%rax,%r14,8)
je 0x33284
xorl %r12d, %r12d
movq %r13, %rdi
movq %r15, %rsi
callq 0x32ae0
incq %r12
movq 0x78(%rbx), %rax
movl 0xc(%rax,%r14,8), %ecx
cmpq %rcx, %r12
jb 0x33268
leaq (%rax,%r14,8), %rax
addq $0xc, %rax
movl $0x0, (%rax)
incq %rbp
movq 0x18(%rsp), %rax
movq (%rax), %rax
cmpq %rax, %rbp
jb 0x331dc
movq 0x80(%rbx), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq %rax, 0x80(%rbx)
movq 0x8(%rsp), %rcx
leaq (%rax,%rcx,8), %rdi
movq 0x50(%rbx), %rax
subq (%rsp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x10(%rsp), %rdi
callq 0x2190
movq %r15, %rdi
callq 0x2190
movq 0x256ce6(%rip), %rax    # 0x289fe0
movq (%rax), %rcx
leaq 0x1f139e(%rip), %rdi    # 0x2246a2
movl $0x17, %esi
movl $0x1, %edx
callq 0x2410
movl $0x1, %ebp
jmp 0x32d07
callq 0x3a62