// File: /workspace/github_c_makefile_star2_O2/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 0x3a73
movl %edx, %ebp
movq %rsi, %r13
movq %rdi, %r15
andq $0x0, 0x40(%rsp)
andq $0x0, 0x98(%rsp)
testq %rsi, %rsi
je 0x3a7b
movl 0x18(%r13), %eax
movl 0x1c(%r13), %ebx
movq 0x20(%r13), %rcx
movq %rcx, 0x10(%rsp)
movq 0x28(%r13), %rcx
movq %rcx, 0x18(%rsp)
movq 0x30(%r13), %rcx
movq %rcx, 0x28(%rsp)
movq 0x38(%r13), %rcx
movq %rcx, 0x20(%rsp)
movq %rbx, %r14
movq %rax, %rcx
jmp 0x3aca
xorl %r13d, %r13d
jmp 0x3d95
pushq $0x1
popq %rdi
pushq $0x50
popq %rsi
callq 0x21f0
movq %rax, %r13
movl $0x800, %edi            # imm = 0x800
callq 0x2280
movq %rax, 0x10(%rsp)
movl $0x800, %edi            # imm = 0x800
callq 0x2280
movq %rax, 0x18(%rsp)
callq 0x3dd4
movq %rax, 0x28(%rsp)
movl $0x100, %r14d           # imm = 0x100
xorl %ebx, %ebx
callq 0x3dd4
movq %rax, 0x20(%rsp)
xorl %eax, %eax
movl $0x100, %ecx            # imm = 0x100
movq %rbx, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq %r14, 0x88(%rsp)
movq %rcx, 0x90(%rsp)
leaq 0x2a47(%rip), %rsi      # 0x6532
movq %r15, %rdi
callq 0x2310
testq %rax, %rax
je 0x3daa
movq %rax, %r14
andl $0x3, %ebp
shll $0x1d, %ebp
movl %ebp, 0x48(%rsp)
leaq 0x40(%rsp), %r15
leaq 0x98(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2350
movq 0x40(%rsp), %rbp
cmpq $-0x1, %rax
je 0x3d32
callq 0x23f0
movq (%rax), %rax
movq %rbp, %rcx
movzbl (%rcx), %edx
incq %rcx
testb $0x20, 0x1(%rax,%rdx,2)
jne 0x3b3e
testb %dl, %dl
je 0x3b16
cmpb $0x23, (%rbp)
je 0x3b16
movq (%r13), %rax
movq %r13, %rbx
movq 0x10(%r13), %r13
cmpq 0x8(%rbx), %rax
jne 0x3b93
leaq (%rax,%rax), %rcx
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rcx
movq %rcx, 0x8(%rbx)
imulq $0x50, %rcx, %rsi
movq %r13, %rdi
callq 0x22c0
movq %rax, %r13
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
movq 0x40(%rsp), %rbp
leaq 0x1(%rax), %rcx
movq %rbx, 0x80(%rsp)
movq %rcx, (%rbx)
imulq $0x50, %rax, %rbx
leaq (%rbx,%r13), %r8
addq $0x10, %r8
leaq 0x14(%r13,%rbx), %r9
leaq 0x18(%r13,%rbx), %rax
leaq 0x1c(%r13,%rbx), %rcx
movq %rcx, 0x78(%rsp)
leaq 0x20(%r13,%rbx), %rcx
movq %rcx, 0x70(%rsp)
leaq 0x24(%r13,%rbx), %rcx
movq %rcx, 0x60(%rsp)
leaq 0x28(%r13,%rbx), %rcx
movq %rcx, 0x58(%rsp)
leaq 0x30(%r13,%rbx), %rcx
movq %rcx, 0x50(%rsp)
leaq (%rbx,%r13), %r11
addq $0x38, %r11
leaq (%rbx,%r13), %r10
addq $0x40, %r10
movq %r10, 0x68(%rsp)
subq $0x8, %rsp
movq %rbp, %rdi
movq %rax, %rbp
leaq 0x29b0(%rip), %rsi      # 0x65c0
leaq 0x2a8(%rsp), %rdx
leaq 0xa8(%rsp), %rcx
xorl %eax, %eax
pushq %r10
pushq %r11
pushq 0x68(%rsp)
leaq 0x6c(%rsp), %r10
pushq %r10
pushq 0x80(%rsp)
pushq 0x90(%rsp)
pushq 0xa8(%rsp)
pushq 0xb8(%rsp)
pushq %rbp
callq 0x22a0
addq $0x50, %rsp
movb 0x4c(%rsp), %al
xorl %ecx, %ecx
cmpb $0x2b, %al
setne %cl
shll $0x1f, %ecx
movq 0x68(%rsp), %rdi
movl 0x8(%rdi), %edx
movl $0x1fffffff, %esi       # imm = 0x1FFFFFFF
andl %esi, %edx
orl 0x48(%rsp), %edx
orl %ecx, %edx
movl %edx, 0x8(%rdi)
cmpb $0x2b, %al
je 0x3c90
movdqu (%rbp), %xmm0
pshufd $0xb1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,3,2]
movdqu %xmm0, (%rbp)
addq %rbx, %r13
leaq 0xa0(%rsp), %rdi
movq 0x18(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x88(%rsp), %rcx
movq 0x20(%rsp), %r8
leaq 0xc(%rsp), %rbx
movq %rbx, %r9
callq 0x3de9
movl 0xc(%rsp), %ecx
movl %ecx, 0x4c(%r13)
movq %rax, 0x18(%rsp)
movq (%rax,%rcx,8), %rax
movq %rax, 0x8(%r13)
leaq 0x2a0(%rsp), %rdi
movq 0x10(%rsp), %rsi
leaq 0x38(%rsp), %rdx
leaq 0x90(%rsp), %rcx
movq 0x28(%rsp), %r8
movq %rbx, %r9
callq 0x3de9
movl 0xc(%rsp), %esi
movl %esi, %ecx
andl $0x1fffffff, %ecx       # imm = 0x1FFFFFFF
movl 0x48(%r13), %edx
movl $0xe0000000, %edi       # imm = 0xE0000000
andl %edi, %edx
orl %ecx, %edx
movl %edx, 0x48(%r13)
movq %rax, 0x10(%rsp)
movq (%rax,%rsi,8), %rax
movq %rax, (%r13)
movq 0x80(%rsp), %r13
jmp 0x3b16
movq %rbp, %rdi
callq 0x2050
movq %r14, %rdi
callq 0x2110
movq 0x38(%rsp), %rsi
shlq $0x3, %rsi
movq 0x10(%rsp), %rdi
callq 0x22c0
movq %rax, %r14
movq 0x30(%rsp), %rsi
shlq $0x3, %rsi
movq 0x18(%rsp), %rdi
callq 0x22c0
movl 0x38(%rsp), %ecx
movl %ecx, 0x18(%r13)
movl 0x30(%rsp), %ecx
movl %ecx, 0x1c(%r13)
movq %r14, 0x20(%r13)
movq %rax, 0x28(%r13)
movq 0x28(%rsp), %rax
movq %rax, 0x30(%r13)
movq 0x20(%rsp), %rax
movq %rax, 0x38(%r13)
movq %r13, %rax
addq $0x4a8, %rsp            # imm = 0x4A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x523f(%rip), %rax      # 0x8ff0
movq (%rax), %rdi
leaq 0x27cd(%rip), %rsi      # 0x6588
leaq 0x27ef(%rip), %rdx      # 0x65b1
movq %r15, %rcx
xorl %eax, %eax
callq 0x2220
pushq $0x1
popq %rdi
callq 0x2370