// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq %rcx, 0x48(%rsp)
movl %esi, %r14d
movq %rdi, 0x40(%rsp)
movq 0x90(%rsp), %rbx
movslq %r9d, %r13
testq %r8, %r8
movq %rdx, 0x8(%rsp)
je 0x8309
leaq 0x25e3(%rip), %rsi      # 0xa71f
movq %rbx, %rdi
movq %r8, %rdx
xorl %eax, %eax
callq 0x2200
movq 0x8(%rsp), %rsi
testq %rsi, %rsi
jne 0x8334
testl %r14d, %r14d
je 0x85d3
leal -0x1(%r14), %eax
movq %rax, 0x38(%rsp)
movl %r14d, %eax
movq %rax, 0x30(%rsp)
xorl %r15d, %r15d
xorl %r12d, %r12d
movq $0x0, (%rsp)
movq %r12, 0x10(%rsp)
leaq (%r15,%r15,2), %rax
shlq $0x4, %rax
movq 0x40(%rsp), %rdx
movl 0x18(%rdx,%rax), %ecx
movl 0x1c(%rdx,%rax), %edi
movl 0x20(%rdx,%rax), %esi
movq 0x48(%rsp), %rax
movq 0x8(%rax), %rax
movl %ecx, %edx
shrl %edx
leaq (%rdx,%rdx,2), %rdx
movq 0x8(%rax,%rdx,8), %rbp
testb $0x1, %cl
movq %rdi, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
jne 0x8233
leal (%rsi,%rdi), %r14d
cmpl %r14d, %edi
jae 0x8288
movq %r15, 0x8(%rsp)
movq (%rsp), %r12
leaq (%r12,%r14), %rax
movq %rax, 0x28(%rsp)
subq %rdi, %r14
addq %rdi, %rbp
incq %r12
xorl %r15d, %r15d
movsbl (%rbp,%r15), %edi
movq %rbx, %rsi
callq 0x21a0
leaq (%r12,%r15), %rax
xorl %edx, %edx
divq %r13
testq %rdx, %rdx
jne 0x8216
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21a0
incq %r15
cmpq %r15, %r14
jne 0x81ed
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
movq %rax, (%rsp)
movq 0x8(%rsp), %r15
jmp 0x8288
leal (%rsi,%rdi), %eax
decl %eax
cmpl %edi, %eax
leaq 0x5fff(%rip), %r12      # 0xe240
jb 0x8288
movl %eax, %r14d
incq %r14
movsbq -0x1(%rbp,%r14), %rax
movsbl (%rax,%r12), %edi
movq %rbx, %rsi
callq 0x21a0
movq (%rsp), %rax
incq %rax
movq %rax, (%rsp)
xorl %edx, %edx
divq %r13
testq %rdx, %rdx
jne 0x827e
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21a0
decq %r14
cmpq 0x20(%rsp), %r14
jg 0x8249
movq 0x10(%rsp), %r12
addq 0x18(%rsp), %r12
cmpq 0x38(%rsp), %r15
je 0x82f6
movslq 0x5f90(%rip), %r14      # 0xe230
testq %r14, %r14
jle 0x82f3
movq %r15, 0x8(%rsp)
movq %r12, %rbp
movq (%rsp), %rax
leaq 0x1(%rax), %r12
movq %r14, %r15
movl $0x4e, %edi
movq %rbx, %rsi
callq 0x21a0
movq %r12, %rax
xorl %edx, %edx
divq %r13
testq %rdx, %rdx
jne 0x82df
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21a0
incq %r12
decq %r15
jne 0x82b8
addq %r14, (%rsp)
movq %rbp, %r12
movq 0x8(%rsp), %r15
addq %r14, %r12
incq %r15
cmpq 0x30(%rsp), %r15
jne 0x817f
jmp 0x85a6
testq %rdx, %rdx
je 0x85e5
movq (%rdx), %rdx
testq %rdx, %rdx
je 0x85e5
leaq 0x23fa(%rip), %rsi      # 0xa71f
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2200
movq 0x8(%rsp), %rsi
testl %r14d, %r14d
je 0x8519
movl %r14d, %eax
movq %rax, 0x30(%rsp)
movq $0x0, 0x18(%rsp)
xorl %r12d, %r12d
movq $0x0, (%rsp)
xorl %ebp, %ebp
movl 0x20(%rsi), %eax
cmpl %eax, %ebp
jae 0x83d6
movq 0x28(%rsi), %rdx
movl %ebp, %ecx
movl (%rdx,%rcx,4), %edx
cmpq %rdx, 0x18(%rsp)
jb 0x83d6
movq 0x30(%rsi), %rdx
movl (%rdx,%rcx,4), %r15d
testq %r15, %r15
je 0x83cf
movq %r12, 0x10(%rsp)
movq (%rsp), %rax
leaq 0x1(%rax), %r12
movq %r15, %r14
movl $0x4e, %edi
movq %rbx, %rsi
callq 0x21a0
movq %r12, %rax
xorl %edx, %edx
divq %r13
testq %rdx, %rdx
jne 0x83b6
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21a0
incq %r12
decq %r14
jne 0x838f
addq %r15, (%rsp)
movq 0x8(%rsp), %rsi
movl 0x20(%rsi), %eax
movq 0x10(%rsp), %r12
addq %r15, %r12
incl %ebp
jmp 0x835e
movq %r12, 0x10(%rsp)
movq 0x18(%rsp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq 0x40(%rsp), %rdx
movl 0x18(%rdx,%rax), %ecx
movl 0x1c(%rdx,%rax), %r12d
movl 0x20(%rdx,%rax), %esi
movq 0x48(%rsp), %rax
movq 0x8(%rax), %rax
movl %ecx, %edx
shrl %edx
leaq (%rdx,%rdx,2), %rdx
movq 0x8(%rax,%rdx,8), %rax
movq %rax, 0x20(%rsp)
testb $0x1, %cl
movq %rsi, 0x38(%rsp)
jne 0x8496
leal (%rsi,%r12), %r14d
cmpl %r14d, %r12d
jae 0x84f0
movq (%rsp), %rax
movq %r12, %rcx
movq %rax, %r12
addq %r14, %rax
movq %rax, 0x50(%rsp)
subq %rcx, %r14
movq %rcx, 0x28(%rsp)
addq %rcx, 0x20(%rsp)
incq %r12
xorl %r15d, %r15d
movq 0x20(%rsp), %rax
movsbl (%rax,%r15), %edi
movq %rbx, %rsi
callq 0x21a0
leaq (%r12,%r15), %rax
xorl %edx, %edx
divq %r13
testq %rdx, %rdx
jne 0x847e
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21a0
incq %r15
cmpq %r15, %r14
jne 0x8451
movq 0x50(%rsp), %rax
subq 0x28(%rsp), %rax
movq %rax, (%rsp)
jmp 0x84f0
leal (%rsi,%r12), %eax
decl %eax
cmpl %r12d, %eax
leaq 0x5d9a(%rip), %r15      # 0xe240
jb 0x84f0
movl %eax, %r14d
incq %r14
movq 0x20(%rsp), %rax
movsbq -0x1(%rax,%r14), %rax
movsbl (%rax,%r15), %edi
movq %rbx, %rsi
callq 0x21a0
movq (%rsp), %rax
incq %rax
movq %rax, (%rsp)
xorl %edx, %edx
divq %r13
testq %rdx, %rdx
jne 0x84e8
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21a0
decq %r14
cmpq %r12, %r14
jg 0x84ae
movq 0x10(%rsp), %r12
addq 0x38(%rsp), %r12
movq 0x18(%rsp), %rcx
incq %rcx
movq %rcx, 0x18(%rsp)
cmpq 0x30(%rsp), %rcx
movq 0x8(%rsp), %rsi
jne 0x835b
jmp 0x8526
xorl %ebp, %ebp
movq $0x0, (%rsp)
xorl %r12d, %r12d
movl 0x20(%rsi), %eax
cmpl %eax, %ebp
jae 0x8599
movl %ebp, %ebp
movq 0x30(%rsi), %rcx
movl (%rcx,%rbp,4), %r14d
testq %r14, %r14
je 0x858c
movq %r12, 0x10(%rsp)
movq (%rsp), %rax
leaq 0x1(%rax), %r12
movq %r14, %r15
movl $0x4e, %edi
movq %rbx, %rsi
callq 0x21a0
movq %r12, %rax
xorl %edx, %edx
divq %r13
testq %rdx, %rdx
jne 0x8573
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21a0
incq %r12
decq %r15
jne 0x854c
addq %r14, (%rsp)
movq 0x8(%rsp), %rsi
movl 0x20(%rsi), %eax
movq 0x10(%rsp), %r12
addq %r14, %r12
incq %rbp
movl %eax, %ecx
cmpq %rcx, %rbp
jb 0x852f
movq 0x10(%rsi), %rax
addq 0x8(%rsi), %rax
cmpq %rax, %r12
jne 0x85e0
movq (%rsp), %rax
xorl %edx, %edx
divq %r13
testq %rdx, %rdx
je 0x85c1
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21a0
movq %r12, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x0, (%rsp)
xorl %r12d, %r12d
jmp 0x85a6
callq 0x275e
callq 0x2786