// File: /workspace/github_c_makefile_star2_O1/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 $0x10098, %rsp          # imm = 0x10098
xorl %r12d, %r12d
movq %r12, 0x30(%rsp)
movq %r12, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq %r12, 0x28(%rsp)
cmpq %r12, 0x8878(%rdi)
jne 0x4c35e
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, %r12d      # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x4c35e
movq %rax, %r14
movabsq $-0x7fffffff00000001, %rax # imm = 0x80000000FFFFFFFF
movq %rax, 0x10(%r14)
movl $0x7fffffff, 0x18(%r14) # imm = 0x7FFFFFFF
movl $0x1, %edi
movl $0x8, %esi
callq 0x4300
testq %rax, %rax
je 0x4bea0
movq %rax, %rbp
movq %r14, 0x40(%rsp)
movq %r14, (%rax)
leaq 0x4d129(%rip), %rsi     # 0x98e40
movq %r15, %rdi
callq 0x40e0
leaq 0x7(%rax), %r14
testq %rax, %rax
cmoveq %rbx, %r14
testq %rbx, %rbx
cmovneq %rbx, %r14
testq %r14, %r14
jne 0x4bd7a
leaq 0x30(%rsp), %rdx
movq %r15, %rdi
movl $0x3, %esi
callq 0x1b3b7
testl %eax, %eax
jne 0x4bd6c
movq %r15, %rdi
callq 0x103cd
testl %eax, %eax
je 0x4bd6c
leaq 0x4db4e(%rip), %rsi     # 0x998ad
movq %r15, %rdi
callq 0x1b717
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %r14
testq %r14, %r14
je 0x4c2f6
leaq 0x4f843(%rip), %rsi     # 0x9b5c4
movq %r14, %rdi
xorl %eax, %eax
callq 0x10014
movq %rax, 0x48(%rsp)
testq %rax, %rax
je 0x4bea7
movq %rbp, 0x38(%rsp)
movq %r13, 0x8(%rsp)
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rbp
movq 0x20(%rsp), %r14
leaq 0x90(%rsp), %rdi
movq 0x48(%rsp), %r12
movq 0x8(%r12), %rsi
movq 0x10(%r12), %r15
subq %rsi, %r15
movl $0x10000, %eax          # imm = 0x10000
cmpq %rax, %r15
movl $0x10000, %ebx          # imm = 0x10000
cmovbq %r15, %rbx
movq %rbx, %rdx
callq 0x43c0
addq %rbx, 0x8(%r12)
cmpq $0xffff, %r15           # imm = 0xFFFF
ja 0x4be16
testb $0x2, 0x30(%r12)
je 0x4be16
movl $0x10000, %edx          # imm = 0x10000
movq %r12, %rdi
leaq 0x90(%rsp), %rsi
movq %rbx, %rcx
callq 0xf2cd
movq %rax, %rbx
testq %rbx, %rbx
jle 0x4bec2
leaq (%rbx,%r13), %rax
addq $0x2, %rax
cmpq %r13, %rax
jbe 0x4c2da
cmpq %rax, %rbp
jae 0x4be46
movq %rax, %rcx
shrq $0x3e, %rcx
jne 0x4be4e
movq %rax, %r15
shrq %r15
jmp 0x4be51
movq %r14, %r12
movq %rbp, %r15
jmp 0x4be6b
xorl %r15d, %r15d
addq %rax, %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x4840
movq %rax, %r12
testq %rax, %rax
je 0x4c315
leaq (%r12,%r13), %rdi
leaq 0x90(%rsp), %r14
movq %r14, %rsi
movq %rbx, %rdx
callq 0x43c0
movq %r14, %rdi
addq %rbx, %r13
movb $0x0, (%r12,%r13)
movq %r12, %r14
movq %r15, %rbp
testl %ebx, %ebx
jns 0x4bdba
jmp 0x4c31b
xorl %ebp, %ebp
jmp 0x4c33a
leaq 0x5112d(%rip), %rsi     # 0x9cfdb
leaq 0x51136(%rip), %rdx     # 0x9cfeb
movl $0x1, %edi
movq %r14, %rcx
jmp 0x4c30c
movq %rbp, 0x18(%rsp)
movq %r14, 0x20(%rsp)
movq %r13, 0x10(%rsp)
setne %al
cmpq $0x2, %r13
setb %cl
orb %al, %cl
jne 0x4c32a
movq %r12, %rdi
callq 0xf6d0
testl %eax, %eax
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq 0x38(%rsp), %rbp
js 0x4c373
movq 0x20(%rsp), %rbx
cmpb $0x1f, (%rbx)
jne 0x4bf64
cmpb $-0x75, 0x1(%rbx)
jne 0x4bf64
leaq 0x50(%rsp), %rdx
movq $0x0, (%rdx)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x4ec27
testq %rax, %rax
je 0x4c373
movq %rax, %r14
movq %rbx, %rdi
callq 0x45c0
leaq 0x10(%rsp), %rdx
movq %r14, 0x10(%rdx)
movq 0x50(%rsp), %rax
movq %rax, (%rdx)
movq %rax, 0x8(%rdx)
leaq 0x4cd78(%rip), %rdi     # 0x98ccd
xorl %esi, %esi
callq 0x4c3d2
testl %eax, %eax
js 0x4c373
movq 0x10(%rsp), %rbx
cmpq %rbx, 0x28(%rsp)
jae 0x4c37a
xorl %r14d, %r14d
movl $0x1, 0x48(%rsp)
leaq 0x10(%rsp), %rdi
movq 0x8(%rsp), %r13
leaq 0x28(%rsp), %r15
movq %r15, %rsi
leaq 0x60(%rsp), %rdx
callq 0x4c46f
cmpl $-0x1, %eax
je 0x4c33a
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
leaq 0x64(%rsp), %rdx
callq 0x4c46f
cmpl $-0x1, %eax
je 0x4c33a
movq %r15, %rsi
leaq 0x10(%rsp), %rdi
movq %rdi, %r15
leaq 0x68(%rsp), %rdx
callq 0x4c46f
cmpl $-0x1, %eax
je 0x4c33a
movq 0x28(%rsp), %rcx
movl $0x1, %eax
cmpq %rbx, %rcx
jae 0x4c04d
movq 0x20(%rsp), %rsi
movq %rcx, %rdx
negq %rdx
incq %rcx
movb $0x1, %dil
movzbl -0x1(%rsi,%rcx), %r8d
cmpl $0x20, %r8d
je 0x4c00f
cmpl $0x9, %r8d
jne 0x4c029
cmpq %rbx, %rcx
setb %dil
decq %rdx
incq %rcx
movq %rbx, %r8
addq %rdx, %r8
jne 0x4bffd
movq %rbx, %rcx
jmp 0x4c04d
testb $0x1, %dil
je 0x4c047
xorl %ecx, %ecx
cmpb $0x2d, %r8b
sete %cl
movq %rcx, %rax
negq %rax
orq $0x1, %rax
subq %rdx, %rcx
jmp 0x4c04d
negq %rdx
movq %rdx, %rcx
cmpq %rbx, %rcx
jae 0x4c33a
movq 0x20(%rsp), %rsi
movb (%rsi,%rcx), %dl
addb $-0x30, %dl
cmpb $0x9, %dl
ja 0x4c33a
xorl %edx, %edx
movb (%rsi,%rcx), %dil
addb $-0x30, %dil
cmpb $0x9, %dil
ja 0x4c091
incq %rcx
movzbl %dil, %edi
leaq (%rdx,%rdx,4), %rdx
leaq (%rdi,%rdx,2), %rdx
cmpq %rcx, %rbx
jne 0x4c06c
movq %rbx, %rcx
imulq %rax, %rdx
movq %rcx, 0x28(%rsp)
movq %rdx, 0x78(%rsp)
movq %r15, %rdi
leaq 0x28(%rsp), %rbx
movq %rbx, %rsi
leaq 0x70(%rsp), %rdx
callq 0x4c46f
cmpl $-0x1, %eax
je 0x4c33a
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x74(%rsp), %rdx
callq 0x4c46f
cmpl $-0x1, %eax
je 0x4c33a
movq %rbp, %r15
movl 0x64(%rsp), %r12d
movl 0x68(%rsp), %eax
leal (%r12,%rax), %ebp
decl %ebp
movl %ebp, 0x68(%rsp)
movslq 0x60(%rsp), %rbx
cmpq $-0x2, %rbx
jle 0x4c3a8
movq 0x40(%rsp), %rcx
cmpl 0x10(%rcx), %ebx
je 0x4c198
movslq 0x8870(%r13), %rax
leal 0x2(%rbx), %ecx
cmpl %ecx, %eax
jge 0x4c169
movq %rax, 0x40(%rsp)
movq 0x8878(%r13), %rdi
movl %ecx, 0x8(%rsp)
movl %ecx, %eax
imulq $0x38, %rax, %r14
movq %r14, %rsi
callq 0x4840
testq %rax, %rax
je 0x4c3c4
imulq $0x38, 0x40(%rsp), %rcx
movq %rax, 0x8878(%r13)
movl 0x8(%rsp), %edx
movl %edx, 0x8870(%r13)
addq %rcx, %rax
subq %rcx, %r14
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x4260
movq 0x8878(%r13), %rax
imulq $0x38, %rbx, %rcx
addq %rax, %rcx
addq $0x38, %rcx
movl %ebx, 0x10(%rcx)
movabsq $0x7fffffff80000000, %rax # imm = 0x7FFFFFFF80000000
movq %rax, 0x14(%rcx)
xorps %xmm0, %xmm0
movups %xmm0, (%rcx)
movq %rcx, (%r15)
xorl %r14d, %r14d
movslq %r14d, %rax
leal 0x1(%rax), %r14d
leaq (%r15,%rax,8), %rax
addq $-0x8, %rax
movq %rcx, %rbx
movl 0x14(%rbx), %ecx
cmpl %ecx, %r12d
jl 0x4c1c1
cmpl 0x18(%rbx), %ebp
jg 0x4c1c1
testl %ecx, %ecx
jne 0x4c1cd
cmpl $-0x1, 0x10(%rbx)
jne 0x4c1cd
movq (%rax), %rbx
decl %r14d
addq $-0x8, %rax
jmp 0x4c1aa
movl (%rbx), %ecx
movl 0x4(%rbx), %eax
incl %ecx
cmpl %eax, %ecx
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq %r15, %rbp
jl 0x4c20e
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 0x4c33a
movq %rax, 0x8(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq 0x8(%rbx), %rdx
movslq (%rbx), %rax
leal 0x1(%rax), %ecx
movl %ecx, (%rbx)
imulq $0x38, %rax, %rbx
movq 0x80(%rsp), %rax
movq %rax, 0x30(%rdx,%rbx)
movaps 0x50(%rsp), %xmm0
movaps 0x60(%rsp), %xmm1
movaps 0x70(%rsp), %xmm2
movups %xmm2, 0x20(%rdx,%rbx)
movups %xmm1, 0x10(%rdx,%rbx)
movq %rdx, %r15
movups %xmm0, (%rdx,%rbx)
movl 0x48(%rsp), %eax
cmpl %eax, %r14d
jl 0x4c27e
addl %eax, %eax
movl %eax, 0x48(%rsp)
movslq %eax, %rsi
shlq $0x3, %rsi
movq %rbp, %rdi
callq 0x4840
testq %rax, %rax
cmovneq %rax, %rbp
je 0x4c33a
addq %rbx, %r15
movslq %r14d, %rax
movq %r15, 0x40(%rsp)
movq %r15, (%rbp,%rax,8)
movq 0x10(%rsp), %rcx
movq 0x28(%rsp), %rax
cmpq %rcx, %rax
jae 0x4c2ba
movq 0x20(%rsp), %rdx
leaq 0x10(%rsp), %rdi
cmpb $0xa, (%rdx,%rax)
je 0x4c2bf
incq %rax
cmpq %rax, %rcx
jne 0x4c2a7
movq %rcx, %rax
jmp 0x4c2bf
leaq 0x10(%rsp), %rdi
incq %rax
movq %rax, 0x28(%rsp)
movq 0x10(%rsp), %rbx
cmpq %rbx, %rax
jb 0x4bf89
jmp 0x4c37f
movq %rbp, 0x18(%rsp)
movq %r14, 0x20(%rsp)
movq %r13, 0x10(%rsp)
callq 0x4050
movl $0x4b, (%rax)
jmp 0x4c32a
leaq 0x50cde(%rip), %rsi     # 0x9cfdb
leaq 0x4d74c(%rip), %rdx     # 0x99a50
movl $0x1, %edi
movq %r15, %rcx
xorl %eax, %eax
callq 0x14378
jmp 0x4c33a
movq %r14, %r12
movq %rbp, %r15
movq %r15, 0x18(%rsp)
movq %r12, 0x20(%rsp)
movq %r13, 0x10(%rsp)
movq 0x8(%rsp), %r13
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq 0x38(%rsp), %rbp
movq 0x20(%rsp), %rdi
callq 0x45c0
movq %rbp, %rdi
callq 0x45c0
movq 0x30(%rsp), %rdi
callq 0x45c0
movq %r13, %rdi
callq 0x4c574
movl %r12d, %eax
addq $0x10098, %rsp          # imm = 0x10098
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %r13
jmp 0x4c33a
movq 0x8(%rsp), %r13
movq %rbp, %rdi
callq 0x45c0
movq 0x20(%rsp), %rdi
callq 0x45c0
movq 0x30(%rsp), %rdi
callq 0x45c0
movq %r13, %rdi
callq 0x4c51f
xorl %r12d, %r12d
jmp 0x4c35e
leaq 0x50c2c(%rip), %rsi     # 0x9cfdb
leaq 0x50c54(%rip), %rdx     # 0x9d00a
movl $0x1, %edi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x14378
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq %r15, %rbp
jmp 0x4c33a