// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x10110, %rsp          # imm = 0x10110
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
leaq -0x10050(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x4250
movq $0x0, -0x10068(%rbp)
movl $0x0, -0x100ac(%rbp)
movl $0x0, -0x100b0(%rbp)
movq $0x0, -0x100b8(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8878(%rax)
je 0x6a1f6
movl $0x0, -0x4(%rbp)
jmp 0x6aa91
movq -0x10(%rbp), %rax
movl $0x1, 0x8870(%rax)
movl $0x1, %edi
movl $0x38, %esi
callq 0x42f0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8878(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8878(%rax)
jne 0x6a23b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6aa91
movq -0x10(%rbp), %rax
movq 0x8878(%rax), %rax
movq %rax, -0x10060(%rbp)
movq -0x10060(%rbp), %rax
movl $0xffffffff, 0x10(%rax) # imm = 0xFFFFFFFF
movq -0x10060(%rbp), %rax
movl $0x80000000, 0x14(%rax) # imm = 0x80000000
movq -0x10060(%rbp), %rax
movl $0x7fffffff, 0x18(%rax) # imm = 0x7FFFFFFF
movl -0x100ac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x100ac(%rbp)
movslq %eax, %rdi
movl $0x8, %esi
callq 0x42f0
movq %rax, -0x10068(%rbp)
cmpq $0x0, -0x10068(%rbp)
jne 0x6a2a9
jmp 0x6aa60
movq -0x10060(%rbp), %rdx
movq -0x10068(%rbp), %rax
movslq -0x100b0(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x18(%rbp), %rdi
leaq 0x85e4b(%rip), %rsi     # 0xf0118
callq 0x40c0
movq %rax, -0x100c0(%rbp)
cmpq $0x0, -0x100c0(%rbp)
je 0x6a2f9
cmpq $0x0, -0x20(%rbp)
jne 0x6a2f9
movq -0x100c0(%rbp), %rax
addq $0x7, %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x6a36b
movq -0x18(%rbp), %rdi
movl $0x3, %esi
leaq -0x28(%rbp), %rdx
callq 0x25370
cmpl $0x0, %eax
jne 0x6a339
movq -0x18(%rbp), %rdi
callq 0x15cf0
cmpl $0x0, %eax
je 0x6a339
movq -0x18(%rbp), %rdi
leaq 0x86855(%rip), %rsi     # 0xf0b85
callq 0x25940
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x6a363
movq -0x18(%rbp), %rcx
movl $0x1, %edi
leaq 0x8a59b(%rip), %rsi     # 0xf48eb
leaq 0x869d1(%rip), %rdx     # 0xf0d28
movb $0x0, %al
callq 0x1b030
jmp 0x6aa60
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x8877b(%rip), %rsi     # 0xf2af1
movb $0x0, %al
callq 0x158b0
movq %rax, -0x10058(%rbp)
cmpq $0x0, %rax
jne 0x6a3ad
movq -0x20(%rbp), %rcx
movl $0x1, %edi
leaq 0x8a551(%rip), %rsi     # 0xf48eb
leaq 0x8a55a(%rip), %rdx     # 0xf48fb
movb $0x0, %al
callq 0x1b030
jmp 0x6aa60
jmp 0x6a3af
movq -0x10058(%rbp), %rdi
leaq -0x10030(%rbp), %rsi
movl $0x10000, %edx          # imm = 0x10000
callq 0x6aaa0
movq %rax, -0x10038(%rbp)
cmpq $0x0, %rax
jle 0x6a3fa
leaq -0x10030(%rbp), %rdi
movq -0x10038(%rbp), %rsi
leaq -0x10050(%rbp), %rdx
callq 0x6ab50
cmpl $0x0, %eax
jge 0x6a3f8
jmp 0x6aa60
jmp 0x6a3af
cmpq $0x0, -0x10038(%rbp)
jl 0x6a40e
cmpq $0x2, -0x10050(%rbp)
jae 0x6a413
jmp 0x6aa60
movq -0x10058(%rbp), %rdi
callq 0x14920
cmpl $0x0, %eax
jge 0x6a429
jmp 0x6aa60
movq -0x10040(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x1f, %eax
jne 0x6a4e6
movq -0x10040(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x8b, %eax
jne 0x6a4e6
movq $0x0, -0x100c8(%rbp)
movq -0x10040(%rbp), %rdi
movq -0x10050(%rbp), %rsi
leaq -0x100c8(%rbp), %rdx
callq 0x6eb00
movq %rax, -0x100d0(%rbp)
cmpq $0x0, -0x100d0(%rbp)
jne 0x6a48d
jmp 0x6aa60
movq -0x10040(%rbp), %rdi
callq 0x45a0
movq -0x100d0(%rbp), %rax
movq %rax, -0x10040(%rbp)
movq -0x100c8(%rbp), %rax
movq %rax, -0x10050(%rbp)
movq -0x100c8(%rbp), %rax
movq %rax, -0x10048(%rbp)
leaq 0x85aab(%rip), %rdi     # 0xeff75
xorl %eax, %eax
movl %eax, %esi
leaq -0x10050(%rbp), %rdx
callq 0x6ab50
cmpl $0x0, %eax
jge 0x6a4e4
jmp 0x6aa60
jmp 0x6a4e6
jmp 0x6a4e8
movq -0x100b8(%rbp), %rax
cmpq -0x10050(%rbp), %rax
jae 0x6aa2d
leaq -0x100a8(%rbp), %rdx
addq $0x10, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b8(%rbp), %rsi
callq 0x6ac00
cmpl $-0x1, %eax
jne 0x6a524
jmp 0x6aa60
leaq -0x100a8(%rbp), %rdx
addq $0x14, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b8(%rbp), %rsi
callq 0x6ac00
cmpl $-0x1, %eax
jne 0x6a54c
jmp 0x6aa60
leaq -0x100a8(%rbp), %rdx
addq $0x18, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b8(%rbp), %rsi
callq 0x6ac00
cmpl $-0x1, %eax
jne 0x6a574
jmp 0x6aa60
leaq -0x100a8(%rbp), %rdx
addq $0x28, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b8(%rbp), %rsi
callq 0x6adb0
cmpl $-0x1, %eax
jne 0x6a59c
jmp 0x6aa60
leaq -0x100a8(%rbp), %rdx
addq $0x20, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b8(%rbp), %rsi
callq 0x6ac00
cmpl $-0x1, %eax
jne 0x6a5c4
jmp 0x6aa60
leaq -0x100a8(%rbp), %rdx
addq $0x24, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b8(%rbp), %rsi
callq 0x6ac00
cmpl $-0x1, %eax
jne 0x6a5ec
jmp 0x6aa60
movl -0x10094(%rbp), %eax
subl $0x1, %eax
addl -0x10090(%rbp), %eax
movl %eax, -0x10090(%rbp)
cmpl $-0x1, -0x10098(%rbp)
jge 0x6a62f
movl -0x10098(%rbp), %ecx
movl $0x1, %edi
leaq 0x8a2cf(%rip), %rsi     # 0xf48eb
leaq 0x8a2f7(%rip), %rdx     # 0xf491a
movb $0x0, %al
callq 0x1b030
jmp 0x6aa60
movl -0x10098(%rbp), %eax
movq -0x10060(%rbp), %rcx
cmpl 0x10(%rcx), %eax
je 0x6a79d
movq -0x10(%rbp), %rax
movl 0x8870(%rax), %eax
movl -0x10098(%rbp), %ecx
addl $0x2, %ecx
cmpl %ecx, %eax
jge 0x6a707
movl -0x10098(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x100dc(%rbp)
movq -0x10(%rbp), %rax
movslq 0x8870(%rax), %rax
imulq $0x38, %rax, %rax
movq %rax, -0x100e8(%rbp)
movq -0x10(%rbp), %rax
movq 0x8878(%rax), %rdi
movslq -0x100dc(%rbp), %rax
imulq $0x38, %rax, %rsi
callq 0x4820
movq %rax, -0x100d8(%rbp)
cmpq $0x0, -0x100d8(%rbp)
jne 0x6a6b6
jmp 0x6aa60
movq -0x100d8(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8878(%rax)
movl -0x100dc(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8870(%rax)
movq -0x10(%rbp), %rax
movq 0x8878(%rax), %rdi
addq -0x100e8(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq 0x8870(%rax), %rax
imulq $0x38, %rax, %rdx
subq -0x100e8(%rbp), %rdx
xorl %esi, %esi
callq 0x4250
movq -0x10(%rbp), %rax
movq 0x8878(%rax), %rax
movl -0x10098(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x10060(%rbp)
movl -0x10098(%rbp), %ecx
movq -0x10060(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x10060(%rbp), %rax
movl $0x80000000, 0x14(%rax) # imm = 0x80000000
movq -0x10060(%rbp), %rax
movl $0x7fffffff, 0x18(%rax) # imm = 0x7FFFFFFF
movq -0x10060(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x10060(%rbp), %rax
movl $0x0, (%rax)
movq -0x10060(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x10060(%rbp), %rcx
movq -0x10068(%rbp), %rax
movl $0x0, -0x100b0(%rbp)
movq %rcx, (%rax)
jmp 0x6a79f
movl -0x10094(%rbp), %ecx
movq -0x10060(%rbp), %rdx
movb $0x1, %al
cmpl 0x14(%rdx), %ecx
movb %al, -0x100f9(%rbp)
jl 0x6a808
movl -0x10090(%rbp), %ecx
movq -0x10060(%rbp), %rdx
movb $0x1, %al
cmpl 0x18(%rdx), %ecx
movb %al, -0x100f9(%rbp)
jg 0x6a808
movq -0x10060(%rbp), %rcx
xorl %eax, %eax
cmpl $0x0, 0x14(%rcx)
movb %al, -0x100fa(%rbp)
jne 0x6a7fc
movq -0x10060(%rbp), %rax
cmpl $-0x1, 0x10(%rax)
sete %al
movb %al, -0x100fa(%rbp)
movb -0x100fa(%rbp), %al
movb %al, -0x100f9(%rbp)
movb -0x100f9(%rbp), %al
testb $0x1, %al
jne 0x6a814
jmp 0x6a83d
movq -0x10068(%rbp), %rax
movl -0x100b0(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x100b0(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x10060(%rbp)
jmp 0x6a79f
movq -0x10060(%rbp), %rax
movl (%rax), %eax
addl $0x1, %eax
movq -0x10060(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jl 0x6a8de
movq -0x10060(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0x6a87a
movq -0x10060(%rbp), %rax
movl 0x4(%rax), %eax
shll %eax
movl %eax, -0x10100(%rbp)
jmp 0x6a887
movl $0x10, %eax
movl %eax, -0x10100(%rbp)
jmp 0x6a887
movl -0x10100(%rbp), %ecx
movq -0x10060(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x10060(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10060(%rbp), %rax
movslq 0x4(%rax), %rax
imulq $0x38, %rax, %rsi
callq 0x4820
movq %rax, -0x100f0(%rbp)
cmpq $0x0, -0x100f0(%rbp)
jne 0x6a8cc
jmp 0x6aa60
movq -0x100f0(%rbp), %rcx
movq -0x10060(%rbp), %rax
movq %rcx, 0x8(%rax)
movl $0x0, -0x100a8(%rbp)
movl $0x0, -0x100a4(%rbp)
movq $0x0, -0x100a0(%rbp)
movq -0x10060(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10060(%rbp), %rcx
movl (%rcx), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, (%rcx)
cltq
imulq $0x38, %rax, %rax
addq %rax, %rdi
movq %rdi, -0x10070(%rbp)
leaq -0x100a8(%rbp), %rsi
movl $0x38, %edx
callq 0x43b0
movq -0x10070(%rbp), %rax
movq %rax, -0x10060(%rbp)
movl -0x100b0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x100b0(%rbp)
cmpl -0x100ac(%rbp), %eax
jl 0x6a9a7
movl -0x100ac(%rbp), %eax
shll %eax
movl %eax, -0x100ac(%rbp)
movq -0x10068(%rbp), %rdi
movslq -0x100ac(%rbp), %rsi
shlq $0x3, %rsi
callq 0x4820
movq %rax, -0x100f8(%rbp)
cmpq $0x0, -0x100f8(%rbp)
jne 0x6a999
jmp 0x6aa60
movq -0x100f8(%rbp), %rax
movq %rax, -0x10068(%rbp)
movq -0x10060(%rbp), %rdx
movq -0x10068(%rbp), %rax
movslq -0x100b0(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x100b8(%rbp), %rcx
xorl %eax, %eax
cmpq -0x10050(%rbp), %rcx
movb %al, -0x10101(%rbp)
jae 0x6a9f6
movq -0x10040(%rbp), %rax
movq -0x100b8(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
setne %al
movb %al, -0x10101(%rbp)
movb -0x10101(%rbp), %al
testb $0x1, %al
jne 0x6aa02
jmp 0x6aa16
movq -0x100b8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x100b8(%rbp)
jmp 0x6a9c0
movq -0x100b8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x100b8(%rbp)
jmp 0x6a4e8
movq -0x10068(%rbp), %rdi
callq 0x45a0
movq -0x10040(%rbp), %rdi
callq 0x45a0
movq -0x28(%rbp), %rdi
callq 0x45a0
movq -0x10(%rbp), %rdi
callq 0x6af70
movl $0x0, -0x4(%rbp)
jmp 0x6aa91
movq -0x10040(%rbp), %rdi
callq 0x45a0
movq -0x10068(%rbp), %rdi
callq 0x45a0
movq -0x28(%rbp), %rdi
callq 0x45a0
movq -0x10(%rbp), %rdi
callq 0x6aff0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x10110, %rsp          # imm = 0x10110
popq %rbp
retq
nopl (%rax)