// File: /workspace/github_c_makefile_star2_O2/lh3[P]bgt/hts.c
// Function: hts_idx_save
void hts_idx_save(const hts_idx_t *idx, const char *fn, int fmt)
{
	char *fnidx;
	fnidx = (char*)calloc(1, strlen(fn) + 5);
	strcpy(fnidx, fn);
	if (fmt == HTS_FMT_CSI) {
		BGZF *fp;
		uint32_t x[3];
		int is_be, i;
		is_be = ed_is_big();
		fp = bgzf_open(strcat(fnidx, ".csi"), "w");
		bgzf_write(fp, "CSI\1", 4);
		x[0] = idx->min_shift; x[1] = idx->n_lvls; x[2] = idx->l_meta;
		if (is_be) {
			for (i = 0; i < 3; ++i)
				bgzf_write(fp, ed_swap_4p(&x[i]), 4);
		} else bgzf_write(fp, &x, 12);
		if (idx->l_meta) bgzf_write(fp, idx->meta, idx->l_meta);
		hts_idx_save_core(idx, fp, HTS_FMT_CSI);
		if (idx->rec_shift > 0) { // FIXME: NOT WORKING ON BIG ENDIAN!!!
			bgzf_write(fp, "RNI\1", 4);
			bgzf_write(fp, &idx->n_rec, 8);
			bgzf_write(fp, &idx->rec_shift, 4);
			bgzf_write(fp, &idx->ridx.n, 4);
			bgzf_write(fp, idx->ridx.offset, idx->ridx.n * 8);
		}
		bgzf_close(fp);
	} else if (fmt == HTS_FMT_TBI) {
		BGZF *fp;
		fp = bgzf_open(strcat(fnidx, ".tbi"), "w");
		bgzf_write(fp, "TBI\1", 4);
		hts_idx_save_core(idx, fp, HTS_FMT_TBI);
		bgzf_close(fp);
	} else if (fmt == HTS_FMT_BAI) {
		FILE *fp;
		fp = fopen(strcat(fnidx, ".bai"), "w");
		fwrite("BAI\1", 1, 4, fp);
		hts_idx_save_core(idx, fp, HTS_FMT_BAI);
		fclose(fp);
	} else abort();
	free(fnidx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %r14
movq %rsi, %rdi
callq 0x2170
leaq 0x5(%rax), %rsi
pushq $0x1
popq %rdi
callq 0x2220
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x20a0
cmpl $0x2, %ebp
je 0x742d
cmpl $0x1, %ebp
je 0x73da
testl %ebp, %ebp
jne 0x7492
movq %rbx, %rdi
callq 0x2170
movl $0x6973632e, (%rbx,%rax) # imm = 0x6973632E
movb $0x0, 0x4(%rbx,%rax)
leaq 0x11273(%rip), %rsi     # 0x18586
movq %rbx, %rdi
callq 0x3ef4
movq %rax, %r15
leaq 0x11d63(%rip), %rsi     # 0x19088
pushq $0x4
popq %rdx
movq %rax, %rdi
callq 0x4cdb
movq 0x4(%r14), %rax
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movl 0x14(%r14), %eax
movl %eax, 0x8(%rsi)
pushq $0xc
popq %rdx
movq %r15, %rdi
callq 0x4cdb
movl 0x14(%r14), %edx
testq %rdx, %rdx
je 0x7363
movq 0x40(%r14), %rsi
movq %r15, %rdi
callq 0x4cdb
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x7497
cmpl $0x0, 0x10(%r14)
jle 0x7473
leaq 0x10(%r14), %r12
leaq 0x11d07(%rip), %rsi     # 0x1908d
pushq $0x4
popq %r13
movq %r15, %rdi
movq %r13, %rdx
callq 0x4cdb
leaq 0x28(%r14), %rsi
pushq $0x8
popq %rdx
movq %r15, %rdi
callq 0x4cdb
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x4cdb
leaq 0x48(%r14), %rsi
movq %r15, %rdi
movq %r13, %rdx
callq 0x4cdb
movq 0x50(%r14), %rsi
movslq 0x48(%r14), %rdx
shlq $0x3, %rdx
movq %r15, %rdi
callq 0x4cdb
jmp 0x7473
movq %rbx, %rdi
callq 0x2170
movl $0x6961622e, (%rbx,%rax) # imm = 0x6961622E
movb $0x0, 0x4(%rbx,%rax)
leaq 0x11191(%rip), %rsi     # 0x18586
movq %rbx, %rdi
callq 0x23c0
movq %rax, %r15
leaq 0x11c9a(%rip), %rdi     # 0x190a1
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
movq %rax, %rcx
callq 0x2410
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x7497
movq %r15, %rdi
callq 0x2160
jmp 0x747b
movq %rbx, %rdi
callq 0x2170
movl $0x6962742e, (%rbx,%rax) # imm = 0x6962742E
movb $0x0, 0x4(%rbx,%rax)
leaq 0x1113e(%rip), %rsi     # 0x18586
movq %rbx, %rdi
callq 0x3ef4
movq %rax, %r15
leaq 0x11c3d(%rip), %rsi     # 0x19097
pushq $0x4
popq %rdx
movq %rax, %rdi
callq 0x4cdb
pushq $0x2
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x7497
movq %r15, %rdi
callq 0x4ddc
movq %rbx, %rdi
callq 0x2050
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2070