// File: karel-brinda[P]ococo/ext/htslib/hts.c
// Function: hts_idx_save_core
static int hts_idx_save_core(const hts_idx_t *idx, BGZF *fp, int fmt)
{
    int32_t i, j;

    #define check(ret) if ((ret) < 0) return -1

    check(idx_write_int32(fp, idx->n));
    if (fmt == HTS_FMT_TBI && idx->l_meta)
        check(bgzf_write(fp, idx->meta, idx->l_meta));

    for (i = 0; i < idx->n; ++i) {
        khint_t k;
        bidx_t *bidx = idx->bidx[i];
        lidx_t *lidx = &idx->lidx[i];
        // write binning index
        check(idx_write_int32(fp, bidx? kh_size(bidx) : 0));
        if (bidx)
            for (k = kh_begin(bidx); k != kh_end(bidx); ++k)
                if (kh_exist(bidx, k)) {
                    bins_t *p = &kh_value(bidx, k);
                    check(idx_write_uint32(fp, kh_key(bidx, k)));
                    if (fmt == HTS_FMT_CSI) check(idx_write_uint64(fp, p->loff));
                    //int j;for(j=0;j<p->n;++j)fprintf(stderr,"%d,%llx,%d,%llx:%llx\n",kh_key(bidx,k),kh_val(bidx, k).loff,j,p->list[j].u,p->list[j].v);
                    check(idx_write_int32(fp, p->n));
                    for (j = 0; j < p->n; ++j) {
                        check(idx_write_uint64(fp, p->list[j].u));
                        check(idx_write_uint64(fp, p->list[j].v));
                    }
                }

        // write linear index
        if (fmt != HTS_FMT_CSI) {
            check(idx_write_int32(fp, lidx->n));
            for (j = 0; j < lidx->n; ++j)
                check(idx_write_uint64(fp, lidx->offset[j]));
        }
    }

    check(idx_write_uint64(fp, idx->n_no_coor));
    return 0;
    #undef check
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x14(%rax), %esi
callq 0x17610
cmpq $0x0, %rax
jge 0x176c5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x17999
cmpl $0x2, -0x1c(%rbp)
jne 0x17703
movq -0x10(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0x17703
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %edx
callq 0x65700
cmpq $0x0, %rax
jge 0x17701
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x17999
jmp 0x17703
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x17972
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x20(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x17760
movq -0x30(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x4c(%rbp)
jmp 0x17767
xorl %eax, %eax
movl %eax, -0x4c(%rbp)
jmp 0x17767
movq -0x48(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x17610
cmpq $0x0, %rax
jge 0x17785
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x17999
cmpq $0x0, -0x30(%rbp)
je 0x178f2
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl (%rcx), %eax
je 0x178f0
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x28(%rbp), %ecx
shrl $0x4, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl -0x28(%rbp), %ecx
andl $0xf, %ecx
shll %ecx
shrl %cl, %eax
andl $0x3, %eax
cmpl $0x0, %eax
jne 0x178e0
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x28(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x28(%rbp), %ecx
movl (%rax,%rcx,4), %esi
callq 0x17650
cmpq $0x0, %rax
jge 0x1780e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x17999
cmpl $0x0, -0x1c(%rbp)
jne 0x17839
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x1a3f0
cmpq $0x0, %rax
jge 0x17837
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x17999
jmp 0x17839
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movl 0x4(%rax), %esi
callq 0x17610
cmpq $0x0, %rax
jge 0x1785b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x17999
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jge 0x178de
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
callq 0x1a3f0
cmpq $0x0, %rax
jge 0x1789f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x17999
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
callq 0x1a3f0
cmpq $0x0, %rax
jge 0x178d1
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x17999
jmp 0x178d3
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x17862
jmp 0x178e0
jmp 0x178e2
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x17797
jmp 0x178f2
cmpl $0x0, -0x1c(%rbp)
je 0x17962
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rax
movl (%rax), %esi
callq 0x17610
cmpq $0x0, %rax
jge 0x17919
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x17999
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x17960
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1a3f0
cmpq $0x0, %rax
jge 0x17953
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x17999
jmp 0x17955
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x17920
jmp 0x17962
jmp 0x17964
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1770a
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x1a3f0
cmpq $0x0, %rax
jge 0x17992
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x17999
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)