// File: /workspace/github_cpp_makefile_star2_O3/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/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);
	}
	fflush(fp);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r12
leaq 0xf8c1(%rip), %rdi      # 0x25a51
movl $0x1, %esi
movl $0x4, %edx
movq %rbx, %rcx
callq 0x3600
leaq 0x19177(%rip), %rax     # 0x2f320
cmpl $0x0, (%rax)
je 0x161c7
movl (%r12), %eax
bswapl %eax
leaq 0x4(%rsp), %rdi
movl %eax, (%rdi)
movl $0x4, %esi
movl $0x1, %edx
jmp 0x161d4
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %rbx, %rcx
callq 0x3600
cmpl $0x0, (%r12)
jle 0x16472
xorl %r15d, %r15d
movq %r12, 0x8(%rsp)
movq 0x8(%r12), %rax
movq (%rax,%r15,8), %r14
movq %r15, %r13
shlq $0x4, %r13
addq 0x10(%r12), %r13
movl 0x4(%r14), %eax
movl %eax, 0x4(%rsp)
leaq 0x1910d(%rip), %rcx     # 0x2f320
cmpl $0x0, (%rcx)
movq %r15, 0x10(%rsp)
je 0x16231
bswapl %eax
movl %eax, (%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %rsp, %rdi
jmp 0x16240
movl $0x4, %esi
movl $0x1, %edx
leaq 0x4(%rsp), %rdi
movq %rbx, %rcx
callq 0x3600
cmpl $0x0, (%r14)
je 0x163ab
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 0x16279
incl %ebp
cmpl (%r14), %ebp
jne 0x16254
jmp 0x163ab
movl %ebp, %edi
movq %rdi, %r12
shlq $0x4, %r12
addq 0x20(%r14), %r12
leaq 0x19093(%rip), %rax     # 0x2f320
cmpl $0x0, (%rax)
je 0x16310
movq 0x18(%r14), %rax
movl (%rax,%rdi,4), %eax
bswapl %eax
movl %eax, (%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %rsp, %r15
movq %r15, %rdi
movq %rbx, %rcx
callq 0x3600
movl 0x4(%r12), %eax
bswapl %eax
movl %eax, (%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x3600
movl 0x4(%r12), %edx
movq 0x8(%r12), %rdi
testq %rdx, %rdx
je 0x1635d
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 0x162ed
jmp 0x1635f
shlq $0x2, %rdi
addq 0x18(%r14), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3600
leaq 0x4(%r12), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3600
movq 0x8(%r12), %rdi
movl 0x4(%r12), %edx
movl $0x10, %esi
movq %rbx, %rcx
callq 0x3600
jmp 0x1626d
xorl %edx, %edx
movl $0x10, %esi
movq %rbx, %rcx
callq 0x3600
movl 0x4(%r12), %eax
testq %rax, %rax
je 0x1626d
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 0x16385
jmp 0x1626d
leaq 0x18f6e(%rip), %rax     # 0x2f320
cmpl $0x0, (%rax)
je 0x163cf
movl (%r13), %eax
bswapl %eax
movl %eax, (%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %rsp, %rdi
jmp 0x163dc
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x3600
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %r15
leaq 0x18f2b(%rip), %rax     # 0x2f320
cmpl $0x0, (%rax)
je 0x1644d
movslq (%r13), %rdx
movq 0x8(%r13), %rdi
testq %rdx, %rdx
jle 0x1641c
xorl %eax, %eax
movq (%rdi,%rax,8), %rcx
bswapq %rcx
movq %rcx, (%rdi,%rax,8)
incq %rax
cmpq %rax, %rdx
jne 0x16409
movl $0x8, %esi
movq %rbx, %rcx
callq 0x3600
movslq (%r13), %rax
testq %rax, %rax
jle 0x16462
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 0x16438
jmp 0x16462
movq 0x8(%r13), %rdi
movslq (%r13), %rdx
movl $0x8, %esi
movq %rbx, %rcx
callq 0x3600
incq %r15
movslq (%r12), %rax
cmpq %rax, %r15
jl 0x161ef
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3690