// File: 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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq 0x3bca19(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x379e2e(%rip), %rsi    # 0x3813ff
movb $0x0, %al
callq 0x22a0
movq -0x10(%rbp), %rdi
callq 0x20a0
movq %rax, %rdi
addq $0xf, %rdi
callq 0x2270
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq 0x379e1a(%rip), %rsi    # 0x38141a
movb $0x0, %al
callq 0x2050
movq -0x18(%rbp), %rdi
leaq 0x391dc0(%rip), %rsi    # 0x3993d2
callq 0x2200
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rcx
leaq 0x3bd1d2(%rip), %rdi    # 0x3c47f8
addq $0x100, %rdi            # imm = 0x100
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x8(%rbp), %rdi
addq $0x38, %rdi
movq -0x20(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x2400
movq -0x8(%rbp), %rdi
addq $0x48, %rdi
movq -0x20(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x2400
movq -0x8(%rbp), %rdi
addq $0x50, %rdi
movq -0x20(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x2400
movq -0x8(%rbp), %rdi
addq $0x58, %rdi
movq -0x20(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x2400
movq -0x8(%rbp), %rdi
addq $0x60, %rdi
movq -0x20(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x2400
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jae 0x7795
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x776b
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x20(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x2400
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq $0x0, (%rax)
je 0x7769
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
addq $0x8, %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rdx
movq -0x20(%rbp), %rcx
movl $0x8, %esi
callq 0x2400
jmp 0x7782
movq -0x20(%rbp), %rcx
leaq -0x30(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x2400
jmp 0x7784
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x76db
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rdx
movq -0x20(%rbp), %rcx
movl $0x8, %esi
callq 0x2400
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jae 0x780d
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shrq $0x2, %rdx
addq $0x1, %rdx
movq -0x20(%rbp), %rcx
movl $0x1, %esi
callq 0x2400
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x77bb
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rdx
movq -0x20(%rbp), %rcx
movl $0x1, %esi
callq 0x2400
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rdx
movq -0x20(%rbp), %rcx
movl $0x8, %esi
callq 0x2400
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rdx
movq -0x20(%rbp), %rcx
movl $0x1, %esi
callq 0x2400
movq -0x20(%rbp), %rcx
leaq 0x3bcf86(%rip), %rdi    # 0x3c47f8
addq $0xd0, %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x20(%rbp), %rcx
leaq 0x3bcf65(%rip), %rdi    # 0x3c47f8
addq $0xd4, %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq $0x1, -0x38(%rbp)
leaq 0x3bcf40(%rip), %rax    # 0x3c47f8
cmpq $0x0, 0x2b0(%rax)
je 0x795b
movq -0x20(%rbp), %rcx
leaq -0x38(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x2400
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jae 0x7959
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shrq %rdx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%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 -0x20(%rbp), %rcx
movl $0x1, %esi
callq 0x2400
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x78e5
jmp 0x795b
movq -0x18(%rbp), %rdi
callq 0x2210
movq -0x20(%rbp), %rdi
callq 0x21f0
movq -0x20(%rbp), %rdi
callq 0x21a0
movq 0x3bc663(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x379a9a(%rip), %rsi    # 0x381421
movb $0x0, %al
callq 0x22a0
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)