// File: /workspace/github_c_makefile_star2_O2/samtools[P]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 if ( len>=4 && !strcasecmp(".vcf",fname+len-4) )
                parser = regidx_parse_vcf;
            else if ( len>=7 && !strcasecmp(".vcf.gz",fname+len-7) )
                parser = regidx_parse_vcf;
            else
                parser = regidx_parse_tab;
        }
    }

    kstring_t str = KS_INITIALIZE;
    htsFile *fp = NULL;
    int ret;
    regidx_t *idx = (regidx_t*) calloc(1,sizeof(regidx_t));
    if (!idx) return NULL;
    idx->free  = free_f;
    idx->parse = parser;
    idx->usr   = usr_dat;
    idx->seq2regs = khash_str2int_init();
    if (!idx->seq2regs) goto error;
    idx->payload_size = payload_size;
    if ( payload_size ) {
        idx->payload = malloc(payload_size);
        if (!idx->payload) goto error;
    }

    if ( !fname ) return idx;

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

    while ((ret = hts_getline(fp, KS_SEP_LINE, &str)) > 0 ) {
        if ( regidx_insert(idx, str.s) ) goto error;
    }
    if (ret < -1) goto error;

    ret = hts_close(fp);
    fp = NULL;
    if ( ret != 0 ) {
        hts_log_error("Close failed .. %s", fname);
        goto error;
    }
    ks_free(&str);
    return idx;

error:
    ks_free(&str);
    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, %r12
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, %rbx
testq %rsi, %rsi
je 0x1bfa4
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
pushq $0x1
popq %rdi
pushq $0x60
popq %rsi
callq 0x4300
testq %rax, %rax
je 0x1bf8f
movq %rax, %r15
movq %r13, 0x20(%rax)
movq %rbp, 0x28(%rax)
movq %r12, 0x30(%rax)
callq 0x1bd74
movq %rax, 0x10(%r15)
testq %rax, %rax
je 0x1bf6d
movl %r14d, 0x38(%r15)
testq %r14, %r14
je 0x1bf21
movq %r14, %rdi
callq 0x4680
movq %rax, 0x40(%r15)
testq %rax, %rax
je 0x1bf6d
testq %rbx, %rbx
je 0x1bf92
leaq 0x5e5f9(%rip), %rsi     # 0x7a526
movq %rbx, %rdi
callq 0x111c7
testq %rax, %rax
je 0x1bf6d
movq %rax, %r14
pushq $0x2
popq %rbp
leaq 0x10(%rsp), %r12
movq %r14, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0x12938
testl %eax, %eax
jle 0x1c097
movq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x1b57a
testl %eax, %eax
je 0x1bf45
jmp 0x1bf70
xorl %r14d, %r14d
leaq 0x10(%rsp), %rdi
callq 0x1b5ec
testq %r14, %r14
je 0x1bf87
movq %r14, %rdi
callq 0x1239d
movq %r15, %rdi
callq 0x1bd98
xorl %r15d, %r15d
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x3b5(%rip), %rbp      # 0x1bbf6
testq %rbx, %rbx
je 0x1bec8
movq %rbx, %rdi
callq 0x41d0
cmpl $0x7, %eax
movq %rax, 0x8(%rsp)
jl 0x1c01a
movl %eax, %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
leaq (%rbx,%r15), %rsi
addq $-0x7, %rsi
leaq 0x59404(%rip), %rdi     # 0x753e3
callq 0x4520
leaq 0xff(%rip), %rbp        # 0x1c0ea
testl %eax, %eax
je 0x1bec8
movq 0x8(%rsp), %rax
cmpl $0x7, %eax
je 0x1c029
leaq (%rbx,%r15), %rsi
addq $-0x8, %rsi
leaq 0x5cf2d(%rip), %rdi     # 0x78f39
callq 0x4520
testl %eax, %eax
jne 0x1c029
jmp 0x1bec8
cmpl $0x4, %eax
jl 0x1c08b
movl %eax, %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
leaq (%rbx,%r15), %rbp
addq $-0x4, %rbp
leaq 0x593a6(%rip), %rdi     # 0x753de
movq %rbp, %rsi
callq 0x4520
testl %eax, %eax
je 0x1c0cf
leaq 0x5c3b6(%rip), %rdi     # 0x78405
movq %rbp, %rsi
callq 0x4520
leaq 0x1b5(%rip), %rbp       # 0x1c213
testl %eax, %eax
je 0x1bec8
cmpl $0x7, 0x8(%rsp)
jl 0x1c08b
addq %rbx, %r15
addq $-0x7, %r15
leaq 0x5c382(%rip), %rdi     # 0x783fd
movq %r15, %rsi
callq 0x4520
testl %eax, %eax
je 0x1bec8
leaq -0x49c(%rip), %rbp      # 0x1bbf6
jmp 0x1bec8
cmpl $-0x1, %eax
jl 0x1bf70
movq %r14, %rdi
callq 0x1239d
testl %eax, %eax
je 0x1c0db
leaq 0x5ce8f(%rip), %rsi     # 0x78f42
leaq 0x5ce94(%rip), %rdx     # 0x78f4e
xorl %r14d, %r14d
pushq $0x1
popq %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x110c6
jmp 0x1bf70
leaq 0x14(%rip), %rbp        # 0x1c0ea
jmp 0x1bec8
leaq 0x10(%rsp), %rdi
callq 0x1b5ec
jmp 0x1bf92