// File: /workspace/github_c_makefile_star2_O0/c-zhou[P]oatk/hmmannot.c
// Function: hmm_annot_read
hmm_annot_db_t *hmm_annot_read(char *annot_file, hmm_annot_db_t *annot_db, OG_TYPE_t og_type) // og_type: OG_MITO or OG_PLTD or OG_MINI
{
    if (!annot_file) return 0;

    FILE *fp;
    char *line = NULL;
    size_t ln = 0;
    ssize_t read;
    char gname[512], sname[512], strand[4];
    hmm_annot_t *annot;
    kh_str_t *h_gnames, *h_snames;
    char **gnames, **snames;
    size_t n_gene, m_gene, n_seg, m_seg;
    uint32_t index;

    if (annot_db) {
        n_gene = m_gene = annot_db->n_gene;
        n_seg = m_seg = annot_db->n_seg;
        gnames = annot_db->gnames;
        snames = annot_db->snames;
        h_gnames = annot_db->h_gnames;
        h_snames = annot_db->h_snames;
    } else {
        MYCALLOC(annot_db, 1);
        n_gene = n_seg = 0;
        m_gene = m_seg = 256;
        MYMALLOC(gnames, m_gene);
        MYMALLOC(snames, m_seg);
        h_gnames = kh_str_init();
        h_snames = kh_str_init();
    }

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

    while ((read = getline(&line, &ln, fp)) != -1) {
        if (is_empty_line(line) || !strncmp(line, "#", 1))
            continue;
        
        kv_pushp(hmm_annot_t, *annot_db, &annot);
        
        sscanf(line, "%s %*s %s %*s %u %u %u %u %u %u %u %s %lf %lf %lf %*s",
                gname, sname, &annot->hmmfrom, &annot->hmmto, &annot->alifrom, 
                &annot->alito, &annot->envfrom, &annot->envto, &annot->modlen, 
                strand, &annot->evalue, &annot->score, &annot->bias);

        annot->strand = strand[0]=='+'? 0 : 1;
        annot->og_type = (uint32_t) og_type;

        if (annot->strand) {
            // reverse strand
            SWAP(annot->alifrom, annot->alito);
            SWAP(annot->envfrom, annot->envto);
        }

        snames = update_name_list(sname, snames, &n_seg, &m_seg, h_snames, &index);
        annot->sid = index;
        annot->sname = snames[index];
        gnames = update_name_list(gname, gnames, &n_gene, &m_gene, h_gnames, &index);
        annot->gid = index;
        annot->gname = gnames[index];
    }
    
    if (line) free(line);
    fclose(fp);
    MYREALLOC(gnames, n_gene);
    MYREALLOC(snames, n_seg);
    annot_db->n_gene = n_gene;
    annot_db->n_seg = n_seg;
    annot_db->gnames = gnames;
    annot_db->snames = snames;
    annot_db->h_gnames = h_gnames;
    annot_db->h_snames = h_snames;

    return annot_db;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x508, %rsp            # imm = 0x508
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movl %edx, -0x44(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x46a3
movq $0x0, -0x30(%rbp)
jmp 0x4c02
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x4725
movq -0x40(%rbp), %rax
movl 0x18(%rax), %eax
movq %rax, -0x4b0(%rbp)
movq %rax, -0x4a8(%rbp)
movq -0x40(%rbp), %rax
movl 0x1c(%rax), %eax
movq %rax, -0x4c0(%rbp)
movq %rax, -0x4b8(%rbp)
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x498(%rbp)
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x4a0(%rbp)
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x488(%rbp)
movq -0x40(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x490(%rbp)
jmp 0x47aa
movl $0x1, %edi
movl $0x50, %esi
callq 0x21f0
movq %rax, -0x40(%rbp)
movq $0x0, -0x4b8(%rbp)
movq $0x0, -0x4a8(%rbp)
movq $0x100, -0x4c0(%rbp) # imm = 0x100
movq $0x100, -0x4b0(%rbp) # imm = 0x100
movq -0x4b0(%rbp), %rdi
shlq $0x3, %rdi
callq 0x2280
movq %rax, -0x498(%rbp)
movq -0x4c0(%rbp), %rdi
shlq $0x3, %rdi
callq 0x2280
movq %rax, -0x4a0(%rbp)
callq 0x4c20
movq %rax, -0x488(%rbp)
callq 0x4c20
movq %rax, -0x490(%rbp)
movq -0x38(%rbp), %rdi
leaq 0x4d9a(%rip), %rsi      # 0x954f
callq 0x2310
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x47f2
movq 0x782c(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x38(%rbp), %rcx
leaq 0x4de6(%rip), %rsi      # 0x95c0
leaq 0x4e08(%rip), %rdx      # 0x95e9
movb $0x0, %al
callq 0x2220
movl $0x1, %edi
callq 0x2370
jmp 0x47f4
movq -0x50(%rbp), %rdx
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x2350
movq %rax, -0x68(%rbp)
cmpq $-0x1, %rax
je 0x4b49
movq -0x58(%rbp), %rdi
callq 0x4c30
cmpl $0x0, %eax
jne 0x483b
movq -0x58(%rbp), %rdi
leaq 0x4dcc(%rip), %rsi      # 0x95f8
movl $0x1, %edx
callq 0x2090
cmpl $0x0, %eax
jne 0x483d
jmp 0x47f4
jmp 0x483f
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq -0x40(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x48ad
movq -0x40(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x486f
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x4d8(%rbp)
jmp 0x487d
movl $0x2, %eax
movq %rax, -0x4d8(%rbp)
jmp 0x487d
movq -0x4d8(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x40(%rbp), %rax
imulq $0x50, 0x8(%rax), %rsi
callq 0x22c0
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rdx
movq (%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, (%rdx)
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x480(%rbp)
movq -0x58(%rbp), %rdi
leaq -0x270(%rbp), %rdx
leaq -0x470(%rbp), %rcx
movq -0x480(%rbp), %r8
addq $0x10, %r8
movq -0x480(%rbp), %r9
addq $0x14, %r9
movq -0x480(%rbp), %rax
addq $0x18, %rax
movq -0x480(%rbp), %r13
addq $0x1c, %r13
movq -0x480(%rbp), %r12
addq $0x20, %r12
movq -0x480(%rbp), %r15
addq $0x24, %r15
movq -0x480(%rbp), %r14
addq $0x28, %r14
leaq -0x474(%rbp), %rbx
movq -0x480(%rbp), %r11
addq $0x30, %r11
movq -0x480(%rbp), %r10
addq $0x38, %r10
movq -0x480(%rbp), %rsi
addq $0x40, %rsi
movq %rsi, -0x4e0(%rbp)
leaq 0x4c91(%rip), %rsi      # 0x95fa
movq %rax, (%rsp)
movq -0x4e0(%rbp), %rax
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movb $0x0, %al
callq 0x22a0
movsbl -0x474(%rbp), %ecx
movl $0x1, %edx
xorl %eax, %eax
cmpl $0x2b, %ecx
cmovel %eax, %edx
movq -0x480(%rbp), %rax
movl 0x48(%rax), %ecx
andl $0x1, %edx
shll $0x1f, %edx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl %edx, %ecx
movl %ecx, 0x48(%rax)
movl -0x44(%rbp), %edx
movq -0x480(%rbp), %rax
movl 0x48(%rax), %ecx
andl $0x3, %edx
shll $0x1d, %edx
andl $0x9fffffff, %ecx       # imm = 0x9FFFFFFF
orl %edx, %ecx
movl %ecx, 0x48(%rax)
movq -0x480(%rbp), %rax
movl 0x48(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x4a70
jmp 0x4a04
movq -0x480(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x4c8(%rbp)
movq -0x480(%rbp), %rax
movl 0x1c(%rax), %ecx
movq -0x480(%rbp), %rax
movl %ecx, 0x18(%rax)
movl -0x4c8(%rbp), %ecx
movq -0x480(%rbp), %rax
movl %ecx, 0x1c(%rax)
jmp 0x4a3a
movq -0x480(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x4cc(%rbp)
movq -0x480(%rbp), %rax
movl 0x24(%rax), %ecx
movq -0x480(%rbp), %rax
movl %ecx, 0x20(%rax)
movl -0x4cc(%rbp), %ecx
movq -0x480(%rbp), %rax
movl %ecx, 0x24(%rax)
jmp 0x4a70
leaq -0x470(%rbp), %rdi
movq -0x4a0(%rbp), %rsi
movq -0x490(%rbp), %r8
leaq -0x4b8(%rbp), %rdx
leaq -0x4c0(%rbp), %rcx
leaq -0x4c4(%rbp), %r9
callq 0x4ca0
movq %rax, -0x4a0(%rbp)
movl -0x4c4(%rbp), %ecx
movq -0x480(%rbp), %rax
movl %ecx, 0x4c(%rax)
movq -0x4a0(%rbp), %rax
movl -0x4c4(%rbp), %ecx
movq (%rax,%rcx,8), %rcx
movq -0x480(%rbp), %rax
movq %rcx, 0x8(%rax)
leaq -0x270(%rbp), %rdi
movq -0x498(%rbp), %rsi
movq -0x488(%rbp), %r8
leaq -0x4a8(%rbp), %rdx
leaq -0x4b0(%rbp), %rcx
leaq -0x4c4(%rbp), %r9
callq 0x4ca0
movq %rax, -0x498(%rbp)
movl -0x4c4(%rbp), %edx
movq -0x480(%rbp), %rax
movl 0x48(%rax), %ecx
andl $0x1fffffff, %edx       # imm = 0x1FFFFFFF
andl $0xe0000000, %ecx       # imm = 0xE0000000
orl %edx, %ecx
movl %ecx, 0x48(%rax)
movq -0x498(%rbp), %rax
movl -0x4c4(%rbp), %ecx
movq (%rax,%rcx,8), %rcx
movq -0x480(%rbp), %rax
movq %rcx, (%rax)
jmp 0x47f4
cmpq $0x0, -0x58(%rbp)
je 0x4b59
movq -0x58(%rbp), %rdi
callq 0x2050
movq -0x50(%rbp), %rdi
callq 0x2110
movq -0x498(%rbp), %rdi
movq -0x4a8(%rbp), %rsi
shlq $0x3, %rsi
callq 0x22c0
movq %rax, -0x498(%rbp)
movq -0x4a0(%rbp), %rdi
movq -0x4b8(%rbp), %rsi
shlq $0x3, %rsi
callq 0x22c0
movq %rax, -0x4a0(%rbp)
movq -0x4a8(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x4b8(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x498(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x4a0(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x488(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x490(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x40(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax,%rax)