// File: /workspace/github_c_makefile_star2_O2/DReichLab[P]adna/hts.c
// Function: hts_idx_save_core
static void hts_idx_save_core(const hts_idx_t *idx, void *fp, int fmt)
{
	int32_t i, size, is_be;
	int is_bgzf = (fmt != HTS_FMT_BAI);
	is_be = ed_is_big();
	if (is_be) {
		uint32_t x = idx->n;
		idx_write(is_bgzf, fp, ed_swap_4p(&x), 4);
	} else idx_write(is_bgzf, fp, &idx->n, 4);
	if (fmt == HTS_FMT_TBI && idx->l_meta) idx_write(is_bgzf, 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
		size = bidx? kh_size(bidx) : 0;
		if (is_be) { // big endian
			uint32_t x = size;
			idx_write(is_bgzf, fp, ed_swap_4p(&x), 4);
		} else idx_write(is_bgzf, fp, &size, 4);
		if (bidx == 0) goto write_lidx;
		for (k = kh_begin(bidx); k != kh_end(bidx); ++k) {
			hts_bin_t *p;
			if (!kh_exist(bidx, k)) continue;
			p = &kh_value(bidx, k);
			if (is_be) { // big endian
				uint32_t x;
				x = kh_key(bidx, k); idx_write(is_bgzf, fp, ed_swap_4p(&x), 4);
				if (fmt == HTS_FMT_CSI) {
					uint64_t y = kh_val(bidx, k).loff;
					idx_write(is_bgzf, fp, ed_swap_4p(&y), 8);
				}
				x = p->n; idx_write(is_bgzf, fp, ed_swap_4p(&x), 4);
				swap_bins(p);
				idx_write(is_bgzf, fp, p->list, 16 * p->n);
				swap_bins(p);
			} else {
				idx_write(is_bgzf, fp, &kh_key(bidx, k), 4);
				if (fmt == HTS_FMT_CSI) idx_write(is_bgzf, fp, &kh_val(bidx, k).loff, 8);
				//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);
				idx_write(is_bgzf, fp, &p->n, 4);
				idx_write(is_bgzf, fp, p->list, p->n << 4);
			}
		}
write_lidx:
		if (fmt != HTS_FMT_CSI) {
			if (is_be) {
				int32_t x = lidx->n;
				idx_write(is_bgzf, fp, ed_swap_4p(&x), 4);
				for (x = 0; x < lidx->n; ++x) ed_swap_8p(&lidx->offset[x]);
				idx_write(is_bgzf, fp, lidx->offset, lidx->n << 3);
				for (x = 0; x < lidx->n; ++x) ed_swap_8p(&lidx->offset[x]);
			} else {
				idx_write(is_bgzf, fp, &lidx->n, 4);
				idx_write(is_bgzf, fp, lidx->offset, lidx->n << 3);
			}
		}
	}
	if (is_be) { // write the number of reads without coordinates
		uint64_t x = idx->n_no_coor;
		idx_write(is_bgzf, fp, &x, 8);
	} else idx_write(is_bgzf, fp, &idx->n_no_coor, 8);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %r14d
movq %rsi, %rbx
movq %rdi, %r15
xorl %ebp, %ebp
cmpl $0x1, %edx
setne %bpl
leaq 0x14(%rdi), %rdx
pushq $0x4
popq %rcx
movl %ebp, %edi
movq %rdx, 0x18(%rsp)
callq 0x7a89
movl %r14d, 0xc(%rsp)
cmpl $0x2, %r14d
jne 0x661d
movl 0x10(%r15), %ecx
testq %rcx, %rcx
je 0x661d
movq 0x38(%r15), %rdx
movl %ebp, %edi
movq %rbx, %rsi
callq 0x7a89
xorl %ecx, %ecx
pushq $0x4
popq %r12
movq %r15, 0x10(%rsp)
movq 0x18(%rsp), %rax
movslq (%rax), %rax
cmpq %rax, %rcx
jge 0x6783
movq 0x28(%r15), %rax
movq (%rax,%rcx,8), %r14
movq %rcx, 0x28(%rsp)
shlq $0x4, %rcx
addq 0x30(%r15), %rcx
movq %rcx, 0x20(%rsp)
testq %r14, %r14
je 0x6723
movl 0x4(%r14), %eax
movl %eax, 0x8(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
leaq 0x8(%rsp), %rdx
movq %r12, %rcx
callq 0x7a89
xorl %r15d, %r15d
xorl %r13d, %r13d
cmpl (%r14), %r13d
je 0x673a
movq 0x10(%r14), %rax
movl %r13d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %eax
movl %r15d, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x6717
movl %r13d, %edx
imulq $0x18, %rdx, %rcx
movq 0x20(%r14), %r12
movq %rcx, 0x30(%rsp)
addq %rcx, %r12
shlq $0x2, %rdx
addq 0x18(%r14), %rdx
movl %ebp, %edi
movq %rbx, %rsi
pushq $0x4
popq %rcx
callq 0x7a89
cmpl $0x0, 0xc(%rsp)
jne 0x66eb
movq 0x20(%r14), %rax
movq 0x30(%rsp), %rcx
leaq (%rax,%rcx), %rdx
addq $0x8, %rdx
movl %ebp, %edi
movq %rbx, %rsi
pushq $0x8
popq %rcx
callq 0x7a89
leaq 0x4(%r12), %rdx
movl %ebp, %edi
movq %rbx, %rsi
pushq $0x4
popq %rcx
callq 0x7a89
movq 0x10(%r12), %rdx
movl 0x4(%r12), %eax
shll $0x4, %eax
movslq %eax, %rcx
movl %ebp, %edi
movq %rbx, %rsi
callq 0x7a89
incl %r13d
addl $0x2, %r15d
jmp 0x667c
andl $0x0, 0x8(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
leaq 0x8(%rsp), %rdx
movq %r12, %rcx
callq 0x7a89
cmpl $0x0, 0xc(%rsp)
pushq $0x4
popq %r12
je 0x6771
movl %ebp, %edi
movq %rbx, %rsi
movq 0x20(%rsp), %r14
movq %r14, %rdx
movq %r12, %rcx
callq 0x7a89
movq 0x8(%r14), %rdx
movl (%r14), %eax
shll $0x3, %eax
movslq %eax, %rcx
movl %ebp, %edi
movq %rbx, %rsi
callq 0x7a89
movq 0x28(%rsp), %rcx
incq %rcx
movq 0x10(%rsp), %r15
jmp 0x6628
addq $0x20, %r15
pushq $0x8
popq %rcx
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x7a89
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq