// File: /workspace/github_c_makefile_star2_O0/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
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 0x63a0
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 0x79d36
movl $0x0, -0x4(%rbp)
jmp 0x7a600
movq -0x10(%rbp), %rax
movl $0x1, 0x8834(%rax)
movl $0x1, %edi
movl $0x30, %esi
callq 0x6460
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8838(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8838(%rax)
jne 0x79d7b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7a600
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 0x6460
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 0x79e35
movq -0x18(%rbp), %rdi
leaq 0x689b8(%rip), %rsi     # 0xe27bd
callq 0x77490
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x79e2d
movq -0x10068(%rbp), %rdi
callq 0x6880
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7a600
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x65f56(%rip), %rsi     # 0xdfd96
callq 0xa2a70
movq %rax, -0x10058(%rbp)
cmpq $0x0, %rax
jne 0x79e7c
movq -0x20(%rbp), %rdi
callq 0x6590
movq -0x10068(%rbp), %rdi
callq 0x6880
movq -0x28(%rbp), %rdi
callq 0x6880
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7a600
jmp 0x79e7e
movq -0x10058(%rbp), %rdi
leaq -0x10030(%rbp), %rsi
movl $0x10000, %edx          # imm = 0x10000
callq 0x7a610
movq %rax, -0x10038(%rbp)
cmpq $0x0, %rax
jle 0x79ec1
leaq -0x10030(%rbp), %rdi
movq -0x10038(%rbp), %rax
movl %eax, %esi
leaq -0x10050(%rbp), %rdx
callq 0x7a6b0
jmp 0x79e7e
cmpq $0x0, -0x10038(%rbp)
jl 0x79ed5
cmpq $0x2, -0x10050(%rbp)
jae 0x79f0c
cmpq $0x0, -0x10040(%rbp)
je 0x79eeb
movq -0x10040(%rbp), %rdi
callq 0x6880
movq -0x10068(%rbp), %rdi
callq 0x6880
movq -0x28(%rbp), %rdi
callq 0x6880
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7a600
movq -0x10058(%rbp), %rdi
callq 0xa23e0
cmpl $0x0, %eax
je 0x79f54
cmpq $0x0, -0x10040(%rbp)
je 0x79f33
movq -0x10040(%rbp), %rdi
callq 0x6880
movq -0x10068(%rbp), %rdi
callq 0x6880
movq -0x28(%rbp), %rdi
callq 0x6880
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7a600
movq -0x10040(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x1f, %eax
jne 0x7a014
movq -0x10040(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x8b, %eax
jne 0x7a014
movq -0x10040(%rbp), %rdi
movq -0x10050(%rbp), %rsi
leaq -0x100b8(%rbp), %rdx
callq 0x7ded0
movq %rax, -0x100c0(%rbp)
movq -0x10040(%rbp), %rdi
callq 0x6880
cmpq $0x0, -0x100c0(%rbp)
jne 0x79fd5
movq -0x10068(%rbp), %rdi
callq 0x6880
movq -0x28(%rbp), %rdi
callq 0x6880
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7a600
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 0x6b400(%rip), %rdi     # 0xe5406
xorl %esi, %esi
leaq -0x10050(%rbp), %rdx
callq 0x7a6b0
jmp 0x7a016
leaq -0x100a0(%rbp), %rdx
addq $0x10, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b0(%rbp), %rsi
callq 0x7a820
cmpl $-0x1, %eax
jne 0x7a066
movq -0x10040(%rbp), %rdi
callq 0x6880
movq -0x10068(%rbp), %rdi
callq 0x6880
movq -0x28(%rbp), %rdi
callq 0x6880
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7a600
leaq -0x100a0(%rbp), %rdx
addq $0x14, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b0(%rbp), %rsi
callq 0x7a820
cmpl $-0x1, %eax
jne 0x7a0b6
movq -0x10040(%rbp), %rdi
callq 0x6880
movq -0x10068(%rbp), %rdi
callq 0x6880
movq -0x28(%rbp), %rdi
callq 0x6880
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7a600
leaq -0x100a0(%rbp), %rdx
addq $0x18, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b0(%rbp), %rsi
callq 0x7a820
cmpl $-0x1, %eax
jne 0x7a106
movq -0x10040(%rbp), %rdi
callq 0x6880
movq -0x10068(%rbp), %rdi
callq 0x6880
movq -0x28(%rbp), %rdi
callq 0x6880
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7a600
leaq -0x100a0(%rbp), %rdx
addq $0x28, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b0(%rbp), %rsi
callq 0x7a9c0
cmpl $-0x1, %eax
jne 0x7a156
movq -0x10040(%rbp), %rdi
callq 0x6880
movq -0x10068(%rbp), %rdi
callq 0x6880
movq -0x28(%rbp), %rdi
callq 0x6880
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7a600
leaq -0x100a0(%rbp), %rdx
addq $0x20, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b0(%rbp), %rsi
callq 0x7a820
cmpl $-0x1, %eax
jne 0x7a1a6
movq -0x10040(%rbp), %rdi
callq 0x6880
movq -0x10068(%rbp), %rdi
callq 0x6880
movq -0x28(%rbp), %rdi
callq 0x6880
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7a600
leaq -0x100a0(%rbp), %rdx
addq $0x24, %rdx
leaq -0x10050(%rbp), %rdi
leaq -0x100b0(%rbp), %rsi
callq 0x7a820
cmpl $-0x1, %eax
jne 0x7a1f6
movq -0x10040(%rbp), %rdi
callq 0x6880
movq -0x10068(%rbp), %rdi
callq 0x6880
movq -0x28(%rbp), %rdi
callq 0x6880
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7a600
movl -0x1008c(%rbp), %eax
subl $0x1, %eax
addl -0x10088(%rbp), %eax
movl %eax, -0x10088(%rbp)
cmpl $-0x1, -0x10090(%rbp)
jge 0x7a25f
movq -0x10040(%rbp), %rdi
callq 0x6880
movq -0x10068(%rbp), %rdi
callq 0x6880
movq -0x28(%rbp), %rdi
callq 0x6880
movq 0x8ad94(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
movl -0x10090(%rbp), %edx
leaq 0x68577(%rip), %rsi     # 0xe27c3
movb $0x0, %al
callq 0x6a80
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7a600
movl -0x10090(%rbp), %eax
movq -0x10060(%rbp), %rcx
cmpl 0x10(%rcx), %eax
je 0x7a3ab
movq -0x10(%rbp), %rax
movl 0x8834(%rax), %eax
movl -0x10090(%rbp), %ecx
addl $0x2, %ecx
cmpl %ecx, %eax
jge 0x7a315
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 0x6c80
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 0x63a0
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 0x7a3ad
movl -0x1008c(%rbp), %ecx
movq -0x10060(%rbp), %rdx
xorl %eax, %eax
cmpl 0x14(%rdx), %ecx
movb %al, -0x100c9(%rbp)
jl 0x7a3e0
movl -0x10088(%rbp), %eax
movq -0x10060(%rbp), %rcx
cmpl 0x18(%rcx), %eax
setle %al
movb %al, -0x100c9(%rbp)
movb -0x100c9(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x7a3ee
jmp 0x7a414
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 0x7a3ad
movq -0x10060(%rbp), %rax
movl (%rax), %eax
addl $0x1, %eax
movq -0x10060(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jl 0x7a497
movq -0x10060(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0x7a44d
movq -0x10060(%rbp), %rax
movl 0x4(%rax), %eax
shll %eax
movl %eax, -0x100d0(%rbp)
jmp 0x7a45a
movl $0x10, %eax
movl %eax, -0x100d0(%rbp)
jmp 0x7a45a
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 0x6c80
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 0x6570
movq -0x10070(%rbp), %rax
movq %rax, -0x10060(%rbp)
movl -0x100a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x100a8(%rbp)
cmpl -0x100a4(%rbp), %eax
jl 0x7a543
movl -0x100a4(%rbp), %eax
shll %eax
movl %eax, -0x100a4(%rbp)
movq -0x10068(%rbp), %rdi
movslq -0x100a4(%rbp), %rsi
shlq $0x3, %rsi
callq 0x6c80
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 0x7a592
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 0x7a59e
jmp 0x7a5b2
movq -0x100b0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x100b0(%rbp)
jmp 0x7a55c
movq -0x100b0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x100b0(%rbp)
movq -0x100b0(%rbp), %rax
cmpq -0x10050(%rbp), %rax
jb 0x7a016
movq -0x10068(%rbp), %rdi
callq 0x6880
movq -0x10040(%rbp), %rdi
callq 0x6880
movq -0x28(%rbp), %rdi
callq 0x6880
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100e0, %rsp          # imm = 0x100E0
popq %rbp
retq
nopl (%rax)