// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]yahs/sdict.c
// Function: write_fasta_file_from_agp_
void _write_fasta_file_from_agp_(const char *fa, const char *agp, FILE *fo, int line_wd, int allow_unknown_oris)
{
    iostream_t *agp_in;
    char *line;
    sdict_t *dict;
    sd_seq_t s;
    char sname[256], type[4], cname[256], cstarts[16], cends[16], oris[256];
    char *name = NULL;
    uint32_t c;
    int64_t i, l, cstart, cend, ns, L;

    agp_in = iostream_open(agp);
    if (agp_in == NULL) {
        fprintf(stderr, "[E::%s] cannot open file %s for reading\n", __func__, agp);
        exit(EXIT_FAILURE);
    }

    dict = make_sdict_from_fa(fa, 0);
    l = L = ns = 0;
    while ((line = iostream_getline(agp_in)) != NULL) {
        if (is_empty_line(line) || !strncmp(line, "#", 1))
            // header or empty lines
            continue;
        sname[0] = type[0] = cname[0] = cstarts[0] = cends[0] = oris[0] = '\0';
        sscanf(line, "%s %*s %*s %*s %s %s %s %s %s", sname, type, cname, cstarts, cends, oris);
        if (!strncmp(type, "N", 1) || !strncmp(type, "U", 1)) {
            cend = strtoul(cname, NULL, 10);
            for (i = 0; i < cend; ++i) {
                fputc('N', fo);
                if (++l % line_wd == 0)
                    fputc('\n', fo);
            }
            continue;
        }
        if (!name) {
            name = strdup(sname);
            fprintf(fo, ">%s\n", name);
            l = 0;
        }
        if (strcmp(sname, name)) {
            ++ns;
            L += l;
            free(name);
            name = strdup(sname);
            if (l % line_wd != 0)
                fputc('\n', fo);
            fprintf(fo, ">%s\n", name);
            l = 0;
        }

        cstart = strtoul(cstarts, NULL, 10);
        cend = strtoul(cends, NULL, 10);
        c = sd_get(dict, cname);
        if (c == UINT32_MAX) {
            fprintf(stderr, "[E::%s] sequence component '%s' not found\n", __func__, cname);
            exit(EXIT_FAILURE);
        }
        s = dict->s[c];
        if (cstart < 1 || cstart > cend || cend > s.len) {
            fprintf(stderr, "[E::%s] invalid sequence component %s:%ld-%ld on %s\n", __func__, cname, cstart, cend, sname);
            if (cend > s.len)
                fprintf(stderr, "[E::%s] sequence end position (%ld) greater than sequence length (%u)\n", __func__, cend, s.len);
            exit(EXIT_FAILURE);
        }
        if (!strncmp(oris, "+", 1) || (allow_unknown_oris && (!strncmp(oris, "?", 1) || !strncmp(oris, "0", 1) || !strncmp(oris, "na", 2)))) {
            // forward
            for (i = cstart - 1; i < cend; ++i) {
                fputc(s.seq[i], fo);
                if (++l % line_wd == 0)
                    fputc('\n', fo);
            }
        } else if (!strncmp(oris, "-", 1)) {
            // reverse
            for (i = cend; i >= cstart; --i) {
                fputc(comp_table[(int) s.seq[i - 1]], fo);
                if (++l % line_wd == 0)
                    fputc('\n', fo);
            }
        } else {
            fprintf(stderr, "[E::%s] unknown orientation of sequence component %s:%ld-%ld on %s: '%s'\n", __func__, cname, cstart, cend, sname, oris);
            if (allow_unknown_oris) {
                fprintf(stderr, "[E::%s] valid identifiers for unorientated sequence include: '?', '0' and 'na'\n", __func__);
                fprintf(stderr, "[E::%s] see %s\n", __func__, AGP_SPEC_ONLINE_DOC);
            }
            exit(EXIT_FAILURE);
        }
    }
    if (l % line_wd != 0)
        fputc('\n', fo);
    ++ns;
    L += l;
    fprintf(stderr, "[I::%s] Number sequences: %ld\n", __func__, ns);
    fprintf(stderr, "[I::%s] Number bases: %ld\n", __func__, L);

    if (name) 
        free(name);
    iostream_close(agp_in);
    sd_destroy(dict);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x368, %rsp            # imm = 0x368
movl %r8d, 0x10(%rsp)
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %r12
movq %rsi, %rdi
callq 0x28ae
testq %rax, %rax
je 0x63da
movq %rax, %rbx
movq %r12, %rdi
xorl %esi, %esi
callq 0x429e
movq %rax, 0x30(%rsp)
movslq %ebp, %rbp
leaq 0x260(%rsp), %r13
movq $0x0, (%rsp)
xorl %ecx, %ecx
movq $0x0, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movq %rcx, 0x8(%rsp)
movq %rbx, %rdi
callq 0x29dc
testq %rax, %rax
je 0x6278
movq %rax, %r14
movq %rax, %rdi
callq 0x288b
testl %eax, %eax
jne 0x5f81
cmpb $0x23, (%r14)
je 0x5f81
xorl %eax, %eax
movb %al, 0x160(%rsp)
movb %al, 0x40(%rsp)
movb %al, 0x50(%rsp)
movb %al, 0x60(%rsp)
movb %al, 0x14(%rsp)
xorl %r12d, %r12d
movb %al, 0x260(%rsp)
movq %r14, %rdi
leaq 0x342f(%rip), %rsi      # 0x9403
movq %r13, %rdx
leaq 0x14(%rsp), %rcx
leaq 0x60(%rsp), %r8
leaq 0x50(%rsp), %r9
xorl %eax, %eax
leaq 0x160(%rsp), %r10
pushq %r10
leaq 0x48(%rsp), %r10
pushq %r10
callq 0x2260
popq %rcx
popq %rdx
movzbl 0x14(%rsp), %eax
cmpl $0x55, %eax
je 0x600f
cmpl $0x4e, %eax
jne 0x6072
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x2320
movq %rax, %r13
testq %rax, %rax
cmovleq %r12, %r13
movq (%rsp), %rax
leaq 0x1(%rax), %r14
movq %r13, %r12
subq $0x1, %r12
jb 0x6061
pushq $0x4e
popq %rdi
movq %r15, %rsi
callq 0x21a0
movq %r14, %rax
cqto
idivq %rbp
testq %rdx, %rdx
jne 0x605c
pushq $0xa
popq %rdi
movq %r15, %rsi
callq 0x21a0
incq %r14
jmp 0x6033
addq %r13, (%rsp)
leaq 0x260(%rsp), %r13
jmp 0x5f81
movq 0x8(%rsp), %r12
testq %r12, %r12
jne 0x60a3
movq %r13, %rdi
callq 0x23b0
movq %rax, %r12
movq %r15, %rdi
leaq 0x359e(%rip), %rsi      # 0x962f
movq %rax, %rdx
xorl %eax, %eax
callq 0x2200
movq $0x0, (%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x21e0
testl %eax, %eax
je 0x6115
movq %r12, %rdi
callq 0x2050
movq %r13, %rdi
callq 0x23b0
movq %rax, %rcx
movq (%rsp), %rax
cqto
idivq %rbp
testq %rdx, %rdx
je 0x60e4
pushq $0xa
popq %rdi
movq %r15, %rsi
movq %rcx, %r12
callq 0x21a0
movq %r12, %rcx
incq 0x20(%rsp)
movq (%rsp), %rax
addq %rax, 0x28(%rsp)
movq %r15, %rdi
leaq 0x3533(%rip), %rsi      # 0x962f
movq %rcx, 0x8(%rsp)
movq %rcx, %rdx
xorl %eax, %eax
callq 0x2200
movq $0x0, (%rsp)
jmp 0x611a
movq %r12, 0x8(%rsp)
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
pushq $0xa
popq %r14
movl %r14d, %edx
callq 0x2320
movq %rax, %r12
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
movl %r14d, %edx
callq 0x2320
movq %rax, 0x18(%rsp)
movq 0x30(%rsp), %r14
movq %r14, %rdi
leaq 0x60(%rsp), %rsi
callq 0x4152
cmpl $-0x1, %eax
je 0x6404
movq 0x8(%r14), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
movl 0x10(%rcx,%rax), %r14d
testq %r12, %r12
movq 0x18(%rsp), %r9
jle 0x6309
cmpq %r9, %r12
jg 0x6309
cmpq %r14, %r9
jg 0x6309
movq 0x8(%rcx,%rax), %r14
movzbl 0x160(%rsp), %eax
cmpl $0x2b, %eax
movq 0x8(%rsp), %rcx
je 0x61cb
cmpl $0x0, 0x10(%rsp)
je 0x6213
cmpw $0x616e, 0x160(%rsp)    # imm = 0x616E
je 0x61cb
cmpl $0x2d, %eax
je 0x621c
cmpl $0x3f, %eax
je 0x61cb
cmpl $0x30, %eax
jne 0x636d
decq %r12
cmpq %r9, %r12
jge 0x5f7c
movsbl (%r14,%r12), %edi
movq %r15, %rsi
callq 0x21a0
movq (%rsp), %rax
incq %rax
movq %rax, (%rsp)
cqto
idivq %rbp
testq %rdx, %rdx
jne 0x6204
pushq $0xa
popq %rdi
movq %r15, %rsi
callq 0x21a0
incq %r12
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %r9
jmp 0x61ce
cmpl $0x2d, %eax
jne 0x636d
movq %r14, 0x38(%rsp)
cmpq %r12, %r9
jl 0x5f7c
movsbq -0x1(%r14,%r9), %rax
leaq 0x7009(%rip), %rcx      # 0xd240
movsbl (%rax,%rcx), %edi
movq %r15, %rsi
movq %r9, %r14
callq 0x21a0
movq (%rsp), %rax
incq %rax
movq %rax, (%rsp)
cqto
idivq %rbp
testq %rdx, %rdx
jne 0x6266
pushq $0xa
popq %rdi
movq %r15, %rsi
callq 0x21a0
decq %r14
movq 0x8(%rsp), %rcx
movq %r14, %r9
movq 0x38(%rsp), %r14
jmp 0x6221
movq (%rsp), %rax
cqto
idivq %rbp
testq %rdx, %rdx
je 0x6291
pushq $0xa
popq %rdi
movq %r15, %rsi
callq 0x21a0
movq 0x20(%rsp), %rcx
incq %rcx
movq 0x28(%rsp), %r12
addq (%rsp), %r12
movq 0x6d47(%rip), %r15      # 0xcff0
movq (%r15), %rdi
leaq 0x347b(%rip), %rsi      # 0x972e
leaq 0x3355(%rip), %r14      # 0x960f
movq %r14, %rdx
xorl %eax, %eax
callq 0x2200
movq (%r15), %rdi
leaq 0x347f(%rip), %rsi      # 0x974d
movq %r14, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2200
movq 0x8(%rsp), %rdi
callq 0x2050
movq %rbx, %rdi
callq 0x297a
movq 0x30(%rsp), %rdi
callq 0x3d02
addq $0x368, %rsp            # imm = 0x368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6ce0(%rip), %rbx      # 0xcff0
movq (%rbx), %rdi
subq $0x8, %rsp
leaq 0x3341(%rip), %rsi      # 0x965f
leaq 0x32ea(%rip), %rdx      # 0x960f
leaq 0x68(%rsp), %rcx
movq %r12, %r8
movq 0x20(%rsp), %r9
xorl %eax, %eax
pushq %r13
callq 0x2200
movq 0x28(%rsp), %rcx
addq $0x10, %rsp
cmpq %r14, %rcx
jle 0x63fc
movq (%rbx), %rdi
leaq 0x333d(%rip), %rsi      # 0x9694
leaq 0x32b1(%rip), %rdx      # 0x960f
movl %r14d, %r8d
xorl %eax, %eax
callq 0x2200
jmp 0x63fc
movq 0x6c7c(%rip), %r14      # 0xcff0
movq (%r14), %rdi
leaq 0x3366(%rip), %rsi      # 0x96e4
leaq 0x328a(%rip), %rdx      # 0x960f
leaq 0x60(%rsp), %rcx
movq %r12, %r8
xorl %eax, %eax
leaq 0x160(%rsp), %r10
pushq %r10
pushq %r13
callq 0x2200
popq %rcx
popq %rdx
cmpl $0x0, 0x10(%rsp)
je 0x63fc
movq (%r14), %rdi
leaq 0x3173(%rip), %rsi      # 0x9526
leaq 0x3255(%rip), %rbx      # 0x960f
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2200
movq (%r14), %rdi
leaq 0x31a8(%rip), %rsi      # 0x9576
leaq 0x31b1(%rip), %rcx      # 0x9586
movq %rbx, %rdx
jmp 0x63f5
movq 0x6c0f(%rip), %rax      # 0xcff0
movq (%rax), %rdi
leaq 0x2f4d(%rip), %rsi      # 0x9338
leaq 0x321d(%rip), %rdx      # 0x960f
movq %r14, %rcx
xorl %eax, %eax
callq 0x2200
pushq $0x1
popq %rdi
callq 0x2360
movq 0x6be5(%rip), %rax      # 0xcff0
movq (%rax), %rdi
leaq 0x321f(%rip), %rsi      # 0x9634
leaq 0x31f3(%rip), %rdx      # 0x960f
leaq 0x60(%rsp), %rcx
jmp 0x63f5