// File: /workspace/github_c_makefile_star2_O3/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 %r12
pushq %rbx
subq $0x10, %rsp
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %r14
movq %rsi, %rdi
callq 0x2170
leaq 0x5(%rax), %rsi
movl $0x1, %edi
callq 0x2220
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x20a0
cmpl $0x2, %ebp
je 0x80ca
cmpl $0x1, %ebp
je 0x8071
testl %ebp, %ebp
jne 0x8131
movq %rbx, %rdi
callq 0x2170
movl $0x6973632e, (%rbx,%rax) # imm = 0x6973632E
movb $0x0, 0x4(%rbx,%rax)
leaq 0x165e2(%rip), %rsi     # 0x1e586
movq %rbx, %rdi
callq 0x43f4
movq %rax, %r15
leaq 0x17122(%rip), %rsi     # 0x1f0d8
movl $0x4, %edx
movq %rax, %rdi
callq 0x5320
movq 0x4(%r14), %rax
movq %rsp, %rsi
movq %rax, (%rsi)
movl 0x14(%r14), %eax
movl %eax, 0x8(%rsi)
movl $0xc, %edx
movq %r15, %rdi
callq 0x5320
movl 0x14(%r14), %edx
testq %rdx, %rdx
je 0x7ff6
movq 0x40(%r14), %rsi
movq %r15, %rdi
callq 0x5320
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x8136
cmpl $0x0, 0x10(%r14)
jle 0x8114
leaq 0x10(%r14), %r12
leaq 0x170c4(%rip), %rsi     # 0x1f0dd
movl $0x4, %edx
movq %r15, %rdi
callq 0x5320
leaq 0x28(%r14), %rsi
movl $0x8, %edx
movq %r15, %rdi
callq 0x5320
movl $0x4, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x5320
leaq 0x48(%r14), %rsi
movl $0x4, %edx
movq %r15, %rdi
callq 0x5320
movq 0x50(%r14), %rsi
movslq 0x48(%r14), %rdx
shlq $0x3, %rdx
movq %r15, %rdi
callq 0x5320
jmp 0x8114
movq %rbx, %rdi
callq 0x2170
movl $0x6961622e, (%rbx,%rax) # imm = 0x6961622E
movb $0x0, 0x4(%rbx,%rax)
leaq 0x164fa(%rip), %rsi     # 0x1e586
movq %rbx, %rdi
callq 0x23b0
movq %rax, %r15
leaq 0x17053(%rip), %rdi     # 0x1f0f1
movl $0x1, %esi
movl $0x4, %edx
movq %rax, %rcx
callq 0x2400
movq %r14, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x8136
movq %r15, %rdi
callq 0x2160
jmp 0x811c
movq %rbx, %rdi
callq 0x2170
movl $0x6962742e, (%rbx,%rax) # imm = 0x6962742E
movb $0x0, 0x4(%rbx,%rax)
leaq 0x164a1(%rip), %rsi     # 0x1e586
movq %rbx, %rdi
callq 0x43f4
movq %rax, %r15
leaq 0x16ff0(%rip), %rsi     # 0x1f0e7
movl $0x4, %edx
movq %rax, %rdi
callq 0x5320
movq %r14, %rdi
movq %r15, %rsi
movl $0x2, %edx
callq 0x8136
movq %r15, %rdi
callq 0x542d
movq %rbx, %rdi
callq 0x2050
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0x2070