// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x291a7f(%rip), %rax    # 0x294fe0
movq (%rax), %rcx
leaq 0x22d0c5(%rip), %rdi    # 0x230630
pushq $0x1a
popq %rsi
pushq $0x1
popq %rbp
movq %rbp, %rdx
callq 0x2410
movq %r14, %rdi
callq 0x2090
leaq 0xf(%rax), %rdi
callq 0x2280
movq %rax, %r15
leaq 0x22d0b7(%rip), %rsi    # 0x23064b
xorl %r13d, %r13d
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2050
leaq 0x245137(%rip), %rsi    # 0x2486e2
movq %r15, 0x10(%rsp)
movq %r15, %rdi
callq 0x2210
movq %rax, %r15
movl $0x100, %edi            # imm = 0x100
addq 0x291a11(%rip), %rdi    # 0x294fd8
pushq $0x4
popq %rsi
movq %rbp, %rdx
movq %rax, %rcx
callq 0x2410
leaq 0x38(%rbx), %rdi
pushq $0x8
popq %r14
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x2410
leaq 0x48(%rbx), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x2410
leaq 0x50(%rbx), %rdi
movq %rdi, 0x8(%rsp)
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x2410
leaq 0x58(%rbx), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x2410
leaq 0x60(%rbx), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x2410
andq $0x0, 0x20(%rsp)
cmpq $0x0, 0x50(%rbx)
je 0x36a4
xorl %r12d, %r12d
pushq $0x8
popq %r14
pushq $0x1
popq %rbp
movq (%rbx), %rax
movq (%rax,%r12,8), %rdi
testq %rdi, %rdi
je 0x3681
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x2410
movq (%rbx), %rax
movq (%rax,%r12,8), %rdi
movq (%rdi), %rdx
testq %rdx, %rdx
je 0x3694
addq $0x8, %rdi
movq %r14, %rsi
jmp 0x368c
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x2410
incq %r12
movq 0x8(%rsp), %rax
movq (%rax), %r13
cmpq %r13, %r12
jb 0x364f
movq 0x18(%rbx), %rdi
pushq $0x8
popq %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x2410
cmpq $0x0, 0x50(%rbx)
je 0x36ef
xorl %r12d, %r12d
pushq $0x1
popq %r14
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rax,%r12,8), %rdi
movq (%rcx,%r12,8), %rdx
shrq $0x2, %rdx
incq %rdx
movq %r14, %rsi
movq %r15, %rcx
callq 0x2410
incq %r12
cmpq 0x50(%rbx), %r12
jb 0x36c4
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 0x292025(%rip), %r12    # 0x295758
leaq 0xd0(%r12), %rdi
pushq $0x4
popq %r13
movq %r13, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x2410
leaq 0xd4(%r12), %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x2410
movq $0x1, 0x18(%rsp)
cmpq $0x0, 0x2b0(%r12)
je 0x37cc
leaq 0x18(%rsp), %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x2410
movq 0x8(%rsp), %rax
cmpq $0x0, (%rax)
je 0x37cc
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 0x379c
movq 0x10(%rsp), %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2200
movq %r15, %rdi
callq 0x2190
movq 0x2917f3(%rip), %rax    # 0x294fe0
movq (%rax), %rcx
leaq 0x22ce5b(%rip), %rdi    # 0x230652
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2410