// File: /workspace/github_c_makefile_star2_O1/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
movl %r9d, %r15d
movq %rcx, 0x48(%rsp)
movl %esi, %r14d
movq %rdi, 0x40(%rsp)
testq %r8, %r8
jne 0x7fca
testq %rdx, %rdx
je 0x7fc7
movq (%rdx), %r8
jmp 0x7fca
xorl %r8d, %r8d
movq %rdx, 0x8(%rsp)
testq %r8, %r8
je 0x845d
movq 0x90(%rsp), %rbx
leaq 0x2738(%rip), %rsi      # 0xa71f
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r8, %rdx
xorl %eax, %eax
callq 0x2200
movslq %r15d, %r13
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
je 0x81ea
testl %r14d, %r14d
je 0x839c
movl %r14d, %eax
movq %rax, 0x30(%rsp)
movq $0x0, 0x18(%rsp)
xorl %r12d, %r12d
movq $0x0, (%rsp)
xorl %ebp, %ebp
cmpl 0x20(%rdx), %ebp
jae 0x80a4
movq 0x28(%rdx), %rcx
movl %ebp, %eax
movl (%rcx,%rax,4), %ecx
cmpq %rcx, 0x18(%rsp)
jb 0x80a4
movq 0x30(%rdx), %rcx
movl (%rcx,%rax,4), %r15d
testq %r15, %r15
je 0x809d
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 0x8087
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21a0
incq %r12
decq %r14
jne 0x8060
addq %r15, (%rsp)
movq 0x10(%rsp), %r12
movq 0x8(%rsp), %rdx
addq %r15, %r12
incl %ebp
jmp 0x802e
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 0x8164
leal (%rsi,%r12), %r14d
cmpl %r14d, %r12d
jae 0x81be
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 0x814c
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21a0
incq %r15
cmpq %r15, %r14
jne 0x811f
movq 0x50(%rsp), %rax
subq 0x28(%rsp), %rax
movq %rax, (%rsp)
jmp 0x81be
leal (%rsi,%r12), %eax
decl %eax
cmpl %r12d, %eax
leaq 0x60cc(%rip), %r15      # 0xe240
jb 0x81be
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 0x81b6
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21a0
decq %r14
cmpq %r12, %r14
jg 0x817c
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), %rdx
jne 0x802e
jmp 0x83a7
testl %r14d, %r14d
je 0x8450
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 0x82c6
leal (%rsi,%rdi), %r14d
cmpl %r14d, %edi
jae 0x831b
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 0x82a9
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21a0
incq %r15
cmpq %r15, %r14
jne 0x8280
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
movq %rax, (%rsp)
movq 0x8(%rsp), %r15
jmp 0x831b
leal (%rsi,%rdi), %eax
decl %eax
cmpl %edi, %eax
leaq 0x5f6c(%rip), %r12      # 0xe240
jb 0x831b
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 0x8311
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21a0
decq %r14
cmpq 0x20(%rsp), %r14
jg 0x82dc
movq 0x10(%rsp), %r12
addq 0x18(%rsp), %r12
cmpq 0x38(%rsp), %r15
je 0x8389
movslq 0x5efd(%rip), %r14      # 0xe230
testq %r14, %r14
jle 0x8386
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 0x8372
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21a0
incq %r12
decq %r15
jne 0x834b
addq %r14, (%rsp)
movq %rbp, %r12
movq 0x8(%rsp), %r15
addq %r14, %r12
incq %r15
cmpq 0x30(%rsp), %r15
jne 0x8212
jmp 0x8423
movq $0x0, (%rsp)
xorl %r12d, %r12d
cmpl 0x20(%rdx), %ebp
jae 0x8416
movl %ebp, %ebp
movq 0x30(%rdx), %rax
movl (%rax,%rbp,4), %r14d
testq %r14, %r14
je 0x8408
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 0x83f2
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21a0
incq %r12
decq %r15
jne 0x83cb
addq %r14, (%rsp)
movq 0x10(%rsp), %r12
movq 0x8(%rsp), %rdx
addq %r14, %r12
incq %rbp
movl 0x20(%rdx), %eax
cmpq %rax, %rbp
jb 0x83ae
movq 0x10(%rdx), %rax
addq 0x8(%rdx), %rax
cmpq %rax, %r12
jne 0x8462
movq (%rsp), %rax
xorl %edx, %edx
divq %r13
testq %rdx, %rdx
je 0x843e
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 0x8423
callq 0x27d0
callq 0x27a8