// File: /workspace/github_c_makefile_star2_O1/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 0xb694(%rip), %rdi      # 0x1b292
movl $0x1, %esi
movl $0x4, %edx
movq %rbx, %rcx
callq 0x3540
leaq 0x12e39(%rip), %rax     # 0x22a50
cmpl $0x0, (%rax)
je 0xfc35
movl (%r12), %eax
bswapl %eax
leaq 0x8(%rsp), %rdi
movl %eax, (%rdi)
movl $0x4, %esi
movl $0x1, %edx
jmp 0xfc42
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %rbx, %rcx
callq 0x3540
cmpl $0x0, (%r12)
jle 0xfef2
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 0x12dcf(%rip), %rcx     # 0x22a50
cmpl $0x0, (%rcx)
movq %r15, 0x20(%rsp)
je 0xfca2
bswapl %eax
movl %eax, 0x8(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0x8(%rsp), %rdi
jmp 0xfcb1
movl $0x4, %esi
movl $0x1, %edx
leaq 0x14(%rsp), %rdi
movq %rbx, %rcx
callq 0x3540
cmpl $0x0, (%r14)
je 0xfe24
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 0xfcea
incl %ebp
cmpl (%r14), %ebp
jne 0xfcc5
jmp 0xfe24
movl %ebp, %edi
movq %rdi, %r12
shlq $0x4, %r12
addq 0x20(%r14), %r12
leaq 0x12d52(%rip), %rax     # 0x22a50
cmpl $0x0, (%rax)
je 0xfd81
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
testq %rdx, %rdx
je 0xfdce
movq 0x8(%r12), %rcx
addq $0x8, %rcx
xorl %eax, %eax
movq -0x8(%rcx), %rsi
bswapq %rsi
movq (%rcx), %rdi
movq %rsi, -0x8(%rcx)
bswapq %rdi
movq %rdi, (%rcx)
incq %rax
addq $0x10, %rcx
cmpq %rax, %rdx
jne 0xfd5f
jmp 0xfdd0
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 0xfcde
xorl %eax, %eax
movl %eax, 0x8(%rsp)
movq 0x8(%r12), %rdi
movl $0x10, %esi
movq %rbx, %rcx
callq 0x3540
movl 0x4(%r12), %eax
testq %rax, %rax
je 0xfcde
movq 0x8(%r12), %rcx
addq $0x8, %rcx
xorl %edx, %edx
movq -0x8(%rcx), %rsi
bswapq %rsi
movq (%rcx), %rdi
movq %rsi, -0x8(%rcx)
bswapq %rdi
movq %rdi, (%rcx)
incq %rdx
addq $0x10, %rcx
cmpq %rdx, %rax
jne 0xfdff
jmp 0xfcde
leaq 0x12c25(%rip), %rax     # 0x22a50
cmpl $0x0, (%rax)
je 0xfe4b
movl (%r13), %eax
bswapl %eax
movl %eax, 0x8(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0x8(%rsp), %rdi
jmp 0xfe58
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x3540
movq 0x18(%rsp), %r12
movq 0x20(%rsp), %r15
leaq 0x12bdf(%rip), %rax     # 0x22a50
cmpl $0x0, (%rax)
je 0xfecd
movslq (%r13), %rdx
testq %rdx, %rdx
jle 0xfe98
movq 0x8(%r13), %rax
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rsi
bswapq %rsi
movq %rsi, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %rdx
jne 0xfe85
movq 0x8(%r13), %rdi
movl $0x8, %esi
movq %rbx, %rcx
callq 0x3540
movslq (%r13), %rax
testq %rax, %rax
jle 0xfee2
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 0xfeb8
jmp 0xfee2
movq 0x8(%r13), %rdi
movslq (%r13), %rdx
movl $0x8, %esi
movq %rbx, %rcx
callq 0x3540
incq %r15
movslq (%r12), %rax
cmpq %rax, %r15
jl 0xfc5d
movq 0x8(%r12), %rax
movq %rax, 0x8(%rsp)
leaq 0x12b4d(%rip), %rcx     # 0x22a50
cmpl $0x0, (%rcx)
je 0xff10
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