// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/samtools-0.1.18/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 $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r15
leaq 0xc1ca5(%rip), %rdi     # 0x35b195
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
movq %rbx, %rcx
callq 0x32760
leaq 0x17be7b(%rip), %rax    # 0x415380
cmpl $0x0, (%rax)
je 0x29951e
movl (%r15), %eax
bswapl %eax
leaq 0x8(%rsp), %rdi
movl %eax, (%rdi)
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
jmp 0x299527
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x32760
xorl %ebp, %ebp
movq %r15, 0x18(%rsp)
movslq (%r15), %rax
cmpq %rax, %rbp
jge 0x2997b4
movq 0x10(%r15), %rax
movq (%rax,%rbp,8), %r14
movq %rbp, 0x28(%rsp)
shlq $0x4, %rbp
addq 0x18(%r15), %rbp
movl 0x4(%r14), %eax
movl %eax, 0x14(%rsp)
leaq 0x17be1a(%rip), %rcx    # 0x415380
cmpl $0x0, (%rcx)
je 0x299578
bswapl %eax
movl %eax, 0x8(%rsp)
leaq 0x8(%rsp), %rdi
jmp 0x29957d
leaq 0x14(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x32760
xorl %r15d, %r15d
movq %r14, 0x20(%rsp)
movq %rbp, 0x30(%rsp)
cmpl (%r14), %r15d
je 0x2996fa
movq 0x10(%r14), %rax
movl %r15d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %eax
leal (%r15,%r15), %ecx
shrl %cl, %eax
testb $0x3, %al
je 0x2995bd
incl %r15d
jmp 0x299598
movl %r15d, %edi
movq %rdi, %r12
shlq $0x4, %r12
addq 0x20(%r14), %r12
leaq 0x17bdae(%rip), %rax    # 0x415380
cmpl $0x0, (%rax)
je 0x2996aa
movq 0x18(%r14), %rax
movl (%rax,%rdi,4), %eax
bswapl %eax
movl %eax, 0x8(%rsp)
movq %rbx, %rbp
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
pushq $0x4
popq %r13
movq %r13, %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
movq %rbp, %rcx
callq 0x32760
movl 0x4(%r12), %eax
bswapl %eax
movl %eax, 0x8(%rsp)
movq %rbx, %rdi
movq %rbp, %rbx
movq %r13, %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x32760
movl 0x4(%r12), %edx
movq 0x8(%r12), %rdi
movq %rdx, %rax
shlq $0x4, %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x29965e
movq (%rdi,%rcx), %rsi
movq 0x8(%rdi,%rcx), %r8
bswapq %rsi
movq %rsi, (%rdi,%rcx)
bswapq %r8
movq %r8, 0x8(%rdi,%rcx)
addq $0x10, %rcx
jmp 0x29963b
pushq $0x10
popq %rsi
movq %rbx, %rcx
callq 0x32760
movl 0x4(%r12), %eax
shlq $0x4, %rax
xorl %ecx, %ecx
movq 0x30(%rsp), %rbp
movq 0x20(%rsp), %r14
cmpq %rcx, %rax
je 0x2995b8
movq 0x8(%r12), %rdx
movq (%rdx,%rcx), %rsi
movq 0x8(%rdx,%rcx), %rdi
bswapq %rsi
movq %rsi, (%rdx,%rcx)
bswapq %rdi
movq %rdi, 0x8(%rdx,%rcx)
addq $0x10, %rcx
jmp 0x29967e
shlq $0x2, %rdi
addq 0x18(%r14), %rdi
pushq $0x4
popq %r13
movq %r13, %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
movq %rbx, %rcx
callq 0x32760
leaq 0x4(%r12), %rdi
movq %r13, %rsi
movq %r14, %rdx
movq 0x20(%rsp), %r14
movq %rbx, %rcx
callq 0x32760
movq 0x8(%r12), %rdi
movl 0x4(%r12), %edx
pushq $0x10
popq %rsi
movq %rbx, %rcx
callq 0x32760
jmp 0x2995b8
leaq 0x17bc7f(%rip), %rax    # 0x415380
cmpl $0x0, (%rax)
je 0x299716
movl (%rbp), %eax
bswapl %eax
movl %eax, 0x8(%rsp)
leaq 0x8(%rsp), %rdi
jmp 0x299719
movq %rbp, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x32760
movq 0x18(%rsp), %r15
leaq 0x17bc4d(%rip), %rax    # 0x415380
cmpl $0x0, (%rax)
je 0x299794
movslq (%rbp), %rdx
testl %edx, %edx
movl $0x0, %eax
cmovgl %edx, %eax
movq 0x8(%rbp), %rdi
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x299761
movq (%rdi,%rcx,8), %rsi
bswapq %rsi
movq %rsi, (%rdi,%rcx,8)
incq %rcx
jmp 0x29974c
pushq $0x8
popq %rsi
movq %rbx, %rcx
callq 0x32760
movl (%rbp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x2997a7
movq 0x8(%rbp), %rdx
movq (%rdx,%rcx,8), %rsi
bswapq %rsi
movq %rsi, (%rdx,%rcx,8)
incq %rcx
jmp 0x29977b
movq 0x8(%rbp), %rdi
movslq (%rbp), %rdx
pushq $0x8
popq %rsi
movq %rbx, %rcx
callq 0x32760
movq 0x28(%rsp), %rbp
incq %rbp
jmp 0x299536
movq 0x8(%r15), %rax
movq %rax, 0x8(%rsp)
leaq 0x17bbbc(%rip), %rcx    # 0x415380
cmpl $0x0, (%rcx)
je 0x2997d1
bswapq %rax
movq %rax, 0x8(%rsp)
leaq 0x8(%rsp), %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x32760
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x32f30