// File: /workspace/github_cpp_makefile_star2_O1/karel-brinda[P]ococo/ext/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};
    FILE *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 = fopen(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)
	kputsn(buf, len, &kstr);
    if (len < 0 || kstr.l < 2) {
	if (kstr.s)
	    free(kstr.s);
	free(idx_stack);
	free(fn2);
	return -1;
    }

    if (fclose(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->end == 0) {
	    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 %eax, %eax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq %rax, 0x28(%rsp)
cmpq %rax, 0x8838(%rdi)
jne 0x2fa4c
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movl $0x1, 0x8834(%rdi)
movl $0x1, %edi
movl $0x30, %esi
callq 0x4360
movq %rax, %r12
movq %rax, 0x8838(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testq %r12, %r12
je 0x2fa4c
movq %rbx, 0x30(%rsp)
movabsq $-0x7fffffff00000001, %rax # imm = 0x80000000FFFFFFFF
movq %rax, 0x10(%r12)
movl $0x7fffffff, 0x18(%r12) # imm = 0x7FFFFFFF
movl $0x1, %edi
movl $0x8, %esi
callq 0x4360
movq %rax, %rbx
movq %r12, (%rax)
testq %r14, %r14
je 0x2f8a7
xorl %eax, %eax
jmp 0x2f8c2
leaq 0x24c22(%rip), %rsi     # 0x544d0
movq %r15, %rdi
callq 0x132c1
movq %rax, %r14
testq %rax, %rax
je 0x2faae
movq %rax, 0x8(%rsp)
leaq 0x2404b(%rip), %rsi     # 0x53919
movq %r14, %rdi
callq 0x4600
testq %rax, %rax
je 0x2fab3
movq %rax, %r13
movq %r12, 0x48(%rsp)
movq %rbx, 0x38(%rsp)
movq 0x10(%rsp), %rbp
leaq 0x90(%rsp), %rdi
movl $0x1, %esi
movl $0x10000, %edx          # imm = 0x10000
movq %rax, %rcx
callq 0x47d0
testq %rax, %rax
jle 0x2f9f9
movq 0x18(%rsp), %rbx
movq 0x20(%rsp), %r12
leaq 0x90(%rsp), %r15
movq %r13, 0x40(%rsp)
movslq %eax, %r13
movq %rbp, %r14
addq %r13, %r14
leaq 0x2(,%r13), %rax
addq %rbp, %rax
cmpq %rax, %rbx
jae 0x2f9a4
movq %rbx, 0x88(%rsp)
leaq 0x1(%r14), %rax
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, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
movq %rcx, %rbx
shrq $0x20, %rbx
orq %rcx, %rbx
incq %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x48d0
movq %r12, %rcx
movq %rax, %r12
testq %rax, %rax
je 0x2f9e2
addq %r12, %rbp
movq %rbp, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x4420
movb $0x0, (%r12,%r14)
movq %r14, %rbp
movl $0x1, %esi
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
movq 0x40(%rsp), %r13
movq %r13, %rcx
callq 0x47d0
testq %rax, %rax
jg 0x2f92b
jmp 0x2f9ef
movq %rcx, %r12
movq 0x88(%rsp), %rbx
jmp 0x2f9bd
movq %rbx, 0x18(%rsp)
movq %r12, 0x20(%rsp)
movq %rbp, 0x10(%rsp)
testq %rax, %rax
jne 0x2fa24
cmpq $0x1, %rbp
jbe 0x2fa24
movq %r13, %rdi
callq 0x4530
movq 0x20(%rsp), %rbx
testl %eax, %eax
je 0x2fa5e
testq %rbx, %rbx
je 0x2fa33
movq %rbx, %rdi
jmp 0x2fa2e
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2fa33
callq 0x4610
movq 0x38(%rsp), %rdi
callq 0x4610
movq 0x8(%rsp), %rdi
callq 0x4610
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x10098, %rsp          # imm = 0x10098
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x1f, (%rbx)
jne 0x2fae0
cmpb $-0x75, 0x1(%rbx)
jne 0x2fae0
leaq 0x50(%rsp), %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x323f6
movq %rax, %r14
movq %rbx, %rdi
callq 0x4610
testq %r14, %r14
je 0x2fac3
leaq 0x10(%rsp), %rdx
movq %r14, 0x10(%rdx)
movq 0x50(%rsp), %rax
movq %rax, (%rdx)
movq %rax, 0x8(%rdx)
leaq 0x230da(%rip), %rdi     # 0x52b7f
xorl %esi, %esi
callq 0x2fea2
jmp 0x2fad7
movq %rbx, %rdi
jmp 0x2fa42
movq %r14, %rdi
callq 0x4440
movq %rbx, %rdi
jmp 0x2fa38
movq 0x38(%rsp), %rdi
callq 0x4610
movq 0x8(%rsp), %rdi
callq 0x4610
testq %r14, %r14
je 0x2fa47
xorl %ebx, %ebx
movl $0x1, %r12d
leaq 0x10(%rsp), %r14
leaq 0x28(%rsp), %r15
movq 0x38(%rsp), %rbp
movq %r14, %rdi
movq %r15, %rsi
leaq 0x60(%rsp), %rdx
callq 0x2ff4e
cmpl $-0x1, %eax
je 0x2fe74
movq %r14, %rdi
movq %r15, %rsi
leaq 0x64(%rsp), %rdx
callq 0x2ff4e
cmpl $-0x1, %eax
je 0x2fe74
movq %r14, %rdi
movq %r15, %rsi
leaq 0x68(%rsp), %rdx
callq 0x2ff4e
cmpl $-0x1, %eax
je 0x2fe74
movq 0x28(%rsp), %rdx
movq 0x10(%rsp), %rax
movl $0x1, %ecx
cmpq %rax, %rdx
jae 0x2fbb7
movq 0x20(%rsp), %rdi
movq %rdx, %rsi
negq %rsi
incq %rdx
movb $0x1, %r8b
movzbl -0x1(%rdi,%rdx), %r9d
cmpl $0x20, %r9d
je 0x2fb79
cmpl $0x9, %r9d
jne 0x2fb93
cmpq %rax, %rdx
setb %r8b
decq %rsi
incq %rdx
movq %rax, %r9
addq %rsi, %r9
jne 0x2fb67
movq %rax, %rdx
jmp 0x2fbb7
testb $0x1, %r8b
je 0x2fbb1
xorl %edx, %edx
cmpb $0x2d, %r9b
sete %dl
movq %rdx, %rcx
negq %rcx
orq $0x1, %rcx
subq %rsi, %rdx
jmp 0x2fbb7
negq %rsi
movq %rsi, %rdx
cmpq %rax, %rdx
jae 0x2fe74
movq 0x20(%rsp), %rdi
movb (%rdi,%rdx), %sil
addb $-0x30, %sil
cmpb $0x9, %sil
ja 0x2fe74
xorl %esi, %esi
movzbl (%rdi,%rdx), %r8d
leal -0x30(%r8), %r9d
cmpb $0x9, %r9b
ja 0x2fbff
leaq (%rsi,%rsi,4), %rsi
incq %rdx
leaq (%r8,%rsi,2), %rsi
addq $-0x30, %rsi
cmpq %rdx, %rax
jne 0x2fbd9
movq %rax, %rdx
imulq %rcx, %rsi
movq %rdx, 0x28(%rsp)
movq %rsi, 0x78(%rsp)
movq %r14, %rdi
movq %r15, %rsi
leaq 0x70(%rsp), %rdx
callq 0x2ff4e
cmpl $-0x1, %eax
je 0x2fe74
movq %r14, %rdi
movq %r15, %rsi
leaq 0x74(%rsp), %rdx
callq 0x2ff4e
cmpl $-0x1, %eax
je 0x2fe74
movl %r12d, 0x40(%rsp)
movl 0x64(%rsp), %r12d
movl 0x68(%rsp), %eax
leal (%r12,%rax), %r13d
decl %r13d
movl %r13d, 0x68(%rsp)
movslq 0x60(%rsp), %r15
cmpq $-0x2, %r15
jle 0x2fe86
movq 0x48(%rsp), %rcx
cmpl 0x10(%rcx), %r15d
je 0x2fd2e
movq %rbp, %r14
movq 0x30(%rsp), %rbx
movslq 0x8834(%rbx), %rbp
leal 0x2(%r15), %eax
cmpl %eax, %ebp
jge 0x2fcf8
leaq (,%rbp,2), %rbx
addq %rbp, %rbx
shlq $0x4, %rbx
movq 0x30(%rsp), %rcx
movl %eax, 0x8834(%rcx)
movq 0x30(%rsp), %rcx
movq 0x8838(%rcx), %rdi
movl %eax, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rsi
callq 0x48d0
movq 0x30(%rsp), %rcx
movq %rax, 0x8838(%rcx)
addq %rax, %rbx
movq 0x30(%rsp), %rax
movslq 0x8834(%rax), %rax
subq %rbp, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rdx
movq %rbx, %rdi
movq 0x30(%rsp), %rbx
xorl %esi, %esi
callq 0x42d0
movq 0x8838(%rbx), %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 %r14, %rbp
movq %rcx, (%r14)
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 0x2fd55
movl 0x18(%r15), %ecx
cmpl %ecx, %r13d
jg 0x2fd55
testl %ecx, %ecx
jne 0x2fd60
movq (%rax), %r15
decl %ebx
addq $-0x8, %rax
jmp 0x2fd42
movl (%r15), %ecx
movl 0x4(%r15), %eax
incl %ecx
cmpl %eax, %ecx
jl 0x2fd96
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 0x48d0
movq %rax, 0x8(%r15)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq 0x8(%r15), %rdx
movslq (%r15), %rax
leal 0x1(%rax), %ecx
movl %ecx, (%r15)
leaq (%rax,%rax,2), %r15
shlq $0x4, %r15
movaps 0x50(%rsp), %xmm0
movaps 0x60(%rsp), %xmm1
movaps 0x70(%rsp), %xmm2
movups %xmm2, 0x20(%rdx,%r15)
movups %xmm1, 0x10(%rdx,%r15)
movq %rdx, %r14
movups %xmm0, (%rdx,%r15)
movl 0x40(%rsp), %r12d
cmpl %r12d, %ebx
jl 0x2fdf5
addl %r12d, %r12d
movslq %r12d, %rsi
shlq $0x3, %rsi
movq %rbp, %rdi
callq 0x48d0
movq %rax, %rbp
addq %r15, %r14
movslq %ebx, %rax
movq %r14, 0x48(%rsp)
movq %r14, (%rbp,%rax,8)
movq 0x10(%rsp), %rax
movq 0x28(%rsp), %rcx
cmpq %rax, %rcx
jae 0x2fe36
movq 0x20(%rsp), %rdx
leaq 0x10(%rsp), %r14
leaq 0x28(%rsp), %r15
cmpb $0xa, (%rdx,%rcx)
je 0x2fe40
incq %rcx
cmpq %rcx, %rax
jne 0x2fe23
movq %rax, %rcx
jmp 0x2fe40
leaq 0x10(%rsp), %r14
leaq 0x28(%rsp), %r15
incq %rcx
movq %rcx, 0x28(%rsp)
cmpq %rax, %rcx
jb 0x2faf7
movq %rbp, %rdi
callq 0x4610
movq 0x20(%rsp), %rdi
callq 0x4610
movq 0x8(%rsp), %rdi
callq 0x4610
xorl %eax, %eax
jmp 0x2fa4c
movq 0x20(%rsp), %rdi
callq 0x4610
movq %rbp, %rdi
jmp 0x2fa38
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
movq 0x8(%rsp), %rdx
leaq 0x60(%rsp), %rcx
callq 0x54ff
jmp 0x2fa47