// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/hts.c
// Function: idx_save_core
static int idx_save_core(const hts_idx_t *idx, BGZF *fp, int fmt)
{
    int32_t i, j;

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

    // VCF TBI/CSI only writes IDs for non-empty bins (ie covered references)
    //
    // NOTE: CSI meta is undefined in spec, so this code has an assumption
    // that we're only using it for Tabix data.
    int nids = idx->n;
    if (idx->meta && idx->l_meta >= 4 && le_to_u32(idx->meta) == TBX_VCF) {
        for (i = nids = 0; i < idx->n; ++i) {
            if (idx->bidx[i])
                nids++;
        }
    }
    check(idx_write_int32(fp, nids));
    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
        if (nids == idx->n || bidx)
            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) {
                        //fprintf(stderr, "\t%ld\t%ld\n", p->list[j].u, p->list[j].v);
                        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]));
        }
    }

    if (!need_idx_ugly_delay_hack(idx)) {
        // Write this for compressed (CSI) indexes, but for BAI we
        // need to save a bit for later.  See hts_idx_close_otf_fp()
        check(idx_write_uint64(fp, idx->n_no_coor));
    }

#ifdef DEBUG_INDEX
    idx_dump(idx);
#endif

    return 0;
    #undef check
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x281d9
movq -0x10(%rbp), %rax
cmpl $0x4, 0x10(%rax)
jb 0x281d9
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x202b0
cmpl $0x2, %eax
jne 0x281d9
movl $0x0, -0x28(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x281d7
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x20(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x281ca
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x281cc
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x281a2
jmp 0x281d9
movq -0x18(%rbp), %rdi
movl -0x28(%rbp), %esi
callq 0x280d0
cmpq $0x0, %rax
jge 0x281f7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x284f5
cmpl $0x2, -0x1c(%rbp)
jne 0x28235
movq -0x10(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0x28235
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %edx
callq 0xffb0
cmpq $0x0, %rax
jge 0x28233
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x284f5
jmp 0x28235
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x284be
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x20(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movl -0x28(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
je 0x2828a
cmpq $0x0, -0x38(%rbp)
je 0x282cc
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x282a5
movq -0x38(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x54(%rbp)
jmp 0x282ac
xorl %eax, %eax
movl %eax, -0x54(%rbp)
jmp 0x282ac
movq -0x50(%rbp), %rdi
movl -0x54(%rbp), %esi
callq 0x280d0
cmpq $0x0, %rax
jge 0x282ca
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x284f5
jmp 0x282cc
cmpq $0x0, -0x38(%rbp)
je 0x28439
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl (%rcx), %eax
je 0x28437
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x2c(%rbp), %ecx
shrl $0x4, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl -0x2c(%rbp), %ecx
andl $0xf, %ecx
shll %ecx
shrl %cl, %eax
andl $0x3, %eax
cmpl $0x0, %eax
jne 0x28427
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %esi
callq 0x28110
cmpq $0x0, %rax
jge 0x28355
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x284f5
cmpl $0x0, -0x1c(%rbp)
jne 0x28380
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x28540
cmpq $0x0, %rax
jge 0x2837e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x284f5
jmp 0x28380
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rax
movl 0x4(%rax), %esi
callq 0x280d0
cmpq $0x0, %rax
jge 0x283a2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x284f5
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jge 0x28425
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
callq 0x28540
cmpq $0x0, %rax
jge 0x283e6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x284f5
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
callq 0x28540
cmpq $0x0, %rax
jge 0x28418
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x284f5
jmp 0x2841a
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x283a9
jmp 0x28427
jmp 0x28429
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x282de
jmp 0x28439
cmpl $0x0, -0x1c(%rbp)
je 0x284ae
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movq (%rax), %rax
movl %eax, %esi
callq 0x280d0
cmpq $0x0, %rax
jge 0x28463
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x284f5
movl $0x0, -0x24(%rbp)
movslq -0x24(%rbp), %rax
movq -0x40(%rbp), %rcx
cmpq (%rcx), %rax
jge 0x284ac
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x28540
cmpq $0x0, %rax
jge 0x2849f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x284f5
jmp 0x284a1
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x2846a
jmp 0x284ae
jmp 0x284b0
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x2823c
movq -0x10(%rbp), %rdi
callq 0x28580
cmpl $0x0, %eax
jne 0x284ee
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x28540
cmpq $0x0, %rax
jge 0x284ec
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x284f5
jmp 0x284ee
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop