// File: /workspace/github_cpp_makefile_star2_O3/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 %r12d, %r12d
movq %r12, 0x70(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq %r12, 0x10(%rsp)
cmpq %r12, 0x8838(%rdi)
jne 0x2f201
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movl $0x1, 0x8834(%rdi)
movl $0x1, %edi
movl $0x30, %esi
callq 0x4360
movq %rax, 0x8838(%rbx)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x2f201
movq %rax, %r13
movq %rbx, 0x88(%rsp)
movabsq $-0x7fffffff00000001, %rax # imm = 0x80000000FFFFFFFF
movq %rax, 0x10(%r13)
movl $0x7fffffff, 0x18(%r13) # imm = 0x7FFFFFFF
movl $0x1, %edi
movl $0x8, %esi
callq 0x4360
movq %r13, %rcx
movq %rax, %r13
movq %rcx, 0x18(%rsp)
movq %rcx, (%rax)
testq %r15, %r15
je 0x2ec4e
xorl %ebx, %ebx
jmp 0x2ec6c
leaq 0x239db(%rip), %rsi     # 0x52630
movq %r14, %rdi
callq 0x13378
movq %rax, %rbx
movq %rax, %r15
testq %rax, %rax
je 0x2ee00
leaq 0x22cce(%rip), %rsi     # 0x51941
movq %r15, %rdi
callq 0x4600
testq %rax, %rax
je 0x2f1e9
movq %rax, %r14
leaq 0x90(%rsp), %rdi
movl $0x1, %esi
movl $0x10000, %edx          # imm = 0x10000
movq %rax, %rcx
callq 0x47d0
testq %rax, %rax
jle 0x2f1f1
movq %r13, (%rsp)
movq %rbx, 0x8(%rsp)
xorl %edi, %edi
xorl %ebx, %ebx
xorl %r13d, %r13d
movq %r14, 0x20(%rsp)
movslq %eax, %r12
leaq (%r12,%r13), %rbp
movq %r13, %r14
leaq (%r12,%r13), %rax
addq $0x2, %rax
cmpq %rax, %rbx
jae 0x2ed45
movq %rbx, 0x28(%rsp)
leaq 0x1(%rbp), %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 %rdi, %r13
movq %rbx, %rsi
callq 0x48d0
movq %rax, %r15
testq %rax, %rax
jne 0x2ed48
movq %r13, %r15
movq %r14, %r13
leaq 0x90(%rsp), %r14
movq 0x28(%rsp), %rbx
jmp 0x2ed69
movq %rdi, %r15
addq %r15, %r14
movq %r14, %rdi
leaq 0x90(%rsp), %r14
movq %r14, %rsi
movq %r12, %rdx
callq 0x4420
movb $0x0, (%r15,%rbp)
movq %rbp, %r13
movl $0x1, %esi
movl $0x10000, %edx          # imm = 0x10000
movq %r14, %rdi
movq 0x20(%rsp), %r14
movq %r14, %rcx
callq 0x47d0
testq %rax, %rax
movq %r15, %rdi
jg 0x2ecbf
movq %rbx, 0x68(%rsp)
movq %rdi, 0x70(%rsp)
movq %r13, 0x60(%rsp)
jne 0x2edde
cmpq $0x1, %r13
jbe 0x2edde
movq %r14, %rdi
callq 0x4530
testl %eax, %eax
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq 0x8(%rsp), %r14
movq (%rsp), %rbx
je 0x2ee08
testq %r15, %r15
je 0x2edce
movq %r15, %rdi
callq 0x4610
movq %rbx, %rdi
callq 0x4610
movq %r14, %rdi
jmp 0x2f1fc
testq %rdi, %rdi
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rbx
movq (%rsp), %r13
je 0x2f1f1
callq 0x4610
jmp 0x2f1f1
movq %r13, %rdi
jmp 0x2f1fc
cmpb $0x1f, (%r15)
jne 0x2ee62
cmpb $-0x75, 0x1(%r15)
jne 0x2ee62
leaq 0x30(%rsp), %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x318a4
movq %rax, %rbx
movq %r15, %rdi
callq 0x4610
testq %rbx, %rbx
je 0x2f216
leaq 0x60(%rsp), %r14
movq %rbx, 0x10(%r14)
movq 0x30(%rsp), %rax
movq %rax, (%r14)
movq %rax, 0x8(%r14)
leaq 0x21d5e(%rip), %rdi     # 0x50bb3
xorl %esi, %esi
movq %r14, %rdx
callq 0x2f23c
movq (%r14), %r13
leaq 0x40(%rsp), %rcx
leaq 0x44(%rsp), %r15
xorl %ebx, %ebx
movl $0x1, 0x20(%rsp)
leaq 0x10(%rsp), %rbp
movq 0x70(%rsp), %r14
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x2f2ed
cmpl $-0x1, %eax
je 0x2f1d1
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x2f2ed
cmpl $-0x1, %eax
je 0x2f1d1
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
leaq 0x48(%rsp), %rcx
callq 0x2f2ed
cmpl $-0x1, %eax
je 0x2f1d1
movq 0x10(%rsp), %rax
movl $0x1, %ecx
cmpq %r13, %rax
jae 0x2ef0d
movzbl (%r14,%rax), %ecx
cmpl $0x20, %ecx
je 0x2eeeb
cmpl $0x9, %ecx
jne 0x2eef8
incq %rax
cmpq %rax, %r13
jne 0x2eedc
jmp 0x2f1d1
xorl %edx, %edx
cmpb $0x2d, %cl
sete %dl
addq %rdx, %rax
movq %rdx, %rcx
negq %rcx
orq $0x1, %rcx
cmpq %r13, %rax
jae 0x2f1d1
movb (%r14,%rax), %dl
addb $-0x30, %dl
cmpb $0x9, %dl
ja 0x2f1d1
xorl %edx, %edx
movzbl (%r14,%rax), %esi
leal -0x30(%rsi), %edi
cmpb $0x9, %dil
ja 0x2ef4d
leaq (%rdx,%rdx,4), %rdx
incq %rax
leaq (%rsi,%rdx,2), %rdx
addq $-0x30, %rdx
cmpq %rax, %r13
jne 0x2ef28
movq %r13, %rax
imulq %rcx, %rdx
movq %rax, 0x10(%rsp)
movq %rdx, 0x58(%rsp)
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
leaq 0x50(%rsp), %rcx
callq 0x2f2ed
cmpl $-0x1, %eax
je 0x2f1d1
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
leaq 0x54(%rsp), %rcx
callq 0x2f2ed
cmpl $-0x1, %eax
je 0x2f1d1
movq %r13, 0x28(%rsp)
movq (%rsp), %rbp
movl 0x44(%rsp), %r12d
movl 0x48(%rsp), %eax
leal (%r12,%rax), %r13d
decl %r13d
movl %r13d, 0x48(%rsp)
movslq 0x40(%rsp), %r15
cmpq $-0x2, %r15
jle 0x2f21f
movq 0x18(%rsp), %r14
cmpl 0x10(%r14), %r15d
je 0x2f078
movq 0x88(%rsp), %rbx
movslq 0x8834(%rbx), %rcx
leal 0x2(%r15), %eax
movq 0x8838(%rbx), %r14
cmpl %eax, %ecx
jge 0x2f04a
leaq (%rcx,%rcx,2), %rdx
shlq $0x4, %rdx
movq %rdx, 0x18(%rsp)
movl %eax, 0x8834(%rbx)
movl %eax, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rsi
movq %r14, %rdi
movq %rcx, 0x80(%rsp)
callq 0x48d0
movq %rax, %r14
movq %rax, 0x8838(%rbx)
movq 0x18(%rsp), %rdi
addq %rax, %rdi
movslq 0x8834(%rbx), %rax
subq 0x80(%rsp), %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rdx
xorl %esi, %esi
callq 0x42d0
leaq (%r15,%r15,2), %rax
shlq $0x4, %rax
addq %rax, %r14
addq $0x30, %r14
movl %r15d, 0x10(%r14)
movabsq $0x7fffffff80000000, %rax # imm = 0x7FFFFFFF80000000
movq %rax, 0x14(%r14)
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movq %r14, (%rbp)
xorl %ebx, %ebx
movslq %ebx, %rax
leal 0x1(%rax), %ebx
leaq -0x8(,%rax,8), %rax
addq %rbp, %rax
cmpl 0x14(%r14), %r12d
jl 0x2f09c
movl 0x18(%r14), %ecx
cmpl %ecx, %r13d
jg 0x2f09c
testl %ecx, %ecx
jne 0x2f0a7
movq (%rax), %r14
decl %ebx
addq $-0x8, %rax
jmp 0x2f089
movl (%r14), %ecx
movl 0x4(%r14), %eax
incl %ecx
cmpl %eax, %ecx
jl 0x2f0dd
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x10, %eax
cmovel %eax, %ecx
movl %ecx, 0x4(%r14)
movq 0x8(%r14), %rdi
movslq %ecx, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rsi
callq 0x48d0
movq %rax, 0x8(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq 0x8(%r14), %rdx
movslq (%r14), %rax
leal 0x1(%rax), %ecx
movl %ecx, (%r14)
leaq (%rax,%rax,2), %r14
shlq $0x4, %r14
movaps 0x30(%rsp), %xmm0
movaps 0x40(%rsp), %xmm1
movaps 0x50(%rsp), %xmm2
movups %xmm2, 0x20(%rdx,%r14)
movups %xmm1, 0x10(%rdx,%r14)
movq %rdx, %r15
movups %xmm0, (%rdx,%r14)
movl 0x20(%rsp), %eax
cmpl %eax, %ebx
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jge 0x2f13a
movq 0x28(%rsp), %r13
leaq 0x40(%rsp), %rcx
movq %rbp, %rax
jmp 0x2f159
addl %eax, %eax
movl %eax, 0x20(%rsp)
movslq %eax, %rsi
shlq $0x3, %rsi
movq %rbp, %rdi
callq 0x48d0
movq 0x60(%rsp), %r13
leaq 0x40(%rsp), %rcx
leaq 0x44(%rsp), %rbp
addq %r14, %r15
movslq %ebx, %rsi
movq %r15, 0x18(%rsp)
movq %r15, (%rax,%rsi,8)
movq 0x10(%rsp), %rsi
cmpq %r13, %rsi
movq %rax, (%rsp)
jae 0x2f196
movq 0x70(%rsp), %rdx
movq %rbp, %r15
cmpb $0xa, (%rdx,%rsi)
je 0x2f199
incq %rsi
cmpq %rsi, %r13
jne 0x2f183
movq %r13, %rsi
jmp 0x2f199
movq %rbp, %r15
incq %rsi
movq %rsi, 0x10(%rsp)
cmpq %r13, %rsi
leaq 0x10(%rsp), %rbp
jb 0x2ee7b
movq (%rsp), %rdi
callq 0x4610
movq 0x70(%rsp), %rdi
callq 0x4610
movq 0x8(%rsp), %rdi
callq 0x4610
xorl %r12d, %r12d
jmp 0x2f201
movq %r14, %rdi
callq 0x4610
movq (%rsp), %rdi
callq 0x4610
movq 0x8(%rsp), %rdi
jmp 0x2f1fc
movq %r15, %rdi
callq 0x4440
movq %r13, %rdi
callq 0x4610
movq %rbx, %rdi
callq 0x4610
movl %r12d, %eax
addq $0x10098, %rsp          # imm = 0x10098
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rdi
jmp 0x2edd1
movq %r14, %rdi
movq %rbp, %rsi
movq 0x8(%rsp), %rdx
leaq 0x40(%rsp), %rcx
callq 0x5483
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x2f201