// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x28(%rsp)
andq $0x0, 0x50(%rsp)
xorps %xmm0, %xmm0
andq $0x0, 0x20(%rsp)
movaps %xmm0, 0x40(%rsp)
xorl %r15d, %r15d
cmpq %r15, 0x8878(%rdi)
jne 0x3d02a
movq %rdx, %rbp
movq %rsi, %r13
movq %rdi, %r12
movl $0x1, 0x8870(%rdi)
pushq $0x1
popq %rdi
pushq $0x38
popq %rsi
callq 0x4300
movq %rax, 0x8878(%r12)
pushq $-0x1
popq %r15
testq %rax, %rax
je 0x3d02a
movq %rax, %rbx
movabsq $-0x7fffffff00000001, %rax # imm = 0x80000000FFFFFFFF
movq %rax, 0x10(%rbx)
movl $0x7fffffff, 0x18(%rbx) # imm = 0x7FFFFFFF
pushq $0x1
popq %rdi
pushq $0x8
popq %rsi
callq 0x4300
testq %rax, %rax
je 0x3ce47
movq %rax, 0x8(%rsp)
movq %rbx, (%rax)
leaq 0x3b0c4(%rip), %rsi     # 0x77ed1
movq %r13, %rdi
callq 0x40e0
leaq 0x7(%rax), %r15
testq %rax, %rax
cmoveq %rbp, %r15
testq %rbp, %rbp
cmovneq %rbp, %r15
testq %r15, %r15
jne 0x3ce7b
pushq $0x3
popq %rsi
leaq 0x28(%rsp), %rdx
movq %r13, %rdi
callq 0x16f81
testl %eax, %eax
je 0x3ce4f
movq 0x28(%rsp), %r15
jmp 0x3ce72
xorl %r13d, %r13d
jmp 0x3d006
movq %r13, %rdi
callq 0xe4f2
testl %eax, %eax
je 0x3ce40
leaq 0x3badc(%rip), %rsi     # 0x7893e
movq %r13, %rdi
callq 0x172e9
movq %rax, %r15
movq %rax, 0x28(%rsp)
testq %r15, %r15
je 0x3cfe2
leaq 0x3d6a4(%rip), %rsi     # 0x7a526
movq %r15, %rdi
xorl %eax, %eax
callq 0xe19b
testq %rax, %rax
je 0x3cf26
movq %rax, %rbp
movq %rbx, 0x18(%rsp)
movq %r12, 0x10(%rsp)
leaq 0xa0(%rsp), %r13
leaq 0x40(%rsp), %r15
movl $0x10000, %r14d         # imm = 0x10000
movq 0x8(%rbp), %rsi
movq 0x10(%rbp), %r12
subq %rsi, %r12
cmpq %r14, %r12
movl $0x10000, %ebx          # imm = 0x10000
cmovbq %r12, %rbx
movq %r13, %rdi
movq %rbx, %rdx
callq 0x43c0
addq %rbx, 0x8(%rbp)
movl $0x10000, %esi          # imm = 0x10000
cmpq $0xffff, %r12           # imm = 0xFFFF
ja 0x3cf0d
testb $0x2, 0x30(%rbp)
je 0x3cf05
movl $0x10000, %edx          # imm = 0x10000
movq %rbp, %rdi
movq %r13, %rsi
movq %rbx, %rcx
callq 0xd55e
movq %rax, %r12
movq %r12, %rsi
testq %r12, %r12
jle 0x3cf3f
movq %r13, %rdi
movq %r15, %rdx
callq 0x3d449
testl %eax, %eax
jns 0x3ceb5
movq 0x10(%rsp), %r12
jmp 0x3cffd
leaq 0x3f086(%rip), %rsi     # 0x7bfb3
leaq 0x3f08f(%rip), %rdx     # 0x7bfc3
pushq $0x1
popq %rdi
movq %r15, %rcx
jmp 0x3cff6
setne %al
movq 0x40(%rsp), %r13
cmpq $0x2, %r13
setb %cl
orb %al, %cl
movq 0x10(%rsp), %r12
jne 0x3cffd
movq %rbp, %rdi
callq 0xd8ee
testl %eax, %eax
pushq $-0x1
popq %r15
js 0x3d001
movq 0x50(%rsp), %rbx
cmpb $0x1f, (%rbx)
jne 0x3d03f
cmpb $-0x75, 0x1(%rbx)
movq 0x18(%rsp), %r14
movq %r13, %rsi
jne 0x3d047
leaq 0x60(%rsp), %rdx
andq $0x0, (%rdx)
movq %rbx, %rdi
callq 0x3f23a
testq %rax, %rax
je 0x3cffd
movq %rax, %r15
movq %rbx, %rdi
callq 0x45c0
leaq 0x40(%rsp), %rdx
movq %r15, 0x10(%rdx)
movq 0x60(%rsp), %rax
movq %rax, (%rdx)
movq %rax, 0x8(%rdx)
leaq 0x3ad8a(%rip), %rdi     # 0x77d56
xorl %esi, %esi
callq 0x3d449
testl %eax, %eax
js 0x3cffd
movq 0x40(%rsp), %rsi
pushq $-0x1
popq %r15
jmp 0x3d047
leaq 0x3efca(%rip), %rsi     # 0x7bfb3
leaq 0x3baf1(%rip), %rdx     # 0x78ae1
pushq $0x1
popq %rdi
movq %r13, %rcx
xorl %eax, %eax
callq 0x110c6
pushq $-0x1
popq %r15
movq 0x8(%rsp), %r13
movq 0x50(%rsp), %rdi
callq 0x45c0
movq %r13, %rdi
callq 0x45c0
movq 0x28(%rsp), %rdi
callq 0x45c0
movq %r12, %rdi
callq 0x3d574
movl %r15d, %eax
addq $0x100a8, %rsp          # imm = 0x100A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %r14
movq %r13, %rsi
leaq 0x70(%rsp), %rdx
leaq 0x74(%rsp), %r13
pushq $0x1
popq %rax
movl %eax, 0x34(%rsp)
xorl %ebx, %ebx
leaq 0x20(%rsp), %rax
movq (%rax), %rax
cmpq %rsi, %rax
jae 0x3d3c6
movq %rsi, 0x38(%rsp)
movq %r14, 0x18(%rsp)
leaq 0x40(%rsp), %r14
movq %r14, %rdi
leaq 0x20(%rsp), %rbp
movq %rbp, %rsi
callq 0x3d4e9
cmpl $-0x1, %eax
je 0x3d001
movq %r14, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x3d4e9
cmpl $-0x1, %eax
je 0x3d001
movq %r14, %rdi
movq %rbp, %rsi
leaq 0x78(%rsp), %rdx
callq 0x3d4e9
cmpl $-0x1, %eax
je 0x3d001
movq 0x20(%rsp), %rsi
movq 0x50(%rsp), %rdi
leaq (%rdi,%rsi), %rax
movq 0x38(%rsp), %r9
cmpq %r9, %rsi
jae 0x3d001
movzbl (%rdi,%rsi), %ecx
cmpl $0x20, %ecx
je 0x3d0ed
cmpl $0x9, %ecx
jne 0x3d0f5
incq %rsi
incq %rax
jmp 0x3d0d6
xorl %r8d, %r8d
cmpb $0x2d, %cl
sete %r8b
leaq (%r8,%rsi), %rdx
cmpq %r9, %rdx
jae 0x3d001
addq %r8, %rdi
movb (%rsi,%rdi), %sil
addb $-0x30, %sil
cmpb $0x9, %sil
ja 0x3d001
addq %r8, %rax
xorl %esi, %esi
cmpq %r9, %rdx
jae 0x3d14b
movb (%rax), %dil
addb $-0x30, %dil
cmpb $0x9, %dil
ja 0x3d14e
incq %rdx
movzbl %dil, %edi
imulq $0xa, %rsi, %rsi
addq %rdi, %rsi
incq %rax
jmp 0x3d126
movq %r9, %rdx
movq %rsi, %rax
negq %rax
cmpb $0x2d, %cl
movq %rdx, 0x20(%rsp)
cmovneq %rsi, %rax
movq %rax, 0x88(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
leaq 0x80(%rsp), %rdx
callq 0x3d4e9
cmpl $-0x1, %eax
je 0x3d001
movq %r14, %rdi
movq %rbp, %rsi
leaq 0x84(%rsp), %rdx
callq 0x3d4e9
cmpl $-0x1, %eax
je 0x3d001
movl 0x74(%rsp), %r12d
movl 0x78(%rsp), %eax
leal (%r12,%rax), %r15d
decl %r15d
movl %r15d, 0x78(%rsp)
movslq 0x70(%rsp), %rbp
cmpq $-0x2, %rbp
movq 0x18(%rsp), %rdx
movq 0x38(%rsp), %rsi
jle 0x3d42a
cmpl 0x10(%rdx), %ebp
je 0x3d271
movq 0x10(%rsp), %rax
movslq 0x8870(%rax), %r13
leal 0x2(%rbp), %r14d
movq 0x8878(%rax), %rbx
cmpl %r14d, %r13d
jge 0x3d244
movl %r14d, %eax
imulq $0x38, %rax, %rsi
movq %rbx, %rdi
movq %rsi, 0x18(%rsp)
callq 0x4840
testq %rax, %rax
je 0x3cf1c
movq %rax, %rbx
imulq $0x38, %r13, %rax
movq 0x10(%rsp), %rcx
movq %rbx, 0x8878(%rcx)
movl %r14d, 0x8870(%rcx)
movq %rbx, %rdi
addq %rax, %rdi
movq 0x18(%rsp), %rdx
subq %rax, %rdx
xorl %esi, %esi
callq 0x4260
movq 0x38(%rsp), %rsi
imulq $0x38, %rbp, %rax
leaq (%rbx,%rax), %rdx
addq $0x38, %rdx
movl %ebp, 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 %ebx, %ebx
movslq %ebx, %rax
leal 0x1(%rax), %ebx
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
addq $-0x8, %rax
movq %rdx, %rbp
movl 0x14(%rbp), %ecx
cmpl %ecx, %r12d
jl 0x3d29f
cmpl 0x18(%rbp), %r15d
jg 0x3d29f
testl %ecx, %ecx
jne 0x3d2aa
cmpl $-0x1, 0x10(%rbp)
jne 0x3d2aa
movq (%rax), %rbp
decl %ebx
addq $-0x8, %rax
jmp 0x3d287
movl (%rbp), %ecx
movl 0x4(%rbp), %eax
incl %ecx
cmpl %eax, %ecx
movq 0x10(%rsp), %r12
pushq $-0x1
popq %r15
jl 0x3d2ef
leal (%rax,%rax), %ecx
testl %eax, %eax
pushq $0x10
popq %rax
cmovel %eax, %ecx
movl %ecx, 0x4(%rbp)
movq 0x8(%rbp), %rdi
movslq %ecx, %rax
imulq $0x38, %rax, %rsi
callq 0x4840
testq %rax, %rax
je 0x3d001
movq %rax, 0x8(%rbp)
movq 0x38(%rsp), %rsi
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq 0x8(%rbp), %rdx
movslq (%rbp), %rax
leal 0x1(%rax), %ecx
movl %ecx, (%rbp)
imulq $0x38, %rax, %r14
movq 0x90(%rsp), %rax
movq %rax, 0x30(%rdx,%r14)
movaps 0x60(%rsp), %xmm0
movaps 0x70(%rsp), %xmm1
movaps 0x80(%rsp), %xmm2
movups %xmm2, 0x20(%rdx,%r14)
movups %xmm1, 0x10(%rdx,%r14)
movq %rdx, %rbp
movups %xmm0, (%rdx,%r14)
movl 0x34(%rsp), %eax
cmpl %eax, %ebx
jge 0x3d34b
movq 0x8(%rsp), %rcx
jmp 0x3d376
addl %eax, %eax
movl %eax, 0x34(%rsp)
movslq %eax, %rsi
shlq $0x3, %rsi
movq 0x8(%rsp), %r13
movq %r13, %rdi
callq 0x4840
testq %rax, %rax
je 0x3d006
movq 0x40(%rsp), %rsi
movq %rax, %rcx
addq %r14, %rbp
movq %rbp, %r14
movslq %ebx, %rax
movq %rcx, 0x8(%rsp)
movq %rbp, (%rcx,%rax,8)
movq 0x20(%rsp), %rcx
movq 0x50(%rsp), %rdi
cmpq %rsi, %rcx
movq %rsi, %rax
cmovaq %rcx, %rax
leaq 0x70(%rsp), %rdx
leaq 0x74(%rsp), %r13
cmpq %rsi, %rcx
jae 0x3d3b9
cmpb $0xa, (%rdi,%rcx)
je 0x3d3b6
incq %rcx
jmp 0x3d3a6
movq %rcx, %rax
incq %rax
movq %rax, 0x20(%rsp)
jmp 0x3d062
movq 0x8(%rsp), %rdi
callq 0x45c0
movq 0x50(%rsp), %rdi
callq 0x45c0
movq 0x28(%rsp), %rdi
callq 0x45c0
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %eax, %eax
movslq 0x8870(%r12), %rcx
cmpq %rcx, %r14
jge 0x3d414
movq 0x8878(%r12), %rdi
addq %rbx, %rdi
movq %rax, %rsi
callq 0x3e380
incq %r14
addq $0x38, %rbx
jmp 0x3d3eb
xorl %r15d, %r15d
testq %rax, %rax
je 0x3d02a
andq $0x0, 0x30(%rax)
jmp 0x3d02a
leaq 0x3eb82(%rip), %rsi     # 0x7bfb3
leaq 0x3ebaa(%rip), %rdx     # 0x7bfe2
pushq $0x1
popq %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x110c6
jmp 0x3cf1c