// File: /workspace/github_c_makefile_star2_O1/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 0x7e24
cmpl $0x1, %ebp
je 0x7dcb
testl %ebp, %ebp
jne 0x7e8b
movq %rbx, %rdi
callq 0x2170
movl $0x6973632e, (%rbx,%rax) # imm = 0x6973632E
movb $0x0, 0x4(%rbx,%rax)
leaq 0x16890(%rip), %rsi     # 0x1e586
movq %rbx, %rdi
callq 0x434c
movq %rax, %r15
leaq 0x17390(%rip), %rsi     # 0x1f098
movl $0x4, %edx
movq %rax, %rdi
callq 0x4f33
movl 0x4(%r14), %eax
leaq 0x4(%rsp), %rsi
movl %eax, (%rsi)
movl 0x8(%r14), %eax
movl %eax, 0x4(%rsi)
movl 0x14(%r14), %eax
movl %eax, 0x8(%rsi)
movl $0xc, %edx
movq %r15, %rdi
callq 0x4f33
movl 0x14(%r14), %edx
testq %rdx, %rdx
je 0x7d50
movq 0x40(%r14), %rsi
movq %r15, %rdi
callq 0x4f33
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x7e90
cmpl $0x0, 0x10(%r14)
jle 0x7e6e
leaq 0x10(%r14), %r12
leaq 0x1732a(%rip), %rsi     # 0x1f09d
movl $0x4, %edx
movq %r15, %rdi
callq 0x4f33
leaq 0x28(%r14), %rsi
movl $0x8, %edx
movq %r15, %rdi
callq 0x4f33
movl $0x4, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x4f33
leaq 0x48(%r14), %rsi
movl $0x4, %edx
movq %r15, %rdi
callq 0x4f33
movq 0x50(%r14), %rsi
movslq 0x48(%r14), %rdx
shlq $0x3, %rdx
movq %r15, %rdi
callq 0x4f33
jmp 0x7e6e
movq %rbx, %rdi
callq 0x2170
movl $0x6961622e, (%rbx,%rax) # imm = 0x6961622E
movb $0x0, 0x4(%rbx,%rax)
leaq 0x167a0(%rip), %rsi     # 0x1e586
movq %rbx, %rdi
callq 0x23b0
movq %rax, %r15
leaq 0x172b9(%rip), %rdi     # 0x1f0b1
movl $0x1, %esi
movl $0x4, %edx
movq %rax, %rcx
callq 0x2400
movq %r14, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x7e90
movq %r15, %rdi
callq 0x2160
jmp 0x7e76
movq %rbx, %rdi
callq 0x2170
movl $0x6962742e, (%rbx,%rax) # imm = 0x6962742E
movb $0x0, 0x4(%rbx,%rax)
leaq 0x16747(%rip), %rsi     # 0x1e586
movq %rbx, %rdi
callq 0x434c
movq %rax, %r15
leaq 0x17256(%rip), %rsi     # 0x1f0a7
movl $0x4, %edx
movq %rax, %rdi
callq 0x4f33
movq %r14, %rdi
movq %r15, %rsi
movl $0x2, %edx
callq 0x7e90
movq %r15, %rdi
callq 0x5040
movq %rbx, %rdi
callq 0x2050
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0x2070