// File: karel-brinda[P]ococo/ext/htslib/cram/cram_index.c
// Function: cram_index_build
int cram_index_build(cram_fd *fd, const char *fn_base, const char *fn_idx) {
    cram_container *c;
    off_t cpos, spos, hpos;
    zfp *fp;
    kstring_t fn_idx_str = {0};

    if (! fn_idx) {
        kputs(fn_base, &fn_idx_str);
        kputs(".crai", &fn_idx_str);
        fn_idx = fn_idx_str.s;
    }

    if (!(fp = zfopen(fn_idx, "wz"))) {
        perror(fn_idx);
        free(fn_idx_str.s);
        return -4;
    }

    free(fn_idx_str.s);

    cpos = htell(fd->fp);
    while ((c = cram_read_container(fd))) {
        int j;

        if (fd->err) {
            perror("Cram container read");
            return -1;
        }

        hpos = htell(fd->fp);

        if (!(c->comp_hdr_block = cram_read_block(fd)))
            return -1;
        assert(c->comp_hdr_block->content_type == COMPRESSION_HEADER);

        c->comp_hdr = cram_decode_compression_header(fd, c->comp_hdr_block);
        if (!c->comp_hdr)
            return -1;

        // 2.0 format
        for (j = 0; j < c->num_landmarks; j++) {
            char buf[1024];
            cram_slice *s;
            int sz;

            spos = htell(fd->fp);
            assert(spos - cpos - c->offset == c->landmark[j]);

            if (!(s = cram_read_slice(fd))) {
		zfclose(fp);
		return -1;
	    }

            sz = (int)(htell(fd->fp) - spos);

	    if (s->hdr->ref_seq_id == -2) {
		cram_index_build_multiref(fd, c, s, fp,
					  cpos, c->landmark[j], sz);
	    } else {
		sprintf(buf, "%d\t%d\t%d\t%"PRId64"\t%d\t%d\n",
			s->hdr->ref_seq_id, s->hdr->ref_seq_start,
			s->hdr->ref_seq_span, (int64_t)cpos,
			c->landmark[j], sz);
		zfputs(buf, fp);
	    }

            cram_free_slice(s);
        }

        cpos = htell(fd->fp);
        assert(cpos == hpos + c->length);

        cram_free_container(c);
    }
    if (fd->err) {
	zfclose(fp);
	return -1;
    }
	

    return (zfclose(fp) >= 0)? 0 : -4;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x490, %rsp            # imm = 0x490
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x42d0
cmpq $0x0, -0x20(%rbp)
jne 0x42873
movq -0x18(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x42bb0
leaq 0x39fe7(%rip), %rdi     # 0x7c849
leaq -0x60(%rbp), %rsi
callq 0x42bb0
movq -0x50(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x39ff1(%rip), %rsi     # 0x7c86f
callq 0x61ca0
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x428ab
movq -0x20(%rbp), %rdi
callq 0x4480
movq -0x50(%rbp), %rdi
callq 0x46a0
movl $0xfffffffc, -0x4(%rbp) # imm = 0xFFFFFFFC
jmp 0x42b9d
movq -0x50(%rbp), %rdi
callq 0x46a0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x42bf0
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
callq 0x4bc50
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x42b66
movq -0x10(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x428fd
leaq 0x39f86(%rip), %rdi     # 0x7c872
callq 0x4480
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x42b9d
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x42bf0
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
callq 0x45bc0
movq -0x28(%rbp), %rcx
movq %rax, 0x48(%rcx)
cmpq $0x0, %rax
jne 0x42930
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x42b9d
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
cmpl $0x1, 0x8(%rax)
jne 0x42940
jmp 0x4295f
leaq 0x39f3f(%rip), %rdi     # 0x7c886
leaq 0x39f6e(%rip), %rsi     # 0x7c8bc
movl $0x215, %edx            # imm = 0x215
leaq 0x39f74(%rip), %rcx     # 0x7c8ce
callq 0x4360
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0x24750
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x42992
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x42b9d
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jge 0x42b10
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x42bf0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
subq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
subq 0x38(%rcx), %rax
movq -0x28(%rbp), %rcx
movq 0x30(%rcx), %rcx
movslq -0x64(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
cmpq %rcx, %rax
jne 0x429e0
jmp 0x429ff
leaq 0x39f23(%rip), %rdi     # 0x7c90a
leaq 0x39ece(%rip), %rsi     # 0x7c8bc
movl $0x222, %edx            # imm = 0x222
leaq 0x39ed4(%rip), %rcx     # 0x7c8ce
callq 0x4360
movq -0x10(%rbp), %rdi
callq 0x4f920
movq %rax, -0x478(%rbp)
cmpq $0x0, %rax
jne 0x42a2a
movq -0x48(%rbp), %rdi
callq 0x61e90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x42b9d
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x42bf0
subq -0x38(%rbp), %rax
movl %eax, -0x47c(%rbp)
movq -0x478(%rbp), %rax
movq (%rax), %rax
cmpl $-0x2, 0x4(%rax)
jne 0x42a87
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x478(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x64(%rbp), %r9
movl (%rax,%r9,4), %r9d
movl -0x47c(%rbp), %eax
movl %eax, (%rsp)
callq 0x42c20
jmp 0x42af6
leaq -0x470(%rbp), %rdi
movq -0x478(%rbp), %rax
movq (%rax), %rax
movl 0x4(%rax), %edx
movq -0x478(%rbp), %rax
movq (%rax), %rax
movl 0x8(%rax), %ecx
movq -0x478(%rbp), %rax
movq (%rax), %rax
movl 0xc(%rax), %r8d
movq -0x30(%rbp), %r9
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x64(%rbp), %rsi
movl (%rax,%rsi,4), %r10d
movl -0x47c(%rbp), %eax
leaq 0x39e5d(%rip), %rsi     # 0x7c934
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x4100
leaq -0x470(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x61b30
movq -0x478(%rbp), %rdi
callq 0x4b920
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x42999
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x42bf0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rdx
movslq (%rdx), %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jne 0x42b39
jmp 0x42b58
leaq 0x39e08(%rip), %rdi     # 0x7c948
leaq 0x39d75(%rip), %rsi     # 0x7c8bc
movl $0x23a, %edx            # imm = 0x23A
leaq 0x39d7b(%rip), %rcx     # 0x7c8ce
callq 0x4360
movq -0x28(%rbp), %rdi
callq 0x4b5e0
jmp 0x428c4
movq -0x10(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x42b82
movq -0x48(%rbp), %rdi
callq 0x61e90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x42b9d
movq -0x48(%rbp), %rdi
callq 0x61e90
movl %eax, %edx
movl $0xfffffffc, %eax       # imm = 0xFFFFFFFC
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovgel %ecx, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x490, %rsp            # imm = 0x490
popq %rbp
retq
nopl (%rax)