// File: /workspace/github_c_makefile_star2_O1/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 $0x10088, %rsp          # imm = 0x10088
xorl %eax, %eax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq %rax, 0x28(%rsp)
cmpq %rax, 0x8838(%rdi)
jne 0x537f9
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r12
movl $0x1, 0x8834(%rdi)
movl $0x1, %edi
movl $0x30, %esi
callq 0x6470
movq %rax, %r13
movq %rax, 0x8838(%r12)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testq %r13, %r13
je 0x537f9
movabsq $-0x7fffffff00000001, %rax # imm = 0x80000000FFFFFFFF
movq %rax, 0x10(%r13)
movl $0x7fffffff, 0x18(%r13) # imm = 0x7FFFFFFF
movl $0x1, %edi
movl $0x8, %esi
callq 0x6470
movq %rax, %r15
movq %r13, (%rax)
testq %r14, %r14
je 0x53251
xorl %eax, %eax
jmp 0x5326c
leaq 0x45623(%rip), %rsi     # 0x9887b
movq %rbx, %rdi
callq 0x51a60
movq %rax, %r14
testq %rax, %rax
je 0x53440
movq %r15, 0x30(%rsp)
movq %rax, 0x8(%rsp)
leaq 0x42bac(%rip), %rsi     # 0x95e29
movq %r14, %rdi
callq 0x6b707
testq %rax, %rax
je 0x53448
movq %rax, %rbp
movq %r13, 0x38(%rsp)
movq %r12, 0x78(%rsp)
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r13
movq 0x20(%rsp), %r15
movq 0x8(%rbp), %rsi
movq 0x10(%rbp), %r12
subq %rsi, %r12
movl $0x10000, %eax          # imm = 0x10000
cmpq %rax, %r12
movl $0x10000, %ebx          # imm = 0x10000
cmovbq %r12, %rbx
leaq 0x80(%rsp), %rdi
movq %rbx, %rdx
callq 0x6570
addq %rbx, 0x8(%rbp)
movl $0x10000, %eax          # imm = 0x10000
cmpq $0xffff, %r12           # imm = 0xFFFF
ja 0x53300
movl $0x10000, %edx          # imm = 0x10000
movq %rbp, %rdi
leaq 0x80(%rsp), %rsi
movq %rbx, %rcx
callq 0x6b053
testq %rax, %rax
jle 0x53380
movslq %eax, %rbx
leaq (%r14,%rbx), %rax
incq %rax
cmpq %r13, %rax
jb 0x5335f
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 %r15, %rdi
movq %r13, %rsi
callq 0x6c80
testq %rax, %rax
je 0x532aa
movq %rax, %r15
leaq (%r15,%r14), %rdi
leaq 0x80(%rsp), %rsi
movq %rbx, %rdx
callq 0x6570
addq %rbx, %r14
movb $0x0, (%r15,%r14)
jmp 0x532aa
movq %r13, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movq %r14, 0x10(%rsp)
setne %al
cmpq $0x2, %r14
setb %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x533b9
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x533af
callq 0x68a0
movq 0x30(%rsp), %rdi
jmp 0x537e5
movq %rbp, %rdi
callq 0x6b37e
movq 0x20(%rsp), %r15
testl %eax, %eax
je 0x533e4
testq %r15, %r15
movq 0x30(%rsp), %rbx
je 0x533dc
movq %r15, %rdi
callq 0x68a0
movq %rbx, %rdi
jmp 0x537e5
cmpb $0x1f, (%r15)
movq 0x30(%rsp), %rbp
movq 0x38(%rsp), %r12
jne 0x53470
cmpb $-0x75, 0x1(%r15)
jne 0x53470
leaq 0x40(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x55d0e
movq %rax, %rbx
movq %r15, %rdi
callq 0x68a0
testq %rbx, %rbx
je 0x53455
leaq 0x10(%rsp), %rdx
movq %rbx, 0x10(%rdx)
movq 0x40(%rsp), %rax
movq %rax, (%rdx)
movq %rax, 0x8(%rdx)
leaq 0x44f29(%rip), %rdi     # 0x98360
xorl %esi, %esi
callq 0x53824
jmp 0x53467
movq %r15, %rdi
jmp 0x537ef
movq %r14, %rdi
callq 0x6590
jmp 0x533af
movq %rbp, %rdi
callq 0x68a0
movq 0x8(%rsp), %rdi
callq 0x68a0
testq %rbx, %rbx
je 0x537f4
xorl %ebx, %ebx
movl $0x1, %r13d
leaq 0x10(%rsp), %r15
leaq 0x28(%rsp), %r14
movq %r15, %rdi
movq %r14, %rsi
leaq 0x50(%rsp), %rdx
callq 0x538b2
cmpl $-0x1, %eax
je 0x537d8
movq %r15, %rdi
movq %r14, %rsi
leaq 0x54(%rsp), %rdx
callq 0x538b2
cmpl $-0x1, %eax
je 0x537d8
movq %r15, %rdi
movq %r14, %rsi
leaq 0x58(%rsp), %rdx
callq 0x538b2
cmpl $-0x1, %eax
je 0x537d8
movq 0x28(%rsp), %rdx
movq 0x10(%rsp), %rax
movl $0x1, %ecx
cmpq %rax, %rdx
jae 0x53542
movq 0x20(%rsp), %rdi
movq %rdx, %rsi
negq %rsi
incq %rdx
movb $0x1, %r8b
movzbl -0x1(%rdi,%rdx), %r9d
cmpl $0x20, %r9d
je 0x53504
cmpl $0x9, %r9d
jne 0x5351e
cmpq %rax, %rdx
setb %r8b
decq %rsi
incq %rdx
movq %rax, %r9
addq %rsi, %r9
jne 0x534f2
movq %rax, %rdx
jmp 0x53542
testb $0x1, %r8b
je 0x5353c
xorl %edx, %edx
cmpb $0x2d, %r9b
sete %dl
movq %rdx, %rcx
negq %rcx
orq $0x1, %rcx
subq %rsi, %rdx
jmp 0x53542
negq %rsi
movq %rsi, %rdx
cmpq %rax, %rdx
jae 0x537d8
movq 0x20(%rsp), %rdi
movb (%rdi,%rdx), %sil
addb $-0x30, %sil
cmpb $0x9, %sil
ja 0x537d8
xorl %esi, %esi
movzbl (%rdi,%rdx), %r8d
leal -0x30(%r8), %r9d
cmpb $0x9, %r9b
ja 0x5358a
leaq (%rsi,%rsi,4), %rsi
incq %rdx
leaq (%r8,%rsi,2), %rsi
addq $-0x30, %rsi
cmpq %rdx, %rax
jne 0x53564
movq %rax, %rdx
imulq %rcx, %rsi
movq %rdx, 0x28(%rsp)
movq %rsi, 0x68(%rsp)
movq %r15, %rdi
leaq 0x28(%rsp), %r14
movq %r14, %rsi
leaq 0x60(%rsp), %rdx
callq 0x538b2
cmpl $-0x1, %eax
je 0x537d8
movq %r15, %rdi
movq %r14, %rsi
leaq 0x64(%rsp), %rdx
callq 0x538b2
cmpl $-0x1, %eax
je 0x537d8
movl %r13d, 0x38(%rsp)
movq %r12, %rcx
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 0x5380b
cmpl 0x10(%rcx), %r15d
je 0x536a9
movq %rbp, 0x30(%rsp)
movq 0x78(%rsp), %r14
movslq 0x8834(%r14), %rbp
leal 0x2(%r15), %eax
cmpl %eax, %ebp
jge 0x53670
leaq (,%rbp,2), %rbx
addq %rbp, %rbx
shlq $0x4, %rbx
movl %eax, 0x8834(%r14)
movq 0x8838(%r14), %rdi
movl %eax, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rsi
callq 0x6c80
movq %rax, 0x8838(%r14)
addq %rax, %rbx
movslq 0x8834(%r14), %rax
subq %rbp, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x63b0
movq 0x8838(%r14), %rax
leaq (%r15,%r15,2), %rcx
shlq $0x4, %rcx
addq %rax, %rcx
addq $0x30, %rcx
movl %r15d, 0x10(%rcx)
movabsq $0x7fffffff80000000, %rax # imm = 0x7FFFFFFF80000000
movq %rax, 0x14(%rcx)
xorps %xmm0, %xmm0
movups %xmm0, (%rcx)
movq 0x30(%rsp), %rbp
movq %rcx, (%rbp)
xorl %ebx, %ebx
movslq %ebx, %rax
leal 0x1(%rax), %ebx
leaq -0x8(,%rax,8), %rax
addq %rbp, %rax
movq %rcx, %r15
cmpl 0x14(%r15), %r12d
jl 0x536c9
cmpl 0x18(%r15), %r13d
jle 0x536d4
movq (%rax), %r15
decl %ebx
addq $-0x8, %rax
jmp 0x536bd
movl (%r15), %ecx
movl 0x4(%r15), %eax
incl %ecx
cmpl %eax, %ecx
jl 0x5370a
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x10, %eax
cmovel %eax, %ecx
movl %ecx, 0x4(%r15)
movq 0x8(%r15), %rdi
movslq %ecx, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rsi
callq 0x6c80
movq %rax, 0x8(%r15)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq 0x8(%r15), %r12
movslq (%r15), %rax
leal 0x1(%rax), %ecx
movl %ecx, (%r15)
leaq (%rax,%rax,2), %r15
shlq $0x4, %r15
movaps 0x40(%rsp), %xmm0
movaps 0x50(%rsp), %xmm1
movaps 0x60(%rsp), %xmm2
movups %xmm2, 0x20(%r12,%r15)
movups %xmm1, 0x10(%r12,%r15)
movups %xmm0, (%r12,%r15)
movl 0x38(%rsp), %r13d
cmpl %r13d, %ebx
jl 0x53766
addl %r13d, %r13d
movslq %r13d, %rsi
shlq $0x3, %rsi
movq %rbp, %rdi
callq 0x6c80
movq %rax, %rbp
leaq 0x28(%rsp), %r14
addq %r15, %r12
movslq %ebx, %rax
movq %r12, (%rbp,%rax,8)
movq 0x10(%rsp), %rax
movq 0x28(%rsp), %rcx
cmpq %rax, %rcx
jae 0x537a2
movq 0x20(%rsp), %rdx
leaq 0x10(%rsp), %r15
cmpb $0xa, (%rdx,%rcx)
je 0x537a7
incq %rcx
cmpq %rcx, %rax
jne 0x5378f
movq %rax, %rcx
jmp 0x537a7
leaq 0x10(%rsp), %r15
incq %rcx
movq %rcx, 0x28(%rsp)
cmpq %rax, %rcx
jb 0x53482
movq %rbp, %rdi
callq 0x68a0
movq 0x20(%rsp), %rdi
callq 0x68a0
movq 0x8(%rsp), %rdi
callq 0x68a0
xorl %eax, %eax
jmp 0x537f9
movq 0x20(%rsp), %rdi
callq 0x68a0
movq %rbp, %rdi
callq 0x68a0
movq 0x8(%rsp), %rdi
callq 0x68a0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x10088, %rsp          # imm = 0x10088
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
movq 0x8(%rsp), %rdx
leaq 0x50(%rsp), %rcx
callq 0x7684
jmp 0x537f4