// File: /workspace/github_cpp_makefile_star2_O2/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
andq $0x0, 0x30(%rsp)
xorps %xmm0, %xmm0
andq $0x0, 0x18(%rsp)
movaps %xmm0, 0x20(%rsp)
xorl %ebx, %ebx
cmpq %rbx, 0x8838(%rdi)
jne 0x25dd3
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %r12
movl $0x1, 0x8834(%rdi)
pushq $0x1
popq %rdi
pushq $0x30
popq %rsi
callq 0x4370
movq %rax, 0x8838(%r12)
pushq $-0x1
popq %rbx
testq %rax, %rax
je 0x25dd3
movq %rax, %r15
movabsq $-0x7fffffff00000001, %rax # imm = 0x80000000FFFFFFFF
movq %rax, 0x10(%r15)
movl $0x7fffffff, 0x18(%r15) # imm = 0x7FFFFFFF
pushq $0x1
popq %rdi
pushq $0x8
popq %rsi
callq 0x4370
movq %rax, %r13
movq %r15, 0x40(%rsp)
movq %r15, (%rax)
testq %rbp, %rbp
je 0x2590f
xorl %r15d, %r15d
jmp 0x2592d
leaq 0x1fc44(%rip), %rsi     # 0x4555a
movq %r14, %rdi
callq 0xfe25
movq %rax, %r15
movq %rax, %rbp
testq %rax, %rax
je 0x259e6
leaq 0x1ef81(%rip), %rsi     # 0x448b5
movq %rbp, %rdi
callq 0x45f0
testq %rax, %rax
je 0x25dbb
movq %rax, %r14
movq %r13, 0x8(%rsp)
movq %r15, 0x10(%rsp)
leaq 0x90(%rsp), %rbp
pushq $0x1
popq %r15
leaq 0x20(%rsp), %r13
movl $0x10000, %edx          # imm = 0x10000
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rcx
callq 0x47d0
testq %rax, %rax
jle 0x2598a
movq %rbp, %rdi
movl %eax, %esi
movq %r13, %rdx
callq 0x25e29
jmp 0x25963
setne %al
movq 0x20(%rsp), %rbp
cmpq $0x2, %rbp
setb %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x259be
movq 0x30(%rsp), %rdi
callq 0x4600
movq 0x8(%rsp), %rdi
callq 0x4600
movq 0x10(%rsp), %rdi
jmp 0x25dce
movq %r14, %rdi
callq 0x4530
movq 0x30(%rsp), %r14
testl %eax, %eax
movq 0x10(%rsp), %r13
je 0x259ee
movq %r14, %rdi
callq 0x4600
movq 0x8(%rsp), %rdi
callq 0x4600
movq %r13, %rdi
jmp 0x25dce
cmpb $0x1f, (%r14)
jne 0x25a3e
cmpb $-0x75, 0x1(%r14)
jne 0x25a3e
leaq 0x50(%rsp), %rdx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2794b
movq %rax, %r15
movq %r14, %rdi
callq 0x4600
testq %r15, %r15
je 0x259dc
leaq 0x20(%rsp), %rdx
movq %r15, 0x10(%rdx)
movq 0x50(%rsp), %rax
movq %rax, (%rdx)
movq %rax, 0x8(%rdx)
leaq 0x1e1b5(%rip), %rdi     # 0x43bec
xorl %esi, %esi
callq 0x25e29
leaq 0x60(%rsp), %rdx
leaq 0x64(%rsp), %rbp
xorl %r14d, %r14d
pushq $0x1
popq %rax
movl %eax, 0x4c(%rsp)
leaq 0x20(%rsp), %r15
leaq 0x18(%rsp), %r13
movq 0x40(%rsp), %r8
movq %r8, 0x40(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x25eda
cmpl $-0x1, %eax
je 0x259a0
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x25eda
cmpl $-0x1, %eax
je 0x259a0
movq %r15, %rdi
movq %r13, %rsi
leaq 0x68(%rsp), %rdx
callq 0x25eda
cmpl $-0x1, %eax
je 0x259a0
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rax
movq 0x30(%rsp), %rbp
leaq (%rdi,%rbp), %rcx
movq 0x10(%rsp), %r15
cmpq %rax, %rdi
jae 0x25dac
movzbl (%rbp,%rdi), %edx
cmpl $0x20, %edx
je 0x25ada
cmpl $0x9, %edx
jne 0x25ae2
incq %rdi
incq %rcx
jmp 0x25ac2
xorl %r8d, %r8d
cmpb $0x2d, %dl
sete %r8b
leaq (%r8,%rdi), %rsi
cmpq %rax, %rsi
jae 0x25dac
leaq (%r8,%rbp), %r9
movb (%rdi,%r9), %dil
addb $-0x30, %dil
cmpb $0x9, %dil
ja 0x25dac
addq %r8, %rcx
xorl %edi, %edi
cmpq %rax, %rsi
jae 0x25b3d
movzbl (%rcx), %r8d
leal -0x30(%r8), %r9d
cmpb $0x9, %r9b
ja 0x25b3a
imulq $0xa, %rdi, %rdi
incq %rsi
addq %r8, %rdi
addq $-0x30, %rdi
incq %rcx
jmp 0x25b14
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), %r13
movq %r13, %rsi
leaq 0x70(%rsp), %rdx
callq 0x25eda
cmpl $-0x1, %eax
je 0x25de7
movq %r15, %rdi
movq %r13, %rsi
leaq 0x74(%rsp), %rdx
callq 0x25eda
cmpl $-0x1, %eax
je 0x25de7
movq 0x40(%rsp), %rcx
movl 0x64(%rsp), %r13d
movl 0x68(%rsp), %eax
leal (%rax,%r13), %r15d
decl %r15d
movl %r15d, 0x68(%rsp)
movslq 0x60(%rsp), %rdx
cmpq $-0x2, %rdx
jle 0x25def
movq %rcx, %rbp
cmpl 0x10(%rcx), %edx
je 0x25c63
movslq 0x8834(%r12), %rbp
leal 0x2(%rdx), %eax
movq 0x8838(%r12), %r14
cmpl %eax, %ebp
jge 0x25c34
imulq $0x30, %rbp, %rcx
movq %rcx, 0x88(%rsp)
movl %eax, 0x8834(%r12)
movl %eax, %eax
imulq $0x30, %rax, %rsi
movq %r14, %rdi
movq %rdx, 0x40(%rsp)
callq 0x48c0
movq %rax, %r14
movq %rax, 0x8838(%r12)
movq 0x88(%rsp), %rdi
addq %rax, %rdi
movslq 0x8834(%r12), %rax
subq %rbp, %rax
imulq $0x30, %rax, %rdx
xorl %esi, %esi
callq 0x42e0
movq 0x40(%rsp), %rdx
imulq $0x30, %rdx, %rax
leaq (%r14,%rax), %rbp
addq $0x30, %rbp
movl %edx, 0x10(%rbp)
movabsq $0x7fffffff80000000, %rax # imm = 0x7FFFFFFF80000000
movq %rax, 0x14(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, (%rbp)
movq 0x8(%rsp), %rax
movq %rbp, (%rax)
xorl %r14d, %r14d
movslq %r14d, %rax
leal 0x1(%rax), %r14d
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
addq $-0x8, %rax
cmpl 0x14(%rbp), %r13d
jl 0x25c89
movl 0x18(%rbp), %ecx
cmpl %ecx, %r15d
jg 0x25c89
testl %ecx, %ecx
jne 0x25c95
movq (%rax), %rbp
decl %r14d
addq $-0x8, %rax
jmp 0x25c77
movl (%rbp), %ecx
movl 0x4(%rbp), %eax
incl %ecx
cmpl %eax, %ecx
jl 0x25cc3
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 0x48c0
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, %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, %rbp
movups %xmm0, (%rdx,%r15)
movl 0x4c(%rsp), %eax
cmpl %eax, %r14d
jge 0x25d10
movq 0x8(%rsp), %rax
jmp 0x25d27
addl %eax, %eax
movl %eax, 0x4c(%rsp)
movslq %eax, %rsi
shlq $0x3, %rsi
movq 0x8(%rsp), %rdi
callq 0x48c0
addq %r15, %rbp
movq %rbp, %r8
movslq %r14d, %rcx
movq %rax, 0x8(%rsp)
movq %rbp, (%rax,%rcx,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 0x25d6f
cmpb $0xa, (%rsi,%rdi)
je 0x25d6c
incq %rdi
jmp 0x25d5c
movq %rdi, %rcx
incq %rcx
movq %rcx, 0x18(%rsp)
cmpq %rax, %rcx
leaq 0x20(%rsp), %r15
leaq 0x18(%rsp), %r13
jb 0x25a61
movq 0x8(%rsp), %rdi
callq 0x4600
movq 0x30(%rsp), %rdi
callq 0x4600
movq 0x10(%rsp), %rdi
callq 0x4600
xorl %ebx, %ebx
jmp 0x25dd3
movq %rbp, %rdi
callq 0x4600
movq 0x8(%rsp), %rdi
jmp 0x25dc6
movq %rbp, %rdi
callq 0x4440
movq %r13, %rdi
callq 0x4600
movq %r15, %rdi
callq 0x4600
movl %ebx, %eax
addq $0x10098, %rsp          # imm = 0x10098
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, %rdi
jmp 0x259a5
movq %rbp, %rdi
callq 0x4600
movq 0x8(%rsp), %rdi
callq 0x4600
movq 0x10(%rsp), %rdi
callq 0x4600
movq 0x311be(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
movl 0x60(%rsp), %edx
leaq 0x1f740(%rip), %rsi     # 0x45560
xorl %eax, %eax
callq 0x4770
jmp 0x25dd3