// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: write_asg_t(asg_t*, char*)
int write_asg_t(asg_t *sg, char* read_file_name)
{
    char* index_name = (char*)malloc(strlen(read_file_name)+15);
    sprintf(index_name, "%s.bin", read_file_name);
    FILE* fp = fopen(index_name, "w");
    uint32_t tmp/**, i**/;
    
    tmp = sg->n_arc;
    fwrite(&tmp, sizeof(tmp), 1, fp);
    // tmp = sg->m_arc;
    fwrite(&tmp, sizeof(tmp), 1, fp);

    tmp = sg->is_srt;
    fwrite(&tmp, sizeof(tmp), 1, fp);


    tmp = sg->n_seq;
    fwrite(&tmp, sizeof(tmp), 1, fp);
    // tmp = sg->m_seq;
    fwrite(&tmp, sizeof(tmp), 1, fp);
    
    tmp = sg->is_symm;
    fwrite(&tmp, sizeof(tmp), 1, fp);
    tmp = sg->r_seq;
    fwrite(&tmp, sizeof(tmp), 1, fp);


    uint32_t Len;

    Len = sg->n_seq*2;
    fwrite(sg->seq_vis, sizeof(sg->seq_vis[0]), Len, fp);

    Len = sg->n_seq*2;
    fwrite(sg->idx, sizeof(sg->idx[0]), Len, fp);


    // for (i = 0; i < sg->n_arc; i++)
    // {
    //     tmp = sg->arc[i].del;
    //     fwrite(&tmp, sizeof(tmp), 1, fp);
    //     tmp = sg->arc[i].el;
    //     fwrite(&tmp, sizeof(tmp), 1, fp);
    //     tmp = sg->arc[i].no_l_indel;
    //     fwrite(&tmp, sizeof(tmp), 1, fp);
    //     tmp = sg->arc[i].ol;
    //     fwrite(&tmp, sizeof(tmp), 1, fp);
    //     tmp = sg->arc[i].strong;
    //     fwrite(&tmp, sizeof(tmp), 1, fp);

    //     uint64_t tmp_64;
    //     tmp_64 = sg->arc[i].ul;
    //     fwrite(&tmp_64, sizeof(tmp_64), 1, fp);

    //     tmp = sg->arc[i].v;
    //     fwrite(&tmp, sizeof(tmp), 1, fp);
    // }
    fwrite(sg->arc, sizeof((*(sg->arc))), sg->n_arc, fp);


    // for (i = 0; i < sg->n_seq; i++)
    // {
    //     tmp = sg->seq[i].c;
    //     fwrite(&tmp, sizeof(tmp), 1, fp);
    //     tmp = sg->seq[i].del;
    //     fwrite(&tmp, sizeof(tmp), 1, fp);
    //     tmp = sg->seq[i].len;
    //     fwrite(&tmp, sizeof(tmp), 1, fp);
    // }
    fwrite(sg->seq, sizeof((*(sg->seq))), sg->n_seq, fp);
    
    free(index_name);    
    fflush(fp);
    fclose(fp);

    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
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 0x233ae2(%rip), %rsi    # 0x38141a
movb $0x0, %al
callq 0x2050
movq -0x18(%rbp), %rdi
leaq 0x24ba88(%rip), %rsi    # 0x3993d2
callq 0x2200
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x24(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x20(%rbp), %rcx
leaq -0x24(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
shrl $0x1f, %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x24(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x24(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x20(%rbp), %rcx
leaq -0x24(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
shrl $0x1f, %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x24(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x8(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x24(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rdi
movl -0x28(%rbp), %eax
movl %eax, %edx
movq -0x20(%rbp), %rcx
movl $0x1, %esi
callq 0x2400
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rdi
movl -0x28(%rbp), %eax
movl %eax, %edx
movq -0x20(%rbp), %rcx
movl $0x8, %esi
callq 0x2400
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
movq -0x20(%rbp), %rcx
movl $0x18, %esi
callq 0x2400
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
movq -0x20(%rbp), %rcx
movl $0x8, %esi
callq 0x2400
movq -0x18(%rbp), %rdi
callq 0x2210
movq -0x20(%rbp), %rdi
callq 0x21f0
movq -0x20(%rbp), %rdi
callq 0x21a0
movl $0x1, %eax
addq $0x30, %rsp
popq %rbp
retq