// File: /workspace/github_c_makefile_star2_O3/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, 0x2c(%rsp)
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r14
movq %rsi, %rdi
callq 0x2db7
testq %rax, %rax
je 0x74ef
movq %rax, %rbx
movq $0x0, 0x8(%rsp)
movq %r14, %rdi
xorl %esi, %esi
callq 0x494f
movq %rax, 0x38(%rsp)
movq %rbx, %rdi
callq 0x2ee7
movq %rax, %r12
movslq %ebp, %r13
movl $0x0, %ecx
movl $0x0, %eax
movq %rax, 0x20(%rsp)
movl $0x0, %r14d
testq %r12, %r12
je 0x73a5
leaq 0x160(%rsp), %rbp
movq $0x0, 0x20(%rsp)
xorl %ecx, %ecx
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
movq %rcx, 0x18(%rsp)
movq %r12, %rdi
callq 0x2d94
testl %eax, %eax
jne 0x70ae
cmpb $0x23, (%r12)
jne 0x70c3
movq %rbx, %rdi
callq 0x2ee7
movq %rax, %r12
testq %rax, %rax
jne 0x709b
jmp 0x73a0
movq %r14, (%rsp)
xorl %eax, %eax
movb %al, 0x260(%rsp)
movb %al, 0x40(%rsp)
movb %al, 0x50(%rsp)
movb %al, 0x60(%rsp)
movb %al, 0x14(%rsp)
movb %al, 0x160(%rsp)
movq %r12, %rdi
leaq 0x3402(%rip), %rsi      # 0xa4f3
movq %rbp, %rdx
leaq 0x14(%rsp), %rcx
leaq 0x60(%rsp), %r8
leaq 0x50(%rsp), %r9
xorl %eax, %eax
leaq 0x260(%rsp), %r10
pushq %r10
leaq 0x48(%rsp), %r10
pushq %r10
callq 0x2260
addq $0x10, %rsp
movzbl 0x14(%rsp), %eax
cmpl $0x55, %eax
je 0x712e
cmpl $0x4e, %eax
jne 0x71a1
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2320
testq %rax, %rax
jle 0x7198
movq %rax, %r14
movq (%rsp), %rax
leaq 0x1(%rax), %rbp
movq %r14, %r12
movl $0x4e, %edi
movq %r15, %rsi
callq 0x21a0
movq %rbp, %rax
cqto
idivq %r13
testq %rdx, %rdx
jne 0x7179
movl $0xa, %edi
movq %r15, %rsi
callq 0x21a0
incq %rbp
decq %r12
jne 0x7152
movq (%rsp), %rax
addq %r14, %rax
movq %rax, %r14
leaq 0x160(%rsp), %rbp
jmp 0x70ae
movq (%rsp), %r14
jmp 0x70ae
movq 0x8(%rsp), %r14
testq %r14, %r14
jne 0x71d2
movq %rbp, %rdi
callq 0x23b0
movq %rax, %r14
movq %r15, %rdi
leaq 0x355f(%rip), %rsi      # 0xa71f
movq %rax, %rdx
xorl %eax, %eax
callq 0x2200
movq $0x0, (%rsp)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x21e0
testl %eax, %eax
je 0x7240
movq %r14, %rdi
callq 0x2050
movq %rbp, %rdi
callq 0x23b0
movq %rax, %r14
movq (%rsp), %rax
cqto
idivq %r13
testq %rdx, %rdx
je 0x720f
movl $0xa, %edi
movq %r15, %rsi
callq 0x21a0
incq 0x18(%rsp)
movq (%rsp), %rax
addq %rax, 0x20(%rsp)
movq %r15, %rdi
leaq 0x34f8(%rip), %rsi      # 0xa71f
movq %r14, 0x8(%rsp)
movq %r14, %rdx
xorl %eax, %eax
callq 0x2200
movq $0x0, (%rsp)
jmp 0x7245
movq %r14, 0x8(%rsp)
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2320
movq %rax, %rbp
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2320
movq %rax, %r12
movq 0x38(%rsp), %r14
movq %r14, %rdi
leaq 0x60(%rsp), %rsi
callq 0x47c3
cmpl $-0x1, %eax
je 0x7570
movq 0x8(%r14), %rcx
movl %eax, %eax
leaq (%rax,%rax,2), %rax
movl 0x10(%rcx,%rax,8), %r14d
testq %rbp, %rbp
jle 0x7442
cmpq %r12, %rbp
jg 0x7442
cmpq %r14, %r12
jg 0x7442
movq 0x8(%rcx,%rax,8), %rax
movq %rax, 0x30(%rsp)
movzbl 0x260(%rsp), %eax
cmpl $0x2b, %eax
movq (%rsp), %r14
je 0x72f3
cmpl $0x0, 0x2c(%rsp)
je 0x732e
cmpw $0x616e, 0x260(%rsp)    # imm = 0x616E
je 0x72f3
cmpl $0x2d, %eax
je 0x7337
cmpl $0x3f, %eax
je 0x72f3
cmpl $0x30, %eax
jne 0x74f7
decq %rbp
movq 0x30(%rsp), %rax
movsbl (%rax,%rbp), %edi
movq %r15, %rsi
callq 0x21a0
incq %r14
movq %r14, %rax
cqto
idivq %r13
testq %rdx, %rdx
jne 0x7324
movl $0xa, %edi
movq %r15, %rsi
callq 0x21a0
incq %rbp
cmpq %rbp, %r12
jne 0x72f6
jmp 0x737d
cmpl $0x2d, %eax
jne 0x74ab
incq %r12
movq 0x30(%rsp), %rax
movsbq -0x2(%rax,%r12), %rax
leaq 0x6ef4(%rip), %rcx      # 0xe240
movsbl (%rax,%rcx), %edi
movq %r15, %rsi
callq 0x21a0
incq %r14
movq %r14, %rax
cqto
idivq %r13
testq %rdx, %rdx
jne 0x7375
movl $0xa, %edi
movq %r15, %rsi
callq 0x21a0
decq %r12
cmpq %rbp, %r12
jg 0x733a
movq %rbx, %rdi
callq 0x2ee7
movq %rax, %r12
testq %rax, %rax
leaq 0x160(%rsp), %rbp
movq 0x18(%rsp), %rcx
jne 0x7096
jmp 0x73a5
movq 0x18(%rsp), %rcx
movq %r14, %rax
cqto
idivq %r13
testq %rdx, %rdx
je 0x73cb
movl $0xa, %edi
movq %r15, %rsi
movq %r14, %r15
movq %rcx, %r14
callq 0x21a0
movq %r14, %rcx
movq %r15, %r14
incq %rcx
movq 0x20(%rsp), %r12
addq %r14, %r12
movq 0x6c13(%rip), %r15      # 0xdff0
movq (%r15), %rdi
leaq 0x3437(%rip), %rsi      # 0xa81e
leaq 0x3311(%rip), %r14      # 0xa6ff
movq %r14, %rdx
xorl %eax, %eax
callq 0x2200
movq (%r15), %rdi
leaq 0x343b(%rip), %rsi      # 0xa83d
movq %r14, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2200
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x741e
callq 0x2050
movq %rbx, %rdi
callq 0x2e85
movq 0x38(%rsp), %rdi
callq 0x42ef
addq $0x368, %rsp            # imm = 0x368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6ba7(%rip), %rbx      # 0xdff0
movq (%rbx), %rdi
subq $0x8, %rsp
leaq 0x32f8(%rip), %rsi      # 0xa74f
leaq 0x32a1(%rip), %rdx      # 0xa6ff
leaq 0x68(%rsp), %rcx
movq %rbp, %r8
movq %r12, %r9
xorl %eax, %eax
leaq 0x168(%rsp), %r10
pushq %r10
callq 0x2200
addq $0x10, %rsp
cmpq %r14, %r12
jle 0x74a1
movq (%rbx), %rdi
leaq 0x32f7(%rip), %rsi      # 0xa784
leaq 0x326b(%rip), %rdx      # 0xa6ff
movq %r12, %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x2200
movl $0x1, %edi
callq 0x2360
movq 0x6b3e(%rip), %rax      # 0xdff0
movq (%rax), %rdi
leaq 0x3318(%rip), %rsi      # 0xa7d4
leaq 0x323c(%rip), %rdx      # 0xa6ff
leaq 0x60(%rsp), %rcx
movq %rbp, %r8
movq %r12, %r9
xorl %eax, %eax
leaq 0x260(%rsp), %r10
pushq %r10
leaq 0x168(%rsp), %r10
pushq %r10
callq 0x2200
addq $0x10, %rsp
jmp 0x74a1
movq %r12, %rdi
callq 0x270b
movq 0x6af2(%rip), %r14      # 0xdff0
movq (%r14), %rdi
leaq 0x32cc(%rip), %rsi      # 0xa7d4
leaq 0x31f0(%rip), %rbx      # 0xa6ff
leaq 0x60(%rsp), %rcx
movq %rbx, %rdx
movq %rbp, %r8
movq %r12, %r9
xorl %eax, %eax
leaq 0x260(%rsp), %r10
pushq %r10
leaq 0x168(%rsp), %r10
pushq %r10
callq 0x2200
addq $0x10, %rsp
movq (%r14), %rdi
leaq 0x30d0(%rip), %rsi      # 0xa616
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2200
movq (%r14), %rdi
leaq 0x310c(%rip), %rsi      # 0xa666
leaq 0x3115(%rip), %rcx      # 0xa676
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2200
jmp 0x74a1
leaq 0x60(%rsp), %rdi
callq 0x26e0