// File: /workspace/github_c_makefile_star2_O3/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 0x3f24
movl %edx, %r12d
movq %rsi, %rbp
movq %rdi, %r15
xorl %eax, %eax
movq %rax, (%rsp)
movq %rax, 0x50(%rsp)
testq %rsi, %rsi
je 0x3f2b
movl 0x18(%rbp), %eax
movl 0x1c(%rbp), %ebx
movq 0x20(%rbp), %rcx
movq %rcx, 0x10(%rsp)
movq 0x28(%rbp), %r13
movq 0x30(%rbp), %rcx
movq %rcx, 0x20(%rsp)
movq 0x38(%rbp), %rcx
movq %rcx, 0x18(%rsp)
movq %rbx, %rcx
movq %rax, %rdx
jmp 0x3f9b
xorl %ebp, %ebp
jmp 0x42ae
movl $0x1, %edi
movl $0x50, %esi
callq 0x21f0
movq %rax, %rbp
movl $0x800, %edi            # imm = 0x800
callq 0x2280
movq %rax, 0x10(%rsp)
movl $0x800, %edi            # imm = 0x800
callq 0x2280
movq %rax, %r13
xorl %ebx, %ebx
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x590c
movq %rax, 0x20(%rsp)
movq %rbx, 0x18(%rax)
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x590c
movq %rax, 0x18(%rsp)
movq %rbx, 0x18(%rax)
movl $0x100, %ecx            # imm = 0x100
xorl %eax, %eax
movl $0x100, %edx            # imm = 0x100
movq %rbx, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq %rcx, 0x90(%rsp)
movq %rdx, 0x98(%rsp)
leaq 0x3576(%rip), %rsi      # 0x7532
movq %r15, %rdi
callq 0x2310
testq %rax, %rax
je 0x42c3
movq %rax, %r14
movq %r13, 0x28(%rsp)
movq %rsp, %rdi
leaq 0x50(%rsp), %rsi
movq %rax, %rdx
callq 0x2350
cmpq $-0x1, %rax
je 0x4247
callq 0x23f0
movq %rax, %r13
andl $0x3, %r12d
shll $0x1d, %r12d
movl %r12d, 0x40(%rsp)
movq %rsp, %r15
leaq 0x50(%rsp), %r12
movq (%rsp), %rdi
movq (%r13), %rax
movq %rdi, %rcx
movzbl (%rcx), %edx
incq %rcx
testb $0x20, 0x1(%rax,%rdx,2)
jne 0x4017
testb %dl, %dl
je 0x402d
cmpb $0x23, (%rdi)
jne 0x4046
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2350
cmpq $-0x1, %rax
jne 0x400c
jmp 0x4247
movq (%rbp), %rax
movq %rbp, %rbx
movq 0x10(%rbp), %rbp
cmpq 0x8(%rbx), %rax
jne 0x4089
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%rbx)
shlq $0x4, %rcx
leaq (%rcx,%rcx,4), %rsi
movq %rbp, %rdi
callq 0x22c0
movq %rax, %rbp
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
movq (%rsp), %rdi
leaq 0x1(%rax), %rcx
movq %rbx, 0x88(%rsp)
movq %rcx, (%rbx)
leaq (%rax,%rax,4), %rbx
shlq $0x4, %rbx
leaq (%rbx,%rbp), %r8
addq $0x10, %r8
leaq 0x14(%rbp,%rbx), %r9
leaq 0x18(%rbp,%rbx), %rax
movq %rax, 0x48(%rsp)
leaq 0x1c(%rbp,%rbx), %rax
movq %rax, 0x80(%rsp)
leaq 0x20(%rbp,%rbx), %rax
movq %rax, 0x78(%rsp)
leaq 0x24(%rbp,%rbx), %rax
movq %rax, 0x68(%rsp)
leaq 0x28(%rbp,%rbx), %rax
movq %rax, 0x60(%rsp)
leaq 0x30(%rbp,%rbx), %rax
movq %rax, 0x58(%rsp)
leaq (%rbx,%rbp), %r11
addq $0x38, %r11
leaq (%rbx,%rbp), %r10
addq $0x40, %r10
movq %r10, 0x70(%rsp)
subq $0x8, %rsp
leaq 0x34b4(%rip), %rsi      # 0x75c0
leaq 0x2a8(%rsp), %rdx
leaq 0xa8(%rsp), %rcx
xorl %eax, %eax
pushq %r10
pushq %r11
pushq 0x70(%rsp)
leaq 0x64(%rsp), %r10
pushq %r10
pushq 0x88(%rsp)
pushq 0x98(%rsp)
pushq 0xb0(%rsp)
pushq 0xc0(%rsp)
pushq 0x90(%rsp)
callq 0x22a0
addq $0x50, %rsp
movb 0x44(%rsp), %al
xorl %ecx, %ecx
cmpb $0x2b, %al
setne %cl
shll $0x1f, %ecx
movq 0x70(%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 0x4195
movq 0x48(%rsp), %rax
movdqu (%rax), %xmm0
pshufd $0xb1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,3,2]
movdqu %xmm0, (%rax)
addq %rbx, %rbp
leaq 0xa0(%rsp), %rdi
movq 0x28(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x90(%rsp), %rcx
movq 0x18(%rsp), %r8
leaq 0xc(%rsp), %rbx
movq %rbx, %r9
callq 0x42cb
movl 0xc(%rsp), %ecx
movl %ecx, 0x4c(%rbp)
movq %rax, 0x28(%rsp)
movq (%rax,%rcx,8), %rax
movq %rax, 0x8(%rbp)
leaq 0x2a0(%rsp), %rdi
movq 0x10(%rsp), %rsi
leaq 0x38(%rsp), %rdx
leaq 0x98(%rsp), %rcx
movq 0x20(%rsp), %r8
movq %rbx, %r9
callq 0x42cb
movl 0xc(%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 0x88(%rsp), %rbp
jne 0x400c
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4255
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 0x28(%rsp), %rdi
callq 0x22c0
movl 0x38(%rsp), %ecx
movl %ecx, 0x18(%rbp)
movl 0x30(%rsp), %ecx
movl %ecx, 0x1c(%rbp)
movq %r14, 0x20(%rbp)
movq %rax, 0x28(%rbp)
movq 0x20(%rsp), %rax
movq %rax, 0x30(%rbp)
movq 0x18(%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 0x2613