// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
movq %r8, %r12
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, %rbx
testq %rsi, %rsi
je 0x12b5e
movl $0x1, %edi
movl $0x68, %esi
callq 0x36a0
movq %rax, %r15
movq %r13, 0x20(%rax)
movq %rbp, 0x28(%rax)
movq %r12, 0x30(%rax)
movl $0x1, %edi
movl $0x28, %esi
callq 0x36a0
movq %rax, 0x10(%r15)
movq $-0x1, 0x50(%r15)
movl $0xffffffff, 0x58(%r15) # imm = 0xFFFFFFFF
movl %r14d, 0x5c(%r15)
testq %r14, %r14
je 0x12ade
movq %r14, %rdi
callq 0x3240
movq %rax, 0x60(%r15)
testq %rbx, %rbx
je 0x12c2f
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
leaq 0x3e55f(%rip), %rsi     # 0x5105d
movq %rbx, %rdi
callq 0xd846
testq %rax, %rax
je 0x12bfe
movq %rax, %rbx
movq %rsp, %r14
movq %rbx, %rdi
movl $0x2, %esi
movq %r14, %rdx
callq 0xe10e
testl %eax, %eax
jle 0x12c15
movq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x12421
testl %eax, %eax
je 0x12b15
movq 0x10(%rsp), %rdi
callq 0x3370
testq %rbx, %rbx
je 0x12c08
movq %rbx, %rdi
callq 0xdf1c
jmp 0x12c08
leaq 0xdc(%rip), %rbp        # 0x12c41
testq %rbx, %rbx
je 0x12a88
movq %rbx, %rdi
callq 0x3380
movq %rax, %r15
cmpl $0x7, %r15d
jl 0x12b9f
movl %r15d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%rbx,%rax), %rsi
addq $-0x7, %rsi
leaq 0x3bc4b(%rip), %rdi     # 0x4e7e1
callq 0x31c0
testl %eax, %eax
je 0x12bf2
cmpl $0x8, %r15d
jl 0x12bc5
movl %r15d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%rbx,%rax), %rsi
addq $-0x8, %rsi
leaq 0x3bc2d(%rip), %rdi     # 0x4e7e9
callq 0x31c0
testl %eax, %eax
je 0x12bf2
cmpl $0x4, %r15d
jl 0x12a88
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
leaq (%rbx,%r15), %rsi
addq $-0x4, %rsi
leaq 0x3bc0d(%rip), %rdi     # 0x4e7f2
callq 0x31c0
testl %eax, %eax
jne 0x12a88
leaq 0x1a9(%rip), %rbp       # 0x12da2
jmp 0x12a88
movq 0x10(%rsp), %rdi
callq 0x3370
movq %r15, %rdi
callq 0x12ef0
xorl %r15d, %r15d
jmp 0x12c2f
movq %r15, %rdi
callq 0x122df
movq 0x10(%rsp), %rdi
callq 0x3370
movq %rbx, %rdi
callq 0xdf1c
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq