// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Process_Read.cpp
// Function: write_All_reads(All_reads*, char*)
void write_All_reads(All_reads* r, char* read_file_name)
{
    fprintf(stderr, "Writing reads to disk... \n");
    char* index_name = (char*)malloc(strlen(read_file_name)+15);
    sprintf(index_name, "%s.bin", read_file_name);
    FILE* fp = fopen(index_name, "w");
	fwrite(&asm_opt.adapterLen, sizeof(asm_opt.adapterLen), 1, fp);
    fwrite(&r->index_size, sizeof(r->index_size), 1, fp);
	fwrite(&r->name_index_size, sizeof(r->name_index_size), 1, fp);
	fwrite(&r->total_reads, sizeof(r->total_reads), 1, fp);
	fwrite(&r->total_reads_bases, sizeof(r->total_reads_bases), 1, fp);
	fwrite(&r->total_name_length, sizeof(r->total_name_length), 1, fp);

	uint64_t i = 0;
	uint64_t zero = 0;
	for (i = 0; i < r->total_reads; i++)
	{
		if (r->N_site[i] != NULL)
		{
			///number of Ns
			fwrite(&r->N_site[i][0], sizeof(r->N_site[i][0]), 1, fp);
			if (r->N_site[i][0])
			{
				fwrite(r->N_site[i]+1, sizeof(r->N_site[i][0]), r->N_site[i][0], fp);
			}
		}
		else
		{
			fwrite(&zero, sizeof(zero), 1, fp);
		}
	}

	fwrite(r->read_length, sizeof(uint64_t), r->total_reads, fp);
	for (i = 0; i < r->total_reads; i++)
	{
		fwrite(r->read_sperate[i], sizeof(uint8_t), r->read_length[i]/4+1, fp);
	}
	
	fwrite(r->name, sizeof(char), r->total_name_length, fp);
	fwrite(r->name_index, sizeof(uint64_t), r->name_index_size, fp);
	fwrite(r->trio_flag, sizeof(uint8_t), r->total_reads, fp);
	fwrite(&(asm_opt.hom_cov), sizeof(asm_opt.hom_cov), 1, fp);
	fwrite(&(asm_opt.het_cov), sizeof(asm_opt.het_cov), 1, fp);

	uint64_t mm = 1;
	if(asm_opt.is_sc) {
		fwrite(&mm, sizeof(mm), 1, fp);
		for (i = 0; i < r->total_reads; i++) {
			fwrite(r->rsc[i], sizeof(uint8_t), ((r->read_length[i]/sc_bn) + ((r->read_length[i]%sc_bn)?1:0)), fp);
		}
	}

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x286a78(%rip), %rax    # 0x289fe0
movq (%rax), %rcx
leaq 0x2210ae(%rip), %rdi    # 0x224620
pushq $0x1a
popq %rsi
pushq $0x1
popq %r13
movq %r13, %rdx
callq 0x2410
movq %r14, %rdi
callq 0x2090
leaq 0xf(%rax), %rdi
callq 0x2280
movq %rax, %r15
leaq 0x22109f(%rip), %rsi    # 0x22463b
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2050
leaq 0x238e4e(%rip), %rsi    # 0x23c3fe
movq %r15, (%rsp)
movq %r15, %rdi
callq 0x2210
movq %rax, %r15
movl $0x100, %edi            # imm = 0x100
addq 0x286a0d(%rip), %rdi    # 0x289fd8
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %rax, %rcx
callq 0x2410
leaq 0x38(%rbx), %rdi
pushq $0x8
popq %r14
movq %r14, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x2410
leaq 0x48(%rbx), %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x2410
leaq 0x50(%rbx), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x2410
leaq 0x58(%rbx), %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x2410
leaq 0x60(%rbx), %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x2410
andq $0x0, 0x10(%rsp)
movq 0x50(%rbx), %rdx
testq %rdx, %rdx
je 0x36a4
xorl %r13d, %r13d
pushq $0x8
popq %rbp
pushq $0x1
popq %r14
movq (%rbx), %rax
movq (%rax,%r13,8), %rdi
testq %rdi, %rdi
je 0x3685
movq %rbp, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x2410
movq (%rbx), %rax
movq (%rax,%r13,8), %rdi
movq (%rdi), %rdx
testq %rdx, %rdx
je 0x3698
addq $0x8, %rdi
movq %rbp, %rsi
jmp 0x3690
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x2410
incq %r13
movq (%r12), %rdx
cmpq %rdx, %r13
jb 0x3653
movq 0x18(%rbx), %rdi
pushq $0x8
popq %rsi
movq %r15, %rcx
callq 0x2410
cmpq $0x0, 0x50(%rbx)
je 0x36ec
xorl %r13d, %r13d
pushq $0x1
popq %r14
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 %r15, %rcx
callq 0x2410
incq %r13
cmpq 0x50(%rbx), %r13
jb 0x36c1
movq 0x8(%rbx), %rdi
movq 0x60(%rbx), %rdx
pushq $0x1
popq %r14
movq %r14, %rsi
movq %r15, %rcx
callq 0x2410
movq 0x40(%rbx), %rdi
movq 0x48(%rbx), %rdx
pushq $0x8
popq %rsi
movq %r15, %rcx
callq 0x2410
movq 0x28(%rbx), %rdi
movq 0x50(%rbx), %rdx
movq %r14, %rsi
movq %r15, %rcx
callq 0x2410
leaq 0x287028(%rip), %rbp    # 0x28a758
leaq 0xd0(%rbp), %rdi
pushq $0x4
popq %r13
movq %r13, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x2410
leaq 0xd4(%rbp), %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x2410
movq $0x1, 0x8(%rsp)
cmpq $0x0, 0x2b0(%rbp)
je 0x37c2
leaq 0x8(%rsp), %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x2410
cmpq $0x0, (%r12)
je 0x37c2
xorl %r12d, %r12d
pushq $0x1
popq %r14
movq 0x18(%rbx), %rax
movq 0x30(%rbx), %rcx
movq (%rcx,%r12,8), %rdi
movq (%rax,%r12,8), %rdx
movq %rdx, %rax
shrq %rax
andl $0x1, %edx
addq %rax, %rdx
movq %r14, %rsi
movq %r15, %rcx
callq 0x2410
incq %r12
cmpq 0x50(%rbx), %r12
jb 0x3792
movq (%rsp), %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2200
movq %r15, %rdi
callq 0x2190
movq 0x2867fe(%rip), %rax    # 0x289fe0
movq (%rax), %rcx
leaq 0x220e56(%rip), %rdi    # 0x224642
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2410