// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]yahs/sdict.c
// Function: write_segs_to_fasta
uint64_t write_segs_to_fasta(sd_seg_t *segs, uint32_t n, sd_aseq_t *aseq, sdict_t *sd, char *name, int line_wd, FILE *fo)
{
    uint32_t i, j;
    uint64_t l, len;
    int64_t k, m;
    sd_seg_t seg;
    char *s;
    if (!name) name = aseq? aseq->name : 0;
    if (!name) {
        fprintf(stderr, "[E::%s] sequence name required\n", __func__);
        exit(EXIT_FAILURE);
    }
    fprintf(fo, ">%s\n", name);
    l = len = 0;
    if (aseq) {
        // with gap information
        for (i = 0, j = 0; i < n; ++i) {
            while (j < aseq->gn && aseq->gs[j] <= i) {
                // print gap
                for (k = 0, m = aseq->gsize[j]; k < m; ++k) {
                    fputc('N', fo);
                    if (++l % line_wd == 0)
                        fputc('\n', fo);
                }
                len += m;
                ++j;
            }
            seg = segs[i];
            s = sd->s[seg.c >> 1].seq;
            if ((seg.c&1) == 0) {
                // forward
                for (k = seg.x, m = seg.x + seg.y; k < m; ++k) {
                    fputc(s[k], fo);
                    if (++l % line_wd == 0)
                        fputc('\n', fo);
                }
            } else {
                // reverse
                for (k = seg.x + seg.y - 1, m = seg.x; k >= m; --k) {
                    fputc(comp_table[(int) s[k]], fo);
                    if (++l % line_wd == 0)
                        fputc('\n', fo);
                }
            }
            len += seg.y;
        }
        while (j < aseq->gn) {
            // print trailing gap
            for (k = 0, m = aseq->gsize[j]; k < m; ++k) {
                fputc('N', fo);
                if (++l % line_wd == 0)
                    fputc('\n', fo);
            }
            len += m;
            ++j;
        }
        if (len != aseq->len + aseq->gap) {
            fprintf(stderr, "[E::%s] sequence length conflict\n", __func__);
            exit(EXIT_FAILURE);
        }
    } else {
        // no gap information provided
        // using default settings
        for (i = 0; i < n; ++i) {
            seg = segs[i];
            s = sd->s[seg.c >> 1].seq;
            if ((seg.c&1) == 0) {
                // forward
                for (k = seg.x, m = seg.x + seg.y; k < m; ++k) {
                    fputc(s[k], fo);
                    if (++l % line_wd == 0)
                        fputc('\n', fo);
                }
            } else {
                // reverse
                for (k = seg.x + seg.y - 1, m = seg.x; k >= m; --k) {
                    fputc(comp_table[(int) s[k]], fo);
                    if (++l % line_wd == 0)
                        fputc('\n', fo);
                }
            }
            len += seg.y;
            
            if (i != n - 1) {
                for (k = 0, m = DEFAULT_AGP_GAP_SIZE; k < m; ++k) {
                    fputc('N', fo);
                    if (++l % line_wd == 0)
                        fputc('\n', fo);
                }
                len += m;
            }
        }
    }

    if (l % line_wd != 0)
        fputc('\n', fo);
    
    return len;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r9d, %r14d
movq %rcx, 0x30(%rsp)
movq %rdx, %r15
movl %esi, %r12d
movq %rdi, 0x28(%rsp)
testq %r8, %r8
jne 0x6e22
testq %r15, %r15
je 0x721e
movq (%r15), %r8
testq %r8, %r8
je 0x721e
movq 0x80(%rsp), %rbx
leaq 0x27fe(%rip), %rsi      # 0x962f
xorl %r13d, %r13d
movq %rbx, %rdi
movq %r8, %rdx
xorl %eax, %eax
callq 0x2200
movl %r12d, %eax
movq %rax, 0x38(%rsp)
movslq %r14d, %rbp
testq %r15, %r15
je 0x7093
pushq $0x4e
popq %r12
xorl %esi, %esi
xorl %eax, %eax
xorl %edi, %edi
movq %r15, 0x18(%rsp)
cmpq 0x38(%rsp), %rsi
je 0x6fe6
movq %rsi, 0x20(%rsp)
cmpl 0x20(%r15), %r13d
movq %rdi, 0x8(%rsp)
movq %r13, 0x10(%rsp)
jae 0x6efd
movq 0x28(%r15), %rcx
movl %r13d, %edx
movl (%rcx,%rdx,4), %ecx
cmpq %rcx, %rsi
jb 0x6efd
movq 0x30(%r15), %rcx
movl (%rcx,%rdx,4), %r13d
movq %rax, (%rsp)
leaq 0x1(%rax), %r14
movq %r13, %r15
subq $0x1, %r15
jb 0x6ed4
movl %r12d, %edi
movq %rbx, %rsi
callq 0x21a0
movq %r14, %rax
xorl %edx, %edx
divq %rbp
testq %rdx, %rdx
jne 0x6ecf
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x21a0
incq %r14
jmp 0x6ea6
movq (%rsp), %rax
addq %r13, %rax
movq 0x8(%rsp), %rcx
addq %r13, %rcx
movq %rcx, %rdi
movq 0x10(%rsp), %r13
incl %r13d
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %rsi
jmp 0x6e74
movq %rax, %r13
imulq $0x30, %rsi, %rax
movq 0x28(%rsp), %rdx
movl 0x18(%rdx,%rax), %ecx
movl 0x1c(%rdx,%rax), %r14d
movl 0x20(%rdx,%rax), %esi
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
movl %ecx, %edx
shrl %edx
imulq $0x18, %rdx, %rdx
movq 0x8(%rax,%rdx), %rax
movq %rax, (%rsp)
testb $0x1, %cl
movq %rsi, 0x40(%rsp)
jne 0x6f77
leal (%rsi,%r14), %r15d
cmpq %r15, %r14
jae 0x6fc2
movq (%rsp), %rax
movsbl (%rax,%r14), %edi
movq %rbx, %rsi
callq 0x21a0
movq %r13, %rax
incq %rax
movq %rax, %r13
xorl %edx, %edx
divq %rbp
testq %rdx, %rdx
jne 0x6f72
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x21a0
incq %r14
jmp 0x6f3e
leal (%rsi,%r14), %r15d
decl %r15d
cmpq %r14, %r15
jl 0x6fc2
movq (%rsp), %rax
movsbq (%rax,%r15), %rax
leaq 0x62ad(%rip), %rcx      # 0xd240
movsbl (%rax,%rcx), %edi
movq %rbx, %rsi
callq 0x21a0
movq %r13, %rax
incq %rax
movq %rax, %r13
xorl %edx, %edx
divq %rbp
testq %rdx, %rdx
jne 0x6fbd
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x21a0
decq %r15
jmp 0x6f7e
movq 0x8(%rsp), %rdi
addq 0x40(%rsp), %rdi
movq 0x20(%rsp), %rsi
incq %rsi
movq %r13, %rax
movq 0x18(%rsp), %r15
movq 0x10(%rsp), %r13
jmp 0x6e64
movq %rdi, 0x8(%rsp)
movl %r13d, %edx
pushq $0x4e
popq %r12
pushq $0xa
popq %r13
movl 0x20(%r15), %ecx
cmpq %rcx, %rdx
jae 0x7067
movq 0x30(%r15), %rcx
movq %rdx, 0x10(%rsp)
movl (%rcx,%rdx,4), %r15d
movq %rax, (%rsp)
leaq 0x1(%rax), %r14
movq %r15, 0x20(%rsp)
subq $0x1, %r15
jb 0x7047
movl %r12d, %edi
movq %rbx, %rsi
callq 0x21a0
movq %r14, %rax
xorl %edx, %edx
divq %rbp
testq %rdx, %rdx
jne 0x7042
movl %r13d, %edi
movq %rbx, %rsi
callq 0x21a0
incq %r14
jmp 0x7019
movq (%rsp), %rax
movq 0x20(%rsp), %rcx
addq %rcx, %rax
addq %rcx, 0x8(%rsp)
movq 0x10(%rsp), %rdx
incq %rdx
movq 0x18(%rsp), %r15
jmp 0x6ff6
movq 0x10(%r15), %rcx
addq 0x8(%r15), %rcx
movq 0x8(%rsp), %r14
cmpq %rcx, %r14
je 0x71f7
movq 0x5f6c(%rip), %rax      # 0xcff0
movq (%rax), %rdi
leaq 0x2778(%rip), %rsi      # 0x9806
jmp 0x722f
decl %r12d
movq %r12, 0x18(%rsp)
xorl %ecx, %ecx
xorl %eax, %eax
movq $0x0, 0x8(%rsp)
cmpq 0x38(%rsp), %rcx
je 0x71f2
movq %rax, %r13
movq %rcx, (%rsp)
imulq $0x30, %rcx, %rax
movq 0x28(%rsp), %rdx
movl 0x18(%rdx,%rax), %ecx
movl 0x1c(%rdx,%rax), %r14d
movl 0x20(%rdx,%rax), %esi
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
movl %ecx, %edx
shrl %edx
imulq $0x18, %rdx, %rdx
movq 0x8(%rax,%rdx), %r12
testb $0x1, %cl
movq %rsi, 0x10(%rsp)
jne 0x7129
leal (%rsi,%r14), %r15d
cmpq %r15, %r14
jae 0x7170
movsbl (%r12,%r14), %edi
movq %rbx, %rsi
callq 0x21a0
movq %r13, %rax
incq %rax
movq %rax, %r13
xorl %edx, %edx
divq %rbp
testq %rdx, %rdx
jne 0x7124
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x21a0
incq %r14
jmp 0x70f4
leal (%rsi,%r14), %r15d
decl %r15d
cmpq %r14, %r15
jl 0x7170
movsbq (%r12,%r15), %rax
leaq 0x60ff(%rip), %rcx      # 0xd240
movsbl (%rax,%rcx), %edi
movq %rbx, %rsi
callq 0x21a0
movq %r13, %rax
incq %rax
movq %rax, %r13
xorl %edx, %edx
divq %rbp
testq %rdx, %rdx
jne 0x716b
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x21a0
decq %r15
jmp 0x7130
movq 0x10(%rsp), %rax
addq %rax, 0x8(%rsp)
movq (%rsp), %rcx
cmpq 0x18(%rsp), %rcx
movq %r13, %rax
je 0x71ea
movslq 0x60a1(%rip), %rcx      # 0xd230
testq %rcx, %rcx
movl $0x0, %r12d
movq %rcx, 0x10(%rsp)
cmovgq %rcx, %r12
leaq 0x1(%rax), %r14
movq %r12, %r15
subq $0x1, %r15
jb 0x71d6
pushq $0x4e
popq %rdi
movq %rbx, %rsi
callq 0x21a0
movq %r14, %rax
xorl %edx, %edx
divq %rbp
testq %rdx, %rdx
jne 0x71d1
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x21a0
incq %r14
jmp 0x71a8
movq %r13, %rax
addq %r12, %rax
movq 0x10(%rsp), %rcx
addq %rcx, 0x8(%rsp)
movq (%rsp), %rcx
incq %rcx
jmp 0x70a8
movq 0x8(%rsp), %r14
xorl %edx, %edx
divq %rbp
testq %rdx, %rdx
je 0x720c
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x21a0
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5dcb(%rip), %rax      # 0xcff0
movq (%rax), %rdi
leaq 0x2539(%rip), %rsi      # 0x9768
leaq 0x25bc(%rip), %rdx      # 0x97f2
xorl %eax, %eax
callq 0x2200
pushq $0x1
popq %rdi
callq 0x2360