// File: /workspace/github_c_makefile_star2_O1/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, 0x18(%rsp)
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r14
movq %rsi, %rdi
callq 0x2d17
testq %rax, %rax
je 0x73e5
movq %rax, %rbx
movq $0x0, 0x10(%rsp)
movq %r14, %rdi
xorl %esi, %esi
callq 0x48bc
movq %rax, 0x38(%rsp)
movq %rbx, %rdi
callq 0x2e4f
movq %rax, %r12
movslq %ebp, %rbp
movl $0x0, %ecx
movl $0x0, %eax
movq %rax, 0x28(%rsp)
movl $0x0, %r14d
testq %r12, %r12
je 0x728f
leaq 0x160(%rsp), %r13
movq $0x0, 0x28(%rsp)
xorl %ecx, %ecx
movq $0x0, 0x10(%rsp)
xorl %r14d, %r14d
movq %rcx, 0x20(%rsp)
movq %r12, %rdi
callq 0x2cf4
testl %eax, %eax
jne 0x6f80
cmpb $0x23, (%r12)
jne 0x6f95
movq %rbx, %rdi
callq 0x2e4f
movq %rax, %r12
testq %rax, %rax
jne 0x6f6d
jmp 0x728a
movq %r14, 0x8(%rsp)
xorl %eax, %eax
movb %al, 0x260(%rsp)
movb %al, 0x40(%rsp)
movb %al, 0x50(%rsp)
movb %al, 0x60(%rsp)
movb %al, 0x1c(%rsp)
movb %al, 0x160(%rsp)
movq %r12, %rdi
leaq 0x352f(%rip), %rsi      # 0xa4f3
movq %r13, %rdx
leaq 0x1c(%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 0x1c(%rsp), %eax
cmpl $0x55, %eax
je 0x7001
cmpl $0x4e, %eax
jne 0x7077
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2320
testq %rax, %rax
jle 0x706d
movq %rax, %r14
movq 0x8(%rsp), %rax
leaq 0x1(%rax), %r13
movq %r14, %r12
movl $0x4e, %edi
movq %r15, %rsi
callq 0x21a0
movq %r13, %rax
cqto
idivq %rbp
testq %rdx, %rdx
jne 0x704d
movl $0xa, %edi
movq %r15, %rsi
callq 0x21a0
incq %r13
decq %r12
jne 0x7026
movq 0x8(%rsp), %rax
addq %r14, %rax
movq %rax, %r14
leaq 0x160(%rsp), %r13
jmp 0x6f80
movq 0x8(%rsp), %r14
jmp 0x6f80
movq 0x10(%rsp), %r14
testq %r14, %r14
jne 0x70a9
movq %r13, %rdi
callq 0x23b0
movq %rax, %r14
movq %r15, %rdi
leaq 0x3689(%rip), %rsi      # 0xa71f
movq %rax, %rdx
xorl %eax, %eax
callq 0x2200
movq $0x0, 0x8(%rsp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x21e0
testl %eax, %eax
je 0x711a
movq %r14, %rdi
callq 0x2050
movq %r13, %rdi
callq 0x23b0
movq %rax, %r14
movq 0x8(%rsp), %rax
cqto
idivq %rbp
testq %rdx, %rdx
je 0x70e7
movl $0xa, %edi
movq %r15, %rsi
callq 0x21a0
incq 0x20(%rsp)
movq 0x8(%rsp), %rax
addq %rax, 0x28(%rsp)
movq %r15, %rdi
leaq 0x361f(%rip), %rsi      # 0xa71f
movq %r14, 0x10(%rsp)
movq %r14, %rdx
xorl %eax, %eax
callq 0x2200
movq $0x0, 0x8(%rsp)
jmp 0x711f
movq %r14, 0x10(%rsp)
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2320
movq %rax, %r13
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 0x472d
cmpl $-0x1, %eax
je 0x73ed
movq 0x8(%r14), %rcx
movl %eax, %eax
leaq (%rax,%rax,2), %rax
movl 0x10(%rcx,%rax,8), %r14d
testq %r13, %r13
jle 0x732c
cmpq %r12, %r13
jg 0x732c
cmpq %r14, %r12
jg 0x732c
movq 0x8(%rcx,%rax,8), %rax
movq %rax, 0x30(%rsp)
movzbl 0x260(%rsp), %eax
cmpl $0x2b, %eax
movq 0x8(%rsp), %r14
je 0x71ce
cmpl $0x0, 0x18(%rsp)
je 0x7213
cmpw $0x616e, 0x260(%rsp)    # imm = 0x616E
je 0x71ce
cmpl $0x2d, %eax
je 0x721c
cmpl $0x3f, %eax
je 0x71ce
cmpl $0x30, %eax
jne 0x7395
cmpq %r12, %r13
jg 0x7267
decq %r13
movq 0x30(%rsp), %rax
movsbl (%rax,%r13), %edi
movq %r15, %rsi
callq 0x21a0
incq %r14
movq %r14, %rax
cqto
idivq %rbp
testq %rdx, %rdx
jne 0x7209
movl $0xa, %edi
movq %r15, %rsi
callq 0x21a0
incq %r13
cmpq %r13, %r12
jne 0x71da
jmp 0x7267
cmpl $0x2d, %eax
jne 0x7395
cmpq %r13, %r12
jl 0x7267
incq %r12
movq 0x30(%rsp), %rax
movsbq -0x2(%rax,%r12), %rax
leaq 0x700a(%rip), %rcx      # 0xe240
movsbl (%rax,%rcx), %edi
movq %r15, %rsi
callq 0x21a0
incq %r14
movq %r14, %rax
cqto
idivq %rbp
testq %rdx, %rdx
jne 0x725f
movl $0xa, %edi
movq %r15, %rsi
callq 0x21a0
decq %r12
cmpq %r13, %r12
jg 0x7224
movq %rbx, %rdi
callq 0x2e4f
movq %rax, %r12
testq %rax, %rax
movq 0x20(%rsp), %rcx
leaq 0x160(%rsp), %r13
jne 0x6f68
jmp 0x728f
movq 0x20(%rsp), %rcx
movq %r14, %rax
cqto
idivq %rbp
testq %rdx, %rdx
je 0x72b5
movl $0xa, %edi
movq %r15, %rsi
movq %r14, %r15
movq %rcx, %r14
callq 0x21a0
movq %r14, %rcx
movq %r15, %r14
incq %rcx
movq 0x28(%rsp), %r12
addq %r14, %r12
movq 0x6d29(%rip), %r15      # 0xdff0
movq (%r15), %rdi
leaq 0x354b(%rip), %rsi      # 0xa81c
leaq 0x3427(%rip), %r14      # 0xa6ff
movq %r14, %rdx
xorl %eax, %eax
callq 0x2200
movq (%r15), %rdi
leaq 0x354f(%rip), %rsi      # 0xa83b
movq %r14, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2200
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x7308
callq 0x2050
movq %rbx, %rdi
callq 0x2ded
movq 0x38(%rsp), %rdi
callq 0x4274
addq $0x368, %rsp            # imm = 0x368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6cbd(%rip), %rbx      # 0xdff0
movq (%rbx), %rdi
subq $0x8, %rsp
leaq 0x340e(%rip), %rsi      # 0xa74f
leaq 0x33b7(%rip), %rdx      # 0xa6ff
leaq 0x68(%rsp), %rcx
movq %r13, %r8
movq %r12, %r9
xorl %eax, %eax
leaq 0x168(%rsp), %r10
pushq %r10
callq 0x2200
addq $0x10, %rsp
cmpq %r14, %r12
jle 0x738b
movq (%rbx), %rdi
leaq 0x340d(%rip), %rsi      # 0xa784
leaq 0x3381(%rip), %rdx      # 0xa6ff
movq %r12, %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x2200
movl $0x1, %edi
callq 0x2360
movq 0x6c54(%rip), %rax      # 0xdff0
movq (%rax), %rdi
leaq 0x342c(%rip), %rsi      # 0xa7d2
leaq 0x3352(%rip), %rdx      # 0xa6ff
leaq 0x60(%rsp), %rcx
movq %r13, %r8
movq %r12, %r9
xorl %eax, %eax
leaq 0x260(%rsp), %r10
pushq %r10
leaq 0x168(%rsp), %r10
pushq %r10
callq 0x2200
addq $0x10, %rsp
cmpl $0x0, 0x18(%rsp)
je 0x738b
callq 0x26e1
jmp 0x738b
movq %r12, %rdi
callq 0x2755
leaq 0x60(%rsp), %rdi
callq 0x272a