// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x30(%rsp)
xorps %xmm0, %xmm0
andq $0x0, 0x18(%rsp)
movaps %xmm0, 0x20(%rsp)
xorl %r12d, %r12d
cmpq %r12, 0x8838(%rdi)
je 0x4b906
movl %r12d, %eax
addq $0x10098, %rsp          # imm = 0x10098
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %r13
movl $0x1, 0x8834(%rdi)
pushq $0x1
popq %rdi
pushq $0x30
popq %rsi
callq 0x6470
movq %rax, 0x8838(%r13)
pushq $-0x1
popq %r12
testq %rax, %rax
je 0x4b8f1
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 0x6470
movq %rax, %r15
movq %rbx, 0x48(%rsp)
movq %rbx, (%rax)
testq %rbp, %rbp
je 0x4b96b
xorl %ebx, %ebx
jmp 0x4b989
leaq 0x3bf2b(%rip), %rsi     # 0x8789d
movq %r14, %rdi
callq 0x4a38c
movq %rax, %rbx
movq %rax, %rbp
testq %rax, %rax
je 0x4be30
leaq 0x39479(%rip), %rsi     # 0x84e09
movq %rbp, %rdi
callq 0x5fdb8
testq %rax, %rax
je 0x4be54
movq %rax, %r14
movq %r13, 0x88(%rsp)
movq %r15, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
leaq 0x90(%rsp), %rbp
leaq 0x20(%rsp), %r15
movl $0x10000, %ebx          # imm = 0x10000
movq 0x8(%r14), %rsi
movq 0x10(%r14), %r12
subq %rsi, %r12
cmpq %rbx, %r12
movl $0x10000, %r13d         # imm = 0x10000
cmovbq %r12, %r13
movq %rbp, %rdi
movq %r13, %rdx
callq 0x6570
addq %r13, 0x8(%r14)
movl $0x10000, %eax          # imm = 0x10000
cmpq $0xffff, %r12           # imm = 0xFFFF
ja 0x4ba15
movl $0x10000, %edx          # imm = 0x10000
movq %r14, %rdi
movq %rbp, %rsi
movq %r13, %rcx
callq 0x5f777
testq %rax, %rax
jle 0x4ba24
movq %rbp, %rdi
movl %eax, %esi
movq %r15, %rdx
callq 0x4bead
jmp 0x4b9c8
setne %al
movq 0x20(%rsp), %r15
cmpq $0x2, %r15
setb %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x4ba61
movq 0x30(%rsp), %rdi
callq 0x68a0
movq 0x10(%rsp), %rdi
callq 0x68a0
movq 0x8(%rsp), %rdi
callq 0x68a0
pushq $-0x1
popq %r12
jmp 0x4b8f1
movq %r14, %rdi
callq 0x5fa52
movq 0x30(%rsp), %r14
testl %eax, %eax
pushq $-0x1
popq %r12
movq 0x10(%rsp), %r13
je 0x4ba83
movq %r14, %rdi
jmp 0x4be38
cmpb $0x1f, (%r14)
jne 0x4bad7
cmpb $-0x75, 0x1(%r14)
jne 0x4bad7
leaq 0x50(%rsp), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x4d9b3
movq %rax, %r15
movq %r14, %rdi
callq 0x68a0
testq %r15, %r15
je 0x4be3d
leaq 0x20(%rsp), %rdx
movq %r15, 0x10(%rdx)
movq 0x50(%rsp), %rax
movq %rax, (%rdx)
movq %rax, 0x8(%rdx)
leaq 0x3b8ad(%rip), %rdi     # 0x8737d
xorl %esi, %esi
callq 0x4bead
leaq 0x60(%rsp), %rdx
leaq 0x64(%rsp), %rbp
xorl %r14d, %r14d
pushq $0x1
popq %rax
movl %eax, 0x44(%rsp)
leaq 0x20(%rsp), %r15
leaq 0x18(%rsp), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4bf41
cmpl $-0x1, %eax
je 0x4be69
movq %r15, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x4bf41
cmpl $-0x1, %eax
je 0x4be69
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x68(%rsp), %rdx
callq 0x4bf41
cmpl $-0x1, %eax
je 0x4be69
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rax
movq 0x30(%rsp), %rbp
leaq (%rdi,%rbp), %rcx
cmpq %rax, %rdi
jae 0x4be35
movzbl (%rbp,%rdi), %edx
cmpl $0x20, %edx
je 0x4bb64
cmpl $0x9, %edx
jne 0x4bb6c
incq %rdi
incq %rcx
jmp 0x4bb4c
xorl %r8d, %r8d
cmpb $0x2d, %dl
sete %r8b
leaq (%r8,%rdi), %rsi
cmpq %rax, %rsi
jae 0x4be35
leaq (%r8,%rbp), %r9
movb (%rdi,%r9), %dil
addb $-0x30, %dil
cmpb $0x9, %dil
ja 0x4be35
addq %r8, %rcx
xorl %edi, %edi
cmpq %rax, %rsi
jae 0x4bbc7
movzbl (%rcx), %r8d
leal -0x30(%r8), %r9d
cmpb $0x9, %r9b
ja 0x4bbc4
imulq $0xa, %rdi, %rdi
incq %rsi
addq %r8, %rdi
addq $-0x30, %rdi
incq %rcx
jmp 0x4bb9e
movq %rsi, %rax
movq %rdi, %rcx
negq %rcx
cmpb $0x2d, %dl
movq %rax, 0x18(%rsp)
cmovneq %rdi, %rcx
movq %rcx, 0x78(%rsp)
leaq 0x20(%rsp), %r15
movq %r15, %rdi
leaq 0x18(%rsp), %rbx
movq %rbx, %rsi
leaq 0x70(%rsp), %rdx
callq 0x4bf41
cmpl $-0x1, %eax
je 0x4be35
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x74(%rsp), %rdx
callq 0x4bf41
cmpl $-0x1, %eax
je 0x4be35
movq %r13, 0x10(%rsp)
movl 0x64(%rsp), %r13d
movl 0x68(%rsp), %eax
leal (%rax,%r13), %r15d
decl %r15d
movl %r15d, 0x68(%rsp)
movslq 0x60(%rsp), %rcx
cmpq $-0x2, %rcx
jle 0x4be70
movq 0x48(%rsp), %rbp
cmpl 0x10(%rbp), %ecx
je 0x4bcf1
movq 0x88(%rsp), %rbp
movslq 0x8834(%rbp), %rbx
leal 0x2(%rcx), %eax
movq 0x8838(%rbp), %r14
cmpl %eax, %ebx
jge 0x4bcc2
imulq $0x30, %rbx, %rdx
movq %rdx, 0x80(%rsp)
movl %eax, 0x8834(%rbp)
movl %eax, %eax
imulq $0x30, %rax, %rsi
movq %r14, %rdi
movq %rcx, 0x48(%rsp)
callq 0x6c80
movq %rax, %r14
movq %rax, 0x8838(%rbp)
movq 0x80(%rsp), %rdi
addq %rax, %rdi
movslq 0x8834(%rbp), %rax
subq %rbx, %rax
imulq $0x30, %rax, %rdx
xorl %esi, %esi
callq 0x63b0
movq 0x48(%rsp), %rcx
imulq $0x30, %rcx, %rax
leaq (%r14,%rax), %rbp
addq $0x30, %rbp
movl %ecx, 0x10(%rbp)
movabsq $0x7fffffff80000000, %rax # imm = 0x7FFFFFFF80000000
movq %rax, 0x14(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, (%rbp)
movq 0x10(%rsp), %rax
movq %rbp, (%rax)
xorl %r14d, %r14d
movslq %r14d, %rax
leal 0x1(%rax), %r14d
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
addq $-0x8, %rax
cmpl 0x14(%rbp), %r13d
jl 0x4bd11
cmpl 0x18(%rbp), %r15d
jle 0x4bd1d
movq (%rax), %rbp
decl %r14d
addq $-0x8, %rax
jmp 0x4bd05
movl (%rbp), %ecx
movl 0x4(%rbp), %eax
incl %ecx
cmpl %eax, %ecx
jl 0x4bd4b
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 $0x30, %rax, %rsi
callq 0x6c80
movq %rax, 0x8(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq 0x8(%rbp), %rdx
movslq (%rbp), %rax
leal 0x1(%rax), %ecx
movl %ecx, (%rbp)
imulq $0x30, %rax, %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, %rbp
movups %xmm0, (%rdx,%rbx)
movl 0x44(%rsp), %eax
cmpl %eax, %r14d
movq 0x10(%rsp), %r13
jl 0x4bdab
addl %eax, %eax
movl %eax, 0x44(%rsp)
movslq %eax, %rsi
shlq $0x3, %rsi
movq %r13, %rdi
callq 0x6c80
movq %rax, %r13
addq %rbx, %rbp
movslq %r14d, %rax
movq %rbp, 0x48(%rsp)
movq %rbp, (%r13,%rax,8)
movq 0x20(%rsp), %rax
movq 0x30(%rsp), %rsi
movq 0x18(%rsp), %rdi
cmpq %rax, %rdi
movq %rax, %rcx
cmovaq %rdi, %rcx
leaq 0x60(%rsp), %rdx
leaq 0x64(%rsp), %rbp
cmpq %rax, %rdi
jae 0x4bdf1
cmpb $0xa, (%rsi,%rdi)
je 0x4bdee
incq %rdi
jmp 0x4bdde
movq %rdi, %rcx
incq %rcx
movq %rcx, 0x18(%rsp)
cmpq %rax, %rcx
leaq 0x20(%rsp), %r15
leaq 0x18(%rsp), %rbx
jb 0x4baf5
movq %r13, %rdi
callq 0x68a0
movq 0x30(%rsp), %rdi
callq 0x68a0
movq 0x8(%rsp), %rdi
callq 0x68a0
xorl %r12d, %r12d
jmp 0x4b8f1
movq %r15, %rdi
jmp 0x4be4a
movq %rbp, %rdi
callq 0x68a0
movq %r13, %rdi
callq 0x68a0
movq 0x8(%rsp), %rdi
callq 0x68a0
jmp 0x4b8f1
movq %rbp, %rdi
callq 0x6590
movq %r15, %rdi
callq 0x68a0
movq %rbx, %rdi
jmp 0x4be4a
movq 0x30(%rsp), %rdi
jmp 0x4be38
movq %rbp, %rdi
callq 0x68a0
movq 0x10(%rsp), %rdi
callq 0x68a0
movq 0x8(%rsp), %rdi
callq 0x68a0
movq 0x67145(%rip), %rax     # 0xb2fd8
movq (%rax), %rdi
movl 0x60(%rsp), %edx
leaq 0x3ba02(%rip), %rsi     # 0x878a3
xorl %eax, %eax
callq 0x6a90
jmp 0x4b8f1