// File: /workspace/github_c_makefile_star2_O3/icebert[P]pblat/htslib/cram/cram_index.c
// Function: cram_index_load
int cram_index_load(cram_fd *fd, const char *fn, const char *fn_idx) {
    char *fn2 = 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));
    idx_stack[idx_stack_ptr] = idx;

    if (!fn_idx) {
	fn2 = hts_idx_getfn(fn, ".crai");
	if (!fn2) {
	    free(idx_stack);
	    return -1;
	}
	fn_idx = fn2;
    }

    if (!(fp = hopen(fn_idx, "r"))) {
	perror(fn_idx);
	free(idx_stack);
	free(fn2);
	return -1;
    }

    // Load the file into memory
    //while ((len = fread(buf, 1, 65536, fp)) > 0)
    while ((len = hread(fp, buf, 65536)) > 0)
	kputsn(buf, len, &kstr);
    if (len < 0 || kstr.l < 2) {
	if (kstr.s)
	    free(kstr.s);
	free(idx_stack);
	free(fn2);
	return -1;
    }

    if (hclose(fp)) {
	if (kstr.s)
	    free(kstr.s);
	free(idx_stack);
	free(fn2);
	return -1;
    }
	

    // Uncompress if required
    if (kstr.s[0] == 31 && (uc)kstr.s[1] == 139) {
	size_t l;
	char *s = zlib_mem_inflate(kstr.s, kstr.l, &l);
	free(kstr.s);
	if (!s) {
	    free(idx_stack);
	    free(fn2);
	    return -1;
	}
	kstr.s = s;
	kstr.l = l;
	kstr.m = l; // conservative estimate of the size allocated
	kputsn("", 0, &kstr); // ensure kstr.s is NUL-terminated
    }


    // Parse it line at a time
    do {
	/* 1.1 layout */
	if (kget_int32(&kstr, &pos, &e.refid) == -1) {
	    free(kstr.s); free(idx_stack); free(fn2); return -1;
	}
	if (kget_int32(&kstr, &pos, &e.start) == -1) {
	    free(kstr.s); free(idx_stack); free(fn2); return -1;
	}
	if (kget_int32(&kstr, &pos, &e.end) == -1) {
	    free(kstr.s); free(idx_stack); free(fn2); return -1;
	}
	if (kget_int64(&kstr, &pos, &e.offset) == -1) {
	    free(kstr.s); free(idx_stack); free(fn2); return -1;
	}
	if (kget_int32(&kstr, &pos, &e.slice) == -1) {
	    free(kstr.s); free(idx_stack); free(fn2); return -1;
	}
	if (kget_int32(&kstr, &pos, &e.len) == -1) {
	    free(kstr.s); free(idx_stack); free(fn2); return -1;
	}

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

	if (e.refid < -1) {
	    free(kstr.s);
	    free(idx_stack);
	    free(fn2);
	    fprintf(stderr, "Malformed index file, refid %d\n", e.refid);
	    return -1;
	}

	if (e.refid != idx->refid) {
	    if (fd->index_sz < e.refid+2) {
		size_t index_end = fd->index_sz * sizeof(*fd->index);
		fd->index_sz = e.refid+2;
		fd->index = realloc(fd->index,
				    fd->index_sz * sizeof(*fd->index));
		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 = idx_stack[--idx_stack_ptr];
	}

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

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

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

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

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

    // dump_index(fd);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10098, %rsp          # imm = 0x10098
xorl %r15d, %r15d
movq %r15, 0x80(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movq %r15, 0x18(%rsp)
cmpq %r15, 0x8838(%rdi)
je 0x53473
movl %r15d, %eax
addq $0x10098, %rsp          # imm = 0x10098
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r13
movl $0x1, 0x8834(%rdi)
movl $0x1, %edi
movl $0x30, %esi
callq 0x6470
movq %rax, 0x8838(%r13)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x5345e
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 0x6470
movq %r12, %rcx
movq %rax, %r12
movq %rcx, 0x30(%rsp)
movq %rcx, (%rax)
testq %rbx, %rbx
je 0x534e8
xorl %eax, %eax
jmp 0x53503
leaq 0x45480(%rip), %rsi     # 0x9896f
movq %r14, %rdi
callq 0x51d7f
movq %rax, %rbx
testq %rax, %rax
je 0x5370d
movq %rax, 0x8(%rsp)
leaq 0x429fa(%rip), %rsi     # 0x95f09
movq %rbx, %rdi
callq 0x6b721
testq %rax, %rax
je 0x53afb
movq %rax, %r14
movq %r12, 0x10(%rsp)
movq 0x8(%rax), %rsi
movq 0x10(%rax), %r12
subq %rsi, %r12
movl $0x10000, %ebx          # imm = 0x10000
cmpq %rbx, %r12
movl $0x10000, %r15d         # imm = 0x10000
cmovbq %r12, %r15
leaq 0x90(%rsp), %rdi
movq %r15, %rdx
callq 0x6570
addq %r15, 0x8(%r14)
cmpq $0xffff, %r12           # imm = 0xFFFF
ja 0x53586
leaq 0x90(%rsp), %rsi
movl $0x10000, %edx          # imm = 0x10000
movq %r14, %rdi
movq %r15, %rcx
callq 0x6b0a3
movq %rax, %rbx
testq %rax, %rax
jle 0x536ee
movq %r14, 0x20(%rsp)
movq %r13, 0x88(%rsp)
xorl %r12d, %r12d
leaq 0x90(%rsp), %r14
xorl %r13d, %r13d
xorl %ebp, %ebp
movslq %ebx, %rbx
leaq (%rbx,%rbp), %rax
incq %rax
cmpq %r13, %rax
jae 0x535b7
movq %r12, %r15
jmp 0x53602
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %r13
shrq $0x10, %r13
orq %rax, %r13
incq %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x6c80
movq %rax, %r15
testq %rax, %rax
je 0x53690
leaq (%r15,%rbp), %rdi
addq %rbx, %rbp
movq %r14, %rsi
movq %rbx, %rdx
callq 0x6570
movq %rbp, 0x38(%rsp)
movb $0x0, (%r15,%rbp)
movq %r15, 0x28(%rsp)
movq %r14, %r12
movq 0x20(%rsp), %r14
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rbp
subq %rsi, %rbp
movl $0x10000, %ebx          # imm = 0x10000
cmpq %rbx, %rbp
movl $0x10000, %r15d         # imm = 0x10000
cmovbq %rbp, %r15
movq %r12, %rdi
movq %r15, %rdx
callq 0x6570
addq %r15, 0x8(%r14)
cmpq $0xffff, %rbp           # imm = 0xFFFF
movq %r12, %r14
ja 0x5367b
movl $0x10000, %edx          # imm = 0x10000
movq 0x20(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0x6b0a3
movq %rax, %rbx
testq %rbx, %rbx
movq 0x38(%rsp), %rbp
movq 0x28(%rsp), %r12
jg 0x535a3
jmp 0x5369c
movq %r12, 0x28(%rsp)
movq %rbp, 0x38(%rsp)
jmp 0x53623
movq %r13, 0x78(%rsp)
movq %r12, 0x80(%rsp)
movq %rbp, 0x70(%rsp)
jne 0x536e1
cmpq $0x1, %rbp
jbe 0x536e1
movq 0x20(%rsp), %rdi
callq 0x6b393
testl %eax, %eax
je 0x53715
testq %r12, %r12
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
je 0x536d7
movq %r12, %rdi
callq 0x68a0
movq 0x10(%rsp), %rdi
jmp 0x53b06
testq %r12, %r12
je 0x536ee
movq %r12, %rdi
callq 0x68a0
movq 0x10(%rsp), %rdi
callq 0x68a0
movq 0x8(%rsp), %rdi
callq 0x68a0
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x5345e
movq %r12, %rdi
jmp 0x53b10
cmpb $0x1f, (%r12)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jne 0x5377c
cmpb $-0x75, 0x1(%r12)
movq 0x10(%rsp), %r14
jne 0x5377c
leaq 0x40(%rsp), %rdx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x56140
movq %rax, %rbx
movq %r12, %rdi
callq 0x68a0
testq %rbx, %rbx
je 0x53b22
leaq 0x70(%rsp), %r14
movq %rbx, 0x10(%r14)
movq 0x40(%rsp), %rax
movq %rax, (%r14)
movq %rax, 0x8(%r14)
leaq 0x44ce5(%rip), %rdi     # 0x98454
xorl %esi, %esi
movq %r14, %rdx
callq 0x53b41
movq (%r14), %rbp
leaq 0x50(%rsp), %rcx
leaq 0x54(%rsp), %r12
xorl %ebx, %ebx
movl $0x1, 0x28(%rsp)
leaq 0x18(%rsp), %r13
movq 0x80(%rsp), %r14
movq %rbp, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x53bd5
cmpl $-0x1, %eax
je 0x53b1a
movq %rbp, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x53bd5
cmpl $-0x1, %eax
je 0x53b1a
movq %rbp, %rdi
movq %r14, %rsi
movq %r13, %rdx
leaq 0x58(%rsp), %rcx
callq 0x53bd5
cmpl $-0x1, %eax
movq 0x10(%rsp), %r12
je 0x53af1
movq 0x18(%rsp), %rax
movl $0x1, %ecx
cmpq %rbp, %rax
jae 0x5382f
movzbl (%r14,%rax), %ecx
cmpl $0x20, %ecx
je 0x5380d
cmpl $0x9, %ecx
jne 0x5381a
incq %rax
cmpq %rax, %rbp
jne 0x537fe
jmp 0x53af1
xorl %edx, %edx
cmpb $0x2d, %cl
sete %dl
addq %rdx, %rax
movq %rdx, %rcx
negq %rcx
orq $0x1, %rcx
cmpq %rbp, %rax
jae 0x53af1
movb (%r14,%rax), %dl
addb $-0x30, %dl
cmpb $0x9, %dl
ja 0x53af1
xorl %edx, %edx
movzbl (%r14,%rax), %esi
leal -0x30(%rsi), %edi
cmpb $0x9, %dil
ja 0x5386f
leaq (%rdx,%rdx,4), %rdx
incq %rax
leaq (%rsi,%rdx,2), %rdx
addq $-0x30, %rdx
cmpq %rax, %rbp
jne 0x5384a
movq %rbp, %rax
imulq %rcx, %rdx
movq %rax, 0x18(%rsp)
movq %rdx, 0x68(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
movq %r13, %rdx
leaq 0x60(%rsp), %rcx
callq 0x53bd5
cmpl $-0x1, %eax
je 0x53af1
movq %rbp, %rdi
movq %r14, %rsi
movq %r13, %rdx
leaq 0x64(%rsp), %rcx
callq 0x53bd5
cmpl $-0x1, %eax
je 0x53af1
movq %rbp, 0x38(%rsp)
movq %r12, %rbp
movl 0x54(%rsp), %r12d
movl 0x58(%rsp), %eax
leal (%r12,%rax), %r13d
decl %r13d
movl %r13d, 0x58(%rsp)
movslq 0x50(%rsp), %r15
cmpq $-0x2, %r15
jle 0x53b27
movq 0x30(%rsp), %r14
cmpl 0x10(%r14), %r15d
je 0x53995
movq 0x88(%rsp), %r14
movslq 0x8834(%r14), %rcx
leal 0x2(%r15), %eax
movq 0x8838(%r14), %rbx
cmpl %eax, %ecx
jge 0x53966
leaq (%rcx,%rcx,2), %rdx
shlq $0x4, %rdx
movq %rdx, 0x20(%rsp)
movl %eax, 0x8834(%r14)
movl %eax, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rsi
movq %rbx, %rdi
movq %rcx, 0x30(%rsp)
callq 0x6c80
movq %rax, %rbx
movq %rax, 0x8838(%r14)
movq 0x20(%rsp), %rdi
addq %rax, %rdi
movslq 0x8834(%r14), %rax
subq 0x30(%rsp), %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rdx
xorl %esi, %esi
callq 0x63b0
leaq (%r15,%r15,2), %rax
shlq $0x4, %rax
leaq (%rbx,%rax), %r14
addq $0x30, %r14
movl %r15d, 0x10(%r14)
movabsq $0x7fffffff80000000, %rax # imm = 0x7FFFFFFF80000000
movq %rax, 0x14(%r14)
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movq %r14, (%rbp)
xorl %ebx, %ebx
movslq %ebx, %rax
leal 0x1(%rax), %ebx
leaq -0x8(,%rax,8), %rax
addq %rbp, %rax
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl 0x14(%r14), %r12d
jl 0x539b8
cmpl 0x18(%r14), %r13d
jle 0x539c3
movq (%rax), %r14
decl %ebx
addq $-0x8, %rax
jmp 0x539ac
movl (%r14), %ecx
movl 0x4(%r14), %eax
incl %ecx
cmpl %eax, %ecx
jl 0x539f9
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x10, %eax
cmovel %eax, %ecx
movl %ecx, 0x4(%r14)
movq 0x8(%r14), %rdi
movslq %ecx, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rsi
callq 0x6c80
movq %rax, 0x8(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq 0x8(%r14), %rdx
movslq (%r14), %rax
leal 0x1(%rax), %ecx
movl %ecx, (%r14)
leaq (%rax,%rax,2), %r14
shlq $0x4, %r14
movaps 0x40(%rsp), %xmm0
movaps 0x50(%rsp), %xmm1
movaps 0x60(%rsp), %xmm2
movups %xmm2, 0x20(%rdx,%r14)
movups %xmm1, 0x10(%rdx,%r14)
movq %rdx, %r13
movups %xmm0, (%rdx,%r14)
movl 0x28(%rsp), %eax
cmpl %eax, %ebx
movq %rbp, %rdi
jge 0x53a4b
movq 0x38(%rsp), %rbp
jmp 0x53a65
addl %eax, %eax
movl %eax, 0x28(%rsp)
movslq %eax, %rsi
shlq $0x3, %rsi
callq 0x6c80
movq %rax, %rdi
movq 0x70(%rsp), %rbp
addq %r14, %r13
movslq %ebx, %rax
movq %r13, 0x30(%rsp)
movq %rdi, 0x10(%rsp)
movq %r13, (%rdi,%rax,8)
movq 0x18(%rsp), %rax
cmpq %rbp, %rax
jae 0x53aa8
movq 0x80(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x54(%rsp), %r12
cmpb $0xa, (%rdx,%rax)
je 0x53ab2
incq %rax
cmpq %rax, %rbp
jne 0x53a95
movq %rbp, %rax
jmp 0x53ab2
leaq 0x50(%rsp), %rcx
leaq 0x54(%rsp), %r12
incq %rax
movq %rax, 0x18(%rsp)
cmpq %rbp, %rax
leaq 0x18(%rsp), %r13
jb 0x53795
movq 0x10(%rsp), %rdi
callq 0x68a0
movq 0x80(%rsp), %rdi
callq 0x68a0
movq 0x8(%rsp), %rdi
callq 0x68a0
xorl %r15d, %r15d
jmp 0x5345e
movq %r14, %rdi
callq 0x68a0
jmp 0x53b03
movq %rbx, %rdi
callq 0x6590
movq %r12, %rdi
callq 0x68a0
movq 0x8(%rsp), %rdi
callq 0x68a0
jmp 0x5345e
movq %r14, %rdi
jmp 0x536d2
movq %r14, %rdi
jmp 0x53b06
movq %r14, %rdi
movq %rbp, %rsi
movq 0x8(%rsp), %rdx
leaq 0x50(%rsp), %rcx
callq 0x764f
jmp 0x53702