// File: /workspace/github_c_makefile_star2_O3/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, %ebp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x14(%rdi), %rax
cmpl $0x1, %edx
jne 0x715d
movl $0x1, %esi
movl $0x4, %edx
movq %rax, %rdi
movq %rax, %r15
movq %rbx, %rcx
callq 0x23b0
jmp 0x718a
movl $0x4, %edx
movq %rbx, %rdi
movq %rax, %rsi
movq %rax, %r15
callq 0x43d3
cmpl $0x2, %ebp
jne 0x718a
movl 0x10(%r14), %edx
testq %rdx, %rdx
je 0x718a
movq 0x38(%r14), %rsi
movq %rbx, %rdi
callq 0x43d3
cmpl $0x0, (%r15)
jle 0x73b8
xorl %eax, %eax
movq %r14, 0x30(%rsp)
movl %ebp, 0x8(%rsp)
movq %r15, 0x28(%rsp)
movq %rax, %rdx
movq 0x28(%r14), %rax
movq %rdx, 0x20(%rsp)
movq (%rax,%rdx,8), %rdx
movl $0x0, %eax
testq %rdx, %rdx
je 0x71c1
movl 0x4(%rdx), %eax
movq %rdx, 0x10(%rsp)
movq 0x20(%rsp), %rcx
shlq $0x4, %rcx
addq 0x30(%r14), %rcx
movq %rcx, 0x18(%rsp)
movl %eax, 0xc(%rsp)
cmpl $0x1, %ebp
jne 0x71fa
movl $0x1, %esi
movl $0x4, %edx
leaq 0xc(%rsp), %rdi
movq %rbx, %rcx
callq 0x23b0
jmp 0x720c
movl $0x4, %edx
movq %rbx, %rdi
leaq 0xc(%rsp), %rsi
callq 0x43d3
movq 0x10(%rsp), %rdx
testq %rdx, %rdx
je 0x732a
cmpl $0x0, (%rdx)
je 0x732a
xorl %r14d, %r14d
xorl %ebp, %ebp
movq 0x10(%rdx), %rax
movl %ebp, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %eax
movl %r14d, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x731c
movq %rbx, %r15
movl %ebp, %eax
leaq (%rax,%rax,2), %r13
leaq (,%r13,8), %r12
addq 0x20(%rdx), %r12
shlq $0x2, %rax
addq 0x18(%rdx), %rax
movl 0x8(%rsp), %ebx
cmpl $0x1, %ebx
jne 0x72b8
movl $0x1, %esi
movl $0x4, %edx
movq %rax, %rdi
movq %r15, %rbx
movq %r15, %rcx
callq 0x23b0
leaq 0x4(%r12), %rdi
movl $0x1, %esi
movl $0x4, %edx
movq %r15, %rcx
callq 0x23b0
movq 0x10(%r12), %rdi
movl 0x4(%r12), %eax
shll $0x4, %eax
movslq %eax, %rdx
movl $0x1, %esi
movq %r15, %rcx
callq 0x23b0
jmp 0x7317
movl $0x4, %edx
movq %r15, %rdi
movq %rax, %rsi
callq 0x43d3
testl %ebx, %ebx
movq %r15, %rbx
jne 0x72ed
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
leaq (%rax,%r13,8), %rsi
addq $0x8, %rsi
movl $0x8, %edx
movq %rbx, %rdi
callq 0x43d3
leaq 0x4(%r12), %rsi
movl $0x4, %edx
movq %rbx, %rdi
callq 0x43d3
movq 0x10(%r12), %rsi
movl 0x4(%r12), %eax
shll $0x4, %eax
movslq %eax, %rdx
movq %rbx, %rdi
callq 0x43d3
movq 0x10(%rsp), %rdx
incl %ebp
addl $0x2, %r14d
cmpl (%rdx), %ebp
jne 0x7228
movl 0x8(%rsp), %ebp
testl %ebp, %ebp
movq 0x30(%rsp), %r14
je 0x739c
cmpl $0x1, %ebp
jne 0x7372
movl $0x1, %esi
movl $0x4, %edx
movq 0x18(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rcx
callq 0x23b0
movq 0x8(%r15), %rdi
movl (%r15), %eax
shll $0x3, %eax
movslq %eax, %rdx
movl $0x1, %esi
movq %rbx, %rcx
callq 0x23b0
jmp 0x739c
movl $0x4, %edx
movq %rbx, %rdi
movq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x43d3
movq 0x8(%r15), %rsi
movl (%r15), %eax
shll $0x3, %eax
movslq %eax, %rdx
movq %rbx, %rdi
callq 0x43d3
movq 0x20(%rsp), %rdx
incq %rdx
movq 0x28(%rsp), %rcx
movslq (%rcx), %rax
cmpq %rax, %rdx
movq %rdx, %rax
jl 0x71a4
addq $0x20, %r14
cmpl $0x1, %ebp
jne 0x73d8
movl $0x1, %esi
movl $0x8, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x23b0
jmp 0x73e8
movl $0x8, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x43d3
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq