// File: /workspace/github_c_makefile_star2_O2/zhou-lab[P]tbmate/htslib/regidx.c
// Function: regidx_init
regidx_t *regidx_init(const char *fname, regidx_parse_f parser, regidx_free_f free_f, size_t payload_size, void *usr_dat)
{
    if ( !parser )
    {
        if ( !fname ) parser = regidx_parse_tab;
        else
        {
            int len = strlen(fname);
            if ( len>=7 && !strcasecmp(".bed.gz",fname+len-7) )
                parser = regidx_parse_bed;
            else if ( len>=8 && !strcasecmp(".bed.bgz",fname+len-8) )
                parser = regidx_parse_bed;
            else if ( len>=4 && !strcasecmp(".bed",fname+len-4) )
                parser = regidx_parse_bed;
            else
                parser = regidx_parse_tab;
        }
    }

    regidx_t *idx = (regidx_t*) calloc(1,sizeof(regidx_t));
    idx->free  = free_f;
    idx->parse = parser;
    idx->usr   = usr_dat;
    idx->seq2regs = khash_str2int_init();
    idx->rid_prev   = -1;
    idx->start_prev = -1;
    idx->end_prev   = -1;
    idx->payload_size = payload_size;
    if ( payload_size ) idx->payload = malloc(payload_size);

    if ( !fname ) return idx;
    
    kstring_t str = {0,0,0};

    htsFile *fp = hts_open(fname,"r");
    if ( !fp ) goto error;

    while ( hts_getline(fp, KS_SEP_LINE, &str) > 0 )
    {
        if ( regidx_insert(idx, str.s) ) goto error;
    }
    regidx_insert(idx, NULL);
    
    free(str.s);
    hts_close(fp);
    return idx;

error:
    free(str.s);
    if ( fp ) hts_close(fp);
    regidx_destroy(idx);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, %r13
movq %rcx, %r14
movq %rdx, %rbx
movq %rsi, %rbp
movq %rdi, 0x18(%rsp)
testq %rsi, %rsi
jne 0xf05e
movq %rbx, 0x8(%rsp)
movq %r13, 0x10(%rsp)
leaq 0x1aa(%rip), %rbp       # 0xf151
movq 0x18(%rsp), %rbx
testq %rbx, %rbx
je 0xf054
movq %rbx, %rdi
callq 0x3380
movq %rax, %r15
cmpl $0x7, %r15d
jl 0xf019
movl %r15d, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
leaq (%rbx,%r12), %rsi
addq $-0x7, %rsi
leaq 0x31864(%rip), %rdi     # 0x40843
callq 0x31c0
leaq 0x291(%rip), %rbp       # 0xf27c
testl %eax, %eax
je 0xf054
cmpl $0x7, %r15d
je 0xf029
leaq (%rbx,%r12), %rsi
addq $-0x8, %rsi
leaq 0x31847(%rip), %rdi     # 0x4084b
callq 0x31c0
testl %eax, %eax
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbx
jne 0xf029
jmp 0xf05e
cmpl $0x4, %r15d
jl 0xf054
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq %r15, %r12
movq 0x18(%rsp), %rax
leaq (%rax,%r12), %rsi
addq $-0x4, %rsi
leaq 0x31817(%rip), %rdi     # 0x40854
callq 0x31c0
testl %eax, %eax
leaq 0x106(%rip), %rbp       # 0xf151
jne 0xf054
leaq 0x228(%rip), %rbp       # 0xf27c
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbx
pushq $0x1
popq %r12
pushq $0x68
popq %rsi
movq %r12, %rdi
callq 0x36b0
movq %rax, %r15
movq %rbx, 0x20(%rax)
movq %rbp, 0x28(%rax)
movq %r13, 0x30(%rax)
pushq $0x28
popq %rsi
movq %r12, %rdi
callq 0x36b0
movq %rax, 0x10(%r15)
orq $-0x1, 0x50(%r15)
orl $-0x1, 0x58(%r15)
movl %r14d, 0x5c(%r15)
testq %r14, %r14
je 0xf0aa
movq %r14, %rdi
callq 0x3240
movq %rax, 0x60(%r15)
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0xf13f
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
leaq 0x33eac(%rip), %rsi     # 0x42f79
callq 0xa84d
testq %rax, %rax
je 0xf116
movq %rax, %rbx
pushq $0x2
popq %rbp
leaq 0x20(%rsp), %r14
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0xb135
testl %eax, %eax
jle 0xf123
movq 0x30(%rsp), %rsi
movq %r15, %rdi
callq 0xea68
testl %eax, %eax
je 0xf0e2
movq 0x30(%rsp), %rdi
callq 0x3370
movq %rbx, %rdi
callq 0xaf35
movq %r15, %rdi
callq 0xf38d
xorl %r15d, %r15d
jmp 0xf13f
movq %r15, %rdi
xorl %esi, %esi
callq 0xea68
movq 0x30(%rsp), %rdi
callq 0x3370
movq %rbx, %rdi
callq 0xaf35
movq %r15, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq