// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/cram/cram_index.c
// Function: cram_index_load
int cram_index_load(cram_fd *fd, const char *fn, const char *fn_idx) {

    char *tfn_idx = NULL;
    char buf[65536];
    ssize_t len;
    kstring_t kstr = {0};
    hFILE *fp;
    cram_index *idx;
    cram_index **idx_stack = NULL, *ep, e;
    int idx_stack_alloc = 0, idx_stack_ptr = 0;
    size_t pos = 0;

    /* Check if already loaded */
    if (fd->index)
        return 0;

    fd->index = calloc((fd->index_sz = 1), sizeof(*fd->index));
    if (!fd->index)
        return -1;

    idx = &fd->index[0];
    idx->refid = -1;
    idx->start = INT_MIN;
    idx->end   = INT_MAX;

    idx_stack = calloc(++idx_stack_alloc, sizeof(*idx_stack));
    if (!idx_stack)
        goto fail;

    idx_stack[idx_stack_ptr] = idx;

    // Support pathX.cram##idx##pathY.crai
    const char *fn_delim = strstr(fn, HTS_IDX_DELIM);
    if (fn_delim && !fn_idx)
        fn_idx = fn_delim + strlen(HTS_IDX_DELIM);

    if (!fn_idx) {
        if (hts_idx_check_local(fn, HTS_FMT_CRAI, &tfn_idx) == 0 && hisremote(fn))
            tfn_idx = hts_idx_getfn(fn, ".crai");

        if (!tfn_idx) {
            hts_log_error("Could not retrieve index file for '%s'", fn);
            goto fail;
        }
        fn_idx = tfn_idx;
    }

    if (!(fp = hopen(fn_idx, "r"))) {
        hts_log_error("Could not open index file '%s'", fn_idx);
        goto fail;
    }

    // Load the file into memory
    while ((len = hread(fp, buf, sizeof(buf))) > 0) {
        if (kputsn(buf, len, &kstr) < 0)
            goto fail;
    }

    if (len < 0 || kstr.l < 2)
        goto fail;

    if (hclose(fp) < 0)
        goto fail;

    // Uncompress if required
    if (kstr.s[0] == 31 && (uc)kstr.s[1] == 139) {
        size_t l = 0;
        char *s = zlib_mem_inflate(kstr.s, kstr.l, &l);
        if (!s)
            goto fail;

        free(kstr.s);
        kstr.s = s;
        kstr.l = l;
        kstr.m = l; // conservative estimate of the size allocated
        if (kputsn("", 0, &kstr) < 0) // ensure kstr.s is NUL-terminated
            goto fail;
    }


    // Parse it line at a time
    while (pos < kstr.l) {
        /* 1.1 layout */
        if (kget_int32(&kstr, &pos, &e.refid) == -1)
            goto fail;

        if (kget_int32(&kstr, &pos, &e.start) == -1)
            goto fail;

        if (kget_int32(&kstr, &pos, &e.end) == -1)
            goto fail;

        if (kget_int64(&kstr, &pos, &e.offset) == -1)
            goto fail;

        if (kget_int32(&kstr, &pos, &e.slice) == -1)
            goto fail;

        if (kget_int32(&kstr, &pos, &e.len) == -1)
            goto fail;

        e.end += e.start-1;
        //printf("%d/%d..%d-offset=%" PRIu64 ",len=%d,slice=%d\n", e.refid, e.start, e.end, e.offset, e.len, e.slice);

        if (e.refid < -1) {
            hts_log_error("Malformed index file, refid %d", e.refid);
            goto fail;
        }

        if (e.refid != idx->refid) {
            if (fd->index_sz < e.refid+2) {
                cram_index *new_idx;
                int new_sz = e.refid+2;
                size_t index_end = fd->index_sz * sizeof(*fd->index);
                new_idx = realloc(fd->index,
                                  new_sz * sizeof(*fd->index));
                if (!new_idx)
                    goto fail;

                fd->index = new_idx;
                fd->index_sz = new_sz;
                memset(((char *)fd->index) + index_end, 0,
                       fd->index_sz * sizeof(*fd->index) - index_end);
            }
            idx = &fd->index[e.refid+1];
            idx->refid = e.refid;
            idx->start = INT_MIN;
            idx->end   = INT_MAX;
            idx->nslice = idx->nalloc = 0;
            idx->e = NULL;
            idx_stack[(idx_stack_ptr = 0)] = idx;
        }

        while (!(e.start >= idx->start && e.end <= idx->end) ||
               (idx->start == 0 && idx->refid == -1)) {
            idx = idx_stack[--idx_stack_ptr];
        }

        // Now contains, so append
        if (idx->nslice+1 >= idx->nalloc) {
            cram_index *new_e;
            idx->nalloc = idx->nalloc ? idx->nalloc*2 : 16;
            new_e = realloc(idx->e, idx->nalloc * sizeof(*idx->e));
            if (!new_e)
                goto fail;

            idx->e = new_e;
        }

        e.nalloc = e.nslice = 0; e.e = NULL;
        *(ep = &idx->e[idx->nslice++]) = e;
        idx = ep;

        if (++idx_stack_ptr >= idx_stack_alloc) {
            cram_index **new_stack;
            idx_stack_alloc *= 2;
            new_stack = realloc(idx_stack, idx_stack_alloc*sizeof(*idx_stack));
            if (!new_stack)
                goto fail;
            idx_stack = new_stack;
        }
        idx_stack[idx_stack_ptr] = idx;

        while (pos < kstr.l && kstr.s[pos] != '\n')
            pos++;
        pos++;
    }

    free(idx_stack);
    free(kstr.s);
    free(tfn_idx);

    // Convert NCList to linear linked list
    link_index(fd);

    //dump_index(fd);

    return 0;

 fail:
    free(kstr.s);
    free(idx_stack);
    free(tfn_idx);
    cram_index_free(fd); // Also sets fd->index = NULL
    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x100a8, %rsp          # imm = 0x100A8
xorl %r14d, %r14d
movq %r14, 0x38(%rsp)
movq %r14, 0x30(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq %r14, 0x18(%rsp)
cmpq %r14, 0x8878(%rdi)
jne 0x49b9b
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r13
movl $0x1, 0x8870(%rdi)
movl $0x1, %edi
movl $0x38, %esi
callq 0x4300
movq %rax, 0x8878(%r13)
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x49b9b
movq %rax, %r12
movabsq $-0x7fffffff00000001, %rax # imm = 0x80000000FFFFFFFF
movq %rax, 0x10(%r12)
movl $0x7fffffff, 0x18(%r12) # imm = 0x7FFFFFFF
movl $0x1, %edi
movl $0x8, %esi
callq 0x4300
testq %rax, %rax
je 0x494e1
movq %rax, %rbp
movq %r12, 0x40(%rsp)
movq %r12, (%rax)
leaq 0x4ca3b(%rip), %rsi     # 0x95ee0
movq %r15, %rdi
callq 0x40e0
leaq 0x7(%rax), %r12
testq %rax, %rax
cmoveq %rbx, %r12
testq %rbx, %rbx
cmovneq %rbx, %r12
testq %r12, %r12
jne 0x49514
leaq 0x38(%rsp), %rdx
movq %r15, %rdi
movl $0x3, %esi
callq 0x1ae7b
testl %eax, %eax
je 0x494e8
movq 0x38(%rsp), %r12
jmp 0x4950b
xorl %ebp, %ebp
jmp 0x49b77
movq %r15, %rdi
callq 0x101ec
testl %eax, %eax
je 0x494da
leaq 0x4d452(%rip), %rsi     # 0x9694d
movq %r15, %rdi
callq 0x1b1ca
movq %rax, %r12
movq %rax, 0x38(%rsp)
testq %r12, %r12
je 0x49b2e
movq %rbp, 0x8(%rsp)
leaq 0x4f0ab(%rip), %rsi     # 0x985cb
movq %r12, %rdi
xorl %eax, %eax
callq 0xfe44
testq %rax, %rax
je 0x4957c
movq %rax, %r15
movq %r13, 0x48(%rsp)
movq 0x8(%rax), %rsi
movq 0x10(%rax), %r13
subq %rsi, %r13
movl $0x10000, %r14d         # imm = 0x10000
cmpq %r14, %r13
movl $0x10000, %ebx          # imm = 0x10000
cmovbq %r13, %rbx
leaq 0xa0(%rsp), %rdi
movq %rbx, %rdx
callq 0x43c0
addq %rbx, 0x8(%r15)
cmpq %r14, %r13
movq %r15, 0x50(%rsp)
jb 0x4959e
movq 0x20(%rsp), %r15
movq %r14, %r13
jmp 0x495ce
leaq 0x50ab8(%rip), %rsi     # 0x9a03b
leaq 0x50ac1(%rip), %rdx     # 0x9a04b
movl $0x1, %edi
movq %r12, %rcx
xorl %eax, %eax
callq 0x13de5
jmp 0x49b72
testb $0x2, 0x30(%r15)
je 0x495c0
leaq 0xa0(%rsp), %rsi
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
movq %rbx, %rcx
callq 0xf129
movq %rax, %r13
movq 0x20(%rsp), %r15
testq %r13, %r13
jle 0x496d9
movq 0x28(%rsp), %r14
movq 0x30(%rsp), %r12
leaq 0xa0(%rsp), %rbp
leaq (%r15,%r13), %rax
addq $0x2, %rax
cmpq %r15, %rax
jbe 0x49b4d
cmpq %rax, %r14
jae 0x49627
movq %rax, %rcx
shrq $0x3e, %rcx
movq %rax, %rbx
shrq %rbx
testq %rcx, %rcx
movl $0x0, %ecx
cmovneq %rcx, %rbx
addq %rax, %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4840
testq %rax, %rax
jne 0x4962d
jmp 0x49b1d
movq %r12, %rax
movq %r14, %rbx
movq %rbx, %r14
movq %rax, %r12
leaq (%rax,%r15), %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x43c0
addq %r13, %r15
movb $0x0, (%r12,%r15)
testl %r13d, %r13d
js 0x49b1d
movq %r14, 0x58(%rsp)
movq %r15, 0x10(%rsp)
movq %rbp, %rdi
movq 0x50(%rsp), %rbp
movq 0x8(%rbp), %rsi
movq 0x10(%rbp), %r15
subq %rsi, %r15
movl $0x10000, %r13d         # imm = 0x10000
cmpq %r13, %r15
movl $0x10000, %ebx          # imm = 0x10000
cmovbq %r15, %rbx
movq %rdi, %r14
movq %rbx, %rdx
callq 0x43c0
addq %rbx, 0x8(%rbp)
cmpq $0xffff, %r15           # imm = 0xFFFF
ja 0x496b9
testb $0x2, 0x30(%rbp)
movq %r15, %r13
je 0x496b9
movl $0x10000, %edx          # imm = 0x10000
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rcx
callq 0xf129
movq %rax, %r13
testq %r13, %r13
movq 0x10(%rsp), %r15
movq %r14, %rbp
movq 0x58(%rsp), %r14
jg 0x495e0
movq %r14, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq %r15, 0x20(%rsp)
testq %r13, %r13
jne 0x49b67
cmpq $0x2, %r15
movq 0x48(%rsp), %r13
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jb 0x49b72
movq 0x50(%rsp), %rdi
callq 0xf4df
testl %eax, %eax
js 0x49b72
movq 0x30(%rsp), %rbx
cmpb $0x1f, (%rbx)
movq 0x8(%rsp), %rbp
jne 0x4977e
cmpb $-0x75, 0x1(%rbx)
jne 0x4977e
leaq 0x60(%rsp), %rdx
movq $0x0, (%rdx)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4c45d
testq %rax, %rax
je 0x49b77
movq %rax, %r15
movq %rbx, %rdi
callq 0x45c0
leaq 0x20(%rsp), %rdx
movq %r15, 0x10(%rdx)
movq 0x60(%rsp), %rax
movq %rax, (%rdx)
movq %rax, 0x8(%rdx)
leaq 0x4c603(%rip), %rdi     # 0x95d6d
xorl %esi, %esi
callq 0x49bce
testl %eax, %eax
js 0x49b77
movq 0x20(%rsp), %r15
cmpq %r15, 0x18(%rsp)
jae 0x49af4
leaq 0x70(%rsp), %rcx
xorl %r12d, %r12d
movl $0x1, 0x58(%rsp)
leaq 0x18(%rsp), %rdx
movq %r15, %rdi
movq 0x30(%rsp), %r15
movq %rdi, 0x10(%rsp)
movq %r15, %rsi
movq %rdx, %rbx
callq 0x49c70
cmpl $-0x1, %eax
je 0x49b77
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
leaq 0x74(%rsp), %rcx
callq 0x49c70
cmpl $-0x1, %eax
je 0x49b77
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
leaq 0x78(%rsp), %rcx
callq 0x49c70
cmpl $-0x1, %eax
je 0x49b77
movq 0x18(%rsp), %rax
movl $0x1, %ecx
cmpq 0x10(%rsp), %rax
jae 0x4983f
movzbl (%r15,%rax), %ecx
cmpl $0x20, %ecx
je 0x4981b
cmpl $0x9, %ecx
jne 0x4982a
incq %rax
cmpq %rax, 0x10(%rsp)
jne 0x4980c
jmp 0x49b77
xorl %edx, %edx
cmpb $0x2d, %cl
sete %dl
addq %rdx, %rax
movq %rdx, %rcx
negq %rcx
orq $0x1, %rcx
cmpq 0x10(%rsp), %rax
jae 0x49b77
movb (%r15,%rax), %dl
addb $-0x30, %dl
cmpb $0x9, %dl
ja 0x49b77
xorl %edx, %edx
movb (%r15,%rax), %sil
addb $-0x30, %sil
cmpb $0x9, %sil
ja 0x4988a
incq %rax
movzbl %sil, %esi
leaq (%rdx,%rdx,4), %rdx
leaq (%rsi,%rdx,2), %rdx
cmpq %rax, 0x10(%rsp)
jne 0x4985c
movq 0x10(%rsp), %rdi
movq %rdi, %rax
jmp 0x4988f
movq 0x10(%rsp), %rdi
imulq %rcx, %rdx
movq %rax, 0x18(%rsp)
movq %rdx, 0x88(%rsp)
movq %r15, %rsi
leaq 0x18(%rsp), %rbx
movq %rbx, %rdx
leaq 0x80(%rsp), %rcx
callq 0x49c70
cmpl $-0x1, %eax
je 0x49b77
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
leaq 0x84(%rsp), %rcx
callq 0x49c70
cmpl $-0x1, %eax
je 0x49b77
movq %rbp, 0x8(%rsp)
movl 0x74(%rsp), %r14d
movl 0x78(%rsp), %eax
leal (%r14,%rax), %ebp
decl %ebp
movl %ebp, 0x78(%rsp)
movslq 0x70(%rsp), %rbx
cmpq $-0x2, %rbx
jle 0x49bb0
movq 0x40(%rsp), %rdx
cmpl 0x10(%rdx), %ebx
je 0x499ab
movslq 0x8870(%r13), %rax
leal 0x2(%rbx), %ecx
movq 0x8878(%r13), %r15
cmpl %ecx, %eax
jge 0x4997d
movq %rax, 0x50(%rsp)
movl %ecx, 0x40(%rsp)
movl %ecx, %eax
imulq $0x38, %rax, %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x4840
testq %rax, %rax
je 0x49b6c
movq %rax, %r15
imulq $0x38, 0x50(%rsp), %rax
movq %r15, 0x8878(%r13)
movl 0x40(%rsp), %ecx
movl %ecx, 0x8870(%r13)
movq %r15, %rdi
addq %rax, %rdi
subq %rax, %r12
xorl %esi, %esi
movq %r12, %rdx
callq 0x4260
imulq $0x38, %rbx, %rax
leaq (%r15,%rax), %rdx
addq $0x38, %rdx
movl %ebx, 0x10(%rdx)
movabsq $0x7fffffff80000000, %rax # imm = 0x7FFFFFFF80000000
movq %rax, 0x14(%rdx)
xorps %xmm0, %xmm0
movups %xmm0, (%rdx)
movq 0x8(%rsp), %rax
movq %rdx, (%rax)
xorl %r12d, %r12d
movslq %r12d, %rax
leal 0x1(%rax), %r12d
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
addq $-0x8, %rax
movq %rdx, %rbx
movl 0x14(%rbx), %ecx
cmpl %ecx, %r14d
jl 0x499d9
cmpl 0x18(%rbx), %ebp
jg 0x499d9
testl %ecx, %ecx
jne 0x499e5
cmpl $-0x1, 0x10(%rbx)
jne 0x499e5
movq (%rax), %rbx
decl %r12d
addq $-0x8, %rax
jmp 0x499c2
movl (%rbx), %ecx
movl 0x4(%rbx), %eax
incl %ecx
cmpl %eax, %ecx
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rbp
jl 0x49a28
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x10, %eax
cmovel %eax, %ecx
movl %ecx, 0x4(%rbx)
movq 0x8(%rbx), %rdi
movslq %ecx, %rax
imulq $0x38, %rax, %rsi
callq 0x4840
testq %rax, %rax
je 0x49b77
movq %rax, 0x8(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq 0x8(%rbx), %rdx
movslq (%rbx), %rax
leal 0x1(%rax), %ecx
movl %ecx, (%rbx)
imulq $0x38, %rax, %rbx
movq 0x90(%rsp), %rax
movq %rax, 0x30(%rdx,%rbx)
movaps 0x60(%rsp), %xmm0
movaps 0x70(%rsp), %xmm1
movaps 0x80(%rsp), %xmm2
movups %xmm2, 0x20(%rdx,%rbx)
movups %xmm1, 0x10(%rdx,%rbx)
movq %rdx, %r15
movups %xmm0, (%rdx,%rbx)
movl 0x58(%rsp), %eax
cmpl %eax, %r12d
jge 0x49a80
movq 0x10(%rsp), %rcx
jmp 0x49aa6
addl %eax, %eax
movl %eax, 0x58(%rsp)
movslq %eax, %rsi
shlq $0x3, %rsi
movq %rbp, %rdi
callq 0x4840
testq %rax, %rax
je 0x49b77
movq 0x20(%rsp), %rcx
movq %rax, %rbp
leaq 0x18(%rsp), %rdx
addq %rbx, %r15
movslq %r12d, %rax
movq %r15, 0x40(%rsp)
movq %r15, (%rbp,%rax,8)
movq 0x18(%rsp), %rax
cmpq %rcx, %rax
movq %rcx, %r15
jae 0x49ade
movq 0x30(%rsp), %rcx
cmpb $0xa, (%rcx,%rax)
je 0x49ade
incq %rax
cmpq %rax, %r15
jne 0x49acd
movq %r15, %rax
incq %rax
movq %rax, 0x18(%rsp)
cmpq %r15, %rax
leaq 0x70(%rsp), %rcx
jb 0x4979e
movq %rbp, %rdi
callq 0x45c0
movq 0x30(%rsp), %rdi
callq 0x45c0
movq 0x38(%rsp), %rdi
callq 0x45c0
movq %r13, %rdi
callq 0x49cf8
xorl %r14d, %r14d
jmp 0x49b9b
movq %r14, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq %r15, 0x20(%rsp)
jmp 0x49b67
leaq 0x50506(%rip), %rsi     # 0x9a03b
leaq 0x4cfb4(%rip), %rdx     # 0x96af0
movl $0x1, %edi
movq %r15, %rcx
xorl %eax, %eax
callq 0x13de5
jmp 0x49b77
movq %r14, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq %r15, 0x20(%rsp)
callq 0x4050
movl $0x4b, (%rax)
movq 0x48(%rsp), %r13
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rbp
movq 0x30(%rsp), %rdi
callq 0x45c0
movq %rbp, %rdi
callq 0x45c0
movq 0x38(%rsp), %rdi
callq 0x45c0
movq %r13, %rdi
callq 0x49d49
movl %r14d, %eax
addq $0x100a8, %rsp          # imm = 0x100A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x50484(%rip), %rsi     # 0x9a03b
leaq 0x504ac(%rip), %rdx     # 0x9a06a
movl $0x1, %edi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x13de5
jmp 0x49b6c