// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rsi, %rbp
movq %rdi, %r14
leaq 0x14(%rdi), %r15
movl %edx, (%rsp)
cmpl $0x1, %edx
jne 0x6f3d
movl $0x1, %esi
movl $0x4, %edx
movq %r15, %rdi
movq %rbp, %rcx
callq 0x23b0
jmp 0x6f4d
movl $0x4, %edx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x4326
movl (%rsp), %ecx
cmpl $0x2, %ecx
jne 0x6f87
movl 0x10(%r14), %edx
testq %rdx, %rdx
je 0x6f87
movq 0x38(%r14), %rax
cmpl $0x1, %ecx
jne 0x6f79
movl $0x1, %esi
movq %rax, %rdi
movq %rbp, %rcx
callq 0x23b0
jmp 0x6f84
movq %rbp, %rdi
movq %rax, %rsi
callq 0x4326
movl (%rsp), %ecx
cmpl $0x0, (%r15)
jle 0x719e
xorl %r12d, %r12d
movq %r14, 0x18(%rsp)
movq %r15, 0x10(%rsp)
movq 0x28(%r14), %rax
movq (%rax,%r12,8), %r15
movl $0x0, %eax
testq %r15, %r15
je 0x6fb4
movl 0x4(%r15), %eax
movq %r12, %rdx
shlq $0x4, %rdx
addq 0x30(%r14), %rdx
movl %eax, 0x4(%rsp)
cmpl $0x1, %ecx
movq %r12, 0x20(%rsp)
movq %rdx, 0x8(%rsp)
jne 0x6feb
movl $0x1, %esi
movl $0x4, %edx
leaq 0x4(%rsp), %rdi
movq %rbp, %rcx
callq 0x23b0
jmp 0x6ffd
movl $0x4, %edx
movq %rbp, %rdi
leaq 0x4(%rsp), %rsi
callq 0x4326
testq %r15, %r15
je 0x710f
cmpl $0x0, (%r15)
je 0x710f
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x10(%r15), %rax
movl %r12d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %eax
movl %r14d, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x70ff
movl %r12d, %eax
leaq (%rax,%rax,2), %rbx
leaq (,%rbx,8), %r13
addq 0x20(%r15), %r13
shlq $0x2, %rax
addq 0x18(%r15), %rax
cmpl $0x1, (%rsp)
jne 0x706b
movl $0x1, %esi
movl $0x4, %edx
movq %rax, %rdi
movq %rbp, %rcx
callq 0x23b0
jmp 0x707b
movl $0x4, %edx
movq %rbp, %rdi
movq %rax, %rsi
callq 0x4326
movl (%rsp), %ecx
testl %ecx, %ecx
jne 0x709e
movq 0x20(%r15), %rax
leaq (%rax,%rbx,8), %rsi
addq $0x8, %rsi
movl $0x8, %edx
movq %rbp, %rdi
callq 0x4326
movl (%rsp), %ecx
leaq 0x4(%r13), %rax
cmpl $0x1, %ecx
jne 0x70be
movl $0x1, %esi
movl $0x4, %edx
movq %rax, %rdi
movq %rbp, %rcx
callq 0x23b0
jmp 0x70ce
movl $0x4, %edx
movq %rbp, %rdi
movq %rax, %rsi
callq 0x4326
movl 0x4(%r13), %ecx
shll $0x4, %ecx
movq 0x10(%r13), %rax
movslq %ecx, %rdx
cmpl $0x1, (%rsp)
jne 0x70f4
movl $0x1, %esi
movq %rax, %rdi
movq %rbp, %rcx
callq 0x23b0
jmp 0x70ff
movq %rbp, %rdi
movq %rax, %rsi
callq 0x4326
incl %r12d
addl $0x2, %r14d
cmpl (%r15), %r12d
jne 0x7016
movl (%rsp), %ecx
testl %ecx, %ecx
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %r15
movq 0x20(%rsp), %r12
je 0x718f
cmpl $0x1, %ecx
jne 0x7146
movl $0x1, %esi
movl $0x4, %edx
movq 0x8(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rcx
callq 0x23b0
jmp 0x715b
movl $0x4, %edx
movq %rbp, %rdi
movq 0x8(%rsp), %r13
movq %r13, %rsi
callq 0x4326
movl (%r13), %ecx
shll $0x3, %ecx
movq 0x8(%r13), %rax
movslq %ecx, %rdx
cmpl $0x1, (%rsp)
jne 0x7181
movl $0x1, %esi
movq %rax, %rdi
movq %rbp, %rcx
callq 0x23b0
jmp 0x718c
movq %rbp, %rdi
movq %rax, %rsi
callq 0x4326
movl (%rsp), %ecx
incq %r12
movslq (%r15), %rax
cmpq %rax, %r12
jl 0x6f9e
addq $0x20, %r14
cmpl $0x1, %ecx
jne 0x71be
movl $0x1, %esi
movl $0x8, %edx
movq %r14, %rdi
movq %rbp, %rcx
callq 0x23b0
jmp 0x71ce
movl $0x8, %edx
movq %rbp, %rdi
movq %r14, %rsi
callq 0x4326
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq