// File: /workspace/github_cpp_makefile_star2_O1/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 0x10c99(%rip), %rdi     # 0x26960
movl $0x1, %esi
movl $0x4, %edx
movq %rbx, %rcx
callq 0x3600
leaq 0x1a640(%rip), %rax     # 0x30320
cmpl $0x0, (%rax)
je 0x15cfe
movl (%r12), %eax
bswapl %eax
leaq 0x4(%rsp), %rdi
movl %eax, (%rdi)
movl $0x4, %esi
movl $0x1, %edx
jmp 0x15d0b
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %rbx, %rcx
callq 0x3600
cmpl $0x0, (%r12)
jle 0x15fb0
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 0x1a5d6(%rip), %rcx     # 0x30320
cmpl $0x0, (%rcx)
movq %r15, 0x10(%rsp)
je 0x15d68
bswapl %eax
movl %eax, (%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %rsp, %rdi
jmp 0x15d77
movl $0x4, %esi
movl $0x1, %edx
leaq 0x4(%rsp), %rdi
movq %rbx, %rcx
callq 0x3600
cmpl $0x0, (%r14)
je 0x15ee5
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 0x15db0
incl %ebp
cmpl (%r14), %ebp
jne 0x15d8b
jmp 0x15ee5
movl %ebp, %edi
movq %rdi, %r12
shlq $0x4, %r12
addq 0x20(%r14), %r12
leaq 0x1a55c(%rip), %rax     # 0x30320
cmpl $0x0, (%rax)
je 0x15e43
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
testq %rdx, %rdx
je 0x15e90
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 0x15e21
jmp 0x15e92
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 0x15da4
xorl %eax, %eax
movl %eax, (%rsp)
movq 0x8(%r12), %rdi
movl $0x10, %esi
movq %rbx, %rcx
callq 0x3600
movl 0x4(%r12), %eax
testq %rax, %rax
je 0x15da4
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 0x15ec0
jmp 0x15da4
leaq 0x1a434(%rip), %rax     # 0x30320
cmpl $0x0, (%rax)
je 0x15f09
movl (%r13), %eax
bswapl %eax
movl %eax, (%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %rsp, %rdi
jmp 0x15f16
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x3600
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %r15
leaq 0x1a3f1(%rip), %rax     # 0x30320
cmpl $0x0, (%rax)
je 0x15f8b
movslq (%r13), %rdx
testq %rdx, %rdx
jle 0x15f56
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 0x15f43
movq 0x8(%r13), %rdi
movl $0x8, %esi
movq %rbx, %rcx
callq 0x3600
movslq (%r13), %rax
testq %rax, %rax
jle 0x15fa0
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 0x15f76
jmp 0x15fa0
movq 0x8(%r13), %rdi
movslq (%r13), %rdx
movl $0x8, %esi
movq %rbx, %rcx
callq 0x3600
incq %r15
movslq (%r12), %rax
cmpq %rax, %r15
jl 0x15d26
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3690