// File: 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
movq %rsp, %rbp
subq $0x100e0, %rsp          # imm = 0x100E0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
leaq -0x10050(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x42d0
movq $0x0, -0x10068(%rbp)
movl $0x0, -0x100a4(%rbp)
movl $0x0, -0x100a8(%rbp)
movq $0x0, -0x100b0(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8838(%rax)
je 0x41716
movl $0x0, -0x4(%rbp)
jmp 0x41ff8
movq -0x10(%rbp), %rax
movl $0x1, 0x8834(%rax)
movl $0x1, %edi
movl $0x30, %esi
callq 0x4370
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8838(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8838(%rax)
jne 0x4175b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41ff8
movq -0x10(%rbp), %rax
movq 0x8838(%rax), %rax
movq %rax, -0x10060(%rbp)
movq -0x10060(%rbp), %rax
movl $0xffffffff, 0x10(%rax) # imm = 0xFFFFFFFF
movq -0x10060(%rbp), %rax
movl $0x80000000, 0x14(%rax) # imm = 0x80000000
movq -0x10060(%rbp), %rax
movl $0x7fffffff, 0x18(%rax) # imm = 0x7FFFFFFF
movl -0x100a4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x100a4(%rbp)
movslq %eax, %rdi
movl $0x8, %esi
callq 0x4370
movq %rax, -0x10068(%rbp)
movq -0x10060(%rbp), %rdx
movq -0x10068(%rbp), %rax
movslq -0x100a8(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
cmpq $0x0, -0x20(%rbp)
jne 0x41815
movq -0x18(%rbp), %rdi
leaq 0x3b064(%rip), %rsi     # 0x7c849
callq 0x19090
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x4180d
movq -0x10068(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41ff8
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x3a1cf(%rip), %rsi     # 0x7b9ef
callq 0x4690
movq %rax, -0x10058(%rbp)
cmpq $0x0, %rax
jne 0x4185c
movq -0x20(%rbp), %rdi
callq 0x4480
movq -0x10068(%rbp), %rdi
callq 0x46a0
movq -0x28(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41ff8
jmp 0x4185e
leaq -0x10030(%rbp), %rdi
movq -0x10058(%rbp), %rcx
movl $0x1, %esi
movl $0x10000, %edx          # imm = 0x10000
callq 0x48b0
movq %rax, -0x10038(%rbp)
cmpq $0x0, %rax
jle 0x418a6
leaq -0x10030(%rbp), %rdi
movq -0x10038(%rbp), %rax
movl %eax, %esi
leaq -0x10050(%rbp), %rdx
callq 0x42010
jmp 0x4185e
cmpq $0x0, -0x10038(%rbp)
jl 0x418ba
cmpq $0x2, -0x10050(%rbp)
jae 0x418f1
cmpq $0x0, -0x10040(%rbp)
je 0x418d0
movq -0x10040(%rbp), %rdi
callq 0x46a0
movq -0x10068(%rbp), %rdi
callq 0x46a0
movq -0x28(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41ff8
movq -0x10058(%rbp), %rdi
callq 0x4590
cmpl $0x0, %eax
je 0x41939
cmpq $0x0, -0x10040(%rbp)
je 0x41918
movq -0x10040(%rbp), %rdi
callq 0x46a0
movq -0x10068(%rbp), %rdi
callq 0x46a0
movq -0x28(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41ff8
movq -0x10040(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x1f, %eax
jne 0x419f9
movq -0x10040(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x8b, %eax
jne 0x419f9
movq -0x10040(%rbp), %rdi
movq -0x10050(%rbp), %rsi
leaq -0x100b8(%rbp), %rdx
callq 0x45840
movq %rax, -0x100c0(%rbp)
movq -0x10040(%rbp), %rdi
callq 0x46a0
cmpq $0x0, -0x100c0(%rbp)
jne 0x419ba
movq -0x10068(%rbp), %rdi
callq 0x46a0
movq -0x28(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41ff8
movq -0x100c0(%rbp), %rax
movq %rax, -0x10040(%rbp)
movq -0x100b8(%rbp), %rax
movq %rax, -0x10050(%rbp)
movq -0x100b8(%rbp), %rax
movq %rax, -0x10048(%rbp)
leaq 0x3aae1(%rip), %rdi     # 0x7c4cc
xorl %esi, %esi
leaq -0x10050(%rbp), %rdx
callq 0x42010
jmp 0x419fb
leaq -0x100a0(%rbp), %rdx
addq $0x10, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b0(%rbp), %rsi
callq 0x420a0
cmpl $-0x1, %eax
jne 0x41a4b
movq -0x10040(%rbp), %rdi
callq 0x46a0
movq -0x10068(%rbp), %rdi
callq 0x46a0
movq -0x28(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41ff8
leaq -0x100a0(%rbp), %rdx
addq $0x14, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b0(%rbp), %rsi
callq 0x420a0
cmpl $-0x1, %eax
jne 0x41a9b
movq -0x10040(%rbp), %rdi
callq 0x46a0
movq -0x10068(%rbp), %rdi
callq 0x46a0
movq -0x28(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41ff8
leaq -0x100a0(%rbp), %rdx
addq $0x18, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b0(%rbp), %rsi
callq 0x420a0
cmpl $-0x1, %eax
jne 0x41aeb
movq -0x10040(%rbp), %rdi
callq 0x46a0
movq -0x10068(%rbp), %rdi
callq 0x46a0
movq -0x28(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41ff8
leaq -0x100a0(%rbp), %rdx
addq $0x28, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b0(%rbp), %rsi
callq 0x42240
cmpl $-0x1, %eax
jne 0x41b3b
movq -0x10040(%rbp), %rdi
callq 0x46a0
movq -0x10068(%rbp), %rdi
callq 0x46a0
movq -0x28(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41ff8
leaq -0x100a0(%rbp), %rdx
addq $0x20, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b0(%rbp), %rsi
callq 0x420a0
cmpl $-0x1, %eax
jne 0x41b8b
movq -0x10040(%rbp), %rdi
callq 0x46a0
movq -0x10068(%rbp), %rdi
callq 0x46a0
movq -0x28(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41ff8
leaq -0x100a0(%rbp), %rdx
addq $0x24, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b0(%rbp), %rsi
callq 0x420a0
cmpl $-0x1, %eax
jne 0x41bdb
movq -0x10040(%rbp), %rdi
callq 0x46a0
movq -0x10068(%rbp), %rdi
callq 0x46a0
movq -0x28(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41ff8
movl -0x1008c(%rbp), %eax
subl $0x1, %eax
addl -0x10088(%rbp), %eax
movl %eax, -0x10088(%rbp)
cmpl $-0x1, -0x10090(%rbp)
jge 0x41c44
movq -0x10040(%rbp), %rdi
callq 0x46a0
movq -0x10068(%rbp), %rdi
callq 0x46a0
movq -0x28(%rbp), %rdi
callq 0x46a0
movq 0x483af(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movl -0x10090(%rbp), %edx
leaq 0x3ac1e(%rip), %rsi     # 0x7c84f
movb $0x0, %al
callq 0x4830
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41ff8
movl -0x10090(%rbp), %eax
movq -0x10060(%rbp), %rcx
cmpl 0x10(%rcx), %eax
je 0x41d90
movq -0x10(%rbp), %rax
movl 0x8834(%rax), %eax
movl -0x10090(%rbp), %ecx
addl $0x2, %ecx
cmpl %ecx, %eax
jge 0x41cfa
movq -0x10(%rbp), %rax
movslq 0x8834(%rax), %rax
imulq $0x30, %rax, %rax
movq %rax, -0x100c8(%rbp)
movl -0x10090(%rbp), %ecx
addl $0x2, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8834(%rax)
movq -0x10(%rbp), %rax
movq 0x8838(%rax), %rdi
movq -0x10(%rbp), %rax
movslq 0x8834(%rax), %rax
imulq $0x30, %rax, %rsi
callq 0x4990
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8838(%rax)
movq -0x10(%rbp), %rax
movq 0x8838(%rax), %rdi
addq -0x100c8(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq 0x8834(%rax), %rax
imulq $0x30, %rax, %rdx
subq -0x100c8(%rbp), %rdx
xorl %esi, %esi
callq 0x42d0
movq -0x10(%rbp), %rax
movq 0x8838(%rax), %rax
movl -0x10090(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x10060(%rbp)
movl -0x10090(%rbp), %ecx
movq -0x10060(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x10060(%rbp), %rax
movl $0x80000000, 0x14(%rax) # imm = 0x80000000
movq -0x10060(%rbp), %rax
movl $0x7fffffff, 0x18(%rax) # imm = 0x7FFFFFFF
movq -0x10060(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x10060(%rbp), %rax
movl $0x0, (%rax)
movq -0x10060(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x10060(%rbp), %rcx
movq -0x10068(%rbp), %rax
movl $0x0, -0x100a8(%rbp)
movq %rcx, (%rax)
jmp 0x41d92
movl -0x1008c(%rbp), %ecx
movq -0x10060(%rbp), %rdx
movb $0x1, %al
cmpl 0x14(%rdx), %ecx
movb %al, -0x100c9(%rbp)
jl 0x41dda
movl -0x10088(%rbp), %ecx
movq -0x10060(%rbp), %rdx
movb $0x1, %al
cmpl 0x18(%rdx), %ecx
movb %al, -0x100c9(%rbp)
jg 0x41dda
movq -0x10060(%rbp), %rax
cmpl $0x0, 0x18(%rax)
sete %al
movb %al, -0x100c9(%rbp)
movb -0x100c9(%rbp), %al
testb $0x1, %al
jne 0x41de6
jmp 0x41e0c
movq -0x10068(%rbp), %rax
movl -0x100a8(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x100a8(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x10060(%rbp)
jmp 0x41d92
movq -0x10060(%rbp), %rax
movl (%rax), %eax
addl $0x1, %eax
movq -0x10060(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jl 0x41e8f
movq -0x10060(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0x41e45
movq -0x10060(%rbp), %rax
movl 0x4(%rax), %eax
shll %eax
movl %eax, -0x100d0(%rbp)
jmp 0x41e52
movl $0x10, %eax
movl %eax, -0x100d0(%rbp)
jmp 0x41e52
movl -0x100d0(%rbp), %ecx
movq -0x10060(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x10060(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10060(%rbp), %rax
movslq 0x4(%rax), %rax
imulq $0x30, %rax, %rsi
callq 0x4990
movq %rax, %rcx
movq -0x10060(%rbp), %rax
movq %rcx, 0x8(%rax)
movl $0x0, -0x100a0(%rbp)
movl $0x0, -0x1009c(%rbp)
movq $0x0, -0x10098(%rbp)
movq -0x10060(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10060(%rbp), %rcx
movl (%rcx), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, (%rcx)
cltq
imulq $0x30, %rax, %rax
addq %rax, %rdi
movq %rdi, -0x10070(%rbp)
leaq -0x100a0(%rbp), %rsi
movl $0x30, %edx
callq 0x4460
movq -0x10070(%rbp), %rax
movq %rax, -0x10060(%rbp)
movl -0x100a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x100a8(%rbp)
cmpl -0x100a4(%rbp), %eax
jl 0x41f3b
movl -0x100a4(%rbp), %eax
shll %eax
movl %eax, -0x100a4(%rbp)
movq -0x10068(%rbp), %rdi
movslq -0x100a4(%rbp), %rsi
shlq $0x3, %rsi
callq 0x4990
movq %rax, -0x10068(%rbp)
movq -0x10060(%rbp), %rdx
movq -0x10068(%rbp), %rax
movslq -0x100a8(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x100b0(%rbp), %rcx
xorl %eax, %eax
cmpq -0x10050(%rbp), %rcx
movb %al, -0x100d1(%rbp)
jae 0x41f8a
movq -0x10040(%rbp), %rax
movq -0x100b0(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
setne %al
movb %al, -0x100d1(%rbp)
movb -0x100d1(%rbp), %al
testb $0x1, %al
jne 0x41f96
jmp 0x41faa
movq -0x100b0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x100b0(%rbp)
jmp 0x41f54
movq -0x100b0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x100b0(%rbp)
movq -0x100b0(%rbp), %rax
cmpq -0x10050(%rbp), %rax
jb 0x419fb
movq -0x10068(%rbp), %rdi
callq 0x46a0
movq -0x10040(%rbp), %rdi
callq 0x46a0
movq -0x28(%rbp), %rdi
callq 0x46a0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100e0, %rsp          # imm = 0x100E0
popq %rbp
retq
nopw %cs:(%rax,%rax)