// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bam_index.c
// Function: bam_index_save
void bam_index_save(const bam_index_t *idx, FILE *fp)
{
	int32_t i, size;
	khint_t k;
	fwrite("BAI\1", 1, 4, fp);
	if (bam_is_be) {
		uint32_t x = idx->n;
		fwrite(bam_swap_endian_4p(&x), 4, 1, fp);
	} else fwrite(&idx->n, 4, 1, fp);
	for (i = 0; i < idx->n; ++i) {
		khash_t(i) *index = idx->index[i];
		bam_lidx_t *index2 = idx->index2 + i;
		// write binning index
		size = kh_size(index);
		if (bam_is_be) { // big endian
			uint32_t x = size;
			fwrite(bam_swap_endian_4p(&x), 4, 1, fp);
		} else fwrite(&size, 4, 1, fp);
		for (k = kh_begin(index); k != kh_end(index); ++k) {
			if (kh_exist(index, k)) {
				bam_binlist_t *p = &kh_value(index, k);
				if (bam_is_be) { // big endian
					uint32_t x;
					x = kh_key(index, k); fwrite(bam_swap_endian_4p(&x), 4, 1, fp);
					x = p->n; fwrite(bam_swap_endian_4p(&x), 4, 1, fp);
					for (x = 0; (int)x < p->n; ++x) {
						bam_swap_endian_8p(&p->list[x].u);
						bam_swap_endian_8p(&p->list[x].v);
					}
					fwrite(p->list, 16, p->n, fp);
					for (x = 0; (int)x < p->n; ++x) {
						bam_swap_endian_8p(&p->list[x].u);
						bam_swap_endian_8p(&p->list[x].v);
					}
				} else {
					fwrite(&kh_key(index, k), 4, 1, fp);
					fwrite(&p->n, 4, 1, fp);
					fwrite(p->list, 16, p->n, fp);
				}
			}
		}
		// write linear index (index2)
		if (bam_is_be) {
			int x = index2->n;
			fwrite(bam_swap_endian_4p(&x), 4, 1, fp);
		} else fwrite(&index2->n, 4, 1, fp);
		if (bam_is_be) { // big endian
			int x;
			for (x = 0; (int)x < index2->n; ++x)
				bam_swap_endian_8p(&index2->offset[x]);
			fwrite(index2->offset, 8, index2->n, fp);
			for (x = 0; (int)x < index2->n; ++x)
				bam_swap_endian_8p(&index2->offset[x]);
		} else fwrite(index2->offset, 8, index2->n, fp);
	}
	{ // write the number of reads coor-less records.
		uint64_t x = idx->n_no_coor;
		if (bam_is_be) bam_swap_endian_8p(&x);
		fwrite(&x, 8, 1, fp);
	}
	fflush(fp);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movq %rdi, %r12
leaq 0xc3fb(%rip), %rdi      # 0x1c3a2
movl $0x1, %esi
movl $0x4, %edx
movq %rbx, %rcx
callq 0x3540
leaq 0x13a90(%rip), %rax     # 0x23a50
cmpl $0x0, (%rax)
je 0xffde
movl (%r12), %eax
bswapl %eax
leaq 0x8(%rsp), %rdi
movl %eax, (%rdi)
movl $0x4, %esi
movl $0x1, %edx
jmp 0xffeb
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %rbx, %rcx
callq 0x3540
cmpl $0x0, (%r12)
jle 0x10297
xorl %r15d, %r15d
movq %r12, 0x18(%rsp)
movq 0x10(%r12), %rax
movq (%rax,%r15,8), %r14
movq %r15, %r13
shlq $0x4, %r13
addq 0x18(%r12), %r13
movl 0x4(%r14), %eax
movl %eax, 0x14(%rsp)
leaq 0x13a26(%rip), %rcx     # 0x23a50
cmpl $0x0, (%rcx)
movq %r15, 0x20(%rsp)
je 0x1004b
bswapl %eax
movl %eax, 0x8(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0x8(%rsp), %rdi
jmp 0x1005a
movl $0x4, %esi
movl $0x1, %edx
leaq 0x14(%rsp), %rdi
movq %rbx, %rcx
callq 0x3540
cmpl $0x0, (%r14)
je 0x101cd
xorl %ebp, %ebp
movq 0x10(%r14), %rax
movl %ebp, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %eax
leal (,%rbp,2), %ecx
shrl %cl, %eax
testb $0x3, %al
je 0x10093
incl %ebp
cmpl (%r14), %ebp
jne 0x1006e
jmp 0x101cd
movl %ebp, %edi
movq %rdi, %r12
shlq $0x4, %r12
addq 0x20(%r14), %r12
leaq 0x139a9(%rip), %rax     # 0x23a50
cmpl $0x0, (%rax)
je 0x10132
movq 0x18(%r14), %rax
movl (%rax,%rdi,4), %eax
bswapl %eax
movl %eax, 0x8(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0x8(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rcx
callq 0x3540
movl 0x4(%r12), %eax
bswapl %eax
movl %eax, 0x8(%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x3540
movl 0x4(%r12), %edx
movq 0x8(%r12), %rdi
testq %rdx, %rdx
je 0x1017f
movq %rdx, %rax
shlq $0x4, %rax
xorl %ecx, %ecx
movq (%rdi,%rcx), %rsi
movq 0x8(%rdi,%rcx), %r8
bswapq %rsi
bswapq %r8
movq %rsi, (%rdi,%rcx)
movq %r8, 0x8(%rdi,%rcx)
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x1010f
jmp 0x10181
shlq $0x2, %rdi
addq 0x18(%r14), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3540
leaq 0x4(%r12), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3540
movq 0x8(%r12), %rdi
movl 0x4(%r12), %edx
movl $0x10, %esi
movq %rbx, %rcx
callq 0x3540
jmp 0x10087
xorl %edx, %edx
movl $0x10, %esi
movq %rbx, %rcx
callq 0x3540
movl 0x4(%r12), %eax
testq %rax, %rax
je 0x10087
movq 0x8(%r12), %rcx
shlq $0x4, %rax
xorl %edx, %edx
movq (%rcx,%rdx), %rsi
movq 0x8(%rcx,%rdx), %rdi
bswapq %rsi
bswapq %rdi
movq %rsi, (%rcx,%rdx)
movq %rdi, 0x8(%rcx,%rdx)
addq $0x10, %rdx
cmpq %rdx, %rax
jne 0x101a7
jmp 0x10087
leaq 0x1387c(%rip), %rax     # 0x23a50
cmpl $0x0, (%rax)
je 0x101f4
movl (%r13), %eax
bswapl %eax
movl %eax, 0x8(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0x8(%rsp), %rdi
jmp 0x10201
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x3540
movq 0x18(%rsp), %r12
movq 0x20(%rsp), %r15
leaq 0x13836(%rip), %rax     # 0x23a50
cmpl $0x0, (%rax)
je 0x10272
movslq (%r13), %rdx
movq 0x8(%r13), %rdi
testq %rdx, %rdx
jle 0x10241
xorl %eax, %eax
movq (%rdi,%rax,8), %rcx
bswapq %rcx
movq %rcx, (%rdi,%rax,8)
incq %rax
cmpq %rax, %rdx
jne 0x1022e
movl $0x8, %esi
movq %rbx, %rcx
callq 0x3540
movslq (%r13), %rax
testq %rax, %rax
jle 0x10287
movq 0x8(%r13), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
bswapq %rsi
movq %rsi, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x1025d
jmp 0x10287
movq 0x8(%r13), %rdi
movslq (%r13), %rdx
movl $0x8, %esi
movq %rbx, %rcx
callq 0x3540
incq %r15
movslq (%r12), %rax
cmpq %rax, %r15
jl 0x10006
movq 0x8(%r12), %rax
movq %rax, 0x8(%rsp)
leaq 0x137a8(%rip), %rcx     # 0x23a50
cmpl $0x0, (%rcx)
je 0x102b5
bswapq %rax
movq %rax, 0x8(%rsp)
leaq 0x8(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3540
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x33f0