// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4a8, %rsp            # imm = 0x4A8
testq %rdi, %rdi
je 0x3db1
movl %edx, %r12d
movq %rsi, %rbp
movq %rdi, %r15
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movq %rax, 0x70(%rsp)
testq %rsi, %rsi
je 0x3db8
movl 0x18(%rbp), %eax
movq %rax, 0x68(%rsp)
movq %rax, 0x20(%rsp)
movl 0x1c(%rbp), %eax
movq %rax, 0x60(%rsp)
movq %rax, 0x18(%rsp)
movq 0x20(%rbp), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rbp), %r13
movq 0x30(%rbp), %rbx
movq 0x38(%rbp), %rax
movq %rax, 0x30(%rsp)
jmp 0x3e36
xorl %ebp, %ebp
jmp 0x4152
movl $0x1, %edi
movl $0x50, %esi
callq 0x21f0
movq %rax, %rbp
xorl %ebx, %ebx
movq %rbx, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movl $0x100, %eax            # imm = 0x100
movq %rax, 0x60(%rsp)
movq %rax, 0x68(%rsp)
movl $0x800, %edi            # imm = 0x800
callq 0x2280
movq %rax, 0x10(%rsp)
movl $0x800, %edi            # imm = 0x800
callq 0x2280
movq %rax, %r13
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x57a0
movq %rax, %r14
movq %rbx, 0x18(%rax)
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x57a0
movq %rax, 0x30(%rsp)
movq %rbx, 0x18(%rax)
movq %r14, %rbx
leaq 0x36f7(%rip), %rsi      # 0x7534
movq %r15, %rdi
callq 0x2310
testq %rax, %rax
je 0x4167
movq %rax, %r14
movq %r13, 0x38(%rsp)
leaq 0x8(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq %rax, %rdx
callq 0x2350
cmpq $-0x1, %rax
je 0x40ef
callq 0x23f0
movq %rax, %r13
andl $0x3, %r12d
shll $0x1d, %r12d
movl %r12d, 0x40(%rsp)
leaq 0x8(%rsp), %r15
leaq 0x70(%rsp), %r12
movq 0x8(%rsp), %rax
movq (%r13), %rcx
movq %rax, %rdx
movzbl (%rdx), %esi
incq %rdx
testb $0x20, 0x1(%rcx,%rsi,2)
jne 0x3e9d
testb %sil, %sil
je 0x3eb4
cmpb $0x23, (%rax)
jne 0x3ecd
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2350
cmpq $-0x1, %rax
jne 0x3e91
jmp 0x40ef
movq %rbx, 0x90(%rsp)
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
jne 0x3f08
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%rbp)
movq 0x10(%rbp), %rdi
shlq $0x4, %rcx
leaq (%rcx,%rcx,4), %rsi
callq 0x22c0
movq %rax, 0x10(%rbp)
movq (%rbp), %rax
movq %rbp, %rdx
movq 0x10(%rbp), %rbp
leaq 0x1(%rax), %rcx
movq %rdx, 0x98(%rsp)
movq %rcx, (%rdx)
leaq (%rax,%rax,4), %rbx
shlq $0x4, %rbx
movq 0x8(%rsp), %rdi
leaq (%rbx,%rbp), %r8
addq $0x10, %r8
leaq 0x14(%rbp,%rbx), %r9
leaq 0x18(%rbp,%rbx), %rax
movq %rax, 0x50(%rsp)
leaq 0x1c(%rbp,%rbx), %rax
movq %rax, 0x48(%rsp)
leaq 0x20(%rbp,%rbx), %rax
movq %rax, 0x58(%rsp)
leaq 0x24(%rbp,%rbx), %r11
leaq 0x28(%rbp,%rbx), %rax
movq %rax, 0x28(%rsp)
leaq 0x30(%rbp,%rbx), %rax
movq %rax, 0x80(%rsp)
leaq 0x38(%rbp,%rbx), %rax
movq %rax, 0x78(%rsp)
leaq (%rbx,%rbp), %r10
addq $0x40, %r10
movq %r10, 0x88(%rsp)
subq $0x8, %rsp
leaq 0x3625(%rip), %rsi      # 0x75c0
leaq 0x2a8(%rsp), %rdx
leaq 0xa8(%rsp), %rcx
xorl %eax, %eax
pushq %r10
pushq 0x88(%rsp)
pushq 0x98(%rsp)
leaq 0x64(%rsp), %r10
pushq %r10
pushq 0x50(%rsp)
movq %r11, 0x58(%rsp)
pushq %r11
pushq 0x90(%rsp)
pushq 0x88(%rsp)
pushq 0x98(%rsp)
callq 0x22a0
addq $0x50, %rsp
movb 0x44(%rsp), %al
xorl %ecx, %ecx
cmpb $0x2b, %al
setne %cl
shll $0x1f, %ecx
movq 0x88(%rsp), %rdi
movl 0x8(%rdi), %edx
movl $0x1fffffff, %esi       # imm = 0x1FFFFFFF
andl %esi, %edx
orl 0x40(%rsp), %edx
orl %ecx, %edx
movl %edx, 0x8(%rdi)
cmpb $0x2b, %al
je 0x403e
movq 0x50(%rsp), %rdx
movl (%rdx), %eax
movq 0x48(%rsp), %rsi
movl (%rsi), %ecx
movl %ecx, (%rdx)
movl %eax, (%rsi)
movq 0x58(%rsp), %rdx
movl (%rdx), %eax
movq 0x28(%rsp), %rsi
movl (%rsi), %ecx
movl %ecx, (%rdx)
movl %eax, (%rsi)
addq %rbx, %rbp
leaq 0xa0(%rsp), %rdi
movq 0x38(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x60(%rsp), %rcx
movq 0x30(%rsp), %r8
leaq 0x4(%rsp), %r9
callq 0x416f
movl 0x4(%rsp), %ecx
movl %ecx, 0x4c(%rbp)
movq %rax, 0x38(%rsp)
movq (%rax,%rcx,8), %rax
movq %rax, 0x8(%rbp)
leaq 0x2a0(%rsp), %rdi
movq 0x10(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0x68(%rsp), %rcx
movq 0x90(%rsp), %rbx
movq %rbx, %r8
leaq 0x4(%rsp), %r9
callq 0x416f
movl 0x4(%rsp), %esi
movl %esi, %ecx
andl $0x1fffffff, %ecx       # imm = 0x1FFFFFFF
movl 0x48(%rbp), %edx
movl $0xe0000000, %edi       # imm = 0xE0000000
andl %edi, %edx
orl %ecx, %edx
movl %edx, 0x48(%rbp)
movq %rax, 0x10(%rsp)
movq (%rax,%rsi,8), %rax
movq %rax, (%rbp)
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2350
cmpq $-0x1, %rax
movq 0x98(%rsp), %rbp
jne 0x3e91
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x40fe
callq 0x2050
movq %r14, %rdi
callq 0x2110
movq 0x20(%rsp), %rsi
shlq $0x3, %rsi
movq 0x10(%rsp), %rdi
callq 0x22c0
movq %rax, %r14
movq 0x18(%rsp), %rsi
shlq $0x3, %rsi
movq 0x38(%rsp), %rdi
callq 0x22c0
movl 0x20(%rsp), %ecx
movl %ecx, 0x18(%rbp)
movl 0x18(%rsp), %ecx
movl %ecx, 0x1c(%rbp)
movq %r14, 0x20(%rbp)
movq %rax, 0x28(%rbp)
movq %rbx, 0x30(%rbp)
movq 0x30(%rsp), %rax
movq %rax, 0x38(%rbp)
movq %rbp, %rax
addq $0x4a8, %rsp            # imm = 0x4A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x2662