// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movq %r8, %r15
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, %rbx
testq %rsi, %rsi
je 0x12dc4
movl $0x1, %edi
movl $0x68, %esi
callq 0x36a0
movq %rax, %r12
movq %r13, 0x20(%rax)
movq %rbp, 0x28(%rax)
movq %r15, 0x30(%rax)
movl $0x1, %edi
movl $0x28, %esi
callq 0x36a0
movq %rax, 0x10(%r12)
movq $-0x1, 0x50(%r12)
movl $0xffffffff, 0x58(%r12) # imm = 0xFFFFFFFF
movl %r14d, 0x5c(%r12)
testq %r14, %r14
je 0x12d46
movq %r14, %rdi
callq 0x3240
movq %rax, 0x60(%r12)
testq %rbx, %rbx
je 0x12e3c
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
leaq 0x3e4be(%rip), %rsi     # 0x51225
movq %rbx, %rdi
callq 0xda3c
testq %rax, %rax
je 0x12db7
movq %rax, %rbx
leaq 0x10(%rsp), %r14
movq %rbx, %rdi
movl $0x2, %esi
movq %r14, %rdx
callq 0xe30c
testl %eax, %eax
jle 0x12e22
movq 0x20(%rsp), %rsi
movq %r12, %rdi
callq 0x12693
testl %eax, %eax
je 0x12d7c
movq 0x20(%rsp), %rdi
callq 0x3370
movq %rbx, %rdi
callq 0xe119
movq %r12, %rdi
callq 0x130fc
xorl %r12d, %r12d
jmp 0x12e3c
leaq 0xe6(%rip), %rbp        # 0x12eb1
testq %rbx, %rbx
je 0x12ceb
movq %rbx, %rdi
callq 0x3380
movq %rax, %r12
cmpl $0x7, %r12d
jl 0x12e4e
movl %r12d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x8(%rsp)
leaq (%rbx,%rax), %rsi
addq $-0x7, %rsi
leaq 0x3ba54(%rip), %rdi     # 0x4e855
callq 0x31c0
leaq 0x1dc(%rip), %rbp       # 0x12fe9
testl %eax, %eax
je 0x12ceb
cmpl $0x7, %r12d
jne 0x12e61
movq 0x8(%rsp), %r12
jmp 0x12e82
movq %r12, %rdi
callq 0x1254e
movq 0x20(%rsp), %rdi
callq 0x3370
movq %rbx, %rdi
callq 0xe119
movq %r12, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x4, %r12d
jl 0x12ceb
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
jmp 0x12e82
movq 0x8(%rsp), %r12
leaq (%rbx,%r12), %rsi
addq $-0x8, %rsi
leaq 0x3b9e8(%rip), %rdi     # 0x4e85d
callq 0x31c0
testl %eax, %eax
je 0x12ceb
leaq (%rbx,%r12), %rsi
addq $-0x4, %rsi
leaq 0x3b9d5(%rip), %rdi     # 0x4e866
callq 0x31c0
testl %eax, %eax
leaq 0x12(%rip), %rbp        # 0x12eb1
jne 0x12ceb
leaq 0x13d(%rip), %rbp       # 0x12fe9
jmp 0x12ceb